def new_from_file(cls, path, remote_url=False): if not path or not os.path.isfile(path): raise UnknownManifestError("Manifest file does not exist %s" % path) type_from_uri = ManifestFileType.from_uri(path) if not type_from_uri: raise UnknownManifestError("Unknown manifest file type %s" % path) return ManifestParserFactory.new(cls.read_manifest_contents(path), type_from_uri, remote_url)
def new( # pylint: disable=redefined-builtin contents, type, remote_url=None, package_dir=None): for _, cls in globals().items(): if (inspect.isclass(cls) and issubclass(cls, BaseManifestParser) and cls != BaseManifestParser and cls.manifest_type == type): return cls(contents, remote_url, package_dir) raise UnknownManifestError("Unknown manifest file type %s" % type)
def new_from_archive(path): assert path.endswith("tar.gz") with tarfile.open(path, mode="r:gz") as tf: for t in sorted(ManifestFileType.items().values()): try: return ManifestParserFactory.new( tf.extractfile(t).read().decode(), t) except KeyError: pass raise UnknownManifestError("Unknown manifest file type in %s archive" % path)
def new_from_dir(cls, path, remote_url=None): assert os.path.isdir(path), "Invalid directory %s" % path type_from_uri = ManifestFileType.from_uri( remote_url) if remote_url else None if type_from_uri and os.path.isfile(os.path.join(path, type_from_uri)): return ManifestParserFactory.new( cls.read_manifest_contents(os.path.join(path, type_from_uri)), type_from_uri, remote_url=remote_url, package_dir=path, ) type_from_dir = ManifestFileType.from_dir(path) if not type_from_dir: raise UnknownManifestError( "Unknown manifest file type in %s directory" % path) return ManifestParserFactory.new( cls.read_manifest_contents(os.path.join(path, type_from_dir)), type_from_dir, remote_url=remote_url, package_dir=path, )