1 | import os |
---|
2 | import sys |
---|
3 | import subprocess |
---|
4 | import socket |
---|
5 | import os.path |
---|
6 | from os import path |
---|
7 | |
---|
8 | |
---|
9 | # from default_param import * |
---|
10 | # from user_param import * |
---|
11 | from param_list import * |
---|
12 | |
---|
13 | # load default_param first, variables defines in user_param will overwrite the default value |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | def OSinfo(runthis): |
---|
18 | red = lambda text: '\033[0;31m' + text + '\033[0m' |
---|
19 | osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) |
---|
20 | theInfo = osstdout.communicate()[0].strip() |
---|
21 | if osstdout.returncode!=0: |
---|
22 | print(red(runthis+" FAILED")) |
---|
23 | print >> sys.stderr, osstdout.returncode |
---|
24 | sys.exit() |
---|
25 | # else: |
---|
26 | # print(runthis+" OK") |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | blue = lambda text: '\033[0;34m' + text + '\033[0m' |
---|
31 | yellow = lambda text: '\033[0;33m' + text + '\033[0m' |
---|
32 | green = lambda text: '\033[0;32m' + text + '\033[0m' |
---|
33 | # blue = lambda text: text |
---|
34 | # yellow = lambda text: text |
---|
35 | # green = lambda text: text |
---|
36 | |
---|
37 | def main(): |
---|
38 | |
---|
39 | defaultConfig = True |
---|
40 | configNamePattern=[] |
---|
41 | for i in range(len(param_list)): |
---|
42 | f=open("user_param.py", "r") |
---|
43 | for line in f: |
---|
44 | line = line.strip().replace(" ","") |
---|
45 | if line.startswith(param_list[i]): |
---|
46 | configNamePattern.append('_'+param_short_list[i]+"="+line.split("=")[1].replace("'","")) |
---|
47 | defaultConfig = False |
---|
48 | |
---|
49 | f.close() |
---|
50 | |
---|
51 | configName = "config" |
---|
52 | |
---|
53 | for i in range(len(configNamePattern)): |
---|
54 | configName = configName+configNamePattern[i] |
---|
55 | |
---|
56 | checkfile = "checkfile.def" |
---|
57 | |
---|
58 | f=open(checkfile, "r") |
---|
59 | for line in f: |
---|
60 | line = line.strip() |
---|
61 | line = line.rstrip() |
---|
62 | if line.startswith("all"): |
---|
63 | allfiles = os.listdir(os.getcwd()) |
---|
64 | g=open("checkfile_new.def", "w") |
---|
65 | for thisfile in allfiles: |
---|
66 | if thisfile.endswith(".nc") and thisfile != "dynamico_grid.nc": |
---|
67 | g.write(thisfile+"\n") |
---|
68 | checkfile = "checkfile_new.def" |
---|
69 | g.close() |
---|
70 | break |
---|
71 | f.close() |
---|
72 | |
---|
73 | if not os.path.exists(configName): |
---|
74 | OSinfo("mkdir -p "+configName) |
---|
75 | |
---|
76 | print(blue("\n ****************************************************************")) |
---|
77 | print(blue(" * Directory " + repr(configName) + " Created ")) |
---|
78 | print(blue(" * Copy results to \'not_validated_reference\' folder")) |
---|
79 | print(blue(" ****************************************************************\n")) |
---|
80 | |
---|
81 | OSinfo("mkdir -p "+configName+"/setup") |
---|
82 | OSinfo("mkdir -p "+configName+"/tmp_reference") |
---|
83 | |
---|
84 | OSinfo("cp user_param.py user_param.def") |
---|
85 | OSinfo("cp user_param.py "+configName+"/setup") |
---|
86 | OSinfo("mv user_param.def "+configName+"/setup") |
---|
87 | |
---|
88 | OSinfo("cp default_param.json "+configName+"/setup") |
---|
89 | |
---|
90 | OSinfo("mv all_param.def "+configName+"/setup") |
---|
91 | OSinfo("mv iodef.xml "+configName+"/setup/") |
---|
92 | OSinfo("cp context_atm.xml "+configName+"/setup/") |
---|
93 | OSinfo("cp context_grid_dynamico.xml "+configName+"/setup/") |
---|
94 | |
---|
95 | |
---|
96 | f=open(checkfile, "r") |
---|
97 | reportfile = open("../plain_report.txt", "a+") |
---|
98 | for line in f: |
---|
99 | line = line.strip() |
---|
100 | line = line.rstrip() |
---|
101 | if not line.startswith("#") and len(line) != 0: |
---|
102 | OSinfo("cp "+line+" "+configName+"/tmp_reference") |
---|
103 | reportfile.write(os.path.basename(os.getcwd())+" "+os.path.basename(os.getcwd())[5:]+"@"+configName+" "+os.path.basename(os.getcwd())[5:]+"@"+configName[7:]+"@"+line+" "+repr(0)+" \n") |
---|
104 | print(yellow("\n ****************************************")) |
---|
105 | print(yellow(" ** "+line+" is stored as temporal reference !!! **")) |
---|
106 | print(yellow("\n ****************************************")) |
---|
107 | f.close() |
---|
108 | reportfile.close() |
---|
109 | |
---|
110 | else: |
---|
111 | print(blue("\n ****************************************************************")) |
---|
112 | print(blue(" * Directory " + repr(configName) + " already exists ")) |
---|
113 | |
---|
114 | tmp_ref = os.path.isdir(configName+'/tmp_reference') |
---|
115 | ref = os.path.isdir(configName+'/reference') and not tmp_ref |
---|
116 | |
---|
117 | if tmp_ref : |
---|
118 | print(blue(" * Start comparing results with NON-validated references")) |
---|
119 | path = configName+"/tmp_reference" |
---|
120 | if ref : |
---|
121 | print(blue(" * Start comparing results with validated references")) |
---|
122 | path = configName+"reference" |
---|
123 | |
---|
124 | OSinfo('cp '+configName+'/setup/user_param.py '+configName+'/setup/user_param.def ') |
---|
125 | print(blue(" ****************************************************************\n")) |
---|
126 | |
---|
127 | f=open(checkfile, "r") |
---|
128 | reportfile = open("../plain_report.txt", "a+") |
---|
129 | for line in f: |
---|
130 | line = line.strip() |
---|
131 | line = line.rstrip() |
---|
132 | if not line.startswith("#") and len(line) != 0: |
---|
133 | OSinfo("cdo -W diffn "+line+" "+path+line+" | > diff.txt") |
---|
134 | # this command should change according to cdo version |
---|
135 | |
---|
136 | if os.stat("diff.txt").st_size==0: |
---|
137 | reportfile.write(os.path.basename(os.getcwd())+" "+os.path.basename(os.getcwd())[5:]+"@"+configName+" "+os.path.basename(os.getcwd())[5:]+"@"+configName[7:]+"@"+line+" "+repr(1)+" \n") |
---|
138 | print(green("\n ****************************************")) |
---|
139 | print(green(" ** "+line+" is valid !!! **")) |
---|
140 | print(green("\n ****************************************")) |
---|
141 | else: |
---|
142 | g=open("diff.txt", "r") |
---|
143 | for gline in g: |
---|
144 | if gline.strip().startswith("0") or ":" in gline: |
---|
145 | reportfile.write(os.path.basename(os.getcwd())+" "+os.path.basename(os.getcwd())[5:]+"@"+configName+" "+os.path.basename(os.getcwd())[5:]+"@"+configName[7:]+"@"+line+" "+repr(1)+" \n") |
---|
146 | print(green("\n ****************************************")) |
---|
147 | print(green(" ** "+line+" is valid !!! **")) |
---|
148 | print(green("\n ****************************************")) |
---|
149 | else: |
---|
150 | reportfile.write(os.path.basename(os.getcwd())+" "+os.path.basename(os.getcwd())[5:]+"@"+configName+" "+os.path.basename(os.getcwd())[5:]+"@"+configName[7:]+"@"+line+" "+repr(-1)+" \n") |
---|
151 | print(green("\n ****************************************")) |
---|
152 | print(green(" ** "+line+" is NOT valid. Please debugging.. **")) |
---|
153 | print(green("\n ****************************************")) |
---|
154 | g.close() |
---|
155 | f.close() |
---|
156 | reportfile.close() |
---|
157 | |
---|
158 | if os.path.exists("checkfile_new.def"): |
---|
159 | OSinfo("rm -f checkfile_new.def") |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | if __name__== "__main__": |
---|
164 | main() |
---|