1#!/usr/local/bin/python3.8
2
3# python3 status: compatible
4
5# ---------------------------------------------------------------------------
6# test python modules
7
8import sys
9from afnipy import module_test_lib
10
11g_help_string = """
12===========================================================================
13python_module_test.py   - test the loading of python modules
14
15   The default behavior of this program is to verify whether a 'standard'
16   list of python modules can be loaded.  The 'standard' list amounds to
17   what is needed for the python programs in AFNI.
18
19   The user may specify a list of python modules to test.
20
21------------------------------------------------------------
22examples:
23
24   a. Use the default behavior to test modules in standard list.
25
26      python_module_test.py
27
28   b. Test a specific list of modules in verbose mode.
29
30      python_module_test.py -test_modules sys os numpy scipy R wx -verb 2
31
32   c. Show the python version and platform information.
33
34      python_module_test.py -python_ver -platform_info
35
36   d. Perform a complete test (applies commands a and c).
37
38      python_module_test.py -full_test
39
40------------------------------------------------------------
41informational options:
42
43   -help                        : display this help
44   -hist                        : display the modification history
45   -show_valid_opts             : display all valid options (short format)
46   -ver                         : display the version number
47
48----------------------------------------
49other options:
50
51   -full_test                   : perform all of the standard tests
52
53      This option applies -platform_info, -python_ver and -test_defaults.
54
55   -platform_info               : display system information
56
57      Platform information can include the OS and version, along with the
58      CPU type.
59
60   -python_ver                  : display the version of python in use
61
62      Show which version of python is being used by the software.
63
64   -test_defaults               : test the default module list
65
66      The default module list will include (hopefully) all python modules
67      used by AFNI programs.
68
69      Note that most programs will not need all of these python libraries.
70
71    -test_modules MOD1 MOD2 ... : test the specified module list
72
73      Perform the same test, but on the modules specified with this option.
74
75    -verb LEVEL                 : specify a verbose level
76
77----------------------------------------
78R Reynolds  30 Oct 2008
79===========================================================================
80"""
81
82g_history = """
83    python_module_test.py history:
84
85    0.1  Oct 30, 2008: submitted
86    0.2  Nov 06, 2008: added -full_test
87    0.3  Nov 21, 2008:
88         - removed R from basic test list
89         - in base usage, set verb level to 2
90    0.4  Apr 05, 2011: added PyQt4 to test list
91    0.5  Aug 12, 2019: python3 compatible
92"""
93
94g_version = "version 0.5, August 12, 2019"
95
96
97# main module for defining and processing use options
98class ModuleTest:
99   # local so not in basic usage stream
100   def __init__(self):
101      self.valid_opts     = None
102      self.user_opts      = None
103      self.verb           = 1
104
105      self.modlist        = []
106      self.show_modtest   = 0
107      self.show_platform  = 0
108      self.show_pyver     = 0
109
110      self.OL             = None        # store option_list module locally
111
112   def init_opts(self):
113      from afnipy import option_list
114      self.OL = option_list
115
116      self.valid_opts = self.OL.OptionList('valid options')
117
118      # terminal arguments
119      self.valid_opts.add_opt('-help', 0, [],                           \
120                      helpstr='display program help')
121      self.valid_opts.add_opt('-hist', 0, [],                           \
122                      helpstr='display program history')
123      self.valid_opts.add_opt('-ver', 0, [],                            \
124                      helpstr='display program version')
125      self.valid_opts.add_opt('-show_valid_opts', 0, [],                \
126                      helpstr='display valid program options')
127
128      # options
129      self.valid_opts.add_opt('-full_test', 0, [],                      \
130                      helpstr='test default modules and get python info')
131
132      self.valid_opts.add_opt('-platform_info', 0, [],                  \
133                      helpstr='display platform information')
134
135      self.valid_opts.add_opt('-python_ver', 0, [],                     \
136                      helpstr='display python version')
137
138      self.valid_opts.add_opt('-test_defaults', 0, [],                  \
139                      helpstr='test default module list')
140
141      self.valid_opts.add_opt('-test_modules', -1, [],                  \
142                      helpstr='test listed modules')
143
144      self.valid_opts.add_opt('-verb', 1, [],                           \
145                      helpstr='set verbose level')
146
147   def read_opts(self):
148      """check for terminal arguments, then read user options"""
149
150      # process any optlist_ options
151      self.valid_opts.check_special_opts(sys.argv)
152
153      # ------------------------------------------------------------
154      # terminal arguments, first
155
156      # cannot have len(argv) <= 1 here, but be consistent with other progs
157      if len(sys.argv) <= 1 or '-help' in sys.argv:
158         print(g_help_string)
159         return 0
160
161      if '-hist' in sys.argv:
162         print(g_history)
163         return 0
164
165      if '-ver' in sys.argv:
166         print(g_version)
167         return 0
168
169      if '-show_valid_opts' in sys.argv:
170         self.valid_opts.show('', 1)
171         return 0
172
173      # ------------------------------------------------------------
174      # read all user options
175
176      self.user_opts = self.OL.read_options(sys.argv, self.valid_opts)
177      if not self.user_opts: return 1         # error condition
178
179      return None     # normal completion
180
181   def process_opts(self):
182      """apply each option"""
183
184      # ----------------------------------------
185      # set verb first
186      self.verb, err = self.user_opts.get_type_opt(int, '-verb')
187      if self.verb == None: self.verb = 1
188      elif err: return 1
189
190      if self.user_opts.find_opt('-full_test'):
191         self.modlist = [] # pass nothing to use defaults
192         self.show_modtest = 1
193         self.show_platform = 1
194         self.show_pyver = 1
195
196      if self.user_opts.find_opt('-test_defaults'):
197         self.modlist = [] # pass nothing to use defaults
198         self.show_modtest = 1
199      else:
200         self.modlist, err = self.user_opts.get_string_list('-test_modules')
201         if self.modlist: self.show_modtest = 1  # then show it
202
203      if self.user_opts.find_opt('-platform_info'):
204         self.show_platform = 1
205
206      if self.user_opts.find_opt('-python_ver'):
207         self.show_pyver = 1
208
209      return None     # normal completion
210
211   def execute(self):
212
213      if self.show_pyver:
214         if self.verb <= 1:
215            vlist = sys.version.split()
216            vstr = vlist[0]
217         else:
218            vstr = sys.version
219         print('python version: %s' % vstr)
220
221      if self.show_platform:
222         try:
223            import platform
224            print('platform: %s' % platform.platform())
225         except: print('platform: ** module not found')
226
227      if self.show_modtest:
228         nfail = module_test_lib.num_import_failures(self.modlist,
229                                     details=1,verb=self.verb)
230         print("\nnumber of python import failures = %d\n" % nfail)
231
232      return None
233
234   def show_failed_command(self):
235      from afnipy import afni_util as UTIL
236      UTIL.show_args_as_command(sys.argv,"** failed command:")
237
238def process():
239   if len(sys.argv) <= 1:       # default behavior, run general test
240      print("")
241      nfail = module_test_lib.num_import_failures(verb=2)
242      print("\nnumber of python import failures = %d\n" % nfail)
243      return nfail
244
245   # create elemnnt and initialize options
246   MT = ModuleTest()
247   MT.init_opts()
248
249   # read options, return on terminal option or bad usage
250   rv = MT.read_opts()
251   if rv != None:
252      if rv: MT.show_failed_command()
253      return rv
254
255   # apply options
256   rv = MT.process_opts()
257   if rv != None:
258      if rv: MT.show_failed_command()
259      return rv
260
261   # do stuff
262   rv = MT.execute()
263   if rv != None:
264      if rv: MT.show_failed_command()
265      return rv
266
267if __name__ == '__main__':
268   rv = process()
269   sys.exit(rv)
270