Beispiel #1
0
def metadata_command():
    """Loads package and prints its metadata."""
    with open_package(read_only=True) as package:
        metadata = package.to_metadata()
        print(json.dumps(metadata, indent=4, sort_keys=True))
    try:
        validate_metadata(metadata)
        print('Valid metadata.')
    except ValidationError as err:
        error(1, err)
Beispiel #2
0
def dump_package_archive(package, output=None, force=False):
    """Saves package as an archive. Returns genereted archive filename.

    Generated archive is a gz compressed tar file with current package
    metadata and all objects files.

    All objects are renamed to its hash and moved to the archive
    root. Objects are included without duplication and links are
    resolved.
    """
    # Checks minimum package requirements
    if package.version is None:
        raise ValueError('Cannot generate archive without package version.')
    if package.product is None:
        raise ValueError('Cannot generate archive without product UID.')
    if not package.objects.all():
        raise ValueError('Cannot generate archive without objects.')
    # Checks metadata complience
    metadata = package.to_metadata()
    try:
        pkgschema.validate_metadata(metadata)
    except pkgschema.ValidationError:
        raise ValueError('Cannot generate archive with invalid metadata.')

    # Checks archive output
    output = _generate_archive_name(package, output)
    if os.path.exists(output) and not force:
        raise FileExistsError('Archive "{}" already exists.'.format(output))

    # Writes archive
    cache = set()
    signature = sign_dict(metadata, config.get_private_key_path())
    metadata = json.dumps(metadata, sort_keys=True)
    with zipfile.ZipFile(output, mode='w') as archive:
        if signature is None:
            archive.writestr('signature', "")
        else:
            archive.writestr('signature', signature)
        archive.writestr('metadata', metadata)
        for obj in package.objects.all():
            sha256sum = obj['sha256sum']
            if sha256sum in cache:
                continue
            cache.add(sha256sum)
            archive.write(os.path.realpath(obj.filename), sha256sum)
    return output
Beispiel #3
0
def upload_metadata(metadata):
    try:
        validate_metadata(metadata)
    except ValidationError:
        raise UpdateHubError('You have an invalid package metadata.')
    url = get_server_url('/packages')
    signature = sign_dict(metadata, config.get_private_key_path())
    payload = json.dumps(metadata, sort_keys=True)
    headers = {'UH-SIGNATURE': signature}
    try:
        response = http.post(
            url, payload=payload, json=True, headers=headers).json()
        return response['uid']
    except http.HTTPError as error:
        raise UpdateHubError('Could not upload metadata: {}'.format(error))
    except (ValueError, KeyError):
        raise UpdateHubError('Could not upload metadata: unknown error.')
Beispiel #4
0
 def test_validate_raises_error_when_filenames_are_invalid(self):
     metadata = get_fixture_metadata()
     metadata['objects'].append(metadata['objects'][0][::-1])
     metadata['objects'][0][0]['filename'] = 'another'
     with self.assertRaises(pkgschema.ValidationError):
         pkgschema.validate_metadata(metadata)
Beispiel #5
0
 def test_validate_raises_error_when_metadata_is_invalid(self):
     with self.assertRaises(pkgschema.ValidationError):
         pkgschema.validate_metadata({})
Beispiel #6
0
 def test_validate_returns_None_when_valid(self):
     metadata = get_fixture_metadata()
     self.assertIsNone(pkgschema.validate_metadata(metadata))