Salome HOME
[test] New test to validate telemac catalogs
[modules/hydrosolver.git] / doc / salome / examples / hs_001_telemac_cata.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Modules Python
5 import sys
6 from os import path, listdir, system, chdir, environ, remove
7 from argparse import ArgumentParser
8 import shutil
9 import re
10
11 import salome
12 from execution.telemac_cas import TelemacCas, get_dico
13
14 # Adding Eficas to PYTHONPATH
15 sys.path.append(environ["EFICAS_NOUVEAU_ROOT"])
16 try:
17     import Telemac.prefs
18 except ImportError as excp:
19     print("Add the path to eficas to PYTHONPATH")
20 if hasattr(Telemac.prefs, 'encoding'):
21     # Hack pour changer le codage par defaut des strings
22     import sys
23     reload(sys)
24     sys.setdefaultencoding(prefs.encoding)
25     del sys.setdefaultencoding
26     # Fin hack
27
28 HEADER = '\033[95m'
29 OKBLUE = '\033[94m'
30 OKGREEN = '\033[92m'
31 WARNING = '\033[93m'
32 FAIL = '\033[91m'
33 ENDC = '\033[0m'
34 BOLD = '\033[1m'
35 UNDERLINE = '\033[4m'
36
37 PREFIX = {'telemac2d':'t2d',
38           'telemac3d':'t3d',
39           'tomawac':'tom',
40           'sisyphe':'sis',
41           'artemis':'art',
42           'waqtel':'waq'}
43
44 def diff_cas(cas1, cas2):
45     """
46     Diff between two cases will return True if cas.get for each keyword (in
47     both files) returns the same thing
48
49     @param cas1 (TelemacCas) cas1
50     @param cas2 (TelemacCas) cas2
51
52     @returns True if identical
53     """
54
55     # Get the list of keys from cas1 and cas2
56     keys = list(cas1.values.keys())
57     keys.extend(key for key in cas2.values.keys() if key not in keys)
58
59     diff = True
60     for key in keys:
61         val1 = cas1.get(key)
62         val2 = cas2.get(key)
63         if val1 != val2:
64             diff = False
65             print("Differences for {}\n{}: {}\n{}:"\
66                   .format(key,
67                           path.basename(cas1.file_name),
68                           val1,
69                           path.basename(cas2.file_name),
70                           val2,
71                           ))
72
73     return diff
74
75
76 def read_write_eficas(module, steering_file, eficas_steering):
77     """
78     Import a steergin file in Eficas and save it back
79
80     @param module Name of the telemac-mascaret module
81     @param steering_file Name of the steering file
82     @param eficas_file Name of the steering file written by Eficas
83     """
84     from InterfaceQT4.eficas_go import getEficasSsIhm
85     code = 'TELEMAC'
86     my_eficas = getEficasSsIhm(code='TELEMAC',labelCode=module)
87
88     handler = my_eficas.fileOpen(steering_file)
89     if not handler:
90         raise Exception(steering_file, "Eficas crashed")
91     if not handler.isJdcValid():
92         report = handler.getJdcRapport()
93         raise Exception(steering_file, report)
94
95     handler.fileSaveAs(eficas_steering)
96
97 def validate_catalog(module, root_dir):
98     """
99     Validate a given Catalog for a given module
100
101     @param module Name of the module
102     @param root_dir Telemac root path
103     """
104     print(" "*2, "~> For module", module)
105     examples_dir = path.join(root_dir, 'examples', module)
106     output_dir = path.join(root_dir, 'examples', 'eficas')
107
108     telemac_dico = path.join(root_dir, 'sources', module, module+'.dico')
109
110     crashed = []
111     different = []
112     for example in sorted(listdir(examples_dir)):
113         example_dir = path.join(examples_dir, example)
114         chdir(example_dir)
115         print("  "*3, "~> In example folder ", example_dir)
116         for case in sorted(listdir(example_dir)):
117             if case.endswith('.cas') and \
118                "_reecrit" not in case and \
119                case[0:3] == PREFIX[module] and \
120                "_reecrit" not in case and \
121                "_ori" not in case:
122                 # Adding lang extension (.fr for translated french case)
123                 print("  "*4, "~> For test case ", case)
124                 root, _ = path.splitext(case)
125
126                 lang = ''
127                 eficas_case = root + "_reecrit.cas"
128
129                 # Import and export in eficas
130                 try:
131                     read_write_eficas(module, case, eficas_case)
132                 except Exception as e:
133                     print(e)
134                     crashed.append(case)
135                     print(" "*8+FAIL+"FAILED"+ENDC)
136                     print(" "*8+"Crashed in eficas")
137                     continue
138
139                 ori_cas = TelemacCas(case, telemac_dico)
140                 eficas_cas = TelemacCas(eficas_case, telemac_dico)
141
142                 isdiff = diff_cas(ori_cas, eficas_cas)
143
144                 if not isdiff:
145                     different.append(case)
146                     print(" "*8+FAIL+"FAILED"+ENDC)
147                     print(" "*8+"Diff in steering case")
148                     continue
149
150                 # Clean up of files
151                 remove(eficas_case)
152
153                 # Passed the test case
154                 print(" "*8+OKGREEN+"PASSED"+ENDC)
155
156     if crashed != []:
157         print("The following test in", module,
158               " crashed in eficas:", crashed)
159     if different != []:
160         print("The following test in", module,
161               " have a difference with normal run:", different)
162
163 salome.salome_init()
164
165 #-----------------
166 #-- Eficas
167 #-----------------
168
169 for module in ['telemac2d', 'telemac3d']:
170     # Testing loading of catalog
171     validate_catalog(module, environ["HOMETEL"])
172