def upload(self, pkg, filename): if not pkg.version: raise InvalidParameter("Version must be specified when uploading a package") current = self.search(pkg.name + "==" + pkg.version) if current and current.type == pkg.type and not self.__overwrite: raise InvalidState("upload would result in overwrite but overwrite mode is not enabled") with open(filename, "rb") as f: self.__repo.upload_file(Package.repo_name(pkg, filename), f) self.refresh_cache()
def repo_name(pkg, filename): if not pkg.version: raise InvalidState( "cannot formulate package repository-name for a package without version" ) return "{name}/{version}/{filename}".format( name=pkg.name, version=pkg.version, filename=os.path.split(filename)[1])
def __extract_wheel(self, zpfile): class InfoCmd(object): def __init__(self): self.metadata = {} def __call__(self, target): m = json.loads(target.read()) self.metadata["name"] = m["name"] self.metadata["version"] = m["version"] self.metadata["requirements"] = set([]) for reqs in m["run_requires"]: self.metadata["requirements"].update(reqs["requires"]) cmd = InfoCmd() self.__seek_and_apply(zpfile, "metadata.json", cmd) if not cmd.metadata: raise InvalidState("Could not find metadata.json") return cmd.metadata
def __extract_source(self, zpfile): class InfoCmd(object): def __init__(self): self.metadata = {} def __call__(self, target): m = {} for line in target.readlines(): k, v = line.split(":") m[k.upper().strip()] = v.strip() self.metadata["name"] = m["name".upper()] self.metadata["version"] = m["version".upper()] cmd = InfoCmd() self.__seek_and_apply(zpfile, "PKG-INFO", cmd) if not cmd.metadata: raise InvalidState("Could not find PKG-INFO") return { "name": cmd.metadata["name"], "version": cmd.metadata["version"], "requirements": self.__read_requirements(zpfile) }
def __extract_wheel(self, zpfile): class InfoCmd(object): def __init__(self): self.metadata = {} def __call__(self, target): def __parse_package_name(package_name): return package_name.replace(" ", "").replace("(", "").replace( ")", "") m = {} requirements_key_name = "Requires-Dist" m[requirements_key_name.upper()] = [] for line in target.readlines(): if ":" not in line: break k, v = line.replace(":", '@@@', 1).split('@@@') if k == requirements_key_name: m[k.upper().strip()].append(v.strip()) else: m[k.upper().strip()] = v.strip() print(m) self.metadata["name"] = m["name".upper()] self.metadata["version"] = m["version".upper()] self.metadata["requirements"] = [ __parse_package_name(r) for r in m[requirements_key_name.upper()] ] cmd = InfoCmd() self.__seek_and_apply(zpfile, "METADATA", cmd) if not cmd.metadata: raise InvalidState("Could not find MATADATA") return cmd.metadata