1 | scr = __name__ == '__main__' |
---|
2 | try: |
---|
3 | from __init__ import DOC_DIR |
---|
4 | except: |
---|
5 | from dreqPy.__init__ import DOC_DIR |
---|
6 | ##if scr: |
---|
7 | ##from __init__ import DOC_DIR |
---|
8 | ##else: |
---|
9 | ##from . import __init__ |
---|
10 | ##DOC_DIR = __init__.DOC_DIR |
---|
11 | |
---|
12 | import os, sys, collections |
---|
13 | |
---|
14 | usingPython3 = sys.version_info >= (3,0) |
---|
15 | |
---|
16 | try: |
---|
17 | import pkgutil |
---|
18 | l = pkgutil.iter_modules() |
---|
19 | ll = map( lambda x: x[1], l ) |
---|
20 | pkgutilFailed=False |
---|
21 | except: |
---|
22 | pkgutilFailed=True |
---|
23 | print ( 'Failed to load pkgutil .. more limited tests on available modules will be done' ) |
---|
24 | ll = [] |
---|
25 | |
---|
26 | requiredModules = ['xml'] |
---|
27 | confirmed = [] |
---|
28 | installFailed = [] |
---|
29 | missingLib = [] |
---|
30 | for x in requiredModules: |
---|
31 | if x in ll or pkgutilFailed: |
---|
32 | try: |
---|
33 | __import__(x) |
---|
34 | confirmed.append( x ) |
---|
35 | except: |
---|
36 | installFailed.append( x ) |
---|
37 | print ( 'Failed to install %s' % x ) |
---|
38 | else: |
---|
39 | missingLib.append( x ) |
---|
40 | |
---|
41 | if len( missingLib ) > 0 or len(installFailed) > 0: |
---|
42 | print ( 'Could not load all required python libraries' ) |
---|
43 | if len(missingLib) > 0: |
---|
44 | print ( 'MISSING LIBRARIES: %s' % str(missingLib) ) |
---|
45 | if len(installFailed) > 0: |
---|
46 | print ( 'LIBRARIES PRESENT BUT FAILED TO INSTALL:%s' % str(missingLib) ) |
---|
47 | all = False |
---|
48 | exit(0) |
---|
49 | else: |
---|
50 | print ( 'Required libraries present' ) |
---|
51 | all = True |
---|
52 | |
---|
53 | import inspect |
---|
54 | class checkbase(object): |
---|
55 | def __init__(self,lab): |
---|
56 | self.lab = lab |
---|
57 | self.ok = True |
---|
58 | self.entryPath = sys.argv[0] |
---|
59 | self.entryPoint = sys.argv[0].split( '/' )[-1] |
---|
60 | self.entryDir = self.entryPath.replace( self.entryPoint, '' ) |
---|
61 | ##if os.path.isdir( 'out' ): |
---|
62 | ##self.docdir = 'out' |
---|
63 | ##elif os.path.isdir( 'docs' ): |
---|
64 | ##self.docdir = 'docs' |
---|
65 | ##else: |
---|
66 | ##assert False, 'Document directory not found' |
---|
67 | |
---|
68 | #document directory |
---|
69 | self.docdir = DOC_DIR |
---|
70 | #schema location |
---|
71 | self.schema = '%s/dreq2Schema.xsd' % self.docdir |
---|
72 | #sample xml location |
---|
73 | self.sampleXml = '%s/dreq2Sample.xml' % self.docdir |
---|
74 | #definition document xml location |
---|
75 | self.defnXml = '%s/dreq2Defn.xml' % self.docdir |
---|
76 | |
---|
77 | ml = inspect.getmembers( self, predicate=inspect.ismethod ) |
---|
78 | ok = True |
---|
79 | for tag,m in ml: |
---|
80 | if tag[:3] == '_ch': |
---|
81 | try: |
---|
82 | self.ok = False |
---|
83 | m() |
---|
84 | ok &= self.ok |
---|
85 | except: |
---|
86 | print ( 'Failed to complete check %s' % tag ) |
---|
87 | raise |
---|
88 | if ok: |
---|
89 | print ( '%s: All checks passed' % lab ) |
---|
90 | else: |
---|
91 | print ( '%s: Errors detected' % lab ) |
---|
92 | |
---|
93 | class check1(checkbase): |
---|
94 | def _ch01_importDreq(self): |
---|
95 | try: |
---|
96 | import dreq |
---|
97 | except: |
---|
98 | from . import dreq |
---|
99 | print ( 'Dreq software import checked: %s' % dreq.version ) |
---|
100 | self.ok = True |
---|
101 | |
---|
102 | def _ch02_importSample(self): |
---|
103 | try: |
---|
104 | import dreq |
---|
105 | except: |
---|
106 | from . import dreq |
---|
107 | self.dq = dreq.loadDreq( manifest='%s/dreqManifest.txt' % self.docdir ) |
---|
108 | print ( 'Dreq sample load checked' ) |
---|
109 | self.ok = True |
---|
110 | |
---|
111 | def _ch03_linkCheck(self): |
---|
112 | try: |
---|
113 | import dreq |
---|
114 | except: |
---|
115 | from . import dreq |
---|
116 | |
---|
117 | nn = 0 |
---|
118 | self.dq = dreq.loadDreq( manifest='%s/dreqManifest.txt' % self.docdir ) |
---|
119 | for section in self.dq.coll : |
---|
120 | ks=[k for k in self.dq.coll[section].attDefn.keys() if self.dq.coll[section].attDefn[k].useClass == 'internalLink'] |
---|
121 | nerr = 0 |
---|
122 | cc = collections.defaultdict( int ) |
---|
123 | for i in self.dq.coll[section].items: |
---|
124 | for k in ks : |
---|
125 | if k in i.__dict__: |
---|
126 | if i.__dict__[k] not in self.dq.inx.uid: |
---|
127 | nerr += 1 |
---|
128 | cc[k] += 1 |
---|
129 | print ('Bad link found: section: %s: %s %s [%s]' % (section, k, i.__dict__[k], i.uid) ) |
---|
130 | if nerr > 0: |
---|
131 | msg = '' |
---|
132 | for k in cc: |
---|
133 | msg += '%s: %s; ' % (k,cc[k]) |
---|
134 | print ( 'Section %s: bad links: %s %s' % (section,nerr,msg) ) |
---|
135 | nn += nerr |
---|
136 | ##print section, ks, nerr |
---|
137 | if nn == 0: |
---|
138 | print ( 'Dreq links checked' ) |
---|
139 | self.ok = nn == 0 |
---|
140 | |
---|
141 | class check2(checkbase): |
---|
142 | |
---|
143 | def _clear_ch03(self): |
---|
144 | os.unlink( '.simpleCheck_check2_err.txt' ) |
---|
145 | os.unlink( '.simpleCheck_check2.txt' ) |
---|
146 | |
---|
147 | def _clear_ch04(self): |
---|
148 | os.unlink( '.simpleCheck_check2_err.txt' ) |
---|
149 | os.unlink( '.simpleCheck_check2.txt' ) |
---|
150 | |
---|
151 | def _getCmd(self): |
---|
152 | if self.entryPoint in ['drq', 'drqTest']: |
---|
153 | self.cmd = self.entryPoint |
---|
154 | else: |
---|
155 | if usingPython3: |
---|
156 | self.cmd = 'python3 %sdreqCmdl.py' % self.entryDir |
---|
157 | else: |
---|
158 | self.cmd = 'python2.7 %sdreqCmdl.py' % self.entryDir |
---|
159 | |
---|
160 | def _ch05_checkMcfg(self): |
---|
161 | self._getCmd() |
---|
162 | thisCmd = '%s -m CMIP -e historical --mcfg 259200,60,64800,40,20,5,100' % self.cmd |
---|
163 | os.popen( '%s 2> .simpleCheck_check5_err.txt 1>.simpleCheck_check5.txt' % thisCmd ).readlines() |
---|
164 | |
---|
165 | ii = open( '.simpleCheck_check5_err.txt' ).readlines() |
---|
166 | if len(ii) > 0: |
---|
167 | print ( 'WARNING[005]: failed to get volume est. with command line call: %s' % self.cmd ) |
---|
168 | self.ok = False |
---|
169 | ##self._clear_ch04() |
---|
170 | return |
---|
171 | |
---|
172 | ii = open( '.simpleCheck_check5.txt' ).readlines() |
---|
173 | if len(ii) < 1: |
---|
174 | print ( 'WARNING[006]: failed to get get volume est. with command line call' ) |
---|
175 | self.ok = False |
---|
176 | ##self._clear_ch04() |
---|
177 | return |
---|
178 | |
---|
179 | self.ok = True |
---|
180 | return |
---|
181 | |
---|
182 | def _ch04_checkCmd(self): |
---|
183 | import os |
---|
184 | self._getCmd() |
---|
185 | |
---|
186 | os.popen( '%s -v 2> .simpleCheck_check2_err.txt 1>.simpleCheck_check2.txt' % self.cmd ).readlines() |
---|
187 | |
---|
188 | ii = open( '.simpleCheck_check2_err.txt' ).readlines() |
---|
189 | if len(ii) > 0: |
---|
190 | print ( 'WARNING[003]: failed to get version with command line call' ) |
---|
191 | self.ok = False |
---|
192 | self._clear_ch04() |
---|
193 | return |
---|
194 | |
---|
195 | ii = open( '.simpleCheck_check2.txt' ).readlines() |
---|
196 | if len(ii) < 1: |
---|
197 | print ( 'WARNING[004]: failed to get version with command line call' ) |
---|
198 | self.ok = False |
---|
199 | self._clear_ch04() |
---|
200 | return |
---|
201 | |
---|
202 | self.ok = True |
---|
203 | return |
---|
204 | |
---|
205 | def _ch03_checkXML(self): |
---|
206 | import os |
---|
207 | os.popen( 'which xmllint 2> .simpleCheck_check2_err.txt 1>.simpleCheck_check2.txt' ).readlines() |
---|
208 | ii = open( '.simpleCheck_check2_err.txt' ).readlines() |
---|
209 | if len(ii) > 0: |
---|
210 | print ( 'WARNING[001]: failed to detect xmllint command line program' ) |
---|
211 | print ( 'optional checks omitted' ) |
---|
212 | self.ok = False |
---|
213 | self._clear_ch03() |
---|
214 | return |
---|
215 | ii = open( '.simpleCheck_check2.txt' ).readlines() |
---|
216 | if len(ii) < 1: |
---|
217 | print ( 'WARNING[002]: failed to detect xmllint command line program' ) |
---|
218 | print ( 'Optional checks omitted' ) |
---|
219 | self.ok = False |
---|
220 | self._clear_ch03() |
---|
221 | return |
---|
222 | |
---|
223 | cmd = 'xmllint --noout --schema %s %s 2> .simpleCheck_check2_err.txt 1>.simpleCheck_check2.txt' % (self.schema,self.sampleXml) |
---|
224 | os.popen( cmd ).readlines() |
---|
225 | ii = open( '.simpleCheck_check2_err.txt' ).readlines() |
---|
226 | if len(ii) == 0: |
---|
227 | print ( 'WARNING[003]: Failed to capture xmllint response' ) |
---|
228 | print ( cmd ) |
---|
229 | self.ok = False |
---|
230 | self._clear_ch03() |
---|
231 | return |
---|
232 | if ii[0].find('validates') != -1: |
---|
233 | print ( 'Sample XML validated' ) |
---|
234 | self.ok = True |
---|
235 | self._clear_ch03() |
---|
236 | else: |
---|
237 | print ( 'Sample XML failed to validate' ) |
---|
238 | print ( cmd ) |
---|
239 | self.ok = False |
---|
240 | return |
---|
241 | |
---|
242 | all &= check1('Suite 1 (dreq module)').ok |
---|
243 | all &= check2('Suite 2 (xmllint & command line)').ok |
---|
244 | |
---|
245 | if all: |
---|
246 | print ( 'ALL CHECK PASSED' ) |
---|