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

root/seattle/trunk/deploymentscripts/create_tar.py@5637

Revision 2810, 3.9 KB (checked in by konp, 10 years ago)

Fixed high cpu usage, added summary tool, a number of other fixes and improvements as well. Note: blackbox node keys have not been added yet.

Line 
1"""
2<Program Name>
3  create_tar.py
4
5<Started>
6  May 2009
7
8<Author>
9  n2k8000@u.washington.edu
10  Konstantin Pik
11
12<Purpose>
13  Builds up a .tar file with the files specified in the files
14  string. This is hardcoded for now as the files shouldn't change
15  and if they do it will be easier to add them to this script (separated
16  by spaces).
17
18  Function creates a file named deploy.tar.
19  TODO: Specify outfilename as a parameter.
20
21<Usage>
22  Currently only takes one extra fn for simplicity & testing
23  ./create_tar.py [extra_fn1 extra_fn2 extra_fnN]
24"""
25
26import os
27import sys
28import subprocess
29
30
31
32def shellexec(cmd_str):
33  """
34  <Purpose>
35    Uses subprocess to execute the command string in the shell.
36     
37  <Arguments>
38    cmd_str:  The string to be treated as a command (or set of commands,
39                separated by ;).
40   
41  <Exceptions>
42    None.
43
44  <Side Effects>
45    None.
46
47  <Returns>
48    A tuple containing (stdout, strerr, returncode)
49
50    Detailed:
51    stdout: stdout printed to console during command execution.
52    strerr: error (note: some programs print to strerr instead of stdout)
53    returncode: the return code of our call. If there are multiple commands,
54                then this is the return code of the last command executed.
55  """
56
57  # get a handle to the subprocess we're creating..
58  handle = subprocess.Popen(cmd_str, shell=True, 
59      stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
60
61  # execute and grab the stdout and err
62  stdoutdata, strerrdata = handle.communicate("")
63
64  # The return code... 
65  returncode = handle.returncode
66 
67  return stdoutdata, strerrdata, returncode
68
69 
70 
71# TODO: add args as 'extra' script files to execute.
72def main():
73  """
74  <Purpose>
75    Main entyr point into the program. Checks that everytyhing is in order,
76    and then creates the tar file to deploy.
77     
78  <Arguments>
79    None.
80   
81  <Exceptions>
82    None.
83
84  <Side Effects>
85    None.
86
87  <Returns>
88    None.
89  """
90
91  print "Starting tar-maker script.."
92  # String of files we're going to be looking for
93  files="runlocaltests.py testprocess.py verifyfiles.mix cleanup_deploy.py hashes.dict upgrade_nodes.sh deploy_helper.py"
94
95  # TODO: add list of 'optional files' to include
96
97  # get the files passed in as arguments
98  files_from_args = ''
99  # 1 skips this file name
100  print
101 
102  for eachfile in range(1, len(sys.argv)):
103    print "Adding custom file: "+sys.argv[eachfile]
104    files_from_args+=' '+sys.argv[eachfile]
105  print
106  # mash the two strings together now
107  files+=files_from_args
108
109  # Total number of files split by spaces
110  total_files=len(files.split(' '))
111
112  # Counter for found files
113  num_files_found=0
114
115  # Temporary tar, incrementally we'll build it up
116  # Will remove the temp files (since I use -update flag)
117  # for building up the .tar
118  if os.path.isfile('./deploy.tar.temp'):
119    os.remove('./deploy.tar.temp')
120
121
122  for filename in files.split(' '):
123    print ' Looking for '+filename+' in '+os.getcwd()
124    if os.path.isfile('./'+filename):
125      print '  File found!'
126      num_files_found += 1
127      shellexec('tar -rf deploy.tar.temp '+filename)
128    else:
129      print '  WARNING: '+filename+' NOT FOUND'
130
131  print
132  print "Found "+str(num_files_found)+" of "+str(total_files)+" necessary files."
133  print
134
135  # Did we find all of the files?
136  if num_files_found == total_files:
137    print
138    print 'All files found, finishing tar..'
139    # rename the file to the final name.
140    # this will over-write current deploy.tar in the dir if one exists 
141    shellexec('mv deploy.tar.temp deploy.tar')
142    return 0
143  else:
144    print 'FATAL ERROR: Not all the files where found, please check that '
145    print '            this script is in the same directory as the files. '
146    print
147    print "Cleaning up temp files..."
148   
149    # remove deploy.tar.temp only if it exists.
150    if os.path.isfile('./deploy.tar.temp'):
151      os.remove('./deploy.tar.temp')
152   
153    print
154    print 'Finished (with errors)'
155    return 1
156
157   
158   
159if __name__ == "__main__":
160  main()
Note: See TracBrowser for help on using the browser.