def build_deb_binary(working_dir, working_dir_project, src_path, f_setup_py): debian_dir = os.path.join(src_path, "debian") process_command(["mkdir", "-p", debian_dir], cwd=src_path) tpl(os.path.join(spkg2deb_home, "control.bin.tpl"), os.path.join(debian_dir, "control")) tpl(os.path.join(spkg2deb_home, "rules.tpl"), os.path.join(debian_dir, "rules")) tpl(os.path.join(spkg2deb_home, "changelog.tpl"), os.path.join(debian_dir, "changelog")) try: process_command_quiet(["dpkg-buildpackage", "-rfakeroot", "-uc", "-us", "-b"], cwd=src_path) except CalledProcessError: raise CalledProcessError("....making binaries failed! (bad Makefile or missing deps)") try: deb_file = PACKAGE_NAME + "_" + VERSION + "_all.deb" deb_renamed = PACKAGE_NAME + "-" + VERSION + ".deb" build_text = "....created binary package: " if (os.path.exists(os.path.join(working_dir_project, deb_file))): deb_file = os.path.join(working_dir_project, deb_file) else: deb_file = os.path.join(working_dir, deb_file) process_command(["cp", os.path.abspath(deb_file), os.path.join(inst_path_bin, deb_renamed)], cwd=working_dir) os.chmod(os.path.join(inst_path_bin, deb_renamed), 0664) print(build_text + os.path.basename(deb_renamed)) except CalledProcessError: print("....error copying binary package to final destination!")
def convert_archive(dest_dir, spkg_file): working_dir = tempfile.mkdtemp() try: expand_tarball(os.path.abspath(spkg_file), cwd=working_dir) except: print(spkg_file) print("....Bad archive! Skipping file!") print("....removing tmp: " + working_dir) shutil.rmtree(working_dir) return dir = os.listdir(working_dir) working_dir_project = os.path.join(os.path.normpath(working_dir), dir[0]) f_spkg_install = None # look in package root for file in os.listdir(working_dir_project): if file == "spkg-install": f_spkg_install = os.path.join(working_dir_project, "spkg-install") if f_spkg_install != None: rewrite_file(f_spkg_install) make_tarball(os.path.basename(spkg_file), os.path.basename(working_dir_project), cwd=working_dir) process_command(["cp", os.path.basename(spkg_file), dest_dir], cwd=working_dir) print("Processed: " + f_spkg_install) else: print("....No spkg-install found! Skipping this package!") print("....removing tmp: " + working_dir) shutil.rmtree(working_dir)
def convert_archive(dest_dir, spkg_file): working_dir = tempfile.mkdtemp() try: expand_tarball(os.path.abspath(spkg_file), cwd=working_dir) except: print(spkg_file) print("....Bad archive! Skipping file!") print("....removing tmp: " + working_dir) shutil.rmtree(working_dir) return dir = os.listdir(working_dir) working_dir_project = os.path.join(os.path.normpath(working_dir), dir[0]) f_spkg_install = None # look in package root for file in os.listdir(working_dir_project): if file == "spkg-install": f_spkg_install = os.path.join(working_dir_project, "spkg-install") if f_spkg_install != None: rewrite_file(f_spkg_install) make_tarball(os.path.basename(spkg_file), os.path.basename(working_dir_project), cwd=working_dir) process_command(["cp", os.path.basename(spkg_file), dest_dir], cwd=working_dir) print("Processed: " + f_spkg_install) else: print("....No spkg-install found! Skipping this package!") print("....removing tmp: " + working_dir) shutil.rmtree(working_dir)
def main(): print("") print("=========================================") print("Create FEMhub package from project source") print("=========================================") print("") print("Example: ./create_package.py -d /project/source -u [email protected]") print("") parser = argparse.ArgumentParser() parser.add_argument('-d', '--dir', action='append', help='input directory containing source files'), parser.add_argument( '-u', '--upload', action='append', help='-u {username} , uploads the package on femhub.org') args = parser.parse_args() if args.dir == None: print("You must specify input source directory!\n") print("Type -h for help\n") exit(1) installScriptFound = False packageName = False if args.dir != None: for dir in args.dir: for file in os.listdir(dir): if file == "spkg-install": rewrite_file(os.path.join(dir, file)) installScriptFound = True try: if installScriptFound: print("spkg-install found!") packageName = generate_package(dir) else: print("spkg-install not found, trying to autogenerate...") generate_install_script(dir) packageName = generate_package(dir) except RuntimeError as e: print(e) print('Skipping source: ' + dir) if args.upload != None and packageName != False: for account in args.upload: process_command([ "scp", packageName + ".spkg", account + "@spilka.math.unr.edu:/var/www3/femhub.org/packages/femhub_st/femhub_spkg/" ])
def install_deps(package): "Install missing dependencies silently." if AUTO_DEPS == False or BUILD_BINARY == False: return print("Installing dependencies for " + package) try: process_command(["sudo", "apt-get", "build-dep", package, "-y"]) except: pass
def install_deps(package): "Install missing dependencies silently." if AUTO_DEPS == False or BUILD_BINARY == False: return print("Installing dependencies for " + package) try: process_command(["sudo", "apt-get", "build-dep", package, "-y"]) except: pass
def expand_tarball(tarball_fname, cwd=None): "expand a tarball" if tarball_fname.endswith('.gz'): opts = 'xzf' elif tarball_fname.endswith('.bz2'): opts = 'xjf' elif tarball_fname.endswith('.spkg'): opts = 'xjf' else: opts = 'xf' args = ['/bin/tar', opts, tarball_fname] process_command(args, cwd=cwd)
def make_tarball(tarball_fname, directory, cwd=None): "create a tarball from a directory" if tarball_fname.endswith('.gz'): opts = 'czf' elif tarball_fname.endswith('.bz2'): opts = 'cjf' elif tarball_fname.endswith('.spkg'): opts = 'cjf' else: opts = 'cf' args = ['/bin/tar', opts, tarball_fname, directory] process_command(args, cwd=cwd)
def generate_package(dir): if (dir.endswith("/")): dir = dir[:-1] finalArgument = getPackageName(dir) try: make_tarball(getPackageName(dir) + ".spkg", finalArgument) except: print("OK: tar did not find the path %s" % finalArgument) (head, tail) = os.path.split(dir) print("Found project directory under: %s" % head) print("Continuing...this may take a while!") make_tarball(getPackageName(dir) + ".spkg", finalArgument, head) process_command(["mv",os.path.join(head,getPackageName(dir) + ".spkg"),"./"]) print("Created package using: tar -cjf " + getPackageName(dir) + ".spkg " + finalArgument) return finalArgument
def main(): print("") print("=========================================") print("Create FEMhub package from project source") print("=========================================") print("") print("Example: ./create_package.py -d /project/source -u [email protected]") print("") parser = argparse.ArgumentParser() parser.add_argument('-d', '--dir', action='append', help='input directory containing source files'), parser.add_argument('-u', '--upload', action='append', help='-u {username} , uploads the package on femhub.org') args = parser.parse_args() if args.dir == None: print("You must specify input source directory!\n") print("Type -h for help\n") exit(1) installScriptFound = False packageName = False if args.dir != None: for dir in args.dir: for file in os.listdir(dir): if file == "spkg-install": rewrite_file(os.path.join(dir, file)) installScriptFound = True try: if installScriptFound: print("spkg-install found!") packageName = generate_package(dir) else: print("spkg-install not found, trying to autogenerate...") generate_install_script(dir) packageName = generate_package(dir) except RuntimeError as e: print(e) print('Skipping source: ' + dir) if args.upload != None and packageName != False: for account in args.upload: process_command(["scp", packageName + ".spkg",account + "@spilka.math.unr.edu:/var/www3/femhub.org/packages/femhub_st/femhub_spkg/"])
def expand_zip(zip_fname, cwd=None): "expand a zip" args = ['/usr/bin/unzip', zip_fname] # Does it have a top dir res = subprocess.Popen( [args[0], '-l', args[1]], cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) contents = [] for line in res.stdout.readlines()[3:-2]: contents.append(line.split()[-1]) commonprefix = os.path.commonprefix(contents) if not commonprefix: extdir = os.path.join(cwd, os.path.basename(zip_fname[:-4])) args.extend(['-d', os.path.abspath(extdir)]) process_command(args, cwd=cwd)
def generate_package(dir): if (dir.endswith("/")): dir = dir[:-1] finalArgument = getPackageName(dir) try: make_tarball(getPackageName(dir) + ".spkg", finalArgument) except: print("OK: tar did not find the path %s" % finalArgument) (head, tail) = os.path.split(dir) print("Found project directory under: %s" % head) print("Continuing...this may take a while!") make_tarball(getPackageName(dir) + ".spkg", finalArgument, head) process_command( ["mv", os.path.join(head, getPackageName(dir) + ".spkg"), "./"]) print("Created package using: tar -cjf " + getPackageName(dir) + ".spkg " + finalArgument) return finalArgument
def build_deb_binary(working_dir, working_dir_project, src_path, f_setup_py): debian_dir = os.path.join(src_path, "debian") process_command(["mkdir", "-p", debian_dir], cwd=src_path) tpl(os.path.join(spkg2deb_home, "control.bin.tpl"), os.path.join(debian_dir, "control")) tpl(os.path.join(spkg2deb_home, "rules.tpl"), os.path.join(debian_dir, "rules")) tpl(os.path.join(spkg2deb_home, "changelog.tpl"), os.path.join(debian_dir, "changelog")) try: process_command_quiet( ["dpkg-buildpackage", "-rfakeroot", "-uc", "-us", "-b"], cwd=src_path) except CalledProcessError: raise CalledProcessError( "....making binaries failed! (bad Makefile or missing deps)") try: deb_file = PACKAGE_NAME + "_" + VERSION + "_all.deb" deb_renamed = PACKAGE_NAME + "-" + VERSION + ".deb" build_text = "....created binary package: " if (os.path.exists(os.path.join(working_dir_project, deb_file))): deb_file = os.path.join(working_dir_project, deb_file) else: deb_file = os.path.join(working_dir, deb_file) process_command([ "cp", os.path.abspath(deb_file), os.path.join(inst_path_bin, deb_renamed) ], cwd=working_dir) os.chmod(os.path.join(inst_path_bin, deb_renamed), 0664) print(build_text + os.path.basename(deb_renamed)) except CalledProcessError: print("....error copying binary package to final destination!")
def build_deb_source(working_dir, working_dir_project, src_path, f_setup_py): debian_dir = os.path.join(working_dir_project, "DEBIAN") process_command(["mkdir", "-p", debian_dir], cwd=working_dir_project) tpl(os.path.join(spkg2deb_home, "control.src.tpl"), os.path.join(debian_dir, "control")) tpl(os.path.join(spkg2deb_home, "rules.tpl"), os.path.join(debian_dir, "rules")) tpl(os.path.join(spkg2deb_home, "changelog.tpl"), os.path.join(debian_dir, "changelog")) process_command(["cp", os.path.join(spkg2deb_home, "preinst.spkg.tpl"), os.path.join(debian_dir, "preinst")], cwd=working_dir) os.chmod(os.path.join(debian_dir, "preinst"), 0755) print("....generated " + os.path.join(debian_dir, "preinst")) fin = open(os.path.join(working_dir_project, "spkg-install")) data = fin.read() fin.close() fout = open(os.path.join(debian_dir, "postinst"), "wt") data = re.sub("SPKG_LOCAL", "FEMHUB_LOCAL", data) data = re.sub("SAGE_LOCAL", "FEMHUB_LOCAL", data) data = re.sub("SAGE_ROOT", "FEMHUB_ROOT", data) fout.write(data) fout.close() os.chmod(os.path.join(debian_dir, "postinst"), 0755) print("....generated " + os.path.join(debian_dir, "postinst")) try: process_command_quiet(["dpkg-deb", "--build", working_dir_project], cwd=working_dir) except CalledProcessError: raise CalledProcessError("....making source package failed!") try: deb_file = PACKAGE_NAME + "-" + VERSION + ".deb" deb_renamed = PACKAGE_NAME + "-" + VERSION + ".deb" build_text = "....created source package: " if (os.path.exists(os.path.join(working_dir_project, deb_file))): deb_file = os.path.join(working_dir_project, deb_file) elif (os.path.exists(os.path.join(working_dir, deb_file))): deb_file = os.path.join(working_dir, deb_file) elif (os.path.exists(os.path.join(working_dir_project, PACKAGE_NAME + ".deb"))): deb_file = os.path.join(working_dir_project, PACKAGE_NAME + ".deb") elif (os.path.exists(os.path.join(working_dir, PACKAGE_NAME + ".deb"))): deb_file = os.path.join(working_dir, PACKAGE_NAME + ".deb") process_command(["cp", os.path.abspath(deb_file), os.path.join(inst_path_src, deb_renamed)], cwd=working_dir) os.chmod(os.path.join(inst_path_src, deb_renamed), 0664) print(build_text + os.path.basename(deb_renamed)) except CalledProcessError: print("....error copying package to final destination!") print(working_dir_project) print(deb_file)
def generate_deb_from_python(working_dir, working_dir_project, src_path, f_setup_py): try: # another bug fix for bad spkg folder structure, duh! f_setup_py_path = os.path.dirname(f_setup_py) #process_command(["find", ".", "-name", "'*.pyc'", "-delete"], # cwd=f_setup_py_path) process_command(["python", "setup.py", "--command-packages=stdeb.command", "bdist_deb"], cwd=f_setup_py_path) if (os.path.exists(os.path.join(f_setup_py_path, "deb_dist"))): try: deb_file = get_stdeb_package_name(f_setup_py_path) process_command(["cp", os.path.abspath(os.path.join( f_setup_py_path, "deb_dist", deb_file)), inst_path_src], cwd=f_setup_py_path) os.chmod(os.path.join(inst_path_src, deb_file), 0664) print("....created package: " + deb_file) except CalledProcessError: print("....error copying deb to final destination!") else: print("....stdb failed in creating deb! Missing deps for Makefile?") raise Exception except: print("....error in creating python deb using stdeb!") print("....proceeding in python => deb with brute force") debian_dir = os.path.join(working_dir_project, "DEBIAN") process_command(["mkdir", "-p", debian_dir], cwd=working_dir) tpl(os.path.join(spkg2deb_home, "control.py.tpl"), os.path.join(debian_dir, "control")) tpl(os.path.join(spkg2deb_home, "rules.tpl"), os.path.join(debian_dir, "rules")) tpl(os.path.join(spkg2deb_home, "changelog.tpl"), os.path.join(debian_dir, "changelog")) process_command(["cp", os.path.join(spkg2deb_home, "preinst.spkg.tpl"), os.path.join(debian_dir, "preinst")], cwd=working_dir) os.chmod(os.path.join(debian_dir, "preinst"), 0755) print("....generated " + os.path.join(debian_dir, "preinst")) process_command(["cp", os.path.join(working_dir_project, "spkg-install"), os.path.join(debian_dir, "postinst")], cwd=working_dir) os.chmod(os.path.join(debian_dir, "postinst"), 0755) print("....generated " + os.path.join(debian_dir, "postinst")) #deb_file = PACKAGE_NAME + "_" + VERSION + "_all.deb" deb_file = working_dir_project build_text = "....created sources package only: " process_command_quiet(["dpkg-deb", "--build", deb_file], cwd=working_dir_project) deb_file = deb_file + ".deb" if (os.path.exists(os.path.join(working_dir_project, deb_file))): deb_file = os.path.join(working_dir_project, deb_file) else: deb_file = os.path.join(working_dir, deb_file) process_command(["cp", os.path.abspath(deb_file), inst_path_src], cwd=working_dir) os.chmod(os.path.join(inst_path_src, deb_file), 0664) print(build_text + os.path.basename(deb_file))
from spkg2deb.ex import CalledProcessError from spkg2deb.settings import Settings from spkg2deb.command import check_call from spkg2deb.command import process_command from spkg2deb.archive import expand_tarball APP_VERSION = "0.2, Nov 6 2011" # -better support for binary and source build methods for FEMhub DISTRIB_ID = platform.linux_distribution()[0] DISTRIB_RELEASE = platform.linux_distribution()[1] DISTRIB_CODENAME = platform.linux_distribution()[2] ARCH = platform.architecture()[0] s = Settings() femhub_deb = os.path.abspath(s.get_destination()) process_command(["mkdir", "-p", femhub_deb]) # create binary path for example ./femhub_deb/ubuntu/11.10/32bit/ inst_path_bin = os.path.abspath(os.path.join(femhub_deb, DISTRIB_ID.lower(), DISTRIB_RELEASE, ARCH)) process_command(["mkdir", "-p", inst_path_bin]) # create source path for example ./femhub_deb/ubuntu/source/ inst_path_src = os.path.abspath(os.path.join(femhub_deb, "source")) process_command(["mkdir", "-p", inst_path_src]) PACKAGES_FAILED = [] PACKAGE_NAME = "" VERSION = "" AUTO_DEPS = True BUILD_BINARY = False BUILD_SOURCE = True def get_script_path():
def generate_deb_from_python(working_dir, working_dir_project, src_path, f_setup_py): try: # another bug fix for bad spkg folder structure, duh! f_setup_py_path = os.path.dirname(f_setup_py) #process_command(["find", ".", "-name", "'*.pyc'", "-delete"], # cwd=f_setup_py_path) process_command([ "python", "setup.py", "--command-packages=stdeb.command", "bdist_deb" ], cwd=f_setup_py_path) if (os.path.exists(os.path.join(f_setup_py_path, "deb_dist"))): try: deb_file = get_stdeb_package_name(f_setup_py_path) process_command([ "cp", os.path.abspath( os.path.join(f_setup_py_path, "deb_dist", deb_file)), inst_path_src ], cwd=f_setup_py_path) os.chmod(os.path.join(inst_path_src, deb_file), 0664) print("....created package: " + deb_file) except CalledProcessError: print("....error copying deb to final destination!") else: print( "....stdb failed in creating deb! Missing deps for Makefile?") raise Exception except: print("....error in creating python deb using stdeb!") print("....proceeding in python => deb with brute force") debian_dir = os.path.join(working_dir_project, "DEBIAN") process_command(["mkdir", "-p", debian_dir], cwd=working_dir) tpl(os.path.join(spkg2deb_home, "control.py.tpl"), os.path.join(debian_dir, "control")) tpl(os.path.join(spkg2deb_home, "rules.tpl"), os.path.join(debian_dir, "rules")) tpl(os.path.join(spkg2deb_home, "changelog.tpl"), os.path.join(debian_dir, "changelog")) process_command([ "cp", os.path.join(spkg2deb_home, "preinst.spkg.tpl"), os.path.join(debian_dir, "preinst") ], cwd=working_dir) os.chmod(os.path.join(debian_dir, "preinst"), 0755) print("....generated " + os.path.join(debian_dir, "preinst")) process_command([ "cp", os.path.join(working_dir_project, "spkg-install"), os.path.join(debian_dir, "postinst") ], cwd=working_dir) os.chmod(os.path.join(debian_dir, "postinst"), 0755) print("....generated " + os.path.join(debian_dir, "postinst")) #deb_file = PACKAGE_NAME + "_" + VERSION + "_all.deb" deb_file = working_dir_project build_text = "....created sources package only: " process_command_quiet(["dpkg-deb", "--build", deb_file], cwd=working_dir_project) deb_file = deb_file + ".deb" if (os.path.exists(os.path.join(working_dir_project, deb_file))): deb_file = os.path.join(working_dir_project, deb_file) else: deb_file = os.path.join(working_dir, deb_file) process_command(["cp", os.path.abspath(deb_file), inst_path_src], cwd=working_dir) os.chmod(os.path.join(inst_path_src, deb_file), 0664) print(build_text + os.path.basename(deb_file))
def build_deb_source(working_dir, working_dir_project, src_path, f_setup_py): debian_dir = os.path.join(working_dir_project, "DEBIAN") process_command(["mkdir", "-p", debian_dir], cwd=working_dir_project) tpl(os.path.join(spkg2deb_home, "control.src.tpl"), os.path.join(debian_dir, "control")) tpl(os.path.join(spkg2deb_home, "rules.tpl"), os.path.join(debian_dir, "rules")) tpl(os.path.join(spkg2deb_home, "changelog.tpl"), os.path.join(debian_dir, "changelog")) process_command([ "cp", os.path.join(spkg2deb_home, "preinst.spkg.tpl"), os.path.join(debian_dir, "preinst") ], cwd=working_dir) os.chmod(os.path.join(debian_dir, "preinst"), 0755) print("....generated " + os.path.join(debian_dir, "preinst")) fin = open(os.path.join(working_dir_project, "spkg-install")) data = fin.read() fin.close() fout = open(os.path.join(debian_dir, "postinst"), "wt") data = re.sub("SPKG_LOCAL", "FEMHUB_LOCAL", data) data = re.sub("SAGE_LOCAL", "FEMHUB_LOCAL", data) data = re.sub("SAGE_ROOT", "FEMHUB_ROOT", data) fout.write(data) fout.close() os.chmod(os.path.join(debian_dir, "postinst"), 0755) print("....generated " + os.path.join(debian_dir, "postinst")) try: process_command_quiet(["dpkg-deb", "--build", working_dir_project], cwd=working_dir) except CalledProcessError: raise CalledProcessError("....making source package failed!") try: deb_file = PACKAGE_NAME + "-" + VERSION + ".deb" deb_renamed = PACKAGE_NAME + "-" + VERSION + ".deb" build_text = "....created source package: " if (os.path.exists(os.path.join(working_dir_project, deb_file))): deb_file = os.path.join(working_dir_project, deb_file) elif (os.path.exists(os.path.join(working_dir, deb_file))): deb_file = os.path.join(working_dir, deb_file) elif (os.path.exists( os.path.join(working_dir_project, PACKAGE_NAME + ".deb"))): deb_file = os.path.join(working_dir_project, PACKAGE_NAME + ".deb") elif (os.path.exists(os.path.join(working_dir, PACKAGE_NAME + ".deb"))): deb_file = os.path.join(working_dir, PACKAGE_NAME + ".deb") process_command([ "cp", os.path.abspath(deb_file), os.path.join(inst_path_src, deb_renamed) ], cwd=working_dir) os.chmod(os.path.join(inst_path_src, deb_renamed), 0664) print(build_text + os.path.basename(deb_renamed)) except CalledProcessError: print("....error copying package to final destination!") print(working_dir_project) print(deb_file)
from spkg2deb.ex import CalledProcessError from spkg2deb.settings import Settings from spkg2deb.command import check_call from spkg2deb.command import process_command from spkg2deb.archive import expand_tarball APP_VERSION = "0.2, Nov 6 2011" # -better support for binary and source build methods for FEMhub DISTRIB_ID = platform.linux_distribution()[0] DISTRIB_RELEASE = platform.linux_distribution()[1] DISTRIB_CODENAME = platform.linux_distribution()[2] ARCH = platform.architecture()[0] s = Settings() femhub_deb = os.path.abspath(s.get_destination()) process_command(["mkdir", "-p", femhub_deb]) # create binary path for example ./femhub_deb/ubuntu/11.10/32bit/ inst_path_bin = os.path.abspath( os.path.join(femhub_deb, DISTRIB_ID.lower(), DISTRIB_RELEASE, ARCH)) process_command(["mkdir", "-p", inst_path_bin]) # create source path for example ./femhub_deb/ubuntu/source/ inst_path_src = os.path.abspath(os.path.join(femhub_deb, "source")) process_command(["mkdir", "-p", inst_path_src]) PACKAGES_FAILED = [] PACKAGE_NAME = "" VERSION = "" AUTO_DEPS = True BUILD_BINARY = False BUILD_SOURCE = True