diff --git a/swh/loader/pypi/client.py b/swh/loader/pypi/client.py index f0ee0c0..d1e4a48 100644 --- a/swh/loader/pypi/client.py +++ b/swh/loader/pypi/client.py @@ -1,237 +1,262 @@ # Copyright (C) 2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import arrow import hashlib import logging import os import requests +from pkginfo import UnpackedSDist + from swh.core import tarball from swh.model import hashutil try: from swh.loader.pypi._version import __version__ except ImportError: __version__ = 'devel' def convert_to_hex(d): """Convert a flat dictionary with bytes in values to the same dictionary with hex as values. Args: dict: flat dictionary with sha bytes in their values. Returns: Mirror dictionary with values as string hex. """ if not d: return d checksums = {} for key, h in d.items(): if isinstance(h, bytes): checksums[key] = hashutil.hash_to_hex(h) else: checksums[key] = h return checksums +def _to_dict(pkginfo): + """Given a pkginfo file, convert it to a dict. + + """ + m = {} + for k in pkginfo: + m[k] = getattr(pkginfo, k) + return m + + +def _project_pkginfo(dir_path): + """Given an uncompressed path holding the pkginfo path, returns a pkginfo. + + """ + project_dirname = os.listdir(dir_path)[0] + pkginfo_path = os.path.join(dir_path, project_dirname, 'PKG-INFO') + if not os.path.exists(pkginfo_path): + return None + pkginfo = UnpackedSDist(pkginfo_path) + return _to_dict(pkginfo) + + class PyPiClient: """PyPi client in charge of discussing with the pypi server. """ def __init__(self, temp_directory=None, cache=False, cache_dir=None): self.version = __version__ self.temp_directory = temp_directory self.do_cache = cache if self.do_cache: self.cache_dir = cache_dir self.cache_raw_dir = os.path.join(cache_dir, 'archives') os.makedirs(self.cache_raw_dir, exist_ok=True) self.session = requests.session() self.params = { 'headers': { 'User-Agent': 'Software Heritage PyPi Loader (%s)' % ( __version__ ) } } def _save_response(self, response): """Log the response from a server request to a cache dir. Args: response (Response): full server response cache_dir (str): system path for cache dir Returns: nothing """ import gzip from json import dumps datepath = arrow.utcnow().isoformat() fname = os.path.join(self.cache_dir, datepath + '.gz') with gzip.open(fname, 'w') as f: f.write(bytes( dumps(response.json()), 'utf-8' )) def _save_raw(self, filepath): """In cache mode, backup the filepath to self.cache_raw_dir Args: filepath (str): Path of the file to save """ _filename = os.path.basename(filepath) _archive = os.path.join(self.cache_raw_dir, _filename) with open(filepath, 'rb') as f: with open(_archive, 'wb') as g: for chunk in f: g.write(chunk) def _get_raw(self, filepath): _filename = os.path.basename(filepath) _archive = os.path.join(self.cache_raw_dir, _filename) if not os.path.exists(_archive): return None with open(_archive, 'rb') as f: with open(filepath, 'wb') as g: for chunk in f: g.write(chunk) return filepath def _get(self, url): """Get query to the url. Args: url (str): Url Raises: ValueError in case of failing to query Returns: Response as dict if ok """ response = self.session.get(url, **self.params) if response.status_code != 200: raise ValueError("Fail to query '%s'. Reason: %s" % ( url, response.status_code)) if self.do_cache: self._save_response(response) return response.json() def info(self, project_url): """Given a metadata project url, retrieve the raw json response Args: project_url (str): Project's pypi to retrieve information Returns: Main project information as dict. """ return self._get(project_url) def release(self, project, release): """Given a project and a release name, retrieve the raw information for said project's release. Args: project (str): Project's name release (dict): Release information Returns: Release information as dict """ release_url = 'https://pypi.org/pypi/%s/%s/json' % (project, release) return self._get(release_url) def fetch_release_artifact(self, project, release): """Fetch for a given release project the associated artifact. This: - fetches the artifact - checks the size, hashes match - uncompress the artifact locally - computes the swh hashes - returns the associated information for the artifact Args: project (str): Project's name release (dict): Release information Returns: tuple (release, archive_path, uncompress_archive_path) where: release is the release information (dict) archive_path is the fetch archive uncompressed_archive_path is the uncompressed archive """ version = release['name'] logging.debug('Release version: %s' % version) path = os.path.join(self.temp_directory, project, version) os.makedirs(path, exist_ok=True) filepath = os.path.join(path, release['filename']) logging.debug('Release local path: %s' % filepath) _filepath = None if self.do_cache: _filepath = self._get_raw(filepath) if not _filepath: # no cache hit, we fetch from pypi url = release['url'] r = self.session.get(url, **self.params) if r.status_code != 200: raise ValueError("Fail to query '%s'. Reason: %s" % ( url, r.status_code)) _len = len(r.content) if _len != release['size']: raise ValueError('Error when checking size: %s != %s' % ( release['size'], _len)) # checking digest and writing h = hashlib.sha256() with open(filepath, 'wb') as f: for chunk in r.iter_content(): h.update(chunk) f.write(chunk) actual_digest = h.hexdigest() if actual_digest != release['sha256']: raise ValueError( 'Error when checking the hash checksum: %s != %s' % ( release['sha256'], actual_digest)) if self.do_cache: self._save_raw(filepath) release['filepath'] = filepath uncompress_path = os.path.join(path, 'uncompress') os.makedirs(uncompress_path, exist_ok=True) nature = tarball.uncompress(filepath, uncompress_path) artifact = convert_to_hex(hashutil.hash_path(filepath)) artifact['archive_type'] = nature for key, value in artifact.items(): release[key] = value - return release, filepath, uncompress_path + pkginfo = _project_pkginfo(uncompress_path) + return release, filepath, uncompress_path, pkginfo diff --git a/swh/loader/pypi/loader.py b/swh/loader/pypi/loader.py index 94b00ac..a25cd87 100644 --- a/swh/loader/pypi/loader.py +++ b/swh/loader/pypi/loader.py @@ -1,178 +1,185 @@ # Copyright (C) 2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import arrow +import logging import os import shutil from tempfile import mkdtemp from swh.loader.core.utils import clean_dangling_folders from swh.loader.core.loader import SWHLoader from swh.model.from_disk import Directory from swh.model.identifiers import ( revision_identifier, snapshot_identifier, identifier_to_bytes, normalize_timestamp ) from .client import PyPiClient from .model import PyPiProject TEMPORARY_DIR_PREFIX_PATTERN = 'swh.loader.pypi.' DEBUG_MODE = '** DEBUG MODE **' class PyPiLoader(SWHLoader): CONFIG_BASE_FILENAME = 'loader/pypi' ADDITIONAL_CONFIG = { 'temp_directory': ('str', '/tmp/swh.loader.pypi/'), 'cache': ('bool', False), 'cache_dir': ('str', ''), 'debug': ('bool', False), # NOT FOR PRODUCTION } def __init__(self): super().__init__(logging_class='swh.loader.pypi.PyPiLoader') self.origin_id = None temp_directory = self.config['temp_directory'] os.makedirs(temp_directory, exist_ok=True) self.temp_directory = mkdtemp( suffix='-%s' % os.getpid(), prefix=TEMPORARY_DIR_PREFIX_PATTERN, dir=temp_directory) self.pypi_client = PyPiClient( temp_directory=self.temp_directory, cache=self.config['cache'], cache_dir=self.config['cache_dir']) self.debug = self.config['debug'] def pre_cleanup(self): """(override) To prevent disk explosion if some other workers exploded in mid-air (OOM killed), we try and clean up dangling files. """ if self.debug: self.log.warn('%s Will not pre-clean up temp dir %s' % ( DEBUG_MODE, self.temp_directory )) return clean_dangling_folders(self.config['temp_directory'], pattern_check=TEMPORARY_DIR_PREFIX_PATTERN, log=self.log) def cleanup(self): """(override) Clean up temporary disk use """ if self.debug: self.log.warn('%s Will not clean up temp dir %s' % ( DEBUG_MODE, self.temp_directory )) return if os.path.exists(self.temp_directory): self.log.debug('Clean up %s' % self.temp_directory) shutil.rmtree(self.temp_directory) def prepare_origin_visit(self, project_name, origin_url, origin_metadata_url=None): """(override) Prepare the origin visit information Args: project_name (str): Project's simple name origin_url (str): Project's main url origin_metadata_url (str): Project's metadata url """ self.origin = { 'url': origin_url, 'type': 'pypi', } self.visit_date = None # loader core will populate it def prepare(self, project_name, origin_url, origin_metadata_url=None): """(override) Keep reference to the origin url (project) and the project metadata url Args: project_name (str): Project's simple name origin_url (str): Project's main url origin_metadata_url (str): Project's metadata url """ self.project_name = project_name self.origin_url = origin_url self.origin_metadata_url = origin_metadata_url self.project = PyPiProject(self.pypi_client, self.project_name, self.origin_metadata_url) def fetch_data(self): """(override) Fetch and collect swh objects. """ self._snapshot = { 'branches': {} } self._contents = [] self._directories = [] self._revisions = [] self._releases = [] - for info, author, release, dir_path in self.project.releases(): + for _, author, release, dir_path, pkginfo in self.project.releases(): + if not pkginfo: + msg = 'No PKG-INFO detected for %s, %s, skipping' % ( + self.project, release) + logging.error(msg) + continue + dir_path = dir_path.encode('utf-8') directory = Directory.from_disk(path=dir_path, data=True) _objects = directory.collect() self._contents.extend(_objects['content'].values()) self._directories.extend(_objects['directory'].values()) date = normalize_timestamp( int(arrow.get(release['date']).timestamp)) name = release['name'].encode('utf-8') message = release['message'].encode('utf-8') if message: message = b'%s: %s' % (name, message) else: message = name _revision = { 'synthetic': True, 'metadata': { - 'original_artifact': [release], - 'project': info, + 'original_artifact': release, + 'project': pkginfo, }, 'author': author, 'date': date, 'committer': author, 'committer_date': date, 'message': message, 'directory': directory.hash, 'parents': [], 'type': 'tar', } _revision['id'] = identifier_to_bytes( revision_identifier(_revision)) self._revisions.append(_revision) branch_name = release['filename'].encode('utf-8') self._snapshot['branches'][branch_name] = { 'target': _revision['id'], 'target_type': 'revision', } self._snapshot['id'] = identifier_to_bytes( snapshot_identifier(self._snapshot)) def store_data(self): """(override) This sends collected objects to storage. """ self.maybe_load_contents(self._contents) self.maybe_load_directories(self._directories) self.maybe_load_revisions(self._revisions) self.maybe_load_releases(self._releases) self.maybe_load_snapshot(self._snapshot) diff --git a/swh/loader/pypi/model.py b/swh/loader/pypi/model.py index 136ee33..3094731 100644 --- a/swh/loader/pypi/model.py +++ b/swh/loader/pypi/model.py @@ -1,204 +1,204 @@ # Copyright (C) 2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os import logging import shutil def info(data): """Given a dict of data, returns a project subset. """ info = data['info'] default = { 'home_page': info['home_page'], 'description': info['description'], 'summary': info['summary'], 'license': info['license'], 'package_url': info['package_url'], 'project_url': info['project_url'], 'upstream': None, } project_urls = info.get('project_urls') if project_urls: homepage = project_urls.get('Homepage') if homepage: default['upstream'] = homepage return default def author(data): """Given a dict of data, returns an author subset. """ name = data['info']['author'] email = data['info']['author_email'] if email: fullname = '%s <%s>' % (name, email) else: fullname = name if not fullname: return {'fullname': b'', 'name': None, 'email': None} return { 'fullname': fullname.encode('utf-8'), 'name': name.encode('utf-8'), 'email': email.encode('utf-8'), } class PyPiProject: """PyPi project representation This permits to extract information for the: - project, either the latest information (from the last revision) - either the information for a given release - Symmetrically for the release author information This also fetches and uncompress the associated release artifacts. """ def __init__(self, client, project, project_metadata_url, data=None): self.client = client self.project = project self.project_metadata_url = project_metadata_url if data: self.data = data else: self.data = client.info(project_metadata_url) self.last_version = self.data['info']['version'] self.cache = { self.last_version: self.data } def _data(self, release_name=None): """Fetch data per release and cache it. Returns the cache retrieved data if already fetched. """ if release_name: data = self.cache.get(release_name) if not data: data = self.client.release(self.project, release_name) self.cache[release_name] = data else: data = self.data return data def info(self, release_name=None): """Compute release information for release provided or the latest one. """ return info(self._data(release_name)) def author(self, release_name=None): """Compute author for the provided release if provided (use the latest release otherwise). """ return author(self._data(release_name)) def _filter_releases(self, version, release): """Filter release to keep only sdist (source distribution) There can be multiple 'package_type' (sdist, bdist_egg, bdist_wheel, bdist_rpm, bdist_msi, bdist_wininst, ...), we are only interested in source distribution (sdist), others bdist* are binary Args: version (str): Release name or version release (dict): Full release object """ if not release: return [] if not isinstance(release, list): release = [release] # Filter only on 'sdist' package type return [rel for rel in release if rel['packagetype'] == 'sdist'] def _cleanup_release_artifacts(self, archive_path, directory_path): """Clean intermediary files which no longer needs to be present. """ if directory_path and os.path.exists(directory_path): logging.debug('Clean up uncompressed archive path %s' % ( directory_path, )) shutil.rmtree(directory_path) if archive_path and os.path.exists(archive_path): logging.debug('Clean up archive %s' % archive_path) os.unlink(archive_path) def _fetch_and_uncompress_releases(self, version, releases): """Fetch an uncompress sdist releases Args: version (str): Release name or version releases ([dict]): List of source distribution release artifacts Yields: tuple (release, filepath, uncompressed_path) """ for release in releases: # flatten the metadata to ease reading _flattenned_release = { 'name': version, 'message': release.get('comment_text', ''), 'sha256': release['digests']['sha256'], 'size': release['size'], 'filename': release['filename'], 'url': release['url'], 'date': release['upload_time'], } # fetch and write locally archives yield self.client.fetch_release_artifact( self.project, _flattenned_release) def releases(self): """Fetch metadata and data per release. This: - downloads and uncompresses the release artifacts. - yields the (version, release) - Clean up the intermediary fetched artifact files Yields: tuple (version, release_info, release, uncompressed_path) where: - release_info (dict): release's associated version info - author (dict): Author information for the release - release (dict): release metadata - uncompressed_path (str): Path to uncompressed artifact """ # The compute information per release releases_dict = self.data['releases'] for version in releases_dict: releases = releases_dict[version] if version == self.last_version: # avoid an extra query release_info = self.info() else: release_info = self.info(release_name=version) author = self.author(version) releases = self._filter_releases(version, releases) if not releases: logging.warn('%s %s: No source artifact found, skipping' % ( self.project, version)) continue _releases = self._fetch_and_uncompress_releases(version, releases) - for _release, _archive, _dir_path in _releases: - yield release_info, author, _release, _dir_path + for _release, _archive, _dir_path, _pkginfo in _releases: + yield release_info, author, _release, _dir_path, _pkginfo self._cleanup_release_artifacts(_archive, _dir_path)