diff --git a/bin/swh-loader-tar-lister b/bin/swh-loader-tar-lister index c464ef1..29cab0e 100755 --- a/bin/swh-loader-tar-lister +++ b/bin/swh-loader-tar-lister @@ -1,227 +1,230 @@ #!/usr/bin/env python3 # Copyright (C) 2015 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 argparse import os import sys from swh.core import config from swh.loader.tar import tarball, build +SWH_AUTHORITY = 1 +GNU_AUTHORITY = 2 + def list_archives_from_dir(path): """Given a path to a directory, walk such directory and yield tuple of tarpath, fname. Args: path: top level directory Returns: Generator of tuple tarpath, filename with tarpath a tarball. """ for dirpath, dirnames, filenames in os.walk(path): for fname in filenames: tarpath = os.path.join(dirpath, fname) if not os.path.exists(tarpath): continue if tarball.is_tarball(tarpath): yield tarpath, fname def list_archives_from_file(mirror_file): """Given a path to a file containing one tarball per line, yield a tuple of tarpath, fname. Args: mirror_file: path to the file containing list of tarpath. Returns: Generator of tuple tarpath, filename with tarpath a tarball. """ with open(mirror_file, 'r') as f: for tarpath in f.readlines(): tarpath = tarpath.strip() if not os.path.exists(tarpath): print('WARN: %s does not exist. Skipped.' % tarpath) continue if tarball.is_tarball(tarpath): yield tarpath, os.path.basename(tarpath) def list_archives_from(path): """From path, list tuple of tarpath, fname. Args: path: top directory to list archives from or custom file format. """ if os.path.isfile(path): yield from list_archives_from_file(path) elif os.path.isdir(path): yield from list_archives_from_dir(path) else: raise ValueError( 'Input incorrect, %s must be a file or a directory.' % path) def compute_message_from(app, conf, root_dir, tarpath, filename, dry_run=False): """Compute and post the message to worker for the archive tarpath. Args: app: instance of the celery app conf: dictionary holding static metadata root_dir: root directory tarball: the archive's representation dry_run: will compute but not send messages Returns: None Raises: ValueError when release number computation error arise. """ origin = build.compute_origin(conf['url_scheme'], conf['type'], root_dir, tarpath) revision = build.compute_revision() - occurrences = [build.gnu_occurrence(tarpath), - build.swh_occurrence(tarpath)] + occurrences = [build.occurrence_with_mtime(GNU_AUTHORITY, tarpath), + build.occurrence_with_ctime(SWH_AUTHORITY, tarpath)] release = build.compute_release(filename, tarpath) if not dry_run: app.tasks['swh.loader.tar.tasks.LoadTarRepository'].delay(tarpath, origin, revision, release, occurrences) def produce_archive_messages_from(app, conf, path, mirror_file=None, dry_run=False): """From path, produce archive tarball messages to celery. Will print error message when some computation arise on archive and continue. Args: app: instance of the celery app conf: dictionary holding static metadata path: top directory to list archives from. mirror_file: a filtering file of tarballs to load dry_run: will compute but not send messages Returns: None Raises: None """ LIMIT = conf['limit'] count = 0 path_source_tarballs = mirror_file if mirror_file else path for tarpath, fname in list_archives_from(path_source_tarballs): count += 1 try: compute_message_from(app, conf, path, tarpath, fname, dry_run) except ValueError: print('Problem with the following archive: %s' % tarpath) if LIMIT and count >= LIMIT: return count return count def load_config(conf_file): """Load the configuration from file. Args: conf_file: path to a configuration file with the following content: [main] # mirror's root directory holding tarballs to load into swh mirror_root_directory = /home/storage/space/mirrors/gnu.org/gnu/ # origin setup's possible scheme url url_scheme = rsync://ftp.gnu.org/gnu/ # origin type used for those tarballs type = ftp # For tryouts purposes (no limit if not specified) limit = 1 Returns: dictionary of data present in the configuration file. """ conf = config.read(conf_file, default_conf={'limit': ('int', None)}) url_scheme = conf['url_scheme'] mirror_dir = conf['mirror_root_directory'] # remove trailing / in configuration (to ease ulterior computation) if url_scheme[-1] == '/': conf.update({ 'url_scheme': url_scheme[0:-1] }) if mirror_dir[-1] == '/': conf.update({ 'mirror_root_directory': mirror_dir[0:-1] }) return conf def parse_args(): """Parse the configuration from the cli. """ cli = argparse.ArgumentParser( description='Tarball producer of local fs tarballs.') cli.add_argument('--dry-run', '-n', action='store_true', help='Dry run (print repo only)') cli.add_argument('--config', '-c', help='configuration file path') args = cli.parse_args() return args if __name__ == '__main__': args = parse_args() config_file = args.config if not config_file: print('Missing configuration file option.') sys.exit(1) # instantiate celery app with its configuration from swh.core.worker import app from swh.loader.tar import tasks # noqa conf = load_config(config_file) nb_tarballs = produce_archive_messages_from( app, conf, conf['mirror_root_directory'], conf.get('mirror_subset_archives'), args.dry_run) print('%s tarball(s) sent to worker.' % nb_tarballs) diff --git a/swh/loader/tar/build.py b/swh/loader/tar/build.py index a400fad..0948fc1 100755 --- a/swh/loader/tar/build.py +++ b/swh/loader/tar/build.py @@ -1,151 +1,151 @@ # Copyright (C) 2015 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 datetime from swh.loader.tar import utils # Static setup EPOCH = 0 UTC_OFFSET = '+0000' SWH_PERSON = 'Software Heritage' SWH_MAIL = 'robot@softwareheritage.org' REVISION_MESSAGE = 'synthetic revision message' RELEASE_MESSAGE = 'synthetic release message' REVISION_TYPE = 'tar' REVISION = { 'author_date': EPOCH, 'author_offset': UTC_OFFSET, 'author_name': SWH_PERSON, 'author_email': SWH_MAIL, 'committer_date': EPOCH, 'committer_offset': UTC_OFFSET, 'committer_name': SWH_PERSON, 'committer_email': SWH_MAIL, 'type': REVISION_TYPE, 'message': REVISION_MESSAGE, } -SWH_AUTHORITY = 1 -GNU_AUTHORITY = 2 def compute_origin(url_scheme, url_type, root_dirpath, tarpath): """Compute the origin. Args: - url_scheme: scheme to build the origin's url - url_type: origin's type - root_dirpath: the top level root directory path - tarpath: file's absolute path Returns: Dictionary origin with keys: - url: origin's url - type: origin's type """ relative_path = utils.commonname(root_dirpath, tarpath) return { 'url': ''.join([url_scheme, os.path.dirname(relative_path)]), 'type': url_type, } def _build_occurrence(tarpath, authority_id, validity_ts): """Build an occurrence from branch_name, authority_id and validity_ts. Args: - tarpath: file's path - authority_id: swh authority id (as per swh's storage values in organization table) - validity_ts: validity timestamp Returns: Occurrence dictionary - tarpath: file's path - authority: swh authority - validity: validity date (e.g. 2015-01-01 00:00:00+00) """ validity = '%s+00' % datetime.datetime.utcfromtimestamp(validity_ts) return { 'branch': os.path.basename(tarpath), 'authority': authority_id, 'validity': validity } -def swh_occurrence(tarpath): - """Compute the occurrence from the tarpath with swh authority. +def occurrence_with_ctime(authority, tarpath): + """Compute the occurrence using the tarpath's ctime. Args: + authority: the authority's uuid tarpath: file's path Returns: Occurrence dictionary (cf. _build_occurrence) """ validity_ts = os.lstat(tarpath).st_ctime - return _build_occurrence(tarpath, SWH_AUTHORITY, validity_ts) + return _build_occurrence(tarpath, authority, validity_ts) def _time_from_path(tarpath): """Compute the modification time from the tarpath. """ return os.lstat(tarpath).st_mtime -def gnu_occurrence(tarpath): - """Compute the occurrence from the tarpath with gnu authority. +def occurrence_with_mtime(authority, tarpath): + """Compute the occurrence from the tarpath using the tarpath's mtime. Args: + authority: the authority's uuid tarpath: file's path Return: Occurrence dictionary (cf. _build_occurrence) """ validity_ts = _time_from_path(tarpath) - return _build_occurrence(tarpath, GNU_AUTHORITY, validity_ts) + return _build_occurrence(tarpath, authority, validity_ts) def compute_revision(): return REVISION def compute_release(filename, tarpath): """Compute a release from a given tarpath, filename. If the tarpath does not contain a recognizable release number, the release can be skipped. Args: filename: file's name without path tarpath: file's absolute path Returns: None if the release number cannot be extracted from the filename. Otherwise a synthetic release is computed with the following keys: - name: the release computed from the filename - date: the modification timestamp as returned by a fstat call - offset: +0000 - author_name: '' - author_email: '' - comment: '' """ release_number = utils.release_number(filename) if release_number: return { 'name': release_number, 'date': _time_from_path(tarpath), 'offset': UTC_OFFSET, 'author_name': SWH_PERSON, 'author_email': SWH_MAIL, 'comment': RELEASE_MESSAGE, } return None