def adminperms(src, verbose=0, dry_run=0): try: spawn(['/usr/bin/chgrp', '-R', 'admin', src]) spawn(['/bin/chmod', '-R', 'g+w', src]) except: return False return True
def reown_paxboms(base_path, user, group, TOOL=CHOWN_TOOL): """ Change ownership files in pax/boms within `base_path` Parameters ---------- base_path : str path to tree somewhere containing bom / pax pairs. We will change ownership of files within the pax archive, and record this in the bom. user : str user to which to change ownership group : str group to which to change ownership TOOL : str, optional path to ``chown`` binary """ for pxbom in find_paxboms(base_path): px, bm = [abspath(f) for f in pxbom] with InTemporaryDirectory() as tmpdir: arch_path = pjoin(tmpdir, 'archive') os.mkdir(arch_path) unpax(px, arch_path) spawn([TOOL, '-R', '%s:%s' % (user, group), arch_path]) os.mkdir('Contents') pax(arch_path, tmpdir) mkbom(arch_path, tmpdir) rs1 = copy_file(pjoin('Contents', 'Archive.bom'), bm) rs2 = copy_file(pjoin('Contents', 'Archive.pax.gz'), px) assert rs1 == (bm, True) assert rs2 == (px, True)
def unpax(pax_file, out_dir=None, TOOL=PAX_TOOL): """ Unpack a pax archive `pax_file` into `out_dir` Parameters ---------- pax_file : str filename of pax file out_dir : None or str, optional directory into which to unpack files; if None, create temporary directory and unpack there. The caller should delete the directory. TOOL : str, optional path to ``pax`` binary Returns ------- out_dir : str directory containing unpacked files """ pwd = os.path.realpath(os.getcwd()) pax_path = os.path.abspath(pax_file) cmd = [TOOL, '-r', '-p', 'e', '-f', pax_path] if pax_file.endswith('.gz'): cmd += ['-z'] elif pax_file.endswith('.bz2'): cmd += ['-j'] if out_dir is None: out_dir = mkdtemp os.chdir(out_dir) try: spawn(cmd) finally: os.chdir(pwd) return out_dir
def mkbom(src, pkgdir, verbose=0, dry_run=0, TOOL='/usr/bin/mkbom'): """ Create a bill-of-materials (BOM) for the given src directory and store it to the given pkg directory """ dest = os.path.join(pkgdir, 'Contents', 'Archive.bom') mkpath(os.path.dirname(dest), verbose=verbose, dry_run=dry_run) spawn([TOOL, src, dest], verbose=verbose, dry_run=dry_run)
def run(self, *args, **kwargs): # Make sure you have libtool >= 2.2.6 for Centos # We ignore distutils build because the native build creates a valid shared libraries and python's default # build doesn't (it then complains that _dbus_bindings.so is missing a symbol). os.environ['PYTHON'] = sys.executable run_autogen() spawn(["make", "-C", root_dir], search_path=True) for name in [ext.name for ext in self.extensions]: from_path = os.path.join(root_dir, name, ".libs", "{}.so".format(name)) to_path = self.get_ext_fullpath(name) log.info("copying extension {} from {} to {}".format(name, from_path, to_path)) shutil.copyfile(from_path, to_path)
def pax(src, pkgdir, verbose=0, dry_run=0, TOOL='/bin/pax'): """ Create a pax gzipped cpio archive of the given src directory and store it to the given pkg directory returns size of archive """ dest = os.path.realpath(os.path.join(pkgdir, 'Contents', 'Archive.pax.gz')) mkpath(os.path.dirname(dest), verbose=verbose, dry_run=dry_run) pwd = os.path.realpath(os.getcwd()) os.chdir(src) try: spawn([TOOL, '-w', '-f', dest, '-x', 'cpio', '-z', '.']) finally: os.chdir(pwd) return os.stat(dest).st_size
def _run_swig(self, sources, swig_dependencies, target, swig_include_dirs, py_dir, c_dir, name): if newer_group(sources + swig_dependencies, target): includes = [] for i in swig_include_dirs: includes.append('-I' + i) cmd = [gsl_Location.get_swig(),] + swig_flags + includes cmd.extend(['-o', target] + sources) print string.join(cmd, " ") spawn(cmd, 1, 1) dst = name src = name + '.py' if c_dir: src = c_dir + "/" + src dst = "." if py_dir: dst = py_dir + "/" copy_file(src, dst)
def adminperms(src, verbose=0, dry_run=0): try: # Awful unavoidable quirk: package must be built as root. spawn(['/usr/sbin/chown', '-R', 'root', src]) spawn(['/usr/bin/chgrp', '-R', 'admin', src]) spawn(['/bin/chmod', '-R', 'u=rwX,g=rwX,o=rX', src]) except: raise RuntimeError('Cannot chown/chgrp/chmod. Are you running sudo?') return True
def byte_compile(py_files, optimize=0, force=0, target_dir=None, verbose=1, dry_run=0, direct=None): if direct is None: direct = (__debug__ and optimize == 0) # "Indirect" byte-compilation: write a temporary script and then # run it with the appropriate flags. if not direct: from tempfile import mktemp from distutils.util import execute, spawn script_name = mktemp(".py") if verbose: print("writing byte-compilation script '%s'" % script_name) if not dry_run: with open(script_name, "w") as script: script.write(""" from py2app.util import byte_compile from modulegraph.modulegraph import * files = [ """) for f in py_files: script.write(repr(f) + ",\n") script.write("]\n") script.write(""" byte_compile(files, optimize=%r, force=%r, target_dir=%r, verbose=%r, dry_run=0, direct=1) """ % (optimize, force, target_dir, verbose)) # Ensure that py2app is on PYTHONPATH, this ensures that # py2app.util can be found even when we're running from # an .egg that was downloaded by setuptools import py2app pp = os.path.dirname(os.path.dirname(py2app.__file__)) if 'PYTHONPATH' in os.environ: pp = '%s:%s'%(pp, os.environ['PYTHONPATH']) cmd = ['/usr/bin/env', 'PYTHONPATH=%s'%(pp,), sys.executable, script_name] if optimize == 1: cmd.insert(3, "-O") elif optimize == 2: cmd.insert(3, "-OO") spawn(cmd, verbose=verbose, dry_run=dry_run) execute(os.remove, (script_name,), "removing %s" % script_name, verbose=verbose, dry_run=dry_run) else: from py_compile import compile from distutils.dir_util import mkpath for mod in py_files: # Terminology from the py_compile module: # cfile - byte-compiled file # dfile - purported source filename (same as 'file' by default) if mod.filename == mod.identifier: cfile = os.path.basename(mod.filename) dfile = cfile + (__debug__ and 'c' or 'o') else: cfile = mod.identifier.replace('.', os.sep) if mod.packagepath: dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c' or 'o') else: dfile = cfile + '.py' + (__debug__ and 'c' or 'o') if target_dir: cfile = os.path.join(target_dir, dfile) if force or newer(mod.filename, cfile): if verbose: print("byte-compiling %s to %s" % (mod.filename, dfile)) if not dry_run: mkpath(os.path.dirname(cfile)) suffix = os.path.splitext(mod.filename)[1] if suffix in ('.py', '.pyw'): fn = cfile + '.py' with zipio.open(mod.filename, 'rb') as fp_in: with open(fn, 'wb') as fp_out: fp_out.write(fp_in.read()) compile(fn, cfile, dfile) os.unlink(fn) elif suffix in PY_SUFFIXES: # Minor problem: This will happily copy a file # <mod>.pyo to <mod>.pyc or <mod>.pyc to # <mod>.pyo, but it does seem to work. copy_file(mod.filename, cfile) else: raise RuntimeError \ ("Don't know how to handle %r" % mod.filename) else: if verbose: print("skipping byte-compilation of %s to %s" % (mod.filename, dfile))
def run_autogen(): global root_dir os.environ['PYTHON'] = sys.executable spawn([os.path.join(root_dir, "autogen.sh")], search_path=False)
def byte_compile(py_files, optimize=0, force=0, target_dir=None, verbose=1, dry_run=0, direct=None): if direct is None: direct = (__debug__ and optimize == 0) # "Indirect" byte-compilation: write a temporary script and then # run it with the appropriate flags. if not direct: from tempfile import mktemp from distutils.util import execute, spawn script_name = mktemp(".py") if verbose: print("writing byte-compilation script '%s'" % script_name) if not dry_run: script = open(script_name, "w") script.write(""" from pluginbuilder.util import byte_compile from modulegraph.modulegraph import * files = [ """) for f in py_files: script.write(repr(f) + ",\n") script.write("]\n") script.write(""" byte_compile(files, optimize=%r, force=%r, target_dir=%r, verbose=%r, dry_run=0, direct=1) """ % (optimize, force, target_dir, verbose)) script.close() cmd = [sys.executable, script_name] if optimize == 1: cmd.insert(1, "-O") elif optimize == 2: cmd.insert(1, "-OO") spawn(cmd, verbose=verbose, dry_run=dry_run) execute(os.remove, (script_name, ), "removing %s" % script_name, verbose=verbose, dry_run=dry_run) else: from py_compile import compile from distutils.dir_util import mkpath for mod in py_files: # Terminology from the py_compile module: # cfile - byte-compiled file # dfile - purported source filename (same as 'file' by default) if mod.filename == mod.identifier: cfile = os.path.basename(mod.filename) dfile = cfile + (__debug__ and 'c' or 'o') else: cfile = mod.identifier.replace('.', os.sep) if mod.packagepath: dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c' or 'o') else: dfile = cfile + '.py' + (__debug__ and 'c' or 'o') if target_dir: cfile = os.path.join(target_dir, dfile) if force or newer(mod.filename, cfile): if verbose: print("byte-compiling %s to %s" % (mod.filename, dfile)) if not dry_run: mkpath(os.path.dirname(cfile)) suffix = os.path.splitext(mod.filename)[1] if suffix in ('.py', '.pyw'): zfile, pth = path_to_zip(mod.filename) if zfile is None: compile(mod.filename, cfile, dfile) else: fn = dfile + '.py' open(fn, 'wb').write(get_zip_data(zfile, pth)) compile(mod.filename, cfile, dfile) os.unlink(fn) elif suffix in PY_SUFFIXES: # Minor problem: This will happily copy a file # <mod>.pyo to <mod>.pyc or <mod>.pyc to # <mod>.pyo, but it does seem to work. copy_file_data(mod.filename, cfile) else: raise RuntimeError \ ("Don't know how to handle %r" % mod.filename) else: if verbose: print("skipping byte-compilation of %s to %s" % \ (mod.filename, dfile))
v = time.strftime("%Y%m%d%H%M") projectname = "getdate" #module1 = Extension("libgetdate") #, sources=["getdate.tab.c", "getdate.c"]) extmodules = [] extmodules.append( Extension("libgetdate", [ "pygetdate.c", "getdate-parser.c", "getdate-lexer-original.c", "getdate-timezones.c" ])) grammary = "getdate-parser.y" grammarc = "getdate-parser.c" if newer(grammary, grammarc): spawn(["make", grammarc], verbose=1) setup( name=projectname, version=v, url="http://projects.zoidtechnologies.com/%s/" % (projectname), author="zoid technologies", author_email="*****@*****.**" % (projectname), py_modules=[ "getdate", ], # headers=["xtime.h"], # ext_modules=[Extension("libgetdate", ["pygetdate.c", "getdate-parser.c", "getdate-lexer-original.c", "getdate-timezones.c"])] # module1,] ext_modules=extmodules, classifiers=[ "Development Status :: 5 - Production/Stable",
def byte_compile(py_files, optimize=0, force=0, target_dir=None, verbose=1, dry_run=0, direct=None): if direct is None: direct = (__debug__ and optimize == 0) # "Indirect" byte-compilation: write a temporary script and then # run it with the appropriate flags. if not direct: from tempfile import mktemp from distutils.util import execute, spawn script_name = mktemp(".py") if verbose: print("writing byte-compilation script '%s'" % script_name) if not dry_run: with open(script_name, "w") as script: script.write(""" from py2app.util import byte_compile from modulegraph.modulegraph import * files = [ """) for f in py_files: script.write(repr(f) + ",\n") script.write("]\n") script.write(""" byte_compile(files, optimize=%r, force=%r, target_dir=%r, verbose=%r, dry_run=0, direct=1) """ % (optimize, force, target_dir, verbose)) # Ensure that py2app is on PYTHONPATH, this ensures that # py2app.util can be found even when we're running from # an .egg that was downloaded by setuptools import py2app pp = os.path.dirname(os.path.dirname(py2app.__file__)) if 'PYTHONPATH' in os.environ: pp = '%s:%s' % (pp, os.environ['PYTHONPATH']) cmd = [ '/usr/bin/env', 'PYTHONPATH=%s' % (pp, ), sys.executable, script_name ] if optimize == 1: cmd.insert(3, "-O") elif optimize == 2: cmd.insert(3, "-OO") spawn(cmd, verbose=verbose, dry_run=dry_run) execute(os.remove, (script_name, ), "removing %s" % script_name, verbose=verbose, dry_run=dry_run) else: from py_compile import compile from distutils.dir_util import mkpath for mod in py_files: # Terminology from the py_compile module: # cfile - byte-compiled file # dfile - purported source filename (same as 'file' by default) if mod.filename == mod.identifier: cfile = os.path.basename(mod.filename) dfile = cfile + (__debug__ and 'c' or 'o') else: cfile = mod.identifier.replace('.', os.sep) if mod.packagepath: dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c' or 'o') else: dfile = cfile + '.py' + (__debug__ and 'c' or 'o') if target_dir: cfile = os.path.join(target_dir, dfile) if force or newer(mod.filename, cfile): if verbose: print("byte-compiling %s to %s" % (mod.filename, dfile)) if not dry_run: mkpath(os.path.dirname(cfile)) suffix = os.path.splitext(mod.filename)[1] if suffix in ('.py', '.pyw'): fn = cfile + '.py' with zipio.open(mod.filename, 'rb') as fp_in: with open(fn, 'wb') as fp_out: fp_out.write(fp_in.read()) compile(fn, cfile, dfile) os.unlink(fn) elif suffix in PY_SUFFIXES: # Minor problem: This will happily copy a file # <mod>.pyo to <mod>.pyc or <mod>.pyc to # <mod>.pyo, but it does seem to work. copy_file(mod.filename, cfile, preserve_times=True) else: raise RuntimeError \ ("Don't know how to handle %r" % mod.filename) else: if verbose: print("skipping byte-compilation of %s to %s" % (mod.filename, dfile))
def byte_compile(py_files, optimize=0, force=0, target_dir=None, verbose=1, dry_run=0, direct=None): if direct is None: direct = __debug__ and optimize == 0 # "Indirect" byte-compilation: write a temporary script and then # run it with the appropriate flags. if not direct: from tempfile import mktemp from distutils.util import execute, spawn script_name = mktemp(".py") if verbose: print "writing byte-compilation script '%s'" % script_name if not dry_run: script = open(script_name, "w") script.write( """ from py2app.util import byte_compile from modulegraph.modulegraph import * files = [ """ ) for f in py_files: script.write(repr(f) + ",\n") script.write("]\n") script.write( """ byte_compile(files, optimize=%r, force=%r, target_dir=%r, verbose=%r, dry_run=0, direct=1) """ % (optimize, force, target_dir, verbose) ) script.close() cmd = [sys.executable, script_name] if optimize == 1: cmd.insert(1, "-O") elif optimize == 2: cmd.insert(1, "-OO") spawn(cmd, verbose=verbose, dry_run=dry_run) execute(os.remove, (script_name,), "removing %s" % script_name, verbose=verbose, dry_run=dry_run) else: from py_compile import compile from distutils.dir_util import mkpath for mod in py_files: # Terminology from the py_compile module: # cfile - byte-compiled file # dfile - purported source filename (same as 'file' by default) if mod.filename == mod.identifier: cfile = os.path.basename(mod.filename) dfile = cfile + (__debug__ and "c" or "o") else: cfile = mod.identifier.replace(".", os.sep) if mod.packagepath: dfile = cfile + os.sep + "__init__.py" + (__debug__ and "c" or "o") else: dfile = cfile + ".py" + (__debug__ and "c" or "o") if target_dir: cfile = os.path.join(target_dir, dfile) if force or newer(mod.filename, cfile): if verbose: print "byte-compiling %s to %s" % (mod.filename, dfile) if not dry_run: mkpath(os.path.dirname(cfile)) suffix = os.path.splitext(mod.filename)[1] if suffix in (".py", ".pyw"): zfile, pth = path_to_zip(mod.filename) if zfile is None: compile(mod.filename, cfile, dfile) else: fn = dfile + ".py" open(fn, "wb").write(get_zip_data(zfile, pth)) compile(mod.filename, cfile, dfile) os.unlink(fn) elif suffix in PY_SUFFIXES: # Minor problem: This will happily copy a file # <mod>.pyo to <mod>.pyc or <mod>.pyc to # <mod>.pyo, but it does seem to work. copy_file(mod.filename, cfile) else: raise RuntimeError("Don't know how to handle %r" % mod.filename) else: if verbose: print "skipping byte-compilation of %s to %s" % (mod.filename, dfile)