def _add_dist_zip(self, path, dist_name): # We need to distinguish between wheels and other zips. Most of the time, # when we have a zip, it contains its contents in an importable form. # But wheels don't have to be importable, so we need to force them # into an importable shape. We can do that by installing it into its own # wheel dir. if dist_name.endswith("whl"): from wheel.install import WheelFile tmp = safe_mkdtemp() whltmp = os.path.join(tmp, dist_name) os.mkdir(whltmp) wf = WheelFile(path) wf.install(overrides=self._get_installer_paths(whltmp), force=True) for (root, _, files) in os.walk(whltmp): pruned_dir = os.path.relpath(root, tmp) for f in files: fullpath = os.path.join(root, f) if os.path.isdir(fullpath): continue target = os.path.join(self._pex_info.internal_cache, pruned_dir, f) self._chroot.copy(fullpath, target) return CacheHelper.dir_hash(whltmp) with open_zip(path) as zf: for name in zf.namelist(): if name.endswith('/'): continue target = os.path.join(self._pex_info.internal_cache, dist_name, name) self._chroot.write(zf.read(name), target) return CacheHelper.zip_hash(zf)
def _add_dist_zip(self, path, dist_name): # We need to distinguish between wheels and other zips. Most of the time, # when we have a zip, it contains its contents in an importable form. # But wheels don't have to be importable, so we need to force them # into an importable shape. We can do that by installing it into its own # wheel dir. if dist_name.endswith("whl"): from wheel.install import WheelFile tmp = safe_mkdtemp() whltmp = os.path.join(tmp, dist_name) os.mkdir(whltmp) wf = WheelFile(path) wf.install(overrides=self._get_installer_paths(whltmp), force=True) for (root, _, files) in os.walk(whltmp): pruned_dir = os.path.relpath(root, tmp) for f in files: fullpath = os.path.join(root, f) if os.path.isdir(fullpath): continue target = os.path.join(self._pex_info.internal_cache, pruned_dir, f) self._chroot.copy(fullpath, target) return CacheHelper.dir_hash(whltmp) with open_zip(path) as zf: for name in zf.namelist(): if name.endswith('/'): continue target = os.path.join(self._pex_info.internal_cache, dist_name, name) self._chroot.write(zf.read(name), target) return CacheHelper.zip_hash(zf)
def ensure_setup_requires_site_dir(reqs_to_resolve, interpreter, site_dir, platforms=None): if not reqs_to_resolve: return None setup_requires_dists = resolve_multi(interpreter, reqs_to_resolve, platforms, None) # FIXME: there's no description of what this does or why it's necessary. overrides = { 'purelib': site_dir, 'headers': os.path.join(site_dir, 'headers'), 'scripts': os.path.join(site_dir, 'bin'), 'platlib': site_dir, 'data': site_dir } # The `python_dist` target builds for the current platform only. # FIXME: why does it build for the current platform only? for obj in setup_requires_dists['current']: wf = WheelFile(obj.location) wf.install(overrides=overrides, force=True) return SetupRequiresSiteDir(site_dir)
def test_install(): def check(*path): return os.path.exists(os.path.join(*path)) def get_supported(): return list( wheel.pep425tags.get_supported()) + [('py3', 'none', 'win32')] tempdir = mkdtemp() whl = WheelFile(TESTWHEEL, context=get_supported) assert whl.supports_current_python(get_supported) try: locs = {} for key in ('purelib', 'platlib', 'scripts', 'headers', 'data'): locs[key] = os.path.join(tempdir, key) os.mkdir(locs[key]) whl.install(overrides=locs) assert len(os.listdir(locs['purelib'])) == 0 assert check(locs['platlib'], 'hello.pyd') assert check(locs['platlib'], 'hello', 'hello.py') assert check(locs['platlib'], 'hello', '__init__.py') assert check(locs['data'], 'hello.dat') assert check(locs['headers'], 'hello.dat') assert check(locs['scripts'], 'hello.sh') assert check(locs['platlib'], 'test-1.0.dist-info', 'RECORD') finally: shutil.rmtree(tempdir)
def _ensure_setup_requires_site_dir(self, dist_targets, interpreter, site_dir): reqs_to_resolve = set() for tgt in dist_targets: for setup_req_lib_addr in tgt.setup_requires: for req_lib in self.context.build_graph.resolve(setup_req_lib_addr): for req in req_lib.requirements: reqs_to_resolve.add(req) if not reqs_to_resolve: return None self.context.log.debug('python_dist target(s) with setup_requires detected. ' 'Installing setup requirements: {}\n\n' .format([req.key for req in reqs_to_resolve])) setup_requires_dists = _resolve_multi(interpreter, reqs_to_resolve, ['current'], None) overrides = { 'purelib': site_dir, 'headers': os.path.join(site_dir, 'headers'), 'scripts': os.path.join(site_dir, 'bin'), 'platlib': site_dir, 'data': site_dir } # The `python_dist` target builds for the current platform only. for obj in setup_requires_dists['current']: wf = WheelFile(obj.location) wf.install(overrides=overrides, force=True) return site_dir
def install_module(mod_name, mod_vals, debug=False): """Checks for dependencies, then installs module""" already_installed = check_install(mod_vals["ImportedName"], mod_vals["Version"]) if not already_installed: for dep in mod_vals["Dependencies"]: if not check_install(dep): if debug: print( "Did not install module; did not have all dependencies. Hopefully, this module will be placed on the waitlist" ) return False print("Installing module %s version %s" % (mod_name, mod_vals["Version"])) print("Downloading module") install_req = requests.get(mod_vals["DownloadLink"]) install_filename = os.path.join("/tmp", mod_vals["Filename"]) install_content = install_req.content with open(install_filename, "wb") as mod_file: mod_file.write(install_content) print("Installing module") wheelfile = WheelFile(install_filename) try: wheelfile.install(force=True) except ValueError as e: print(e) installed = check_install(mod_vals["ImportedName"], mod_vals["Version"]) os.remove(install_filename) if installed: print("Successfully installed") else: print("Failed to install") return installed else: if debug: print( "Not installing module. It is %s that this module was already installed" % already_installed) return already_installed
def test_install(): tempdir = mkdtemp() def get_supported(): return list(wheel.pep425tags.get_supported()) + [('py3', 'none', 'win32')] whl = WheelFile(TESTWHEEL, context=get_supported) assert whl.supports_current_python(get_supported) try: locs = {} for key in ('purelib', 'platlib', 'scripts', 'headers', 'data'): locs[key] = os.path.join(tempdir, key) os.mkdir(locs[key]) whl.install(overrides=locs) assert len(os.listdir(locs['purelib'])) == 0 assert check(locs['platlib'], 'hello.pyd') assert check(locs['platlib'], 'hello', 'hello.py') assert check(locs['platlib'], 'hello', '__init__.py') assert check(locs['data'], 'hello.dat') assert check(locs['headers'], 'hello.dat') assert check(locs['scripts'], 'hello.sh') assert check(locs['platlib'], 'test-1.0.dist-info', 'RECORD') finally: shutil.rmtree(tempdir)
def ensure_setup_requires_site_dir(reqs_to_resolve, interpreter, site_dir, platforms=None): if not reqs_to_resolve: return None setup_requires_dists = resolve_multi(interpreter, reqs_to_resolve, platforms, None) # FIXME: there's no description of what this does or why it's necessary. overrides = { 'purelib': site_dir, 'headers': os.path.join(site_dir, 'headers'), 'scripts': os.path.join(site_dir, 'bin'), 'platlib': site_dir, 'data': site_dir } # The `python_dist` target builds for the current platform only. # FIXME: why does it build for the current platform only? for obj in setup_requires_dists['current']: wf = WheelFile(obj.location) wf.install(overrides=overrides, force=True) return SetupRequiresSiteDir(site_dir)