API Reference¶
license_tools.tools.cargo_tools¶
Tools related to Cargo/Rust.
- class license_tools.tools.cargo_tools.PackageVersion(name: str, version: str, checksum: str)¶
Container for holding a package version.
- license_tools.tools.cargo_tools.analyze_metadata(path: Path | str) dict[str, str | list[str]] | None¶
Analyze the Rust package metadata for the given directory.
- Parameters:
path – The directory/file to analyze. Should either be a directory or Cargo.toml file.
- Returns:
The package metadata.
- license_tools.tools.cargo_tools.check_metadata(path: Path | str) str¶
Render the relevant details for the given package.
- Parameters:
path – The package path.
- Returns:
The rendered dictionary-like representation of the relevant fields.
- license_tools.tools.cargo_tools.download_from_lock_file(lock_path: Path | str, target_directory: Path | str) None¶
Download the packages from the given lock file.
- Parameters:
lock_path – The lock file to read.
target_directory – The directory to write the packages to.
- license_tools.tools.cargo_tools.get_package_versions(lock_path: Path | str) Generator[PackageVersion]¶
Get the packages from the given lock file.
- Parameters:
lock_path – The lock file to read.
- Returns:
The packages retrieved from lock file.
license_tools.tools.font_tools¶
Tools related to fonts.
- license_tools.tools.font_tools.analyze_font(path: Path) dict[str, None | dict[str, str | int | datetime]] | None¶
Analyze the given font and provide a human-readable mapping for the head and name sections.
- Parameters:
path – The font path.
- Returns:
None if this is no known font type, the parsed results grouped by section name otherwise.
- license_tools.tools.font_tools.check_font(path: Path) str | None¶
Render the relevant details for the given font.
- Parameters:
path – The font path.
- Returns:
None if no results could be determined, otherwise the rendered dictionary-like representation of the names section which usually holds the copyright data.
- license_tools.tools.font_tools.convert_font_direction_hint(value: int) str¶
Convert the given font direction hint to a human-readable representation.
- Parameters:
value – The hint value.
- Returns:
The human-readable representation.
- license_tools.tools.font_tools.convert_head_flags(value: int) str¶
Convert the given head flags value to a human-readable string.
- Parameters:
value – The flags value.
- Returns:
The corresponding human-readable interpretation.
- license_tools.tools.font_tools.convert_loc_format(value: int) str¶
Convert the given loc value to a human-readable representation.
- Parameters:
value – The loc value.
- Returns:
The human-readable representation.
- license_tools.tools.font_tools.convert_mac_style(value: int) str¶
Convert the mac style to a human-readable representation.
- Parameters:
value – The style flags.
- Returns:
The corresponding string representation.
- license_tools.tools.font_tools.convert_timestamp_to_datetime(value: int) datetime¶
Convert the given font timestamp to a datetime object.
- Parameters:
value – The font timestamp since the font epoch in 1904.
- Returns:
The regular datetime object.
- license_tools.tools.font_tools.convert_timestamp_to_string(value: int) str¶
Convert the given font timestamp to a string.
- Parameters:
value – The font timestamp since the font epoch in 1904.
- Returns:
The corresponding string representation.
- license_tools.tools.font_tools.dump_to_ttx(source_path: Path, target_path: Path) Path¶
Utility function to convert the given font to its XML representation.
- Parameters:
source_path – The font to read.
target_path – The path to write to.
- Returns:
The target path. The same as the target_path if its suffix is .ttx, otherwise the target_path with the suffix added.
- license_tools.tools.font_tools.handle_ttfont_head(head: table__h_e_a_d) OrderedDict[str, str | int | datetime]¶
Handle the head section of a TTFont instance.
- Parameters:
head – The section to handle.
- Returns:
The parsed/human-readable mapping.
- license_tools.tools.font_tools.handle_ttfont_names(names: table__n_a_m_e) OrderedDict[str, str | int | datetime]¶
Handle the names section of a TTFont instance.
- Parameters:
names – The names to handle.
- Returns:
The parsed/human-readable mapping.
license_tools.tools.image_tools¶
Tools related to images.
license_tools.tools.linking_tools¶
Tools related to binary linking.
Check which other shared objects a shared object links to.
- Parameters:
path – The file path to analyze.
- Returns:
The analysis results if the path points to a shared object, None otherwise.
license_tools.tools.pip_tools¶
Tools related to pip.
- license_tools.tools.pip_tools.analyze_metadata(path: Path | str) PackageInfo¶
Analyze the Python package metadata for the given directory.
- Parameters:
path – The directory to analyze. Should either be a .dist-info one or the parent.
- Returns:
The package metadata.
- license_tools.tools.pip_tools.check_metadata(path: Path | str) str¶
Render the relevant details for the given package.
- Parameters:
path – The package path.
- Returns:
The rendered dictionary-like representation of the relevant fields.
- license_tools.tools.pip_tools.download_package(package_definition: str, download_directory: Path | str, index_url: str | None = None, prefer_sdist: bool = False) None¶
Download the given package and save it to the given directory.
- Parameters:
package_definition – The Python package definition to download.
download_directory – The directory to download the package to.
index_url – The PyPI index URL to use. Uses the default one from the .pypirc file if unset.
prefer_sdist – Download the source distribution instead of the wheel.
license_tools.tools.rpm_tools¶
Tools related to RPM files.
- class license_tools.tools.rpm_tools.DependencyFlags(*values)¶
Flags used to indicate how to handle dependency relationships.
- class license_tools.tools.rpm_tools.FileColor(*values)¶
Enumeration of file “color”/types.
- class license_tools.tools.rpm_tools.FileDigestAlgorithm(*values)¶
Enumeration of allowed file digest algorithms.
- class license_tools.tools.rpm_tools.FileFlags(*values)¶
Flags used to indicate the file types.
- class license_tools.tools.rpm_tools.FileModes(*values)¶
Enumeration of known file modes.
- class license_tools.tools.rpm_tools.VerifyFlags(*values)¶
Flags used to indicate how to verify the file.
- license_tools.tools.rpm_tools.check_rpm_headers(path: Path) str | None¶
Render the relevant header details for the given RPM.
- Parameters:
path – The RPM path.
- Returns:
None if no results could be determined, otherwise the rendered dictionary-like representation of the header section which usually holds the copyright data.
- license_tools.tools.rpm_tools.extract(archive_path: Path, target_path: Path) None¶
Extract the given RPM file.
- Parameters:
archive_path – The RPM file to unpack.
target_path – The directory to unpack to.
license_tools.tools.scancode_tools¶
Convenience interface for the ScanCode toolkit project using some predefined configuration and returning dataclass instances instead of dictionaries.
- class license_tools.tools.scancode_tools.Author(author: str, start_line: int, end_line: int)¶
Matching information about an author.
- class license_tools.tools.scancode_tools.Copyright(copyright: str, start_line: int, end_line: int)¶
Matching information about copyrights.
- class license_tools.tools.scancode_tools.Copyrights(copyrights: list[Copyright] = <factory>, holders: list[Holder] = <factory>, authors: list[Author] = <factory>)¶
Copyright-specific results.
- class license_tools.tools.scancode_tools.Email(email: str, start_line: int, end_line: int)¶
Matching information about an e-mail.
- class license_tools.tools.scancode_tools.Emails(emails: list[Email] = <factory>)¶
E-mail-specific results.
- class license_tools.tools.scancode_tools.FileInfo(date: date, size: int, sha1: str | None, md5: str | None, sha256: str | None, mime_type: str, file_type: str, programming_language: str | None, is_binary: bool, is_text: bool, is_archive: bool, is_media: bool, is_source: bool, is_script: bool, sha1_git: str | None = None)¶
File-specific results.
- sha1_git: str | None = None¶
The hash according to the Git blob SHA1 convention (https://git-scm.com/book/en/v2/Git-Internals-Git-Objects#_object_storage).
- class license_tools.tools.scancode_tools.FileResults(path: Path, short_path: str, retrieve_copyrights: bool = False, retrieve_emails: bool = False, retrieve_urls: bool = False, retrieve_licenses: bool = False, retrieve_file_info: bool = False, copyrights: Copyrights | None = None, emails: Emails | None = None, urls: Urls | None = None, licenses: Licenses | None = None, file_info: FileInfo | None = None, email_limit: int | None = 50, url_limit: int | None = 50)¶
Container for all available file-level results.
- copyrights: Copyrights | None = None¶
The detected copyrights.
- class license_tools.tools.scancode_tools.Holder(holder: str, start_line: int, end_line: int)¶
Matching information about a copyright holder.
- class license_tools.tools.scancode_tools.LicenseClue(score: float, start_line: int, end_line: int, matched_length: int, match_coverage: float, matcher: str, license_expression: str, license_expression_spdx: str, rule_identifier: str, rule_relevance: int, rule_url: str | None, from_file: str | None, matched_text: str | None = None)¶
Matching information about a license. Compared to regular detections/matches, these are rather considered clues and not perfect detections.
Currently the same as
LicenseMatch.
- class license_tools.tools.scancode_tools.LicenseDetection(license_expression: str, license_expression_spdx: str, identifier: str, matches: list[LicenseMatch] = <factory>)¶
Information on a specific detected license.
- matches: list[LicenseMatch]¶
The corresponding detailed match data.
- class license_tools.tools.scancode_tools.LicenseMatch(score: float, start_line: int, end_line: int, matched_length: int, match_coverage: float, matcher: str, license_expression: str, license_expression_spdx: str, rule_identifier: str, rule_relevance: int, rule_url: str | None, from_file: str | None, matched_text: str | None = None)¶
Matching information about a license.
- class license_tools.tools.scancode_tools.Licenses(detected_license_expression: str | None = None, detected_license_expression_spdx: str | None = None, percentage_of_license_text: float = 0.0, license_detections: list[LicenseDetection] = <factory>, license_clues: list[LicenseClue] = <factory>)¶
Information on all detected licenses.
- detected_license_expression_spdx: str | None = None¶
The detected license expression in SPDX format.
- get_scores_of_detected_license_expression_spdx() list[float]¶
Attempt to resolve the scores for the detected license expression.
- Returns:
The corresponding scores if they could be resolved.
- license_clues: list[LicenseClue]¶
The corresponding license clues.
- license_detections: list[LicenseDetection]¶
The corresponding license detections.
- class license_tools.tools.scancode_tools.PackageResults(api_data_url: str | None = None, bug_tracking_url: str | None = None, code_view_url: str | None = None, copyright: str | None = None, datasource_id: str | None = None, declared_license_expression: str | None = None, declared_license_expression_spdx: str | None = None, description: str | None = None, download_url: str | None = None, extracted_license_statement: str | None = None, holder: str | None = None, homepage_url: str | None = None, keywords: list[str] = <factory>, license_detections: list[LicenseDetection] = <factory>, md5: str | None = None, name: str | None = None, namespace: str | None = None, notice_text: str | None = None, other_license_detections: list[LicenseDetection] = <factory>, other_license_expression: str | None = None, other_license_expression_spdx: str | None = None, parties: list[Party] = <factory>, primary_language: str | None = None, purl: str | None = None, qualifiers: dict[str, str]=<factory>, release_date: date | None = None, repository_download_url: str | None = None, repository_homepage_url: str | None = None, sha1: str | None = None, sha256: str | None = None, sha512: str | None = None, size: int | None = None, source_packages: list[str] | None = None, subpath: str | None = None, type: str | None = None, vcs_url: str | None = None, version: str | None = None, is_virtual: bool = False, is_private: bool = False)¶
Container for package-specific data, based upon
packagedcode.models.PackageData.- declared_license_expression_spdx: str | None = None¶
License expression derived from metadata in SPDX format.
- classmethod from_rpm(path: Path) PackageResults¶
Get the results for the given RPM path.
- Parameters:
path – The RPM path to run on.
- Returns:
The corresponding results.
- license_detections: list[LicenseDetection]¶
Detected licenses.
- other_license_detections: list[LicenseDetection]¶
Additional license detections.
- other_license_expression_spdx: str | None = None¶
Another/a secondary license derived from the metadata in SPDX format.
- repository_download_url: str | None = None¶
Download URL for this package in its package repository.
- class license_tools.tools.scancode_tools.Party(type: Literal[None, 'person', 'project', 'organization'] = None, role: str | None = None, name: str | None = None, email: str | None = None, url: str | None = None)¶
A party related to a package.
- class license_tools.tools.scancode_tools.Url(url: str, start_line: int, end_line: int)¶
Matching information about an URL.
license_tools.tools.translation_tools¶
Tools related to translations.
- license_tools.tools.translation_tools.check_compiled_gettext_metadata(path: Path) str | None¶
Check the metadata of the given compiled gettext translation file.
- Parameters:
path – The file path to analyze.
- Returns:
The analysis results if the path points to a compiled gettext translation file, None otherwise.
license_tools.utils.archive_utils¶
Archive handling.
license_tools.utils.download_utils¶
Download handling.
- exception license_tools.utils.download_utils.ChecksumError¶
Error indicating a wrong checksum.
- class license_tools.utils.download_utils.Download(url: str, filename: str, sha256: str | None = None)¶
Configuration for one file download.
- verify_checksum(data: bytes) None¶
Check if the checksum of the given data matches the expected one.
Raises
ChecksumErrorif something is wrong.- Parameters:
data – The data to check.
- exception license_tools.utils.download_utils.DownloadError¶
Error indicating some (generic) download failure.
- license_tools.utils.download_utils.download_file(url: str, file_object: BinaryIO) None¶
Download the given file to the given file object.
- Parameters:
url – The download URL to use.
file_object – The binary file to download to. Reset after writing.
- license_tools.utils.download_utils.download_file_to_directory(download: Download, directory: Path, session: Session | None = None) None¶
Download the given file to the given directory.
- Parameters:
download – Download to perform.
directory – Directory to download to.
session – Session to use.
- license_tools.utils.download_utils.download_one_file_per_second(downloads: list[Download], directory: Path) None¶
Download the given files with not more than one request per second. This conforms to https://crates.io/data-access#api accordingly.
- Parameters:
downloads – List of downloads to perform.
directory – Directory to download to.
license_tools.utils.path_utils¶
- class license_tools.utils.path_utils.DirectoryWithFixedNameContext(directory: str | Path, fallback_to_random_if_exists: bool = True, delete_afterwards: bool = True)¶
Create the directory with the given name. Similar to tempfile.TemporaryDirectory, but with a fixed name.
- Parameters:
directory – The target directory to create temporarily.
fallback_to_random_if_exists – Specify whether to fail if the target directory already exists or whether to fall back to a random directory with the same parent in this case.
delete_afterwards – Specify whether to delete the directory on exit.
- license_tools.utils.path_utils.get_file_type(path: Path) str¶
Get the file type.
- Param:
The file to check.
- Returns:
The guessed file type.
- license_tools.utils.path_utils.get_files_from_directory(directory: str | Path, prefix: str | None = None) Generator[tuple[Path, str]]¶
Get the files from the given directory, recursively.
- Parameters:
directory – The directory to walk through.
prefix – Custom prefix to use.
- Returns:
For each file, the complete Path object as well as the path string relative to the given directory.
license_tools.utils.rendering_utils¶
Rendering utilities.
- license_tools.utils.rendering_utils.render_dictionary(dictionary: dict[str, Any], verbose_names_mapping: dict[str, str], multi_value_keys: set[str]) str¶
Render the given dictionary as string.
- Parameters:
dictionary – The dictionary to render.
verbose_names_mapping – The mapping dictionary to use for the keys. Keys not available inside this dictionary will be skipped.
multi_value_keys – Dictionary keys which could have multiple values.
license_tools.retrieval¶
Retrieve license-related data.
- class license_tools.retrieval.RetrievalFlags¶
Data retrieval flags to get shorter parameter lists.
- classmethod all(as_kwargs: bool = False) int | dict[str, bool]¶
Utility method to enable all flags.
- Param:
If enabled, return kwargs instead of the integer value.
- Returns:
The value for all flags enabled.
- classmethod is_set(flags: int, flag: int) bool¶
Check if the given flag is set.
- Parameters:
flags – The flags to check inside.
flag – The flag value to check for.
- Returns:
The check result.
- classmethod to_int(retrieve_copyrights: bool = False, retrieve_emails: bool = False, retrieve_file_info: bool = False, retrieve_urls: bool = False, retrieve_ldd_data: bool = False, retrieve_font_data: bool = False, retrieve_python_metadata: bool = False, retrieve_cargo_metadata: bool = False, retrieve_image_metadata: bool = False) int¶
Convert the given boolean parameter values to a single integer flag value.
- Parameters:
retrieve_copyrights – Whether to retrieve copyright information.
retrieve_emails – Whether to retrieve e-mails.
retrieve_file_info – Whether to retrieve file-specific information.
retrieve_urls – Whether to retrieve URLs.
retrieve_ldd_data – Whether to retrieve linking data for shared objects.
retrieve_font_data – Whether to retrieve font data.
retrieve_python_metadata – Whether to retrieve Python package metadata.
retrieve_cargo_metadata – Whether to retrieve Cargo metadata.
retrieve_image_metadata – Whether to retrieve image metadata.
- Returns:
The flags derived from the given parameters.
- license_tools.retrieval.run(*, directory: Path | str | None = None, file_path: Path | str | None = None, archive_path: Path | str | None = None, package_definition: str | None = None, download_url: str | None = None, index_url: str | None = None, prefer_sdist: bool = False, job_count: int = 4, retrieve_copyrights: bool = False, retrieve_emails: bool = False, retrieve_file_info: bool = False, retrieve_urls: bool = False, retrieve_ldd_data: bool = False, retrieve_font_data: bool = False, retrieve_python_metadata: bool = False, retrieve_cargo_metadata: bool = False, retrieve_image_metadata: bool = False, file_size_limit: int = 9223372036854775807) list[FileResults]¶
Run the analysis for the given input definition.
The directory, file_path, archive_path and package_definition parameters are mutually exclusive, but exactly one has to be set.
- Parameters:
directory – The directory to run on.
file_path – The file to run on.
archive_path – The package archive to run on.
package_definition – The package definition to run for.
download_url – The package URL to download and run on.
index_url – The PyPI index URL to use. Uses the default one from the .pypirc file if unset.
prefer_sdist – For PyPI downloads, prefer/use the source distribution over/instead of the wheel.
job_count – The number of parallel jobs to use.
retrieve_copyrights – Whether to retrieve copyright information.
retrieve_emails – Whether to retrieve e-mails.
retrieve_file_info – Whether to retrieve file-specific information.
retrieve_urls – Whether to retrieve URLs.
retrieve_ldd_data – Whether to retrieve linking data for shared objects.
retrieve_font_data – Whether to retrieve font data.
retrieve_python_metadata – Whether to retrieve Python package metadata.
retrieve_cargo_metadata – Whether to retrieve Cargo metadata.
retrieve_image_metadata – Whether to retrieve image metadata.
file_size_limit – The file size limit in bytes to skip analysis for.
- Returns:
The requested results.
- license_tools.retrieval.run_on_directory(directory: str, job_count: int = 4, retrieval_flags: int = 0, prefix: str | None = None, allow_random_directory_for_archive: bool = True, delete_unpacked_archive_directories: bool = True, file_size_limit: int = 9223372036854775807) Generator[FileResults]¶
Run the analysis on the given directory.
- Parameters:
directory – The directory to analyze.
job_count – The number of parallel jobs to use.
retrieval_flags – Values to retrieve.
prefix – Custom prefix to use.
allow_random_directory_for_archive – Allow a random directory for unpacking archives.
delete_unpacked_archive_directories – Delete the directories of unpacked archives afterwards.
file_size_limit – The file size limit in bytes to skip analysis for.
- Returns:
The requested results per file.
- license_tools.retrieval.run_on_downloaded_archive_file(download_url: str, job_count: int = 4, retrieval_flags: int = 0, file_size_limit: int = 9223372036854775807) Generator[FileResults]¶
Run the analysis on the given archive file after downloading it.
- Parameters:
download_url – The URL to download the archive from.
job_count – The number of parallel jobs to use.
retrieval_flags – Values to retrieve.
file_size_limit – The file size limit in bytes to skip analysis for.
- Returns:
The requested results.
- license_tools.retrieval.run_on_downloaded_package_file(package_definition: str, index_url: str | None = None, job_count: int = 4, retrieval_flags: int = 0, prefer_sdist: bool = False, file_size_limit: int = 9223372036854775807) Generator[FileResults]¶
Run the analysis for the given package definition.
- Parameters:
package_definition – The package definition to get the files for.
index_url – The PyPI index URL to use. Uses the default one from the .pypirc file if unset.
job_count – The number of parallel jobs to use.
retrieval_flags – Values to retrieve.
prefer_sdist – Download the source distribution instead of the wheel.
file_size_limit – The file size limit in bytes to skip analysis for.
- Returns:
The requested results.
- license_tools.retrieval.run_on_file(path: Path, short_path: str, retrieval_flags: int = 0, file_size_limit: int = 9223372036854775807) FileResults¶
Run the analysis on the given file.
- Parameters:
path – The file path to analyze.
short_path – The short path to use for display.
retrieval_flags – Values to retrieve.
file_size_limit – The file size limit in bytes to skip analysis for.
- Returns:
The requested results.
- license_tools.retrieval.run_on_package_archive_file(archive_path: Path, job_count: int = 4, retrieval_flags: int = 0, file_size_limit: int = 9223372036854775807) Generator[FileResults]¶
Run the analysis on the given package archive file.
- Parameters:
archive_path – The package archive path to analyze.
job_count – The number of parallel jobs to use.
retrieval_flags – Values to retrieve.
file_size_limit – The file size limit in bytes to skip analysis for.
- Returns:
The requested results.