annotate data_stores/tester.py @ 2:269d246ce6d0 draft default tip

Uploaded
author damion
date Fri, 23 Oct 2015 17:53:29 -0400
parents 5c5027485f7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
1 #!/usr/bin/python
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
2 # -*- coding: utf-8 -*-
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
3 import optparse
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
4 import sys
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
5 import difflib
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
6 import os
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
7
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
8
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
9 class MyParser(optparse.OptionParser):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
10 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
11 From http://stackoverflow.com/questions/1857346/python-optparse-how-to-include-additional-info-in-usage-output
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
12 Provides a better class for displaying formatted help info in epilog() portion of optParse; allows for carriage returns.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
13 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
14 def format_epilog(self, formatter):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
15 return self.epilog
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
16
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
17 def stop_err( msg ):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
18 sys.stderr.write("%s\n" % msg)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
19 sys.exit(1)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
20
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
21
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
22 def __main__(self):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
23
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
24
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
25 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
26 (This is run only in context of command line.)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
27 FUTURE: ALLOW GLOB IMPORT OF ALL FILES OF GIVEN SUFFX, each to its own version, initial date taken from file date
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
28 FUTURE: ALLOW dates of versions to be adjusted.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
29 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
30 options, args = self.get_command_line()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
31 self.options = options
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
32
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
33 if options.test_ids:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
34 return self.test(options.test_ids)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
35
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
36
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
37 def get_command_line(self):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
38 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
39 *************************** Parse Command Line *****************************
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
40
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
41 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
42 parser = MyParser(
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
43 description = 'Tests a program against given input/output files.',
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
44 usage = 'tester.py [program] [input files] [output files] [parameters]',
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
45 epilog="""
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
46
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
47
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
48 parser.add_option('-t', '--tests', dest='test_ids', help='Enter "all" or comma-separated id(s) of tests to run.')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
49
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
50 return parser.parse_args()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
51
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
52
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
53
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
54 def test(self, test_ids):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
55 # Future: read this spec from test-data folder itself?
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
56 tests = {
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
57 '1': {'input':'a1','outputs':'','options':''}
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
58 }
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
59 self.test_suite('keydb.py', test_ids, tests, '/tmp/')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
60
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
61
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
62 def test_suite(self, program, test_ids, tests, output_dir):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
63
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
64 if test_ids == 'all':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
65 test_ids = sorted(tests.keys())
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
66 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
67 test_ids = test_ids.split(',')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
68
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
69 for test_id in test_ids:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
70 if test_id in tests:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
71 test = tests[test_id]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
72 test['program'] = program
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
73 test['base_dir'] = base_dir = os.path.dirname(__file__)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
74 executable = os.path.join(base_dir,program)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
75 if not os.path.isfile(executable):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
76 stop_err('\n\tUnable to locate ' + executable)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
77 # Each output file has to be prefixed with the output (usualy /tmp/) folder
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
78 test['tmp_output'] = (' ' + test['outputs']).replace(' ',' ' + output_dir)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
79 # Note: output_dir output files don't get cleaned up after each test. Should they?!
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
80 params = '%(base_dir)s/%(program)s %(base_dir)s/test-data/%(input)s%(tmp_output)s %(options)s' % test
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
81 print("Test " + test_id + ': ' + params)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
82 print("................")
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
83 os.system(params)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
84 print("................")
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
85 for file in test['outputs'].split(' '):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
86 try:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
87 f1 = open(test['base_dir'] + '/test-data/' + file)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
88 f2 = open(output_dir + file)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
89 except IOError as details:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
90 stop_err('Error in test setup: ' + str(details)+'\n')
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
91
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
92 #n=[number of context lines
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
93 diff = difflib.context_diff(f1.readlines(), f2.readlines(), lineterm='',n=0)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
94 # One Galaxy issue: it doesn't convert entities when user downloads file.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
95 # BUT IT appears to when generating directly to command line?
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
96 print '\nCompare ' + file
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
97 print '\n'.join(list(diff))
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
98
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
99 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
100 stop_err("\nExpecting one or more test ids from " + str(sorted(tests.keys())))
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
101
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
102 stop_err("\nTest finished.")
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
103
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
104
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
105 if __name__ == '__main__':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
106
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
107 keydb = KeyDb()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
108 keydb.__main__()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
109