def parse(fileobj=None, location=None): """Reads the pkg(5) publisher json formatted data at 'location' or from the provided file-like object 'fileobj' and returns a list of tuples of the format (publisher object, pkg_names). pkg_names is a list of strings representing package names or FMRIs. If any pkg_names not specific to a publisher were provided, the last tuple returned will be of the format (None, pkg_names). 'fileobj' is an optional file-like object that must support a 'read' method for retrieving data. 'location' is an optional string value that should either start with a leading slash and be pathname of a file or a URI string. If it is a URI string, supported protocol schemes are 'file', 'ftp', 'http', and 'https'. 'fileobj' or 'location' must be provided.""" if location is None and fileobj is None: raise api_errors.InvalidResourceLocation(location) if location: if location.find("://") == -1: # Convert the file path to a URI. location = os.path.abspath(location) location = urlparse.urlunparse( ("file", "", urllib.pathname2url(location), "", "", "")) try: fileobj = urllib2.urlopen(location) except (EnvironmentError, ValueError, urllib2.HTTPError), e: raise api_errors.RetrievalError(e, location=location)
def parse(data=None, fileobj=None, location=None): """Reads the pkg(5) publisher JSON formatted data at 'location' or from the provided file-like object 'fileobj' and returns a list of tuples of the format (publisher object, pkg_names). pkg_names is a list of strings representing package names or FMRIs. If any pkg_names not specific to a publisher were provided, the last tuple returned will be of the format (None, pkg_names). 'data' is an optional string containing the p5i data. 'fileobj' is an optional file-like object that must support a 'read' method for retrieving data. 'location' is an optional string value that should either start with a leading slash and be pathname of a file or a URI string. If it is a URI string, supported protocol schemes are 'file', 'ftp', 'http', and 'https'. 'data' or 'fileobj' or 'location' must be provided.""" if data is None and location is None and fileobj is None: raise api_errors.InvalidResourceLocation(location) if location is not None: if location.find("://") == -1 and \ not location.startswith("file:/"): # Convert the file path to a URI. location = os.path.abspath(location) location = urlparse.urlunparse( ("file", "", urllib.pathname2url(location), "", "", "")) try: fileobj = urllib2.urlopen(location) except (EnvironmentError, ValueError, urllib2.HTTPError) as e: raise api_errors.RetrievalError(e, location=location) try: if data is not None: dump_struct = json.loads(data) else: dump_struct = json.load(fileobj) except (EnvironmentError, urllib2.HTTPError) as e: raise api_errors.RetrievalError(e) except ValueError as e: # Not a valid JSON file. raise api_errors.InvalidP5IFile(e) try: ver = int(dump_struct["version"]) except KeyError: raise api_errors.InvalidP5IFile(_("missing version")) except ValueError: raise api_errors.InvalidP5IFile(_("invalid version")) if ver > CURRENT_VERSION: raise api_errors.UnsupportedP5IFile() result = [] try: plist = dump_struct.get("publishers", []) for p in plist: alias = p.get("alias", None) prefix = p.get("name", None) if not prefix: prefix = "Unknown" pub = publisher.Publisher(prefix, alias=alias) pkglist = p.get("packages", []) result.append((pub, pkglist)) for r in p.get("repositories", []): rargs = {} for prop in ("collection_type", "description", "name", "refresh_seconds", "registration_uri"): val = r.get(prop, None) if val is None or val == "None": continue rargs[prop] = val for prop in ("legal_uris", "mirrors", "origins", "related_uris"): val = r.get(prop, []) if not isinstance(val, list): continue rargs[prop] = val repo = publisher.Repository(**rargs) pub.repository = repo pkglist = dump_struct.get("packages", []) if pkglist: result.append((None, pkglist)) except (api_errors.PublisherError, TypeError, ValueError) as e: raise api_errors.InvalidP5IFile(str(e)) return result
location = os.path.abspath(location) location = urlparse.urlunparse( ("file", "", urllib.pathname2url(location), "", "", "")) try: fileobj = urllib2.urlopen(location) except (EnvironmentError, ValueError, urllib2.HTTPError), e: raise api_errors.RetrievalError(e, location=location) try: if data: dump_struct = json.loads(data) else: dump_struct = json.load(fileobj) except (EnvironmentError, urllib2.HTTPError), e: raise api_errors.RetrievalError(e) except ValueError, e: # Not a valid JSON file. raise api_errors.InvalidP5IFile(e) try: ver = int(dump_struct["version"]) except KeyError: raise api_errors.InvalidP5IFile(_("missing version")) except ValueError: raise api_errors.InvalidP5IFile(_("invalid version")) if ver > CURRENT_VERSION: raise api_errors.UnsupportedP5IFile() result = []