This file is indexed.

/usr/lib/python2.7/dist-packages/code_saturne/autovnv/Study.py is in code-saturne-data 4.2.0+repack-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2015 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Standard modules import
#-------------------------------------------------------------------------------

import os, sys
import shutil, re
import subprocess
import threading
import string
import time
import logging
import fnmatch

#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------

from cs_exec_environment import get_shell_type, enquote_arg
from autovnv.Parser import Parser
from autovnv.TexMaker import Report1, Report2
try:
    from autovnv.Drawing import Plotter
except Exception:
    pass
try:
    from autovnv.PlotVTK import PlotVTK
except Exception:
    pass
from autovnv.Command import run_autovnv_command
import cs_runcase

#-------------------------------------------------------------------------------
# log config.
#-------------------------------------------------------------------------------

logging.basicConfig()
log = logging.getLogger(__file__)
#log.setLevel(logging.DEBUG)
log.setLevel(logging.NOTSET)

#-------------------------------------------------------------------------------

def nodot(item):
    return item[0] != '.'

#-------------------------------------------------------------------------------

class Case(object):
    def __init__(self, pkg, rlog, diff, parser, study, data, repo, dest):
        """
        @type data: C{Dictionary}
        @param data: contains all keyword and value read in the parameters file
        """
        self.__log      = rlog
        self.__diff     = diff
        self.__parser   = parser
        self.__study    = study
        self.__data     = data
        self.__repo     = repo
        self.__dest     = dest

        self.node       = data['node']
        self.label      = data['label']
        self.compute    = data['compute']
        self.plot       = data['post']
        self.run_id     = data['run_id']
        self.compare    = data['compare']

        self.is_compil  = "not done"
        self.is_run     = "not done"
        self.is_time    = "not done"
        self.is_plot    = "not done"
        self.is_compare = "not done"
        self.threshold  = "default"
        self.diff_value = []
        self.subdomains = None
        self.run_dir    = ""

        self.resu = 'RESU'

        # Specific case for coupling

        coupling = os.path.join(self.__repo, self.label, "coupling_parameters.py")
        if os.path.isfile(coupling):
            import cs_case_coupling
            try:
                exec(compile(open(coupling).read(), '<string>', 'exec'))
            except Exception:
                execfile(coupling)
            run_ref = os.path.join(self.__repo, self.label, "runcase")
            self.exe, self.pkg = self.__get_exe(pkg, run_ref)
            self.subdomains = []
            for d in locals()['domains']:
                if d['solver'] == self.pkg.code_name:
                    self.subdomains.append(d['domain'])
            self.resu = 'RESU_COUPLING'

        else:
            run_ref = os.path.join(self.__repo, self.label, "SCRIPTS", "runcase")
            self.exe, self.pkg = self.__get_exe(pkg, run_ref)


    def __get_exe(self, old_pkg, run_ref):
        """
        Return the name of the exe of the case, in order to mix
        Code_Saturne and NEPTUNE_CFD test cases in the same study.
        """

        # Read the runcase script from the Repository

        runcase = cs_runcase.runcase(run_ref)

        if runcase.cmd_name == "code_saturne":
            from Base.XMLinitialize import XMLinit
            from cs_package import package
            pkg = package(old_pkg.scriptdir)
        elif runcase.cmd_name == "neptune_cfd":
            from core.XMLinitialize import XMLinit
            from nc_package import package
            pkg = package(old_pkg.scriptdir)

        return runcase.cmd_name, pkg


    def __update_domain(self, subdir, xmlonly=False):
        """
        Update path for the script in the Repository.
        """
        # 1) Load the xml file of parameters in order to update it
        #    with the __backwardCompatibility method.

        from Base.XMLengine import Case
        from Pages.ScriptRunningModel import ScriptRunningModel
        from cs_exec_environment import \
            separate_args, get_command_single_value, update_command_single_value, assemble_args

        if self.exe == "code_saturne":
            from Base.XMLinitialize import XMLinit
        elif self.exe == "neptune_cfd":
            from core.XMLinitialize import XMLinit

        for fn in os.listdir(os.path.join(self.__repo, subdir, "DATA")):
            fp = os.path.join(self.__repo, subdir, "DATA", fn)
            if os.path.isfile(fp):
                fd = os.open(fp , os.O_RDONLY)
                f = os.fdopen(fd)
                l = f.readline()
                f.close()
                if l.startswith('''<?xml version="1.0" encoding="utf-8"?><Code_Saturne_GUI''') or \
                   l.startswith('''<?xml version="1.0" encoding="utf-8"?><NEPTUNE_CFD_GUI'''):
                    try:
                        case = Case(package = self.pkg, file_name = fp)
                    except:
                        print("Parameters file reading error.\n")
                        print("This file is not in accordance with XML specifications.")
                        sys.exit(1)

                    case['xmlfile'] = fp
                    case.xmlCleanAllBlank(case.xmlRootNode())
                    XMLinit(case).initialize()
                    case.xmlSaveDocument()

        # 2) Create RESU directory if needed
        if not xmlonly:
            r = os.path.join(self.__repo, subdir, "RESU")
            if not os.path.isdir(r):
                os.makedirs(r)

        # 3) Update the GUI script from the Repository
        ref = os.path.join(self.__repo, subdir, "DATA", self.pkg.guiname)

        have_gui = 1
        try:
            f = open(ref, mode = 'r')
        except IOError:
           print("Warning SaturneGUI does not exist: %s\n" % ref)
           have_gui = 0

        if have_gui:
           lines = f.readlines()
           f.close()

           for i in range(len(lines)):
               if re.search(r'^prefix=', lines[i]):
                   if xmlonly:
                       lines[i] = "prefix=\n"
                   else:
                       lines[i] = "prefix=" + self.pkg.get_dir('prefix') + "\n"
               if re.search(r'^export PATH=', lines[i]):
                   if xmlonly:
                       lines[i] = 'export PATH="":$PATH\n'
                   else:
                       lines[i] = 'export PATH="' + self.pkg.get_dir('bindir') +'":$PATH\n'
           f = open(ref, mode = 'w')
           f.writelines(lines)
           f.close()

        # 4) Update the runcase script from the Repository
        self.update_runcase_path(os.path.join(self.__repo, subdir, "SCRIPTS"), None, xmlonly)


    def update_runcase_path(self, subdir, destdir=None, xmlonly=False):
        """
        Update path for the script in the Repository.
        """
        if not destdir:
            ref = os.path.join(self.__repo, subdir, "runcase")
        else:
            ref = os.path.join(destdir, subdir, "runcase")

        try:
            f = open(ref, mode = 'r')
        except IOError:
            print("Error: can not open %s\n" % ref)
            sys.exit(1)

        lines = f.readlines()
        f.close()

        for i in range(len(lines)):
            if lines[i].strip()[0:1] == '#':
                continue
            if xmlonly:
                if re.search(r'^export PATH=', lines[i]):
                    lines[i] = 'export PATH="":$PATH\n'
            else:
                if re.search(r'^export PATH=', lines[i]):
                    lines[i] = 'export PATH="' + self.pkg.get_dir('bindir') +'":$PATH\n'
                j = lines[i].find(self.pkg.name)

        f = open(ref, mode = 'w')
        f.writelines(lines)
        f.close()


    def update(self, xmlonly=False):
        """
        Update path for the script in the Repository.
        """
        # 1) Load the xml file of parameters in order to update it
        #    with the __backwardCompatibility method.

        from Base.XMLengine import Case
        from Pages.ScriptRunningModel import ScriptRunningModel
        from cs_exec_environment import \
            separate_args, get_command_single_value, update_command_single_value, assemble_args, enquote_arg

        if self.exe == "code_saturne":
            from Base.XMLinitialize import XMLinit
        elif self.exe == "neptune_cfd":
            from core.XMLinitialize import XMLinit

        if self.subdomains:
            self.update_runcase_path(d, self.label, xmlonly)
            cdirs = []
            for d in self.subdomains:
                cdirs.append(os.path.join(self.label, d))
        else:
            cdirs = (self.label,)

        for d in cdirs:
            self.__update_domain(d, xmlonly)

        # Update the runcase script from the Repository in case of coupling

        if self.subdomains:
            ref = os.path.join(self.__repo, self.label, "runcase")
            try:
                f = open(ref, mode = 'r')
                lines = f.readlines()
                f.close()
                for i in range(len(lines)):
                    if lines[i].strip()[0:1] == '#':
                        continue
                    if xmlonly:
                        if re.search(r'^export PATH=', lines[i]):
                            lines[i] = 'export PATH="":$PATH\n'
                    else:
                        if re.search(r'^export PATH=', lines[i]):
                            lines[i] = 'export PATH="' + self.pkg.get_dir('bindir') +'":$PATH\n'
                f = open(ref, mode = 'w')
                f.writelines(lines)
                f.close()
            except IOError:
                pass


    def compile(self, d):
        """
        Just compile user sources if exist.
        @rtype: C{String}
        @return: the status of the succes of the compilation.
        """

        if self.subdomains:
            sdirs = []
            for sd in self.subdomains:
                sdirs.append(os.path.join(d, self.label, sd, 'SRC'))
        else:
            sdirs = (os.path.join(d, self.label, 'SRC'),)

        home = os.getcwd()

        self.is_compil = "OK"

        for s in sdirs:

            os.chdir(s)
            cmd = os.path.join(self.pkg.get_dir('bindir'), self.exe) + " compile -t"
            p = subprocess.Popen(cmd,
                                 shell=True,
                                 executable=get_shell_type(),
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 universal_newlines=True)
            output = p.communicate()
            o = output[1]
            if o.find('erreur') != -1 or o.find('error') != -1:
                self.is_compil = "KO"

        os.chdir(home)

        return self.is_compil


    def __suggest_run_id(self):

        cmd = enquote_arg(os.path.join(self.pkg.get_dir('bindir'), self.exe)) + " run --suggest-id"
        if self.subdomains:
            cmd += " --coupling=coupling_parameters.py"
        p = subprocess.Popen(cmd,
                             shell=True,
                             executable=get_shell_type(),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             universal_newlines=True)
        i = p.communicate()[0]
        run_id = " ".join(i.split())

        return run_id, os.path.join(self.__dest, self.label, self.resu, run_id)


    def __updateRuncase(self, run_id):
        """
        Update the command line in the launcher C{runcase}.
        """
        from cs_exec_environment import separate_args, \
                                 get_command_single_value

        if self.subdomains:
            run_ref = os.path.join(self.__repo, self.label, "runcase")
            run_new = os.path.join(self.__dest, self.label, "runcase")
            run_ref_win = os.path.join(self.__repo, self.label, "runcase.bat")
        else:
            run_ref = os.path.join(self.__repo, self.label, "SCRIPTS", "runcase")
            run_new = os.path.join(self.__dest, self.label, "SCRIPTS", "runcase")
            run_ref_win = os.path.join(self.__repo, self.label, "SCRIPTS", "runcase.bat")

        if sys.platform.startswith('win'):
            if self.subdomains:
                run_new = os.path.join(self.__dest, self.label, "runcase.bat")
            else:
                run_new = os.path.join(self.__dest, self.label, "SCRIPTS", "runcase.bat")

        # Read the runcase from the Repository

        try:
            f = open(run_ref, mode = 'r')
        except:
            try:
                f = open(run_ref_win, mode = 'r')
            except IOError:
                print("Error: can not open %s\n" % run_ref)
                sys.exit(1)

        for line in f.readlines():
            if sys.platform.startswith('win'):
                if re.search(self.exe, line):
                    #we suppress "\" if needed
                    content = line.split()
                    if re.search(r'^\\' + self.exe, line):
                        content[0] = self.exe
                    run_cmd = " ".join(content)
            else:
                if re.search(r'^\\' + self.exe, line):
                    run_cmd = " ".join(line.split())
        f.close()

        # Write the new runcase

        try:
            f = open(run_new, mode = 'r')
        except IOError:
            print("Error: can not open %s\n" % run_new)
            sys.exit(1)

        lines = f.readlines()
        f.close()

        for i in range(len(lines)):
            l = lines[i].strip()
            if len(l) == 0:
                continue
            if l[0] == '#':
                continue
            if sys.platform.startswith('win'):
                if re.search(self.exe, lines[i]):
                    lines[i] = run_cmd + " --id=" + run_id
                elif re.search(r' cd ', lines[i]): # do not switch to batch submit directory
                    lines[i] = '# ' + lines[i]
            else:
                if re.search(r'^\\' + self.exe, lines[i]):
                    lines[i] = run_cmd + " --id=" + run_id
                elif re.search(r' cd ', lines[i]): # do not switch to batch submit directory
                    lines[i] = '# ' + lines[i]

        f = open(run_new, mode = 'w')
        f.writelines(lines)
        f.close()


    def run(self):
        """
        Run the case a thread.
        """
        home = os.getcwd()
        if self.subdomains:
            os.chdir(os.path.join(self.__dest, self.label))
        else:
            os.chdir(os.path.join(self.__dest, self.label, 'SCRIPTS'))

        if self.run_id:
            run_id = self.run_id
            run_dir = os.path.join(self.__dest, self.label, self.resu, run_id)

            if os.path.isdir(run_dir):
                print("Warning: the directory %s already exists in the destination." % run_dir)

                if os.path.isfile(os.path.join(run_dir, "error")):
                    self.is_run = "KO"
                    self.is_time = 0.
                    error = 1
                else:
                    self.is_run = "OK"
                    self.is_time = 0.
                    error = 0
                os.chdir(home)

                return error

        else:
            run_id, run_dir = self.__suggest_run_id()

            while os.path.isdir(run_dir):
                time.sleep(5)
                run_id, run_dir = self.__suggest_run_id()

        self.run_id  = run_id
        self.run_dir = run_dir

        self.__updateRuncase(run_id)

        if sys.platform.startswith('win'):
            error, self.is_time = run_autovnv_command("runcase.bat", self.__log)
        else:
            error, self.is_time = run_autovnv_command("./runcase", self.__log)

        if not error:
            self.is_run = "OK"
        else:
            self.is_run = "KO"

        os.chdir(home)

        return error


    def runCompare(self, studies, r, d, threshold, args, reference=None):
        home = os.getcwd()

        node = None

        if reference:
            result = os.path.join(reference, self.label, self.resu)
        else:
            result = os.path.join(self.__repo, self.label, self.resu)
        repo = self.check_dir(studies, node, result, r, "repo")
        repo = os.path.join(result, repo, 'checkpoint', 'main')

        result = os.path.join(self.__dest, self.label, self.resu)
        dest = self.check_dir(studies, node, result, d, "dest")
        dest = os.path.join(result, dest, 'checkpoint', 'main')

        cmd = self.__diff + ' ' + repo + ' ' + dest

        self.threshold = "default"
        if threshold != None:
            cmd += ' --threshold ' + threshold
            self.threshold = threshold

        if args != None:
            cmd += (" " + args)
            l = args.split()
            try:
                i = l.index('--threshold')
                self.threshold = l[i+1]
            except:
                pass

        l = subprocess.Popen(cmd,
                             shell=True,
                             executable=get_shell_type(),
                             stdout=subprocess.PIPE,
                             universal_newlines=True).stdout
        lines = l.readlines()

        tab = []
        for i in range(len(lines)):
            if lines[i].find("Support") != -1:
                line = [x.replace("\""," ").strip() for x in lines[i].split(";")]
                name = line[0]
                info = [x.split(":") for x in line[1:]]
                info = [[x[0].strip(),x[1].strip()] for x in info]
                if info[1][1] not in ['i4', 'u4', 'c']:
                    line = [x.strip() for x in lines[i+1].split(";")]
                    vals = [x.split(":") for x in line]
                    vals = [[x[0].strip(),x[1].strip()] for x in vals]
                    tab.append([name.replace("_", "\_"),
                                vals[1][1],
                                vals[2][1],
                                self.threshold])

        os.chdir(home)

        return tab


    def check_dir(self, studies, node, result, rep, attr):
        """
        Check coherency between xml file of parameters and repository or destination.
        """
        # 1. Check if the given result directory exists.
        if rep != "":
            rep_f = os.path.join(result, rep)
            rep_e = os.path.join(result, rep, 'error')

            if not os.path.isdir(rep_f):
                msg = "Study %s case %s:\nthe directory %s does not exist.\nStop.\n" % \
                    (self.__study, self.label, rep_f)
                studies.reporting(msg)
                sys.exit(1)

            if os.path.isfile(rep_e):
                msg = "Study %s case %s:\nthe directory %s contains an error file.\nStop.\n" % \
                    (self.__study, self.label, rep_f)
                studies.reporting(msg)
                sys.exit(1)

            return rep

        # 2. The result directory must be read automatically;
        #    check if there is a single result directory.
        elif rep == "":
            if len(list(filter(nodot, os.listdir(result)))) == 0:
                msg = "Study %s case %s:\nthere is no result directory in %s\nStop.\n" % \
                    (self.__study, self.label, result)
                studies.reporting(msg)
                sys.exit(1)

            # if no run_id is specified in the xml file
            # only one result directory allowed in RESU
            if len(list(filter(nodot, os.listdir(result)))) > 1 and self.run_id == "":
                msg = "Study %s case %s:\nthere are several directories in %s\nStop.\n" % \
                    (self.__study, self.label, result)
                studies.reporting(msg)
                sys.exit(1)

            rep = self.run_id
            # if no run_id is specified in the xml file
            # the only result directory present in RESU is taken
            if rep == "":
                rep = list(filter(nodot, os.listdir(result)))[0]

            rep_f = os.path.join(result, rep)
            if not os.path.isdir(rep_f):
                msg = "Study %s case %s:\nthe directory %s does not exist.\nStop.\n" % \
                    (self.__study, self.label, rep_f)
                studies.reporting(msg)
                sys.exit(1)

            rep_e = os.path.join(result, rep, 'error')
            if os.path.isfile(rep_e):
                msg = "Study %s case %s:\nthe directory %s contains an error file.\nStop.\n" % \
                    (self.__study, self.label, rep_f)
                studies.reporting(msg)
                sys.exit(1)

            # 3. Update the file of parameters with the name of the result directory
            if node:
                self.__parser.setAttribute(node, attr, rep)
            return rep

        else:
            studies.reporting('Error: check compare/script/plot/probe/resu/input failed.')
            sys.exit(1)


    def check_dirs(self, studies, node, repo, dest, reference=None):
        """
        Check coherency between xml file of parameters and repository and destination.
        """
        if repo != None:
            # build path to RESU directory with path to study and case label in the repo
            if reference:
                result = os.path.join(reference, self.label, self.resu)
            else:
                result = os.path.join(self.__repo, self.label, self.resu)
            self.check_dir(studies, node, result, repo, "repo")

        if dest != None:
            # build path to RESU directory with path to study and case label in the dest
            result = os.path.join(self.__dest, self.label, self.resu)
            self.check_dir(studies, node, result, dest, "dest")

#-------------------------------------------------------------------------------

class Study(object):
    """
    Create, run and compare all cases for a given study.
    """
    def __init__(self, pkg, parser, study, exe, dif, rlog):
        """
        Constructor.
          1. initialize attributes,
          2. build the list of the cases,
          3. build the list of the keywords from the runcase.
        @type parser: C{Parser}
        @param parser: instance of the parser
        @type study: C{String}
        @param study: label of the current study
        @type exe: C{String}
        @param exe: name of the solver executable: C{code_saturne} or C{neptune_cfd}.
        @type dif: C{String}
        @param dif: name of the diff executable: C{cs_io_dump -d}.
        """
        # Initialize attributes
        self.__parser   = parser
        self.__main_exe = exe
        self.__diff     = dif
        self.__log      = rlog

        self.__repo = os.path.join(self.__parser.getRepository(),  study)
        self.__dest = os.path.join(self.__parser.getDestination(), study)

        if not os.path.isdir(self.__repo):
            print("Error: the directory %s does not exist" % self.__repo)
            sys.exit(1)

        self.label = study

        self.Cases = []
        self.matplotlib_figures = []
        self.vtk_figures = []

        # build the list of the cases
        self.cases = parser.getCasesLabel(study)
        if not self.cases:
            print("\n\n\nWarning: no case defined in %s study\n\n\n" % study)
        else:
            for data in self.__parser.getCasesKeywords(self.label):
                c = Case(pkg,
                         self.__log,
                         self.__diff,
                         self.__parser,
                         self.label,
                         data,
                         self.__repo,
                         self.__dest)
                self.Cases.append(c)


    def getCasesLabel(self):
        """
        Return the list of the labels of the cases of the current study.
        @rtype: C{List}
        @return: labels of the cases
        """
        return self.cases


    def createCases(self):
        """
        Create a single study with its all cases.
        """
        repbase = os.getcwd()

        # Create study if necessary
        if not os.path.isdir(self.__dest):
            cmd = self.__main_exe + " create --quiet --study " + self.__dest
            retval, t = run_autovnv_command(cmd, self.__log)
            shutil.rmtree(os.path.join(self.__dest, "CASE1"))

            # Link meshes and copy other files
            ref = os.path.join(self.__repo, "MESH")
            if os.path.isdir(ref):
                l = os.listdir(ref)
                meshes = []
                for cpr in ["", ".gz"]:
                    for fmt in ["unv",
                                "med",
                                "ccm",
                                "cgns",
                                "neu",
                                "msh",
                                "des"]:
                        meshes += fnmatch.filter(l, "*." + fmt + cpr)
                des = os.path.join(self.__dest, "MESH")
                for m in l:
                    if m in meshes:
                        if sys.platform.startswith('win'):
                            shutil.copy2(os.path.join(ref, m), os.path.join(des, m))
                        else:
                            os.symlink(os.path.join(ref, m), os.path.join(des, m))
                    elif m != ".svn":
                        t = os.path.join(ref, m)
                        if os.path.isdir(t):
                            shutil.copytree(t, os.path.join(des, m))
                        elif os.path.isfile(t):
                            shutil.copy2(t, des)

            # Copy external scripts for post-processing
            ref = os.path.join(self.__repo, "POST")
            if os.path.isdir(ref):
                des = os.path.join(self.__dest, "POST")
                shutil.rmtree(des)
                shutil.copytree(ref, des, symlinks=True)

        # Create cases
        os.chdir(self.__dest)

        for c in self.Cases:
            if not os.path.isdir(c.label):
                e = os.path.join(c.pkg.get_dir('bindir'), c.exe)
                if c.subdomains:
                    os.mkdir(c.label)
                    os.chdir(c.label)
                    refdir = os.path.join(self.__repo, c.label)
                    for node in os.listdir(refdir):
                        ref = os.path.join(self.__repo, c.label, node)
                        if node in c.subdomains:
                            cmd = e + " create --case " + node \
                                  + " --quiet --noref --copy-from " \
                                  + ref
                            retval, t = run_autovnv_command(cmd, self.__log)
                        elif os.path.isdir(ref):
                            shutil.copytree(ref, node, symlinks=True)
                        else:
                            shutil.copy2(ref, node)
                    c.update_runcase_path(c.label, destdir=self.__dest)
                    os.chdir(self.__dest)
                else:
                    cmd = e + " create --case " + c.label  \
                          + " --quiet --noref --copy-from "    \
                          + os.path.join(self.__repo, c.label)
                    retval, t = run_autovnv_command(cmd, self.__log)
            else:
                print("Warning: the case %s already exists in the destination." % c.label)

        os.chdir(repbase)


    def getRunDirectories(self):
        list_cases = []
        list_dir   = []

        for case in self.Cases:
            if case.is_run != "KO":
                list_cases.append(case.label)
                list_dir.append(case.run_dir)

        return " ".join(list_cases), " ".join(list_dir)

#-------------------------------------------------------------------------------

class Studies(object):
    """
    Manage all Studies and all Cases described in the files of parameters.
    """
    def __init__(self, pkg, f, v, x, r, n, c, d, p, exe, dif, log, dis_tex):
        """
        Constructor.
          1. create if necessary the destination directory,
          2. initialize the parser and the plotter,
          3. build the list of the studies,
          4. start the report.
        @type f: C{String}
        @param f: xml file of parameters.
        @type v: C{True} or C{False}
        @param v: verbose mode.
        @type x: C{True} or C{False}
        @param x: update xml in repo only.
        @type r: C{True} or C{False}
        @param r: have to run the case.
        @type c: C{True} or C{False}
        @param c: have to do a comparison.
        @type exe: C{String}
        @param exe: name of the solver executable: C{code_saturne} or C{neptune_cfd}.
        @type dif: C{String}
        @param dif: name of the diff executable: C{cs_io_dump -d}.
        """

        # create a first xml parser only for
        #   the repository verification and
        #   the destination creation

        self.__parser = Parser(f)

        # Test if the repository exists

        self.repo = self.getRepository()

        # create if necessary the destination directory

        self.__xmlupdate = x
        if not self.__xmlupdate:
            self.dest = self.getDestination()
        else:
            self.dest = self.getRepository()
        if not os.path.isdir(self.dest):
            os.makedirs(self.dest)

        # copy the xml file of parameters for update and restart

        file = os.path.join(self.dest, os.path.basename(f))
        try:
            shutil.copyfile(f, file)
        except:
            pass

        # create a new parser, which is definitive and the plotter

        self.__parser  = Parser(file)
        try:
            self.__plotter = Plotter(self.__parser)
        except Exception:
            self.__plotter = None
        try:
            self.__plotvtk = PlotVTK(self.__parser)
        except Exception:
            self.__plotvtk = None

        # build the list of the studies

        doc = os.path.join(self.dest, log)
        self.__log = open(doc, "w")
        self.labels  = self.__parser.getStudiesLabel()
        self.studies = []
        for l in self.labels:
            self.studies.append( [l, Study(pkg, self.__parser, l, \
                                  exe, dif, self.__log)] )

        # start the report
        self.report = os.path.join(self.dest, "report.txt")
        self.reportFile = open(self.report, mode='w')
        self.reportFile.write('\n')

        # attributes

        self.__verbose = v
        self.__running = r
        self.__n_iter  = n
        self.__compare = c
        self.__ref     = d
        self.__postpro = p
        self.__dis_tex = dis_tex

        # in case of restart
        iok = 0
        for l, s in self.studies:
            for case in s.Cases:
                if case.compute == 'on':
                   iok+=1
        if not iok:
            self.__running = False

        if self.__xmlupdate:
            os.remove(file)
            os.remove(doc)

    def reporting(self, msg, screen_only=False):
        """
        Write the report.
        http://www.developpez.net/forums/d651999/autres-langages/python-zope/general-python/console-ecrire-ligne-precedente/
        Carriage return "chr(13)" renvoi en debut de ligne et line feed "chr(10)" effectue un saut de ligne.
        @type l: C{String}
        @param l: the sentence to be writing.
        """
        if not screen_only:
            s = chr(10)
        else:
            s  = ""

        if not self.__verbose:
            sys.stdout.write (msg + chr(13) + s)
            sys.stdout.flush()

        if not screen_only:
            self.reportFile.write(msg + '\n')
            self.reportFile.flush()


    def getRepository(self):
        """
        Return the directory of the repository.
        @rtype: C{String}
        @return: the directory of the repository of all studies.
        """
        r = self.__parser.getRepository()

        if not os.path.isdir(r):
            self.reporting('Error: the repository does not exist.')
            sys.exit(1)

        return r


    def getDestination(self):
        """
        Return the directory of the destination.
        @rtype: C{String}
        @return: the directory of the destination.
        """
        return self.__parser.getDestination()


    def updateRepository(self, xml_only=False):
        """
        Create all studies and all cases.
        """
        iok = 0
        for l, s in self.studies:
            self.reporting('  o Update repository: ' + l)
            for case in s.Cases:
                self.reporting('    - update  %s' % case.label)
                case.update(xml_only)
                if case.compile(os.path.join(self.repo, l)) == "OK":
                    self.reporting('    - compile %s --> OK' % case.label)
                else:
                    self.reporting('    - compile %s --> FAILED' % case.label)
                    iok+=1
        if iok:
            self.reporting('Error: compilation failed. Number of failed cases: %s' % iok)
            sys.exit(1)


    def createStudies(self):
        """
        Create all studies and all cases.
        """
        for l, s in self.studies:
            self.reporting('  o Create study: ' + l)
            s.createCases()
            for c in s.getCasesLabel():
                self.reporting('    - create case: ' + c)


    def compilation(self):
        """
        Compile sources of all cases.
        """
        iok = 0
        for l, s in self.studies:
            self.reporting('  o Compile study: ' + l)
            for case in s.Cases:
                if case.compute == 'on':
                    if case.compile(os.path.join(self.dest, l)) == "OK":
                        self.reporting('    - compile %s --> OK' % case.label)
                    else:
                        self.reporting('    - compile %s --> FAILED' % case.label)
                        iok+=1
        if iok:
            self.reporting('Error: compilation failed. Number of failed cases: %s' % iok)
            sys.exit(1)


    def prepro(self, l, s, case):
        """
        Launch external additional scripts with arguments.
        """
        pre, label, nodes, args = self.__parser.getPrepro(case.node)
        for i in range(len(label)):
            if pre[i]:
                # search if the script is the directoty MESH
                # if not, the script is searched in the directories of the current case
                cmd = os.path.join(self.dest, l, "MESH", label[i])
                if not os.path.isfile(cmd):
                    filePath = ""
                    for root, dirs, fs in os.walk(os.path.join(self.dest, l, case.label)):
                        if label[i] in fs:
                            filePath = root
                            break

                    cmd = os.path.join(filePath, label[i])

                if os.path.isfile(cmd):
                    cmd += " " + args[i]
                    cmd += " -c " + os.path.join(self.dest, l, case.label)
                    repbase = os.getcwd()
                    os.chdir(os.path.join(self.dest, l, "MESH"))
                    # Prepro external script might need pythondir and pkgpythondir
                    pdir = case.pkg.get_dir('pythondir')
                    pdir = pdir + ":" + case.pkg.get_dir('pkgpythondir')
                    # Pythondirs might be different for NEPTUNE_CFD
                    # and have to be added as well
                    if case.pkg.code_name == "NEPTUNE_CFD":
                        sdir = case.pkg.get_cs_dir('pythondir')
                        sdir = sdir + ":" + case.pkg.get_cs_dir('pkgpythondir')
                        pdir = pdir + ":" + sdir
                    retcode, t = run_autovnv_command(cmd, self.__log, pythondir = pdir)
                    os.chdir(repbase)
                    self.reporting('    - script %s --> OK (%s s)' % (cmd, t))
                else:
                    self.reporting('    - script %s not found' % cmd)


    def run(self):
        """
        Update and run all cases.
        Warning, if the makup of the case is repeated in the xml file of parameters,
        the run of the case is also repeated.
        """
        for l, s in self.studies:
            self.reporting('  o Script prepro and run for study: ' + l)
            for case in s.Cases:
                self.prepro(l, s, case)
                if self.__running:
                    if case.compute == 'on' and case.is_compil != "KO":

                        if self.__n_iter:
                            if case.subdomains:
                                case_dir = os.path.join(self.dest, s.label, case.label,
                                                        case.subdomains[0], "DATA")
                            else:
                                case_dir = os.path.join(self.dest, s.label, case.label, "DATA")
                            os.chdir(case_dir)
                            # Create a control_file in each case DATA
                            if not os.path.exists('control_file'):
                                control_file = open('control_file','w')
                                control_file.write("time_step_limit " + str(self.__n_iter) + "\n")
                                # Flush to ensure that control_file content is seen
                                # when control_file is copied to the run directory on all systems
                                control_file.flush()
                                control_file.close

                        self.reporting('    - running %s ...' % case.label, True)
                        error = case.run()
                        if not error:
                            if not case.run_id:
                                self.reporting('    - run %s --> Warning suffix is not read' % case.label)

                            self.reporting('    - run %s --> OK (%s s) in %s' % (case.label, case.is_time, case.run_id))
                            self.__parser.setAttribute(case.node, "compute", "off")

                            # update dest="" attribute
                            n1 = self.__parser.getChilds(case.node, "compare")
                            n2 = self.__parser.getChilds(case.node, "script")
                            n3 = self.__parser.getChilds(case.node, "data")
                            n4 = self.__parser.getChilds(case.node, "probe")
                            n5 = self.__parser.getChilds(case.node, "resu")
                            n6 = self.__parser.getChilds(case.node, "input")
                            for n in n1 + n2 + n3 + n4 + n5 + n6:
                                if self.__parser.getAttribute(n, "dest") == "":
                                    self.__parser.setAttribute(n, "dest", case.run_id)
                        else:
                            self.reporting('    - run %s --> FAILED' % case.label)

                        self.__log.flush()


    def check_compare(self, destination=True):
        """
        Check coherency between xml file of parameters and repository.
        Stop if you try to make a comparison with a file which does not exist.
        """
        for l, s in self.studies:
            # reference directory passed in autovnv command line overwrites
            # destination in all cases (even if compare is defined by a compare
            # markup with a non empty destination)
            ref = None
            if self.__ref:
                ref = os.path.join(self.__ref, s.label)
            for case in s.Cases:
                if case.compare == 'on' and case.is_run != "KO":
                    compare, nodes, repo, dest, threshold, args = self.__parser.getCompare(case.node)
                    if compare:
                        is_checked = False
                        for i in range(len(nodes)):
                            if compare[i]:
                                is_checked = True
                                if destination == False:
                                    dest[i]= None
                                case.check_dirs(self, nodes[i], repo[i], dest[i], reference=ref)
                    if not compare or not is_checked:
                        node = None
                        repo = ""
                        dest = ""
                        if destination == False:
                            dest = None
                        case.check_dirs(self, node, repo, dest, reference=ref)


    def compare(self):
        """
        Compare the results of the new computations with those from the Repository.
        """
        if self.__compare:
            for l, s in self.studies:
                self.reporting('  o Compare study: ' + l)
                # reference directory passed in autovnv command line overwrites
                # destination in all cases (even if compare is defined by a
                # compare markup with a non empty destination)
                ref = None
                if self.__ref:
                    ref = os.path.join(self.__ref, s.label)
                for case in s.Cases:
                    if case.compare == 'on' and case.is_run != "KO":
                        is_compare, nodes, repo, dest, t, args = self.__parser.getCompare(case.node)
                        if is_compare:
                            for i in range(len(nodes)):
                                if is_compare[i]:
                                    case.is_compare = "done"
                                    diff_value = case.runCompare(self, repo[i], dest[i], t[i], args[i], reference=ref)
                                    case.diff_value += diff_value
                                    if diff_value:
                                        self.reporting('    - compare %s (with args: %s) --> DIFFERENCES FOUND' % (case.label, args[i]))
                                    else:
                                        self.reporting('    - compare %s (with args: %s) --> NO DIFFERENCES FOUND' % (case.label, args[i]))
                        if not is_compare or case.is_compare != "done":
                            repo = ""
                            dest = ""
                            t    = None
                            args = None
                            case.is_compare = "done"
                            case.diff_value += case.runCompare(self, repo, dest, t, args, reference=ref)
                            if case.diff_value:
                                self.reporting('    - compare %s (default mode) --> DIFFERENCES FOUND' % (case.label))
                            else:
                                self.reporting('    - compare %s (default mode) --> NO DIFFERENCES FOUND' % (case.label))



    def check_script(self, destination=True):
        """
        Check coherency between xml file of parameters and repository.
        Stop if you try to run a script with a file which does not exist.
        """
        for l, s in self.studies:
            for case in s.Cases:
                script, label, nodes, args, repo, dest = self.__parser.getScript(case.node)
                for i in range(len(nodes)):
                    if script[i] and case.is_run != "KO":
                        if destination == False:
                            dest[i] = None
                        case.check_dirs(self, nodes[i], repo[i], dest[i])


    def scripts(self):
        """
        Launch external additional scripts with arguments.
        """
        for l, s in self.studies:
            self.reporting('  o Script postpro study: ' + l)
            for case in s.Cases:
                script, label, nodes, args, repo, dest = self.__parser.getScript(case.node)
                for i in range(len(label)):
                    if script[i] and case.is_run != "KO":
                        cmd = os.path.join(self.dest, l, "POST", label[i])
                        if os.path.isfile(cmd):
                            cmd += " " + args[i]
                            if repo[i]:
                                r = os.path.join(self.repo,  l, case.label, "RESU", repo[i])
                                cmd += " -r " + r
                            if dest[i]:
                                d = os.path.join(self.dest, l, case.label, "RESU", dest[i])
                                cmd += " -d " + d
                            retcode, t = run_autovnv_command(cmd, self.__log)
                            self.reporting('    - script %s --> OK (%s s)' % (cmd, t))
                        else:
                            self.reporting('    - script %s not found' % cmd)


    def postpro(self):
        """
        Launch external additional scripts with arguments.
        """
        for l, s in self.studies:
            # fill results directories and ids for the cases of the current study
            # that were not run by the current autovnv command
            for case in s.Cases:
                if case.is_run != "KO":
                    if case.run_dir == "":
                        resu = os.path.join(self.dest, l, case.label, case.resu)
                        rep = case.check_dir(self, None, resu, "", "dest")
                        case.run_id = rep
                        case.run_dir = os.path.join(resu, rep)

            self.reporting('  o Postprocessing cases of study: ' + l)
            script, label, nodes, args = self.__parser.getPostPro(l)
            for i in range(len(label)):
                if script[i]:
                    cmd = os.path.join(self.dest, l, "POST", label[i])
                    if os.path.isfile(cmd):
                        list_cases, list_dir = s.getRunDirectories()
                        cmd += ' ' + args[i] + ' -c "' + list_cases + '" -d "' + list_dir + '" -s ' + l
                        retcode, t = run_autovnv_command(cmd, self.__log)
                        self.reporting('    - postpro %s --> OK (%s s)' % (cmd, t))
                    else:
                        self.reporting('    - postpro %s not found' % cmd)


    def check_plot(self, destination=True):
        """
        Check coherency between xml file of parameters and repository.
        Stop if you try to make a plot of a file which does not exist.
        """
        for l, s in self.studies:
            for case in s.Cases:
                if case.plot == "on" and case.is_run != "KO":
                    for node in self.__parser.getChilds(case.node, "data"):
                        plots, file, dest, repo = self.__parser.getResult(node)
                        if destination == False:
                            dest = None
                        case.check_dirs(self, node, repo, dest)

                    for node in self.__parser.getChilds(case.node, "probes"):
                        file, dest, fig = self.__parser.getProbes(node)
                        if destination == False:
                            dest = None
                        repo = None
                        case.check_dirs(self, node, repo, dest)

                    for node in self.__parser.getChilds(case.node, "resu"):
                        plots, file, dest, repo = self.__parser.getResult(node)
                        if destination == False:
                            dest = None
                        case.check_dirs(self, node, repo, dest)

                    for node in self.__parser.getChilds(case.node, "input"):
                        file, dest, repo, tex = self.__parser.getInput(node)
                        if destination == False:
                            dest = None
                        case.check_dirs(self, node, repo, dest)


    def plot(self):
        """
        Plot data.
        """
        if self.__plotter:
            for l, s in self.studies:
                self.reporting('  o Plot study: ' + l)
                self.__plotter.plot_study(l, s, self.__dis_tex)

        if self.__plotvtk:
            for l, s in self.studies:
                self.__plotvtk.plot_study(l, s)


    def build_reports(self, report1, report2):
        """
        @type report1: C{String}
        @param report1: name of the global report.
        @type report2: C{String}
        @param report2: name of the detailed report.
        @rtype: C{List} of C{String}
        @return: list of file to be attached to the report.
        """
        attached_files = []

        # Fisrt global report
        doc1 = Report1(self.dest,
                       report1,
                       self.__log,
                       self.report,
                       self.__parser.write())

        for l, s in self.studies:
            for case in s.Cases:
                if case.diff_value:
                    is_nodiff = "KO"
                else:
                    is_nodiff = "OK"

                doc1.add_row(l,
                             case.label,
                             case.is_compil,
                             case.is_run,
                             case.is_time,
                             case.is_compare,
                             is_nodiff)

        attached_files.append(doc1.close())

        # Second detailed report
        if self.__compare or self.__postpro:
            doc2 = Report2(self.dest, report2, self.__log)

            for l, s in self.studies:
                doc2.appendLine("\\section{%s}" % l)

                if s.matplotlib_figures or s.vtk_figures:
                    doc2.appendLine("\\subsection{Graphical results}")
                    for g in s.matplotlib_figures:
                        doc2.addFigure(g)
                    for g in s.vtk_figures:
                        doc2.addFigure(g)

                for case in s.Cases:
                    if case.is_compare == "done":
                        run_id = None
                        if case.run_id != "":
                            run_id = case.run_id
                        doc2.appendLine("\\subsection{Comparison for case %s (run_id: %s)}" % (case.label, run_id))
                        if case.diff_value:
                            doc2.add_row(case.diff_value, l, case.label)
                        elif self.__compare:
                            doc2.appendLine("No difference between the repository and the destination.")

                    # handle the input nodes that are inside case nodes
                    if case.plot == "on" and case.is_run != "KO":
                        nodes = self.__parser.getChilds(case.node, "input")
                        if nodes:
                            doc2.appendLine("\\subsection{Results for case %s}" % case.label)
                            for node in nodes:
                                f, dest, repo, tex = self.__parser.getInput(node)
                                doc2.appendLine("\\subsubsection{%s}" % f)

                                if dest:
                                    d = dest
                                    dd = self.dest
                                elif repo:
                                    d = repo
                                    dd = self.repo
                                else:
                                    d = ""
                                    dd = ""

                                ff = os.path.join(dd, l, case.label, "RESU", d, f)

                                if not os.path.isfile(ff):
                                    print("\n\nWarning: this file does not exist: %s\n\n" % ff)
                                elif tex == 'on':
                                    doc2.addTexInput(ff)
                                else:
                                    doc2.addInput(ff)

                # handle the input nodes that are inside postpro nodes
                if self.__postpro:
                    script, label, nodes, args = self.__parser.getPostPro(l)
                    doc2.appendLine("\\subsection{Results for post-processing cases}")
                    for i in range(len(label)):
                        if script[i]:
                            input_nodes = self.__parser.getChilds(nodes[i], "input")
                            if input_nodes:
                                for node in input_nodes:
                                    f, dest, repo, tex = self.__parser.getInput(node)
                                    doc2.appendLine("\\subsubsection{%s}" % f)

                                    if dest:
                                        d = dest
                                        dd = self.dest
                                    elif repo:
                                        d = repo
                                        dd = self.repo
                                    else:
                                        d = ""
                                        dd = ""

                                    ff = os.path.join(dd, l, "POST", d, f)

                                    if not os.path.isfile(ff):
                                        print("\n\nWarning: this file does not exist: %s\n\n" % ff)
                                    elif tex == 'on':
                                        doc2.addTexInput(ff)
                                    else:
                                        doc2.addInput(ff)

            attached_files.append(doc2.close())

        return attached_files


    def getlabel(self):
        return self.labels


    def logs(self):
        try:
            self.reportFile.close()
        except:
            pass
        self.reportFile = open(self.report, mode='r')
        return self.reportFile.read()


    def __del__(self):
        try:
            self.__log.close()
        except:
            pass
        try:
            self.reportFile.close()
        except:
            pass

#-------------------------------------------------------------------------------