swh.loader.package.npm.loader module

class swh.loader.package.npm.loader.NpmPackageInfo(url: str, filename: Optional[str], raw_info: Dict[str, Any], date: Optional[str], shasum: str, version: str, *, revision_extrinsic_metadata: List[swh.loader.package.loader.RawExtrinsicMetadataCore] = [])[source]

Bases: swh.loader.package.loader.BasePackageInfo

shasum

sha1 checksum

classmethod from_metadata(project_metadata: Dict[str, Any], version: str)swh.loader.package.npm.loader.NpmPackageInfo[source]
class swh.loader.package.npm.loader.NpmLoader(*args, **kwds)[source]

Bases: swh.loader.package.loader.PackageLoader

Load npm origin’s artifact releases into swh archive.

visit_type = 'npm'
info() → Dict[source]

Return the project metadata information (fetched from npm registry)

get_versions() → Sequence[str][source]

Return the list of all published package versions.

Returns

Sequence of published versions

get_default_version() → str[source]

Retrieve the latest release version if any.

Returns

Latest version

get_metadata_authority()[source]

For package loaders that get extrinsic metadata, returns the authority the metadata are coming from.

get_extrinsic_snapshot_metadata()[source]

Returns metadata items, used by build_extrinsic_snapshot_metadata.

get_package_info(version: str) → Iterator[Tuple[str, swh.loader.package.npm.loader.NpmPackageInfo]][source]
Given a release version of a package, retrieve the associated

package information for such version.

Parameters

version – Package version

Returns

(branch name, package metadata)

resolve_revision_from(known_artifacts: Dict, p_info: swh.loader.package.npm.loader.NpmPackageInfo) → Optional[bytes][source]

Resolve the revision from a snapshot and an artifact metadata dict.

If the artifact has already been downloaded, this will return the existing revision targeting that uncompressed artifact directory. Otherwise, this returns None.

Parameters
  • snapshot – Snapshot

  • p_info – Package information

Returns

None or revision identifier

build_revision(p_info: swh.loader.package.npm.loader.NpmPackageInfo, uncompressed_path: str, directory: bytes) → Optional[swh.model.model.Revision][source]

Build the revision from the archive metadata (extrinsic artifact metadata) and the intrinsic metadata.

Parameters
  • p_info – Package information

  • uncompressed_path – Artifact uncompressed path on disk

Returns

Revision object

swh.loader.package.npm.loader.artifact_to_revision_id(known_artifacts: Dict, p_info: swh.loader.package.npm.loader.NpmPackageInfo) → Optional[bytes][source]

Given metadata artifact, solves the associated revision id.

The following code allows to deal with 2 metadata formats:

  • old format sample:

    {
        'package_source': {
            'sha1': '05181c12cd8c22035dd31155656826b85745da37',
        }
    }
    
  • new format sample:

    {
        'original_artifact': [{
            'checksums': {
                'sha256': '6975816f2c5ad4046acc676ba112f2fff945b01522d63948531f11f11e0892ec', # noqa
                ...
            },
        }],
        ...
    }
    
swh.loader.package.npm.loader.extract_npm_package_author(package_json: Dict[str, Any])swh.model.model.Person[source]

Extract package author from a package.json file content and return it in swh format.

Parameters

package_json – Dict holding the content of parsed package.json file

Returns

Person

swh.loader.package.npm.loader.load_json(json_bytes)[source]

Try to load JSON from bytes and return a dictionary.

First try to decode from utf-8. If the decoding failed, try to detect the encoding and decode again with replace error handling.

If JSON is malformed, an empty dictionary will be returned.

Parameters

json_bytes (bytes) – binary content of a JSON file

Returns

JSON data loaded in a dictionary

Return type

dict

swh.loader.package.npm.loader.extract_intrinsic_metadata(dir_path: str) → Dict[source]
Given an uncompressed path holding the pkginfo file, returns a

pkginfo parsed structure as a dict.

The release artifact contains at their root one folder. For example: $ tar tvf zprint-0.0.6.tar.gz drwxr-xr-x root/root 0 2018-08-22 11:01 zprint-0.0.6/ …

Parameters

dir_path (str) – Path to the uncompressed directory representing a release artifact from npm.

Returns

the pkginfo parsed structure as a dict if any or None if none was present.