def __init__(self, arg, options): if is_archive_file(arg): name = path_to_fileurl(arg) elif os.path.isdir(arg): name = path_to_fileurl(arg) else: name = arg if is_url(name): self.download_url = name filename = Link(self.download_url).filename pkg = Package(filename, options.alias) else: pkg = Package(name, options.alias) self.download_url = get_python_version_url(pkg.version) if not self.download_url: logger.error("Unknown python version: `%s`" % pkg.name) raise UnknownVersionException filename = Link(self.download_url).filename self.pkg = pkg self.install_dir = os.path.join(PATH_PYTHONS, pkg.name) self.build_dir = os.path.join(PATH_BUILD, pkg.name) self.download_file = os.path.join(PATH_DISTS, filename) self.options = options self.logfile = os.path.join(PATH_LOG, 'build.log') self.patches = [] if Version(self.pkg.version) >= '3.1': self.configure_options = ['--with-computed-gotos'] else: self.configure_options = []
def install_setuptools(self): options = self.options pkgname = self.pkg.name if options.no_setuptools: logger.log("Skip installation of setuptools.") return download_url = DISTRIBUTE_SETUP_DLSITE filename = Link(download_url).filename download_file = os.path.join(PATH_DISTS, filename) dl = Downloader() dl.download(filename, download_url, download_file) install_dir = os.path.join(PATH_PYTHONS, pkgname) path_python = os.path.join(install_dir,"bin","python") try: s = Subprocess(log=self.logfile, cwd=PATH_DISTS, verbose=self.options.verbose) logger.info("Installing distribute into %s" % install_dir) s.check_call([path_python, filename]) # installing pip easy_install = os.path.join(install_dir, 'bin', 'easy_install') if os.path.isfile(easy_install): logger.info("Installing pip into %s" % install_dir) s.check_call([easy_install, 'pip']) except: logger.error("Failed to install setuptools. See %s/build.log to see why." % (ROOT)) logger.log("Skip installation of setuptools.")
def __init__(self, arg, options): super(PythonInstallerMacOSX, self).__init__(arg, options) # check for version version = Version(self.pkg.version) if version < '2.6' and (version != '2.4.6' and version < '2.5.5'): logger.error("`%s` is not supported on MacOSX Snow Leopard" % self.pkg.name) raise NotSupportedVersionException # set configure options target = get_macosx_deployment_target() if target: self.configure_options.append('MACOSX_DEPLOYMENT_TARGET=%s' % target) # set build options if options.framework and options.static: logger.error("Can't specify both framework and static.") raise Exception if options.framework: self.configure_options.append('--enable-framework=%s' % os.path.join(self.install_dir, 'Frameworks')) elif not options.static: self.configure_options.append('--enable-shared') if options.universal: self.configure_options.append('--enable-universalsdk=/') self.configure_options.append('--with-universal-archs=intel') # note: skip `make test` to avoid hanging test_threading. if is_python25(version) or is_python24(version): self.options.no_test = True
def install_setuptools(self): options = self.options pkgname = self.pkg.name if options.no_setuptools: logger.info("Skip installation setuptools.") return if re.match("^Python-3.*", pkgname): is_python3 = True else: is_python3 = False download_url = DISTRIBUTE_SETUP_DLSITE filename = Link(download_url).filename download_file = os.path.join(PATH_DISTS, filename) dl = Downloader() dl.download(filename, download_url, download_file) install_dir = os.path.join(PATH_PYTHONS, pkgname) path_python = os.path.join(install_dir,"bin","python") try: s = Subprocess(log=self.logfile, cwd=PATH_DISTS) logger.info("Installing distribute into %s" % install_dir) s.check_call("%s %s" % (path_python, filename)) if os.path.isfile("%s/bin/easy_install" % (install_dir)) and not is_python3: logger.info("Installing pip into %s" % install_dir) s.check_call("%s/bin/easy_install pip" % (install_dir)) except: logger.error("Failed to install setuptools. See %s/build.log to see why." % (ROOT)) logger.info("Skip install setuptools.")
def __init__(self, arg, options): if is_archive_file(arg): name = path_to_fileurl(arg) elif os.path.isdir(arg): name = path_to_fileurl(arg) else: name = arg if is_url(name): self.download_url = name filename = Link(self.download_url).filename pkg = Package(filename, options.alias) else: pkg = Package(name, options.alias) self.download_url = get_python_version_url(pkg.version) if not self.download_url: logger.error("Unknown python version: `%s`" % pkg.name) raise UnknownVersionException filename = Link(self.download_url).filename self.pkg = pkg self.install_dir = os.path.join(PATH_PYTHONS, pkg.name) self.build_dir = os.path.join(PATH_BUILD, pkg.name) self.download_file = os.path.join(PATH_DISTS, filename) self.options = options self.logfile = os.path.join(PATH_LOG, 'build.log') self.patches = [] if Version(self.pkg.version) >= '3.1': self.configure_options = ['--with-computed-gotos'] else: self.configure_options = []
def run_command_use(self, options, args): if len(args) < 2: logger.error("Unrecognized command line argument: ( 'pythonbrew venv use <project>' )") sys.exit(1) workon_home = None activate = None if self._pkgname: workon_home = self._workon_home activate = os.path.join(workon_home, args[1], 'bin', 'activate') else: for pkgname in get_installed_pythons_pkgname(): workon_home = os.path.join(PATH_VENVS, pkgname) if os.path.isdir(workon_home): if len([d for d in os.listdir(workon_home) if d == args[1]]) > 0: activate = os.path.join(workon_home, args[1], 'bin', 'activate') break if not activate or not os.path.exists(activate): logger.error('`%s` environment does not exist. Try `pythonbrew venv create %s`.' % (args[1], args[1])) sys.exit(1) self._write("""\ echo '# Using `%(arg)s` environment (found in %(workon_home)s)' echo '# To leave an environment, simply run `deactivate`' source '%(activate)s' """ % {'arg': args[1], 'workon_home': workon_home, 'activate': activate})
def __init__(self, arg, options): super(PythonInstallerMacOSX, self).__init__(arg, options) # check for version version = Version(self.pkg.version) if version < '2.6' and (version != '2.4.6' and version < '2.5.5'): logger.error("`%s` is not supported on MacOSX Snow Leopard" % self.pkg.name) raise NotSupportedVersionException # set configure options target = get_macosx_deployment_target() if target: self.configure_options.append('MACOSX_DEPLOYMENT_TARGET=%s' % target) # set build options if options.framework and options.static: logger.error("Can't specify both framework and static.") raise Exception if options.framework: self.configure_options.append( '--enable-framework=%s' % os.path.join(self.install_dir, 'Frameworks')) elif not options.static: self.configure_options.append('--enable-shared') if options.universal: self.configure_options.append('--enable-universalsdk=/') self.configure_options.append('--with-universal-archs=intel') # note: skip `make test` to avoid hanging test_threading. if is_python25(version) or is_python24(version): self.options.test = False
def run_command_create(self, options, args): if not os.access(PATH_VENVS, os.W_OK): logger.error( "Can not create a virtual environment in %s.\nPermission denied." % PATH_VENVS) sys.exit(1) if not self._pkgname: logger.error( "Unknown python version: ( 'pythonbrew venv create <project> -p VERSION' )" ) sys.exit(1) virtualenv_options = [] if options.no_site_packages: virtualenv_options.append('--no-site-packages') for arg in args[1:]: target_dir = os.path.join(self._workon_home, arg) logger.info("Creating `%s` environment into %s" % (arg, self._workon_home)) # make command cmd = [self._py, self._venv, '-p', self._target_py] cmd.extend(virtualenv_options) cmd.append(target_dir) # create environment s = Subprocess(verbose=True) s.call(cmd)
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) cmd = args[0] if not cmd in ('init', 'create', 'delete', 'use', 'list', 'clone', 'rename', 'print_activate'): self.parser.print_help() sys.exit(1) # initialize? if cmd == 'init': self.run_command_init() return # target python interpreter if options.python: pkgname = Package(options.python).name if not is_installed(pkgname): logger.error('%s is not installed.' % pkgname) sys.exit(1) else: pkgname = get_using_python_pkgname() self._pkgname = pkgname if self._pkgname: self._target_py = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python') self._workon_home = os.path.join(PATH_VENVS, pkgname) self._py = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python') # is already installed virtualenv? if not os.path.exists(self._venv) or not os.path.exists(self._venv_clone): self.run_command_init() # Create a shell script self.__getattribute__('run_command_%s' % cmd)(options, args)
def run_command_clone(self, options, args): if len(args) < 3: logger.error("Unrecognized command line argument: ( 'pythonbrew venv clone <source> <target>' )") sys.exit(1) if not os.access(PATH_VENVS, os.W_OK): logger.error("Can not clone a virtual environment in %s.\nPermission denied." % PATH_VENVS) sys.exit(1) if not self._pkgname: logger.error("Unknown python version: ( 'pythonbrew venv clone <source> <target> -p VERSION' )") sys.exit(1) source, target = args[1], args[2] source_dir = os.path.join(self._workon_home, source) target_dir = os.path.join(self._workon_home, target) if not os.path.isdir(source_dir): logger.error('%s does not exist.' % source_dir) sys.exit(1) if os.path.isdir(target_dir): logger.error('Can not overwrite %s.' % target_dir) sys.exit(1) logger.info("Cloning `%s` environment into `%s` on %s" % (source, target, self._workon_home)) # Copies source to target cmd = [self._py, self._venv_clone, source_dir, target_dir] s = Subprocess() s.call(cmd)
def download_unpack(self): content_type = self.content_type if is_html(content_type): logger.error("Invalid content-type: `%s`" % content_type) sys.exit(1) if is_file(self.download_url): path = fileurl_to_path(self.download_url) if os.path.isdir(path): logger.info('Copying %s into %s' % (path, self.build_dir)) if os.path.isdir(self.build_dir): shutil.rmtree(self.build_dir) shutil.copytree(path, self.build_dir) return if os.path.isfile(self.download_file): logger.info("Use the previously fetched %s" % (self.download_file)) else: msg = Link(self.download_url).show_msg try: dl = Downloader() dl.download(msg, self.download_url, self.download_file) except: unlink(self.download_file) logger.info("\nInterrupt to abort. `%s`" % (self.download_url)) sys.exit(1) # unpack if not unpack_downloadfile(self.content_type, self.download_file, self.build_dir): sys.exit(1)
def patch(self): version = self.pkg.version try: s = Subprocess(log=self.logfile, cwd=self.build_dir) patches = [] if is_macosx_snowleopard(): if is_python24(version): patch_dir = os.path.join(PATH_PATCHES_MACOSX_PYTHON24,'files') patches = ['patch-configure', 'patch-Makefile.pre.in', 'patch-Lib-cgi.py', 'patch-Lib-site.py', 'patch-setup.py', 'patch-Include-pyport.h', 'patch-Mac-OSX-Makefile.in', 'patch-Mac-OSX-IDLE-Makefile.in', 'patch-Mac-OSX-PythonLauncher-Makefile.in', 'patch-configure-badcflags.diff', 'patch-configure-arch_only.diff', 'patch-macosmodule.diff', 'patch-mactoolboxglue.diff', 'patch-pymactoolbox.diff'] elif is_python25(version): patch_dir = os.path.join(PATH_PATCHES_MACOSX_PYTHON25,'files') patches = ['patch-Makefile.pre.in.diff', 'patch-Lib-cgi.py.diff', 'patch-Lib-distutils-dist.py.diff', 'patch-setup.py.diff', 'patch-configure-badcflags.diff', 'patch-configure-arch_only.diff', 'patch-64bit.diff', 'patch-pyconfig.h.in.diff', 'patch-Modules-posixmodule.c.diff'] if patches: logger.info("Patching %s" % self.pkg.name) for patch in patches: s.check_call("patch -p0 < %s" % os.path.join(patch_dir, patch)) except: logger.error("Failed to patch `%s`" % self.build_dir) sys.exit(1)
def copy_libs(source, target): """Copies every lib inside source's site-packages folder into target's site-packages without replacing already existing libs. source and target are the names of the venvs""" # File to copy never-the-less (to add libs to the PATH) easy_inst = "easy-install.pth" pkgname = get_using_python_pkgname() if not pkgname: logger.error('Can not use venv command before switching a python. Try \'pythonbrew switch <version of python>\'.') sys.exit(1) source_path = glob.glob(os.path.join(PATH_VENVS, pkgname) + "/" + source + "/lib/python*/site-packages/")[0] target_path = glob.glob(os.path.join(PATH_VENVS, pkgname) + "/" + target + "/lib/python*/site-packages/")[0] path, dirs, files = os.walk(source_path).next() for curr_dir in dirs: if not os.path.exists(target_path + curr_dir): shutil.copytree(source_path+curr_dir, target_path+curr_dir) for curr_file in files: if not os.path.exists(target_path + curr_file): shutil.copyfile(source_path+curr_file, target_path+curr_file) elif curr_file == easy_inst: os.remove(target_path+curr_file) shutil.copyfile(source_path+curr_file, target_path+curr_file)
def run_command(self, options, args): if args: # Uninstall pythonidae for arg in args: pkg = Package(arg) pkgname = pkg.name pkgpath = os.path.join(PATH_PYTHONS, pkgname) venvpath = os.path.join(PATH_VENVS, pkgname) if not is_installed(pkgname): logger.error("`%s` is not installed." % pkgname) continue if get_using_python_pkgname() == pkgname: off() for d in os.listdir(PATH_BIN): # remove symlink path = os.path.join(PATH_BIN, d) if os.path.islink(path): basename = os.path.basename(os.path.realpath(path)) tgtpath = os.path.join(pkgpath, "bin", basename) if os.path.isfile(tgtpath) and os.path.samefile(path, tgtpath): unlink(path) rm_r(pkgpath) rm_r(venvpath) else: self.parser.print_help()
def install(self): if os.path.isdir(self.install_dir): logger.info("You are already installed `%s`" % self.pkg.name) sys.exit() self.ensure() self.download() logger.info("") logger.info("This could take a while. You can run the following command on another shell to track the status:") logger.info(" tail -f %s" % self.logfile) logger.info("") self.unpack() self.patch() logger.info("Installing %s into %s" % (self.pkg.name, self.install_dir)) try: self.configure() self.make() self.make_install() except: rm_r(self.install_dir) logger.error("Failed to install %s. See %s to see why." % (self.pkg.name, self.logfile)) logger.info(" pythonbrew install --force %s" % self.pkg.version) sys.exit(1) self.install_setuptools() logger.info("Installed %(pkgname)s successfully. Run the following command to switch to %(pkgname)s." % {"pkgname":self.pkg.name}) logger.info("") logger.info(" pythonbrew switch %s" % self.pkg.version)
def install_setuptools(self): options = self.options pkgname = self.pkg.name if options.no_setuptools: logger.log("Skip installation of setuptools.") return download_url = DISTRIBUTE_SETUP_DLSITE filename = Link(download_url).filename download_file = os.path.join(PATH_DISTS, filename) dl = Downloader() dl.download(filename, download_url, download_file) install_dir = os.path.join(PATH_PYTHONS, pkgname) path_python = os.path.join(install_dir, "bin", "python") try: s = Subprocess(log=self.logfile, cwd=PATH_DISTS, verbose=self.options.verbose) logger.info("Installing distribute into %s" % install_dir) s.check_call([path_python, filename]) # installing pip easy_install = os.path.join(install_dir, 'bin', 'easy_install') if os.path.isfile(easy_install): logger.info("Installing pip into %s" % install_dir) s.check_call([easy_install, 'pip']) except: logger.error( "Failed to install setuptools. See %s/build.log to see why." % (ROOT)) logger.log("Skip installation of setuptools.")
def run_command(self, options, args): if args: # Uninstall pythons for arg in args: pkg = Package(arg) pkgname = pkg.name pkgpath = os.path.join(PATH_PYTHONS, pkgname) venvpath = os.path.join(PATH_VENVS, pkgname) if not is_installed(pkgname): logger.error("`%s` is not installed." % pkgname) continue if get_using_python_pkgname() == pkgname: off() for d in os.listdir(PATH_BIN): # remove symlink path = os.path.join(PATH_BIN, d) if os.path.islink(path): basename = os.path.basename(os.path.realpath(path)) tgtpath = os.path.join(pkgpath, 'bin', basename) if os.path.isfile(tgtpath) and os.path.samefile( path, tgtpath): unlink(path) rm_r(pkgpath) rm_r(venvpath) else: self.parser.print_help()
def unpack_downloadfile(content_type, download_file, target_dir): logger.info("Extracting %s into %s" % (os.path.basename(download_file), target_dir)) if is_gzip(content_type, download_file): untar_file(download_file, target_dir) else: logger.error("Cannot determine archive format of %s" % download_file) return False return True
def extract_downloadfile(content_type, download_file, target_dir): logger.info("Extracting %s into %s" % (os.path.basename(download_file), target_dir)) if is_gzip(content_type, download_file): untar_file(download_file, target_dir) else: logger.error("Cannot determine archive format of %s" % download_file) return False return True
def _symlink_venv(self, srcbin, dstbin, venv_dir): """Create a symlink. """ src = os.path.join(venv_dir, 'bin', srcbin) dst = os.path.join(PATH_BIN, dstbin) if os.path.isfile(src): symlink(src, dst) else: logger.error("%s was not found in your path." % src)
def _symlink(self, srcbin, dstbin, pkgname): """Create a symlink. """ src = os.path.join(PATH_PYTHONS, pkgname, 'bin', srcbin) dst = os.path.join(PATH_BIN, dstbin) if os.path.isfile(src): symlink(src, dst) else: logger.error("%s was not found in your path." % src)
def _symlink(self, srcbin, dstbin, pkgname): """Create a symlink. """ src = os.path.join(PATH_PYTHONS, pkgname, 'bin', srcbin) dst = os.path.join(PATH_BIN, dstbin) if os.path.isfile(src): symlink(src, dst) else: logger.error("%s was not found in your path." % src)
def untar_file(filename, location): if not os.path.exists(location): os.makedirs(location) if filename.lower().endswith('.gz') or filename.lower().endswith('.tgz'): mode = 'r:gz' elif filename.lower().endswith('.bz2') or filename.lower().endswith( '.tbz'): mode = 'r:bz2' elif filename.lower().endswith('.tar'): mode = 'r' else: logger.error('Cannot determine compression type for file %s' % filename) mode = 'r:*' tar = tarfile.open(filename, mode) try: # note: python<=2.5 doesnt seem to know about pax headers, filter them leading = has_leading_dir([ member.name for member in tar.getmembers() if member.name != 'pax_global_header' ]) for member in tar.getmembers(): fn = member.name if fn == 'pax_global_header': continue if leading: fn = split_leading_dir(fn)[1] path = os.path.join(location, fn) if member.isdir(): if not os.path.exists(path): os.makedirs(path) else: try: fp = tar.extractfile(member) except (KeyError, AttributeError): e = sys.exc_info()[1] # Some corrupt tar files seem to produce this # (specifically bad symlinks) logger.error( 'In the tar file %s the member %s is invalid: %s' % (filename, member.name, e)) continue if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) destfp = open(path, 'wb') try: shutil.copyfileobj(fp, destfp) finally: destfp.close() fp.close() # note: configure ...etc os.chmod(path, member.mode) # note: the file timestamps should be such that asdl_c.py is not invoked. os.utime(path, (member.mtime, member.mtime)) finally: tar.close()
def run_command_print_activate(self, options, args): if len(args) < 2: logger.error("Unrecognized command line argument: ( 'pythonbrew venv print_activate <project>' )") sys.exit(1) activate = os.path.join(self._workon_home, args[1], 'bin', 'activate') if not os.path.exists(activate): logger.error('`%s` environment already does not exist. Try `pythonbrew venv create %s`.' % (args[1], args[1])) sys.exit(1) logger.log(activate)
def patch(self): version = self.pkg.version try: s = Subprocess(log=self.logfile, cwd=self.build_dir) patches = [] eds = {} if is_python24(version): patch_dir = PATH_PATCHES_MACOSX_PYTHON24 patches = ['patch-configure', 'patch-Makefile.pre.in', 'patch-Lib-cgi.py.diff', 'patch-Lib-site.py.diff', 'patch-setup.py.diff', 'patch-Include-pyport.h', 'patch-Mac-OSX-Makefile.in', 'patch-Mac-OSX-IDLE-Makefile.in', 'patch-Mac-OSX-PythonLauncher-Makefile.in', 'patch-configure-badcflags.diff', 'patch-configure-arch_only.diff', 'patch-macosmodule.diff', 'patch-mactoolboxglue.diff', 'patch-pymactoolbox.diff', 'patch-gestaltmodule.c.diff'] elif is_python25(version): patch_dir = PATH_PATCHES_MACOSX_PYTHON25 patches = ['patch-Makefile.pre.in.diff', 'patch-Lib-cgi.py.diff', 'patch-Lib-distutils-dist.py.diff', 'patch-setup.py.diff', 'patch-configure-badcflags.diff', 'patch-configure-arch_only.diff', 'patch-64bit.diff', 'patch-pyconfig.h.in.diff', 'patch-gestaltmodule.c.diff'] eds = {'_localemodule.c.ed': 'Modules/_localemodule.c', 'locale.py.ed': 'Lib/locale.py'} elif is_python26(version): patch_dir = PATH_PATCHES_MACOSX_PYTHON26 patches = ['patch-Lib-cgi.py.diff', 'patch-Lib-distutils-dist.py.diff', 'patch-Mac-IDLE-Makefile.in.diff', 'patch-Mac-Makefile.in.diff', 'patch-Mac-PythonLauncher-Makefile.in.diff', 'patch-Mac-Tools-Doc-setup.py.diff', 'patch-setup.py-db46.diff', 'patch-Lib-ctypes-macholib-dyld.py.diff', 'patch-setup_no_tkinter.py.diff'] eds = {'_localemodule.c.ed': 'Modules/_localemodule.c', 'locale.py.ed': 'Lib/locale.py'} if patches or eds: logger.info("Patching %s" % self.pkg.name) for patch in patches: s.check_call("patch -p0 < %s" % os.path.join(patch_dir, patch)) for (ed, source) in eds.items(): ed = os.path.join(patch_dir, ed) s.check_call('ed - %s < %s' % (source, ed)) except: logger.error("Failed to patch `%s`" % self.build_dir) sys.exit(1)
def untar_file(filename, location): if not os.path.exists(location): os.makedirs(location) if filename.lower().endswith('.gz') or filename.lower().endswith('.tgz'): mode = 'r:gz' elif filename.lower().endswith('.bz2') or filename.lower().endswith('.tbz'): mode = 'r:bz2' elif filename.lower().endswith('.tar'): mode = 'r' else: logger.error('Cannot determine compression type for file %s' % filename) mode = 'r:*' tar = tarfile.open(filename, mode) try: # note: python<=2.5 doesnt seem to know about pax headers, filter them leading = has_leading_dir([ member.name for member in tar.getmembers() if member.name != 'pax_global_header' ]) for member in tar.getmembers(): fn = member.name if fn == 'pax_global_header': continue if leading: fn = split_leading_dir(fn)[1] path = os.path.join(location, fn) if member.isdir(): if not os.path.exists(path): os.makedirs(path) else: try: fp = tar.extractfile(member) except (KeyError, AttributeError): e = sys.exc_info()[1] # Some corrupt tar files seem to produce this # (specifically bad symlinks) logger.error('In the tar file %s the member %s is invalid: %s' % (filename, member.name, e)) continue if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) destfp = open(path, 'wb') try: shutil.copyfileobj(fp, destfp) finally: destfp.close() fp.close() # note: configure ...etc os.chmod(path, member.mode) # note: the file timestamps should be such that asdl_c.py is not invoked. os.utime(path, (member.mtime, member.mtime)) finally: tar.close()
def run_command(self, options, args): if not args: logger.error("Unrecognized command line argument: argument not found.") sys.exit(1) pkg = Package(args[0]) pkgname = pkg.name pkgdir = "%s/%s" % (PATH_PYTHONS, pkgname) if not os.path.isdir(pkgdir): logger.error("`%s` is not installed." % pkgname) sys.exit(1) self._switch_dir(pkgdir) logger.info("Switched to %s" % pkgname)
def run_command_delete(self, options, args): for arg in args[1:]: target_dir = os.path.join(self._workon_home, arg) if not os.path.isdir(target_dir): logger.error('%s already does not exist.' % target_dir) else: if not os.access(target_dir, os.W_OK): logger.error("Can not delete %s.\nPermission denied." % target_dir) continue logger.info('Deleting `%s` environment in %s' % (arg, self._workon_home)) # make command rm_r(target_dir)
def run_command_init(self): if os.path.exists(self._venv): logger.info('venv command is already initialized.') return if not os.access(PATH_DISTS, os.W_OK): logger.error("Can not initialize venv command: Permission denied.") sys.exit(1) d = Downloader() download_file = os.path.join(PATH_DISTS, 'virtualenv.tar.gz') d.download('virtualenv.tar.gz', VIRTUALENV_DLSITE, download_file) logger.info('Extracting virtualenv into %s' % self._venv_dir) untar_file(download_file, self._venv_dir)
def run_command_rename(self, options, args): if len(args) < 3: logger.error( "Unrecognized command line argument: ( 'pythonbrew venv rename <source> <target>' )" ) sys.exit(1) if not os.access(PATH_VENVS, os.W_OK): logger.error( "Can not rename a virtual environment in %s.\nPermission denied." % PATH_VENVS) sys.exit(1) source_dir = os.path.join(self._workon_home, args[1]) target_dir = os.path.join(self._workon_home, args[2]) if not os.path.isdir(source_dir): logger.error('%s does not exist.' % source_dir) sys.exit(1) if os.path.isdir(target_dir): logger.error('Can not overwrite %s.' % target_dir) sys_exit(1) os.rename(source_dir, target_dir)
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) pkg = Package(args[0]) pkgname = pkg.name if not is_installed(pkgname): logger.error("`%s` is not installed." % pkgname) sys.exit(1) pkgbin = os.path.join(PATH_PYTHONS,pkgname,'bin') set_current_path(pkgbin) logger.info("Switched to %s" % pkgname)
def _do_patch(self): try: s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose) if self.patches: logger.info("Patching %s" % self.pkg.name) for patch in self.patches: if type(patch) is dict: for (ed, source) in patch.items(): s.shell('ed - "%s" < "%s"' % (source, ed)) else: s.shell('patch -p0 < "%s"' % patch) except: logger.error("Failed to patch `%s`.\n%s" % (self.build_dir, sys.exc_info()[1])) sys.exit(1)
def _do_patch(self): try: s = Subprocess(log=self.logfile, cwd=self.build_dir, verbose=self.options.verbose) if self.patches: logger.info("Patching %s" % self.pkg.name) for patch in self.patches: if type(patch) is dict: for (ed, source) in patch.items(): s.shell('ed - %s < %s' % (source, ed)) else: s.shell("patch -p0 < %s" % patch) except: logger.error("Failed to patch `%s`.\n%s" % (self.build_dir, sys.exc_info()[1])) sys.exit(1)
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) pkg = Package(args[0]) pkgname = pkg.name if not is_installed(pkgname): logger.error("`%s` is not installed." % pkgname) sys.exit(1) pkgbin = os.path.join(PATH_PYTHONS, pkgname, 'bin') set_current_path(pkgbin) logger.info("Switched to %s" % pkgname)
def run_command(self, options, args): if options.default: # create only one instance as default of an application. pythons = self._get_pythons([options.default]) for pkgname in pythons: if args: bin = args[0] self._symlink(bin, bin, pkgname) else: self._symlink('python', 'py', pkgname) elif options.venv: if options.pythons: pkgname = Package(options.pythons[0]).name else: pkgname = get_using_python_pkgname() if not is_installed(pkgname): logger.error('`%s` is not installed.') sys.exit(1) venv_pkgdir = os.path.join(PATH_VENVS, pkgname) venv_dir = os.path.join(venv_pkgdir, options.venv) if not os.path.isdir(venv_dir): logger.error("`%s` environment was not found in %s." % (options.venv, venv_pkgdir)) sys.exit(1) pkg = Package(pkgname) if args: bin = args[0] dstbin = '%s%s-%s' % (bin, pkg.version, options.venv) self._symlink(bin, dstbin, pkgname) else: dstbin = 'py%s-%s' % (pkg.version, options.venv) self._symlink('python', dstbin, pkgname) else: pythons = self._get_pythons(options.pythons) for pkgname in pythons: if options.remove: # remove symlinks for bin in os.listdir(PATH_BIN): path = os.path.join(PATH_BIN, bin) if os.path.islink(path): unlink(path) else: # create symlinks if args: bin = args[0] self._symlink_version_suffix(bin, bin, pkgname) else: self._symlink_version_suffix('python', 'py', pkgname)
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) pkg = Package(args[0]) pkgname = pkg.name pkgdir = os.path.join(PATH_PYTHONS, pkgname) if not os.path.isdir(pkgdir): logger.error("`%s` is not installed." % pkgname) sys.exit(1) pkgbin = os.path.join(pkgdir,'bin') self._set_temp(pkgbin) logger.info("Using `%s`" % pkgname)
def run_command_use(self, options, args): if len(args) < 2: logger.error("Unrecognized command line argument: ( 'pythonbrew venv use <project>' )") sys.exit(1) activate = os.path.join(self._workon_home, args[1], 'bin', 'activate') if not os.path.exists(activate): logger.error('`%s` environment already does not exist. Try `pythonbrew venv create %s`.' % (args[1], args[1])) sys.exit(1) self._write("""\ echo '# Using `%(arg)s` environment (found in %(workon_home)s)' echo '# To leave an environment, simply run `deactivate`' source '%(activate)s' """ % {'arg': args[1], 'workon_home': self._workon_home, 'activate': activate})
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) pkg = Package(args[0]) pkgname = pkg.name pkgdir = os.path.join(PATH_PYTHONS, pkgname) if not os.path.isdir(pkgdir): logger.error("`%s` is not installed." % pkgname) sys.exit(1) pkgbin = os.path.join(pkgdir, 'bin') self._set_temp(pkgbin) logger.info("Using `%s`" % pkgname)
def run_command(self, options, args): if options.default: # create only one instance as default of an application. pythons = self._get_pythons([options.default]) for pkgname in pythons: if args: bin = args[0] self._symlink(bin, bin, pkgname) else: self._symlink('python', 'py', pkgname) elif options.venv: if options.pythons: pkgname = Package(options.pythons[0]).name else: pkgname = get_using_python_pkgname() if not is_installed(pkgname): logger.error('`%s` is not installed.') sys.exit(1) venv_pkgdir = os.path.join(PATH_VENVS, pkgname) venv_dir = os.path.join(venv_pkgdir, options.venv) if not os.path.isdir(venv_dir): logger.error("`%s` environment was not found in %s." % (options.venv, venv_pkgdir)) sys.exit(1) pkg = Package(pkgname) if args: bin = args[0] dstbin = '%s%s-%s' % (bin, pkg.version, options.venv) self._symlink(bin, dstbin, pkgname) else: dstbin = 'py%s-%s' % (pkg.version, options.venv) self._symlink('python', dstbin, pkgname) else: pythons = self._get_pythons(options.pythons) for pkgname in pythons: if options.remove: # remove symlinks for bin in os.listdir(PATH_BIN): path = os.path.join(PATH_BIN, bin) if os.path.islink(path): unlink(path) else: # create symlinks if args: bin = args[0] self._symlink_version_suffix(bin, bin, pkgname) else: self._symlink_version_suffix('python', 'py', pkgname)
def _update_config(self, options, args): # config.cfg update # TODO: Automatically create for config.cfg download_url = PYTHONBREW_UPDATE_URL_CONFIG if not download_url: logger.error("Invalid download url in config.cfg. `%s`" % download_url) sys.exit(1) distname = Link(PYTHONBREW_UPDATE_URL_CONFIG).filename download_file = PATH_ETC_CONFIG try: d = Downloader() d.download(distname, download_url, download_file) except: logger.error("Failed to download. `%s`" % download_url) sys.exit(1) logger.log("The config.cfg has been updated.")
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) pythons = self._get_pythons(options.pythons) for d in pythons: if options.verbose: logger.info('`%s` running...' % d) path = os.path.join(PATH_PYTHONS, d, 'bin', args[0]) if os.path.isfile(path) and os.access(path, os.X_OK): subprocess.call([path] + args[1:]) else: path = os.path.join(PATH_PYTHONS, d, 'bin', 'python') if os.path.isfile(path) and os.access(path, os.X_OK): subprocess.call([path] + args) else: logger.error('%s: No such file or directory.' % path)
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) pythons = self._get_pythons(options.pythons) for d in pythons: if options.verbose: logger.info('`%s` running...' % d) path = os.path.join(PATH_PYTHONS, d, 'bin', args[0]) if os.path.isfile(path) and os.access(path, os.X_OK): subprocess.call([path] + args[1:]) else: path = os.path.join(PATH_PYTHONS, d, 'bin', 'python') if os.path.isfile(path) and os.access(path, os.X_OK): subprocess.call([path] + args) else: logger.error('%s: No such file or directory.' % path)
def available_install(self, options, args): logger.log('# Pythons') if args: pkg = Package(args[0]) _re = re.compile(r"%s" % pkg.name) pkgs = [] for pkgname in self._get_packages_name(options): if _re.match(pkgname): pkgs.append(pkgname) if pkgs: for pkgname in pkgs: logger.log("%s" % pkgname) else: logger.error("`%s` was not found." % pkg.name) else: for pkgname in self._get_packages_name(options): logger.log("%s" % pkgname)
def _update_config(self, options, args): # config.cfg update # TODO: Automatically create for config.cfg download_url = PYTHONBREW_UPDATE_URL_CONFIG if not download_url: logger.error("Invalid download url in config.cfg. `%s`" % download_url) sys.exit(1) distname = Link(PYTHONBREW_UPDATE_URL_CONFIG).filename download_file = PATH_ETC_CONFIG try: d = Downloader() d.download(distname, download_url, download_file) except: logger.error("Failed to download. `%s`" % download_url) sys.exit(1) logger.log("The config.cfg has been updated.")
def available_install(self, options, args): logger.log('# Pythons') if args: pkg = Package(args[0]) _re = re.compile(r"%s" % pkg.name) pkgs = [] for pkgname in self._get_packages_name(options): if _re.match(pkgname): pkgs.append(pkgname) if pkgs: for pkgname in pkgs: logger.log("%s" % pkgname) else: logger.error("`%s` was not found." % pkg.name) else: for pkgname in self._get_packages_name(options): logger.log("%s" % pkgname)
def install(self): # cleanup if os.path.isdir(self.build_dir): shutil.rmtree(self.build_dir) # get content type. if is_file(self.download_url): path = fileurl_to_path(self.download_url) self.content_type = mimetypes.guess_type(path)[0] else: headerinfo = get_headerinfo_from_url(self.download_url) self.content_type = headerinfo['content-type'] if is_html(self.content_type): # note: maybe got 404 or 503 http status code. logger.error("Invalid content-type: `%s`" % self.content_type) return if os.path.isdir(self.install_dir): logger.info("You are already installed `%s`" % self.pkg.name) return self.download_and_extract() logger.info( "\nThis could take a while. You can run the following command on another shell to track the status:" ) logger.info(" tail -f %s\n" % self.logfile) self.patch() logger.info("Installing %s into %s" % (self.pkg.name, self.install_dir)) try: self.configure() self.make() self.make_install() except: rm_r(self.install_dir) logger.error("Failed to install %s. See %s to see why." % (self.pkg.name, self.logfile)) logger.log(" pythonbrew install --force %s" % self.pkg.version) sys.exit(1) self.symlink() self.install_setuptools() logger.info( "\nInstalled %(pkgname)s successfully. Run the following command to switch to %(pkgname)s." % {"pkgname": self.pkg.name}) logger.info(" pythonbrew switch %s" % self.pkg.alias)
def install(self): # cleanup if os.path.isdir(self.build_dir): shutil.rmtree(self.build_dir) # get content type. if is_file(self.download_url): path = fileurl_to_path(self.download_url) self.content_type = mimetypes.guess_type(path)[0] else: headerinfo = get_headerinfo_from_url(self.download_url) self.content_type = headerinfo["content-type"] if is_html(self.content_type): # note: maybe got 404 or 503 http status code. logger.error("Invalid content-type: `%s`" % self.content_type) return if os.path.isdir(self.install_dir): logger.info("You are already installed `%s`" % self.pkg.name) return self.download_and_extract() logger.info( "\nThis could take a while. You can run the following command on another shell to track the status:" ) logger.info(" tail -f %s\n" % self.logfile) self.patch() logger.info("Installing %s into %s" % (self.pkg.name, self.install_dir)) try: self.configure() self.make() self.make_install() except: rm_r(self.install_dir) logger.error("Failed to install %s. See %s to see why." % (self.pkg.name, self.logfile)) logger.log(" pythonbrew install --force %s" % self.pkg.version) sys.exit(1) self.symlink() self.install_setuptools() logger.info( "\nInstalled %(pkgname)s successfully. Run the following command to switch to %(pkgname)s." % {"pkgname": self.pkg.name} ) logger.info(" pythonbrew switch %s" % self.pkg.alias)
def run_command_init(self): if os.path.exists(self._venv): logger.info('Remove virtualenv. (%s)' % self._venv_dir) rm_r(self._venv_dir) if os.path.exists(self._venv_clone): logger.info('Remove virtualenv-clone. (%s)' % self._venv_clone_dir) rm_r(self._venv_clone_dir) if not os.access(PATH_DISTS, os.W_OK): logger.error("Can not initialize venv command: Permission denied.") sys.exit(1) d = Downloader() download_file = os.path.join(PATH_DISTS, 'virtualenv.tar.gz') d.download('virtualenv.tar.gz', VIRTUALENV_DLSITE, download_file) logger.info('Extracting virtualenv into %s' % self._venv_dir) untar_file(download_file, self._venv_dir) download_file = os.path.join(PATH_DISTS, 'virtualenv-clone.tar.gz') d.download('virtualenv-clone.tar.gz', VIRTUALENV_CLONE_DLSITE, download_file) logger.info('Extracting virtualenv-clone into %s' % self._venv_clone_dir) untar_file(download_file, self._venv_clone_dir)
def _update_pythonbrew(self, options, args): if options.master: version = 'master' elif options.develop: version = 'develop' else: version = get_stable_version() # check for version if not options.force and Version(version) <= VERSION: logger.info( "You are already running the installed latest version of pythonbrew." ) return download_url = get_pythonbrew_update_url(version) if not download_url: logger.error("`pythonbrew-%s` was not found in pypi." % version) sys.exit(1) headinfo = get_headerinfo_from_url(download_url) content_type = headinfo['content-type'] if not options.master and not options.develop: if not is_gzip(content_type, Link(download_url).filename): logger.error("content type should be gzip. content-type:`%s`" % content_type) sys.exit(1) filename = "pythonbrew-%s" % version distname = "%s.tgz" % filename download_file = os.path.join(PATH_DISTS, distname) try: d = Downloader() d.download(distname, download_url, download_file) except: logger.error("Failed to download. `%s`" % download_url) sys.exit(1) extract_dir = os.path.join(PATH_BUILD, filename) rm_r(extract_dir) if not extract_downloadfile(content_type, download_file, extract_dir): sys.exit(1) try: logger.info("Installing %s into %s" % (extract_dir, ROOT)) s = Subprocess() s.check_call([ sys.executable, os.path.join(extract_dir, 'pythonbrew_install.py'), '--upgrade' ]) except: logger.error("Failed to update pythonbrew.") sys.exit(1) logger.info("The pythonbrew has been updated.")
def download_and_extract(self): if is_file(self.download_url): path = fileurl_to_path(self.download_url) if os.path.isdir(path): logger.info('Copying %s into %s' % (path, self.build_dir)) shutil.copytree(path, self.build_dir) return if os.path.isfile(self.download_file): logger.info("Use the previously fetched %s" % (self.download_file)) else: base_url = Link(self.download_url).base_url try: dl = Downloader() dl.download(base_url, self.download_url, self.download_file) except: unlink(self.download_file) logger.error("Failed to download.\n%s" % (sys.exc_info()[1])) sys.exit(1) # extracting if not extract_downloadfile(self.content_type, self.download_file, self.build_dir): sys.exit(1)
def run_command(self, options, args): if options.python: pkgname = Package(options.python).name else: pkgname = get_using_python_pkgname() if not is_installed(pkgname): logger.error('`%s` is not installed.' % pkgname) sys.exit(1) logger.info('Using %s' % pkgname) # build a path python = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python') # Download bootstrap.py download_url = BOOTSTRAP_DLSITE filename = Link(download_url).filename bootstrap = os.path.join(os.getcwd(), filename) # fetching into current directory try: d = Downloader() d.download(filename, download_url, bootstrap) except: e = sys.exc_info()[1] logger.error("%s" % (e)) sys.exit(1) # call bootstrap.py if subprocess.call([python, bootstrap, '-d']): logger.error('Failed to bootstrap.') sys.exit(1) # call buildout subprocess.call(['./bin/buildout'])
def run_command(self, options, args): if not args: self.parser.print_help() sys.exit(1) cmd = args[0] if not cmd in ('init', 'create', 'delete', 'use', 'list'): self.parser.print_help() sys.exit(1) # initialize? if cmd == 'init': self.run_command_init() return # target python interpreter if options.python: pkgname = Package(options.python).name if not is_installed(pkgname): logger.error('%s is not installed.' % pkgname) sys.exit(1) else: # check using python under pythonbrew pkgname = get_using_python_pkgname() if not pkgname: logger.error( 'Can not use venv command before switching a python. Try \'pythonbrew switch <version of python>\'.' ) sys.exit(1) self._pkgname = pkgname self._target_py = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python') self._workon_home = os.path.join(PATH_VENVS, pkgname) self._py = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python') # is already installed virtualenv? if not os.path.exists(self._venv): self.run_command_init() # Create a shell script self.__getattribute__('run_command_%s' % cmd)(options, args)