Changeset View
Changeset View
Standalone View
Standalone View
swh/model/cli.py
# Copyright (C) 2018-2020 The Software Heritage developers | # Copyright (C) 2018-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import sys | import sys | ||||
from typing import List | from typing import List | ||||
# WARNING: do not import unnecessary things here to keep cli startup time under | # WARNING: do not import unnecessary things here to keep cli startup time under | ||||
# control | # control | ||||
import click | import click | ||||
from swh.core.cli import swh as swh_cli_group | from swh.core.cli import swh as swh_cli_group | ||||
from swh.model.identifiers import SWHID | from swh.model.swhid import SWHID, SWHIDObjectType, swhid | ||||
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) | CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) | ||||
# Mapping between dulwich types and Software Heritage ones. Used by snapshot ID | # Mapping between dulwich types and Software Heritage ones. Used by snapshot ID | ||||
# computation. | # computation. | ||||
_DULWICH_TYPES = { | _DULWICH_TYPES = { | ||||
b"blob": "content", | b"blob": "content", | ||||
b"tree": "directory", | b"tree": "directory", | ||||
Show All 15 Lines | def convert(self, value, param, ctx) -> SWHID: | ||||
try: | try: | ||||
return parse_swhid(value) | return parse_swhid(value) | ||||
except ValidationError as e: | except ValidationError as e: | ||||
self.fail(f'"{value}" is not a valid SWHID: {e}', param, ctx) | self.fail(f'"{value}" is not a valid SWHID: {e}', param, ctx) | ||||
def swhid_of_file(path): | def swhid_of_file(path): | ||||
from swh.model.from_disk import Content | from swh.model.from_disk import Content | ||||
from swh.model.identifiers import CONTENT, swhid | |||||
object = Content.from_file(path=path).get_data() | object = Content.from_file(path=path).get_data() | ||||
return swhid(CONTENT, object) | return swhid(SWHIDObjectType.CONTENT, object) | ||||
def swhid_of_file_content(data): | def swhid_of_file_content(data): | ||||
from swh.model.from_disk import Content | from swh.model.from_disk import Content | ||||
from swh.model.identifiers import CONTENT, swhid | |||||
object = Content.from_bytes(mode=644, data=data).get_data() | object = Content.from_bytes(mode=644, data=data).get_data() | ||||
return swhid(CONTENT, object) | return swhid(SWHIDObjectType.CONTENT, object) | ||||
def swhid_of_dir(path: bytes, exclude_patterns: List[bytes] = None) -> str: | def swhid_of_dir(path: bytes, exclude_patterns: List[bytes] = None) -> str: | ||||
from swh.model.from_disk import ( | from swh.model.from_disk import ( | ||||
Directory, | Directory, | ||||
accept_all_directories, | accept_all_directories, | ||||
ignore_directories_patterns, | ignore_directories_patterns, | ||||
) | ) | ||||
from swh.model.identifiers import DIRECTORY, swhid | |||||
dir_filter = ( | dir_filter = ( | ||||
ignore_directories_patterns(path, exclude_patterns) | ignore_directories_patterns(path, exclude_patterns) | ||||
if exclude_patterns | if exclude_patterns | ||||
else accept_all_directories | else accept_all_directories | ||||
) | ) | ||||
object = Directory.from_disk(path=path, dir_filter=dir_filter).get_data() | object = Directory.from_disk(path=path, dir_filter=dir_filter).get_data() | ||||
return swhid(DIRECTORY, object) | return swhid(SWHIDObjectType.DIRECTORY, object) | ||||
def swhid_of_origin(url): | def swhid_of_origin(url): | ||||
from swh.model.identifiers import SWHID, origin_identifier | from swh.model.identifiers import origin_identifier | ||||
return str(SWHID(object_type="origin", object_id=origin_identifier({"url": url}))) | return str( | ||||
SWHID( | |||||
object_type=SWHIDObjectType.ORIGIN, | |||||
object_id=origin_identifier({"url": url}), | |||||
) | |||||
) | |||||
def swhid_of_git_repo(path): | def swhid_of_git_repo(path): | ||||
import dulwich.repo | import dulwich.repo | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.identifiers import SWHID, snapshot_identifier | from swh.model.identifiers import snapshot_identifier | ||||
repo = dulwich.repo.Repo(path) | repo = dulwich.repo.Repo(path) | ||||
branches = {} | branches = {} | ||||
for ref, target in repo.refs.as_dict().items(): | for ref, target in repo.refs.as_dict().items(): | ||||
obj = repo[target] | obj = repo[target] | ||||
if obj: | if obj: | ||||
branches[ref] = { | branches[ref] = { | ||||
"target": hashutil.bytehex_to_hash(target), | "target": hashutil.bytehex_to_hash(target), | ||||
"target_type": _DULWICH_TYPES[obj.type_name], | "target_type": _DULWICH_TYPES[obj.type_name], | ||||
} | } | ||||
else: | else: | ||||
branches[ref] = None | branches[ref] = None | ||||
for ref, target in repo.refs.get_symrefs().items(): | for ref, target in repo.refs.get_symrefs().items(): | ||||
branches[ref] = { | branches[ref] = { | ||||
"target": target, | "target": target, | ||||
"target_type": "alias", | "target_type": "alias", | ||||
} | } | ||||
snapshot = {"branches": branches} | snapshot = {"branches": branches} | ||||
return str(SWHID(object_type="snapshot", object_id=snapshot_identifier(snapshot))) | return str( | ||||
SWHID( | |||||
object_type=SWHIDObjectType.SNAPSHOT, | |||||
object_id=snapshot_identifier(snapshot), | |||||
) | |||||
) | |||||
def identify_object(obj_type, follow_symlinks, exclude_patterns, obj): | def identify_object(obj_type, follow_symlinks, exclude_patterns, obj): | ||||
from urllib.parse import urlparse | from urllib.parse import urlparse | ||||
if obj_type == "auto": | if obj_type == "auto": | ||||
if obj == "-" or os.path.isfile(obj): | if obj == "-" or os.path.isfile(obj): | ||||
obj_type = "content" | obj_type = "content" | ||||
▲ Show 20 Lines • Show All 136 Lines • Show Last 20 Lines |