abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
package.py
Go to the documentation of this file.
1 import os
2 import sys
3 import optparse
4 import zipfile
5 import tarfile
6 import tempfile
7 import time
8 import py_compile
9 
10 def zip_library(f, extra_files = []):
11  lib = "%s/lib/python%s/"%(sys.prefix,sys.version[:3])
12 
13  zf = zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED)
14 
15  for root, _, files in os.walk(lib):
16  arcroot = os.path.relpath(root, lib)
17  for f in files:
18  _, ext = os.path.splitext(f)
19  if ext in ['.py']:
20  zf.write(os.path.join(root,f), os.path.join(arcroot, f))
21 
22  for s, r in extra_files:
23  zf.write( s, r )
24 
25  zf.close()
26 
27 def add_python_lib(tf, lib_dir, lib, mtime):
28 
29  _, prefix = os.path.split(lib)
30 
31  for root, _, files in os.walk(lib):
32 
33  relpath = os.path.relpath(root, lib)
34 
35  if '.hg' in relpath.split('/'):
36  continue
37 
38  if relpath=='.':
39  arcroot = lib_dir
40  else:
41  arcroot = os.path.join( lib_dir, os.path.relpath(root, lib) )
42 
43  arcroot = os.path.join(arcroot, prefix)
44 
45  add_dir(tf, arcroot, mtime)
46 
47  for f in files:
48  _, ext = os.path.splitext(f)
49  if ext in ['.py', '.so']:
50  add_file( tf, os.path.join(root,f), os.path.join(arcroot, f), 0666, mtime)
51 
52 def add_dir(tf, dir, mtime):
53  ti = tarfile.TarInfo(dir)
54  ti.mode = 0777
55  ti.mtime = mtime
56  ti.type = tarfile.DIRTYPE
57 
58  tf.addfile(ti)
59 
60 def add_fileobj(tf, f, arcname, mode, mtime):
61  ti = tarfile.TarInfo(arcname)
62  ti.mode = mode
63  ti.mtime = mtime
64 
65  f.seek(0, os.SEEK_END)
66  ti.size = f.tell()
67 
68  f.seek(0, os.SEEK_SET)
69  tf.addfile(ti, f)
70 
71 def add_file(tf, fname, arcname, mode, mtime):
72  print "\t adding %s as %s"%(fname, arcname)
73 
74  with open(fname, "rb") as f:
75  add_fileobj(tf, f, arcname, mode, mtime)
76 
77 def package(pyabc_dir, extra_bin, extra_lib, extra_files, abc_exe, abc_sh, pyabc, ofname, scripts_dir, use_sys):
78 
79  mtime = time.time()
80 
81  tf = tarfile.open(ofname, "w:gz")
82 
83  add_dir(tf, "%s"%pyabc_dir, mtime)
84 
85  add_dir(tf, "%s/bin"%pyabc_dir, mtime)
86 
87  add_file(tf, abc_exe, "%s/bin/abc_exe"%pyabc_dir, 0777, mtime)
88  add_file(tf, abc_sh, "%s/bin/abc"%pyabc_dir, 0777, mtime)
89 
90  if scripts_dir:
91  for fn in os.listdir(scripts_dir):
92  if fn.startswith('.'):
93  continue
94  fullname = os.path.join(scripts_dir, fn)
95  if os.path.isfile(fullname):
96  fnroot, fnext = os.path.splitext(fn)
97  if fnext==".sh":
98  add_file( tf, fullname, os.path.join("%s/bin"%pyabc_dir, fnroot), 0777, mtime)
99  elif fnext not in ( '.pyc', '.pyo'):
100  add_file( tf, fullname, os.path.join("%s/scripts"%pyabc_dir, fn), 0666, mtime)
101 
102  for bin in extra_bin:
103  add_file( tf, bin, os.path.join("%s/bin"%pyabc_dir, os.path.basename(bin)), 0777, mtime)
104 
105  lib_dir = "%s/lib"%pyabc_dir
106 
107  add_dir(tf, lib_dir, mtime)
108 
109  for lib in extra_lib:
110  add_python_lib( tf, lib_dir, lib, mtime)
111 
112  for file, dest in extra_files:
113  add_file(tf, file, '%s/%s'%(pyabc_dir, dest), 0666, mtime)
114 
115  for entry in os.listdir(pyabc):
116  if entry.endswith('.py'):
117  add_file( tf, os.path.join(pyabc, entry), os.path.join("%s/lib"%pyabc_dir, entry), 0666, mtime)
118 
119  if not use_sys:
120  # ZIP standard library
121  zf = tempfile.NamedTemporaryFile("w+b")
122  #zip_library(zf, [(pyabc, "pyabc.py")])
123  zip_library(zf, [])
124  zf.flush()
125 
126  add_fileobj(tf, zf, "%s/lib/python_library.zip"%pyabc_dir, 0666, mtime)
127 
128  zf.close()
129 
130  # add all extensions
131 
132  lib_dynload = os.path.join(sys.exec_prefix,"lib", "python%s"%sys.version[:3], "lib-dynload")
133 
134  for fn in os.listdir(lib_dynload):
135  fullname = os.path.join(lib_dynload, fn)
136  if os.path.isfile(fullname):
137  add_file( tf, fullname, os.path.join("%s/lib"%pyabc_dir, fn), 0666, mtime)
138 
139  tf.close()
140 
141 
142 def main(args):
143 
144  usage = "usage: %prog [options]"
145 
146  parser = optparse.OptionParser(usage)
147 
148  parser.add_option("-d", "--pyabc_dir", dest="pyabc_dir", help="name of generated directory" )
149  parser.add_option("-b", "--extra_bin", dest="extra_bin", help="extra binaries to pack" )
150  parser.add_option("-l", "--extra_lib", dest="extra_lib", help="extra directories in lib to pack" )
151  parser.add_option("-f", "--extra_files", dest="extra_files", help="additional files (comma separated pairs of file:dest" )
152  parser.add_option("-a", "--abc", dest="abc", help="location of the ABC exeutable")
153  parser.add_option("-s", "--abc_sh", dest="abc_sh", help="location of the ABC setup script")
154  parser.add_option("-p", "--pyabc", dest="pyabc", help="location of pyabc.py")
155  parser.add_option("-o", "--out", dest="out", help="location of output tar gzipped file")
156  parser.add_option("-x", "--scripts", dest="scripts", default="scripts", help="location of scripts")
157  parser.add_option("-S", "--system", action="store_false", dest="sys", default=True, help="use default python installation")
158 
159  options, args = parser.parse_args(args)
160 
161  if len(args) > 1:
162  parser.print_help()
163  return 1
164 
165  if not options.pyabc_dir or not options.abc or not options.abc_sh or not options.pyabc or not options.out:
166  parser.print_help()
167  return 1
168 
169  extra_bin = options.extra_bin.split(',') if options.extra_bin else []
170  extra_lib = options.extra_lib.split(',') if options.extra_lib else []
171  extra_files = [ s.split(':') for s in options.extra_files.split(',')] if options.extra_files else []
172 
173  return package(options.pyabc_dir, extra_bin, extra_lib, extra_files, options.abc, options.abc_sh, options.pyabc, options.out, options.scripts, options.sys)
174 
175 if __name__=="__main__":
176  main(sys.argv)
def add_dir
Definition: package.py:52
def main
Definition: package.py:142
def add_python_lib
Definition: package.py:27
def zip_library
Definition: package.py:10
def add_fileobj
Definition: package.py:60
def package
Definition: package.py:77
def add_file
Definition: package.py:71