def __init__(self, name, url, disable_cache=False): if name == "pypi": raise ValueError("The name [pypi] is reserved for repositories") self._packages = [] self._name = name self._url = url.rstrip("/") self._cache_dir = Path(CACHE_DIR) / "cache" / "repositories" / name self._cache = CacheManager( { "default": "releases", "serializer": "json", "stores": { "releases": {"driver": "file", "path": str(self._cache_dir)}, "packages": {"driver": "dict"}, "matches": {"driver": "dict"}, }, } ) self._session = CacheControl( requests.session(), cache=FileCache(str(self._cache_dir / "_http")) ) url_parts = urlparse.urlparse(self._url) if not url_parts.username: self._session.auth = get_http_basic_auth( Config.create("auth.toml"), self.name ) self._disable_cache = disable_cache
def __init__(self, name, url, disable_cache=False): if name == "pypi": raise ValueError("The name [pypi] is reserved for repositories") self._packages = [] self._name = name self._url = url.rstrip("/") self._cache_dir = Path(CACHE_DIR) / "cache" / "repositories" / name self._cache = CacheManager( { "default": "releases", "serializer": "json", "stores": { "releases": {"driver": "file", "path": str(self._cache_dir)}, "packages": {"driver": "dict"}, "matches": {"driver": "dict"}, }, } ) self._session = CacheControl( requests.session(), cache=FileCache(str(self._cache_dir / "_http")) ) url_parts = urlparse.urlparse(self._url) if not url_parts.username: self._session.auth = get_http_basic_auth(self.name) self._disable_cache = disable_cache
def publish(self, repository_name, username, password): if repository_name: self._io.write_line( "Publishing <info>{}</info> (<comment>{}</comment>) " "to <fg=cyan>{}</>".format( self._package.pretty_name, self._package.pretty_version, repository_name, )) else: self._io.write_line( "Publishing <info>{}</info> (<comment>{}</comment>) " "to <fg=cyan>PyPI</>".format(self._package.pretty_name, self._package.pretty_version)) if not repository_name: url = "https://upload.pypi.org/legacy/" repository_name = "pypi" else: # Retrieving config information repository = self._poetry.config.get( "repositories.{}".format(repository_name)) if repository is None: raise RuntimeError( "Repository {} is not defined".format(repository_name)) url = repository["url"] if not (username and password): # Check if we have a token first token = self._poetry.config.get( "pypi-token.{}".format(repository_name)) if token: logger.debug( "Found an API token for {}.".format(repository_name)) username = "******" password = token else: auth = get_http_basic_auth(self._poetry.config, repository_name) if auth: logger.debug( "Found authentication information for {}.".format( repository_name)) username = auth[0] password = auth[1] # Requesting missing credentials if not username: username = self._io.ask("Username:"******"Password:") # TODO: handle certificates self._uploader.auth(username, password) return self._uploader.upload(url)
def test_get_http_basic_auth(config): config.merge( {"http-basic": { "foo": { "username": "******", "password": "******" } }}) assert get_http_basic_auth(config, "foo") == ("foo", "bar")
def publish(self, repository_name, username, password): if repository_name: self._io.writeln( "Publishing <info>{}</info> (<comment>{}</comment>) " "to <fg=cyan>{}</>".format( self._package.pretty_name, self._package.pretty_version, repository_name, )) else: self._io.writeln( "Publishing <info>{}</info> (<comment>{}</comment>) " "to <fg=cyan>PyPI</>".format(self._package.pretty_name, self._package.pretty_version)) if not repository_name: url = "https://upload.pypi.org/legacy/" repository_name = "pypi" else: # Retrieving config information config_file = TomlFile(Path(CONFIG_DIR) / "config.toml") if not config_file.exists(): raise RuntimeError("Config file does not exist. " "Unable to get repository information") config = config_file.read() if ("repositories" not in config or repository_name not in config["repositories"]): raise RuntimeError( "Repository {} is not defined".format(repository_name)) url = config["repositories"][repository_name]["url"] if not (username and password): auth = get_http_basic_auth(self._poetry.auth_config, repository_name) if auth: username = auth[0] password = auth[1] # Requesting missing credentials if not username: username = self._io.ask("Username:"******"Password:") # TODO: handle certificates self._uploader.auth(username, password) return self._uploader.upload(url)
def install(self, package, update=False): args = ["install", "--no-deps"] if package.source_type == "legacy" and package.source_url: parsed = urlparse.urlparse(package.source_url) if parsed.scheme == "http": self._io.write_error( " <warning>Installing from unsecure host: {}</warning>".format( parsed.netloc ) ) args += ["--trusted-host", parsed.netloc] auth = get_http_basic_auth(package.source_reference) if auth: index_url = "{scheme}://{username}:{password}@{netloc}{path}".format( scheme=parsed.scheme, username=auth[0], password=auth[1], netloc=parsed.netloc, path=parsed.path, ) else: index_url = package.source_url args += ["--index-url", index_url] if update: args.append("-U") if package.hashes and not package.source_type: # Format as a requirements.txt # We need to create a requirements.txt file # for each package in order to check hashes. # This is far from optimal but we do not have any # other choice since this is the only way for pip # to verify hashes. req = self.create_temporary_requirement(package) args += ["-r", req] try: self.run(*args) finally: os.unlink(req) else: req = self.requirement(package) if not isinstance(req, list): args.append(req) else: args += req self.run(*args)
def install(self, package, update=False): args = ["install", "--no-deps"] if package.source_type == "legacy" and package.source_url: parsed = urlparse.urlparse(package.source_url) if parsed.scheme == "http": self._io.write_error( " <warning>Installing from unsecure host: {}</warning>". format(parsed.netloc)) args += ["--trusted-host", parsed.netloc] auth = get_http_basic_auth(package.source_reference) if auth: index_url = "{scheme}://{username}:{password}@{netloc}{path}".format( scheme=parsed.scheme, username=auth[0], password=auth[1], netloc=parsed.netloc, path=parsed.path, ) else: index_url = package.source_url args += ["--index-url", index_url] if update: args.append("-U") if package.hashes and not package.source_type: # Format as a requirements.txt # We need to create a requirements.txt file # for each package in order to check hashes. # This is far from optimal but we do not have any # other choice since this is the only way for pip # to verify hashes. req = self.create_temporary_requirement(package) args += ["-r", req] try: self.run(*args) finally: os.unlink(req) else: req = self.requirement(package) if not isinstance(req, list): args.append(req) else: args += req self.run(*args)
def publish(self, repository_name, username, password): if repository_name: self._io.writeln( "Publishing <info>{}</info> (<comment>{}</comment>) " "to <fg=cyan>{}</>".format( self._package.pretty_name, self._package.pretty_version, repository_name, ) ) else: self._io.writeln( "Publishing <info>{}</info> (<comment>{}</comment>) " "to <fg=cyan>PyPI</>".format( self._package.pretty_name, self._package.pretty_version ) ) if not repository_name: url = "https://upload.pypi.org/legacy/" repository_name = "pypi" else: # Retrieving config information config_file = TomlFile(Path(CONFIG_DIR) / "config.toml") if not config_file.exists(): raise RuntimeError( "Config file does not exist. " "Unable to get repository information" ) config = config_file.read() if ( "repositories" not in config or repository_name not in config["repositories"] ): raise RuntimeError( "Repository {} is not defined".format(repository_name) ) url = config["repositories"][repository_name]["url"] if not (username and password): auth = get_http_basic_auth(repository_name) if auth: username = auth[0] password = auth[1] # Requesting missing credentials if not username: username = self._io.ask("Username:"******"Password:") # TODO: handle certificates self._uploader.auth(username, password) return self._uploader.upload(url)
def publish(self, repository_name, username, password, cert=None, client_cert=None): if repository_name: self._io.write_line( "Publishing <c1>{}</c1> (<b>{}</b>) " "to <info>{}</info>".format( self._package.pretty_name, self._package.pretty_version, repository_name, ) ) else: self._io.write_line( "Publishing <c1>{}</c1> (<b>{}</b>) " "to <info>PyPI</info>".format( self._package.pretty_name, self._package.pretty_version ) ) if not repository_name: url = "https://upload.pypi.org/legacy/" repository_name = "pypi" else: # Retrieving config information repository = self._poetry.config.get( "repositories.{}".format(repository_name) ) if repository is None: raise RuntimeError( "Repository {} is not defined".format(repository_name) ) url = repository["url"] if not (username and password): # Check if we have a token first token = self._poetry.config.get("pypi-token.{}".format(repository_name)) if token: logger.debug("Found an API token for {}.".format(repository_name)) username = "******" password = token else: auth = get_http_basic_auth(self._poetry.config, repository_name) if auth: logger.debug( "Found authentication information for {}.".format( repository_name ) ) username = auth[0] password = auth[1] resolved_client_cert = client_cert or get_client_cert( self._poetry.config, repository_name ) # Requesting missing credentials but only if there is not a client cert defined. if not resolved_client_cert: if username is None: username = self._io.ask("Username:"******"Password:") self._uploader.auth(username, password) return self._uploader.upload( url, cert=cert or get_cert(self._poetry.config, repository_name), client_cert=resolved_client_cert, )
def test_get_http_basic_auth_missing(config): assert get_http_basic_auth(config, "foo") is None
def test_get_http_basic_auth_without_password(config): config.merge({"http-basic": {"foo": {"username": "******"}}}) assert get_http_basic_auth(config, "foo") == ("foo", None)
def test_get_http_basic_auth_without_password(config): config.add_property("http-basic.foo.username", "foo") assert get_http_basic_auth(config, "foo") == ("foo", None)
def test_get_http_basic_auth(config): config.add_property("http-basic.foo.username", "foo") config.add_property("http-basic.foo.password", "bar") assert get_http_basic_auth(config, "foo") == ("foo", "bar")