Please note that the CVS and issue trackers have moved to GitHub. These Trac pages are no longer kept up-to-date.

root/seattle/branches/nacl_repy/preparetest.py@5149

Revision 5149, 7.0 KB (checked in by cmatthew, 7 years ago)

Changed prepare test to account for preprocessing errors.

Line 
1"""
2<Author>
3  Cosmin Barsan
4 
5  Edited to add an optional argument to also copy the repy tests by
6  Brent Couvrette on November 13, 2008.
7
8  Conrad Meyer, Thu Nov 26 2009: Move dynamic ports code from run_tests.py
9  to preparetest.py.
10<Start Date>
11  October 3, 2008
12
13<Description>
14  This script has been adapted from the bash script preparetest.  The
15  script first erases all the files in the target folder, then copies
16  the necessary test files to it. Afterwards, the .mix files in the
17  target folder are run through the preprocessor.  It is required that
18  the folder passed in as an argument to the script exists.
19
20<Usage>
21  preparetest.py <target_folder> <-t>
22
23  if -t is specified, the repy tests will also be included, otherwise, they will not
24
25<Notes>
26  This file is used as a library by trunk/www/deploy_state_transitions.py
27  If you make ANY changes to this file please let Ivan know so that the
28  other script can continue to function correctly. Thanks. (IB 01/19/09)
29
30  This file is also used directly by trunk/dist/make_base_installers.py. Also
31  let Zack know if any adaptions are made to this file so the base installers
32  can continue to be created correctly.  See ticket #501 about removing or
33  adapting make_base_installer.py's dependence on this file.  (Zack 7/2/09)
34
35"""
36
37import sys
38import glob
39import os
40import stat
41import random
42import shutil
43import subprocess
44
45sys.path.insert(0, os.path.join(os.getcwd(), "repy", "tests"))
46import testportfiller
47sys.path = sys.path[1:]
48
49
50#define a function to use for copying the files matching the file expression to the target folder
51#file_expr may contain wildcards
52#target must specify an existing directory with no wildcards
53def copy_to_target(file_expr, target):
54  files_to_copy = glob.glob(file_expr)
55  for file_path in files_to_copy:
56    if os.path.isfile(file_path):
57      shutil.copyfile(file_path,target +"/"+os.path.basename(file_path))
58
59
60#iterate through the .mix files in current folder and run them through the preprocessor
61#script_path must specify the name of the preprocessor script
62#the working directory must be set to the directory containing the preprocessor script prior to executing this function.
63def process_mix(script_path):
64  mix_files = glob.glob("*.mix")
65 
66  for file_path in mix_files:
67    #generate a .py file for the .mix file specified by file_path
68    processed_file_path = (os.path.basename(file_path)).replace(".mix",".py")
69    (theout, theerr, rc) =  exec_command("python " + script_path + " " + file_path + " " + processed_file_path)
70   
71    if rc != 0:
72      print theout.rstrip()
73      print "Error: could not preprocess", file_path, "Stopping."
74      sys.exit(1)
75 
76
77def exec_command(command):
78# Windows does not like close_fds and we shouldn't need it so...
79  p =  subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
80  rc = p.wait()
81  # get the output and close
82  theout = p.stdout.read()
83  p.stdout.close()
84 
85  # get the errput and close
86  theerr = p.stderr.read()
87  p.stderr.close()
88
89  # FreeBSD prints on stdout, when it gets a signal...
90  # I want to look at the last line.   it ends in \n, so I use index -2
91  if len(theout.split('\n')) > 1 and theout.split('\n')[-2].strip() == 'Terminated':
92    # lose the last line
93    theout = '\n'.join(theout.split('\n')[:-2])
94   
95    # however we threw away an extra '\n' if anything remains, let's replace it
96    if theout != '':
97      theout = theout + '\n'
98
99  # everyone but FreeBSD uses stderr
100  if theerr.strip() == 'Terminated':
101    theerr = ''
102
103  return (theout, theerr, rc)
104
105
106helpstring = """python preparetest.py [-t] <foldername>"""
107
108# Prints the given error message and the help string, then exits
109def help_exit(errMsg):
110  print errMsg
111  print helpstring
112  sys.exit(1)
113
114
115def setup_lind_tests(target):
116  """ This copies over serialize to be a py file."""
117
118  # need to copy over serialize.repy to be a python file so it can be imported
119  shutil.copyfile(target+'/serialize.repy',target +"/serialize.py")
120
121
122def main():
123  repytest = False
124  RANDOMPORTS = False
125       
126  target_dir = None
127  for arg in sys.argv[1:]:
128    # -t means we will copy repy tests
129    if arg == '-t':
130      repytest = True
131
132    # The user wants us to fill in the port numbers randomly.
133    elif arg == '-randomports':
134      RANDOMPORTS = True
135
136    # Not a flag? Assume it's the target directory
137    else:
138      target_dir = arg
139
140  # We need a target dir. If one isn't found in argv, quit.
141  if target_dir is None:
142    help_exit("Please pass the target directory as a parameter.")
143
144  #store root directory
145  current_dir = os.getcwd()
146
147  # Make sure they gave us a valid directory
148  if not( os.path.isdir(target_dir) ):
149    help_exit("given foldername is not a directory")
150
151  #set working directory to the test folder
152  os.chdir(target_dir) 
153  files_to_remove = glob.glob("*")
154
155  #clean the test folder
156  for f in files_to_remove: 
157    if os.path.isdir(f):
158      shutil.rmtree(f)         
159    else:
160      os.remove(f)
161
162  #go back to root project directory
163  os.chdir(current_dir) 
164
165  #now we copy the necessary files to the test folder
166  copy_to_target("repy/*", target_dir)
167  copy_to_target("nodemanager/*", target_dir)
168  copy_to_target("portability/*", target_dir)
169  copy_to_target("seattlelib/*", target_dir)
170  copy_to_target("seash/*", target_dir)
171  copy_to_target("shims/*", target_dir)
172  copy_to_target("softwareupdater/*", target_dir)
173  copy_to_target("autograder/nm_remote_api.mix", target_dir)
174  copy_to_target("keydaemon/*", target_dir)
175  # The license must be included in anything we distribute.
176  copy_to_target("LICENSE.TXT", target_dir)
177 
178  if repytest:
179    # Only copy the tests if they were requested.
180    copy_to_target("repy/tests/restrictions.*", target_dir)
181    copy_to_target("utf/*.py", target_dir)
182    copy_to_target("repy/testsV2/*", target_dir)
183    #copy_to_target("nodemanager/tests/*", target_dir)
184    #copy_to_target("portability/tests/*", target_dir)
185    #copy_to_target("seash/tests/*", target_dir)
186    copy_to_target("seattlelib/tests/*", target_dir)
187    #copy_to_target("keydaemon/tests/*", target_dir)
188    copy_to_target("dist/update_crontab_entry.py", target_dir)
189    copy_to_target("shims/tests/*", target_dir)
190
191    # The web server is used in the software updater tests
192    #copy_to_target("assignments/webserver/*", target_dir)
193    #copy_to_target("softwareupdater/test/*", target_dir)
194
195
196  #set working directory to the test folder
197  os.chdir(target_dir)
198
199  #call the process_mix function to process all mix files in the target directory
200  process_mix("repypp.py")
201
202  #insatll lind test files
203  setup_lind_tests(target_dir)
204
205  # set up dynamic port information
206  if RANDOMPORTS:
207    portstouseasints = random.sample(range(52000, 53000), 3)
208    portstouseasstr = []
209    for portint in portstouseasints:
210      portstouseasstr.append(str(portint))
211   
212    print "Randomly chosen ports: ",portstouseasstr
213    testportfiller.replace_ports(portstouseasstr, portstouseasstr)
214  else:
215    # if this isn't specified, just use the default ports...
216    testportfiller.replace_ports(['12345','12346','12347'], ['12345','12346','12347'])
217
218  #go back to root project directory
219  os.chdir(current_dir) 
220
221
222if __name__ == '__main__':
223  main()
Note: See TracBrowser for help on using the browser.