def build_venv(cls, path, executable=None): if executable is not None: # Create virtualenv by using an external executable try: p = subprocess.Popen(" ".join([executable, "-"]), stdin=subprocess.PIPE, shell=True) p.communicate(encode(CREATE_VENV_COMMAND.format(path))) except CalledProcessError as e: raise EnvCommandError(e) return try: from venv import EnvBuilder # use the same defaults as python -m venv if os.name == "nt": use_symlinks = False else: use_symlinks = True builder = EnvBuilder(with_pip=True, symlinks=use_symlinks) build = builder.create except ImportError: # We fallback on virtualenv for Python 2.7 from virtualenv import create_environment build = create_environment build(path)
def generate_env_name(cls, name, cwd): # type: (str, str) -> str name = name.lower() sanitized_name = re.sub(r'[ $`!*@"\\\r\n\t]', "_", name)[:42] h = hashlib.sha256(encode(cwd)).digest() h = base64.urlsafe_b64encode(h).decode()[:8] return "{}-{}".format(sanitized_name, h)
def build(self, target_dir=None): # type: (Path) -> Path self._io.writeln(' - Building <info>sdist</info>') if target_dir is None: target_dir = self._path / 'dist' if not target_dir.exists(): target_dir.mkdir(parents=True) target = target_dir / '{}-{}.tar.gz'.format( self._package.pretty_name, self._package.version ) gz = GzipFile(target.as_posix(), mode='wb') tar = tarfile.TarFile(target.as_posix(), mode='w', fileobj=gz, format=tarfile.PAX_FORMAT) try: tar_dir = '{}-{}'.format( self._package.pretty_name, self._package.version ) files_to_add = self.find_files_to_add(exclude_build=False) for relpath in files_to_add: path = self._path / relpath tar_info = tar.gettarinfo( str(path), arcname=pjoin(tar_dir, str(relpath)) ) tar_info = self.clean_tarinfo(tar_info) if tar_info.isreg(): with path.open('rb') as f: tar.addfile(tar_info, f) else: tar.addfile(tar_info) # Symlinks & ? setup = self.build_setup() tar_info = tarfile.TarInfo(pjoin(tar_dir, 'setup.py')) tar_info.size = len(setup) tar.addfile(tar_info, BytesIO(setup)) pkg_info = encode(PKG_INFO.format( name=self._meta.name, version=self._meta.version, summary=self._meta.summary, home_page=self._meta.home_page, author=to_str(self._meta.author), author_email=to_str(self._meta.author_email), )) tar_info = tarfile.TarInfo(pjoin(tar_dir, 'PKG-INFO')) tar_info.size = len(pkg_info) tar.addfile(tar_info, BytesIO(pkg_info)) finally: tar.close() gz.close() self._io.writeln(' - Built <fg=cyan>{}</>'.format(target.name)) return target
def generate_env_name(cls, name: str, cwd: str) -> str: name = name.lower() sanitized_name = re.sub(r'[ $`!*@"\\\r\n\t]', "_", name)[:42] h = hashlib.sha256(encode(cwd)).digest() h = base64.urlsafe_b64encode(h).decode()[:8] return f"{sanitized_name}-{h}"
def run(self, bin, *args, **kwargs): """ Run a command inside the Python environment. """ bin = self._bin(bin) cmd = [bin] + list(args) shell = kwargs.get("shell", False) call = kwargs.pop("call", False) input_ = kwargs.pop("input_", None) if shell: cmd = list_to_shell_command(cmd) try: if self._is_windows: kwargs["shell"] = True if input_: output = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=encode(input_), check=True, **kwargs).stdout elif call: return subprocess.call(cmd, stderr=subprocess.STDOUT, **kwargs) else: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, **kwargs) except CalledProcessError as e: raise EnvCommandError(e, input=input_) return decode(output)
def _run(self, cmd, **kwargs): """ Run a command inside the Python environment. """ shell = kwargs.get("shell", False) call = kwargs.pop("call", False) input_ = kwargs.pop("input_", None) if shell: cmd = list_to_shell_command(cmd) try: if self._is_windows: kwargs["shell"] = True if input_: p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs) output = p.communicate(encode(input_))[0] elif call: return subprocess.call(cmd, stderr=subprocess.STDOUT, **kwargs) else: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, **kwargs) except CalledProcessError as e: raise EnvCommandError(e) return decode(output)
def build_setup(self): # type: () -> bytes before, extra, after = [], [], [] # If we have a build script, use it if self._package.build: after += [ "from {} import *".format(self._package.build.split(".")[0]), "build(setup_kwargs)", ] if self._module.is_in_src(): before.append("package_dir = \\\n{}\n".format(pformat({"": "src"}))) extra.append("'package_dir': package_dir,") if self._module.is_package(): packages, package_data = self.find_packages(self._module.path.as_posix()) before.append("packages = \\\n{}\n".format(pformat(sorted(packages)))) before.append("package_data = \\\n{}\n".format(pformat(package_data))) extra.append("'packages': packages,") extra.append("'package_data': package_data,") else: extra.append("'py_modules': {!r},".format(to_str(self._module.name))) dependencies, extras = self.convert_dependencies( self._package, self._package.requires ) if dependencies: before.append( "install_requires = \\\n{}\n".format(pformat(sorted(dependencies))) ) extra.append("'install_requires': install_requires,") if extras: before.append("extras_require = \\\n{}\n".format(pformat(extras))) extra.append("'extras_require': extras_require,") entry_points = self.convert_entry_points() if entry_points: before.append("entry_points = \\\n{}\n".format(pformat(entry_points))) extra.append("'entry_points': entry_points,") if self._package.python_versions != "*": python_requires = self._meta.requires_python extra.append("'python_requires': {!r},".format(python_requires)) return encode( SETUP.format( before="\n".join(before), name=to_str(self._meta.name), version=to_str(self._meta.version), description=to_str(self._meta.summary), long_description=to_str(self._meta.description), author=to_str(self._meta.author), author_email=to_str(self._meta.author_email), url=to_str(self._meta.home_page), extra="\n ".join(extra), after="\n".join(after), ) )
def _run(self, cmd: List[str], **kwargs: Any) -> Union[int, str]: """ Run a command inside the Python environment. """ call = kwargs.pop("call", False) input_ = kwargs.pop("input_", None) try: if self._is_windows: kwargs["shell"] = True if kwargs.get("shell", False): cmd = list_to_shell_command(cmd) if input_: output = subprocess.run( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=encode(input_), check=True, **kwargs, ).stdout elif call: return subprocess.call(cmd, stderr=subprocess.STDOUT, **kwargs) else: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, **kwargs) except CalledProcessError as e: raise EnvCommandError(e, input=input_) return decode(output)
def build_pkg_info(self): pkg_info = PKG_INFO.format( name=self._meta.name, version=self._meta.version, summary=self._meta.summary, home_page=self._meta.home_page, author=to_str(self._meta.author), author_email=to_str(self._meta.author_email), ) if self._meta.keywords: pkg_info += "Keywords: {}\n".format(self._meta.keywords) if self._meta.requires_python: pkg_info += "Requires-Python: {}\n".format(self._meta.requires_python) for classifier in self._meta.classifiers: pkg_info += "Classifier: {}\n".format(classifier) for extra in sorted(self._meta.provides_extra): pkg_info += "Provides-Extra: {}\n".format(extra) for dep in sorted(self._meta.requires_dist): pkg_info += "Requires-Dist: {}\n".format(dep) return encode(pkg_info)
def create_temporary_requirement(self, package: "Package") -> str: fd, name = tempfile.mkstemp("reqs.txt", f"{package.name}-{package.version}") try: os.write(fd, encode(self.requirement(package, formatted=True))) finally: os.close(fd) return name
def create_temporary_requirement(self, package): fd, name = tempfile.mkstemp( "reqs.txt", "{}-{}".format(package.name, package.version)) try: os.write(fd, encode(self.requirement(package, formatted=True))) finally: os.close(fd) return name
def create_temporary_requirement(self, package): fd, name = tempfile.mkstemp( "reqs.txt", "{}-{}".format(package.name, package.version) ) try: os.write(fd, encode(self.requirement(package, formatted=True))) finally: os.close(fd) return name
def test_auth_with_request_on_the_same_host(): auth = Auth("https://python-poetry.org", "foo", "bar") request = Request("GET", "https://python-poetry.org/docs/") assert "Authorization" not in request.headers request = auth(request) assert "Authorization" in request.headers assert request.headers["Authorization"] == "Basic {}".format( decode(base64.b64encode(encode(":".join(("foo", "bar"))))))
def test_auth_with_request_with_same_authentication(): auth = Auth("https://poetry.eustace.io", "foo", "bar") request = Request("GET", "https://*****:*****@poetry.eustace.io/docs/") assert "Authorization" not in request.headers request = auth(request) assert "Authorization" in request.headers assert request.headers["Authorization"] == "Basic {}".format( decode(base64.b64encode(encode(":".join(("foo", "bar"))))))
def create_temporary_requirement(self, package: Package) -> str: fd, name = tempfile.mkstemp("reqs.txt", f"{package.name}-{package.version}") req = self.requirement(package, formatted=True) if isinstance(req, list): req = " ".join(req) try: os.write(fd, encode(req)) finally: os.close(fd) return name
def test_get_should_invalid_cache_on_too_many_redirects_error(mocker): delete_cache = mocker.patch("cachecontrol.caches.file_cache.FileCache.delete") response = Response() response.encoding = "utf-8" response.raw = BytesIO(encode('{"foo": "bar"}')) mocker.patch( "cachecontrol.adapter.CacheControlAdapter.send", side_effect=[TooManyRedirects(), response], ) repository = PyPiRepository() repository._get("https://pypi.org/pypi/async-timeout/json") assert delete_cache.called
def test_get_should_invalid_cache_on_too_many_redirects_error(mocker: MockerFixture): delete_cache = mocker.patch("cachecontrol.caches.file_cache.FileCache.delete") response = Response() response.status_code = 200 response.encoding = "utf-8" response.raw = BytesIO(encode('{"foo": "bar"}')) mocker.patch( "poetry.utils.authenticator.Authenticator.get", side_effect=[TooManyRedirects(), response], ) repository = PyPiRepository() repository._get("https://pypi.org/pypi/async-timeout/json") assert delete_cache.called
def build_venv( cls, path, executable=None): # type: (Union[Path,str], Optional[str]) -> () if executable is not None: # Create virtualenv by using an external executable try: p = subprocess.Popen( list_to_shell_command([executable, "-"]), stdin=subprocess.PIPE, shell=True, ) p.communicate(encode(CREATE_VENV_COMMAND.format(path))) except CalledProcessError as e: raise EnvCommandError(e) return try: from venv import EnvBuilder # use the same defaults as python -m venv if os.name == "nt": use_symlinks = False else: use_symlinks = True builder = EnvBuilder(with_pip=True, symlinks=use_symlinks) builder.create(str(path)) except ImportError: try: # We fallback on virtualenv for Python 2.7 from virtualenv import create_environment create_environment(str(path)) except ImportError: # since virtualenv>20 we have to use cli_run from virtualenv import cli_run cli_run([str(path)])
def build_setup(self): # type: () -> bytes before, extra, after = [], [], [] package_dir = {} # If we have a build script, use it if self._package.build: after += [ "from {} import *".format(self._package.build.split(".")[0]), "build(setup_kwargs)", ] modules = [] packages = [] package_data = {} for include in self._module.includes: if isinstance(include, PackageInclude): if include.is_package(): pkg_dir, _packages, _package_data = self.find_packages(include) if pkg_dir is not None: package_dir[""] = os.path.relpath(pkg_dir, str(self._path)) packages += _packages package_data.update(_package_data) else: if include.source is not None: package_dir[""] = str(include.base.relative_to(self._path)) modules.append(include.elements[0].relative_to(include.base).stem) else: pass if package_dir: before.append("package_dir = \\\n{}\n".format(pformat(package_dir))) extra.append("'package_dir': package_dir,") if packages: before.append("packages = \\\n{}\n".format(pformat(sorted(packages)))) extra.append("'packages': packages,") if package_data: before.append("package_data = \\\n{}\n".format(pformat(package_data))) extra.append("'package_data': package_data,") if modules: before.append("modules = \\\n{}".format(pformat(modules))) extra.append("'py_modules': modules,".format()) dependencies, extras = self.convert_dependencies( self._package, self._package.requires ) if dependencies: before.append( "install_requires = \\\n{}\n".format(pformat(sorted(dependencies))) ) extra.append("'install_requires': install_requires,") if extras: before.append("extras_require = \\\n{}\n".format(pformat(extras))) extra.append("'extras_require': extras_require,") entry_points = self.convert_entry_points() if entry_points: before.append("entry_points = \\\n{}\n".format(pformat(entry_points))) extra.append("'entry_points': entry_points,") if self._package.python_versions != "*": python_requires = self._meta.requires_python extra.append("'python_requires': {!r},".format(python_requires)) return encode( SETUP.format( before="\n".join(before), name=to_str(self._meta.name), version=to_str(self._meta.version), description=to_str(self._meta.summary), long_description=to_str(self._meta.description), author=to_str(self._meta.author), author_email=to_str(self._meta.author_email), url=to_str(self._meta.home_page), extra="\n ".join(extra), after="\n".join(after), ) )
def build_pkg_info(self): return encode(self.get_metadata_content())