Changeset View
Changeset View
Standalone View
Standalone View
swh/model/cli.py
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
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 | 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(CONTENT, object) | ||||
def swhid_of_dir(path): | def swhid_of_dir(path, exclude_patterns=None): | ||||
from swh.model.from_disk import Directory | from swh.model.from_disk import ( | ||||
Directory, | |||||
accept_all_directories, | |||||
ignore_directories_patterns, | |||||
) | |||||
marmoute: (nits: maybe we could have a default value for this new arguments since if was not necessary in… | |||||
from swh.model.identifiers import DIRECTORY, swhid | from swh.model.identifiers import DIRECTORY, swhid | ||||
object = Directory.from_disk(path=path).get_data() | dir_filter = ( | ||||
ignore_directories_patterns(path.decode(), exclude_patterns) | |||||
douarddaAuthorUnsubmitted Done Inline Actionsisn't there some encoding risk here? This path.decode() frighten me a bit. douardda: isn't there some encoding risk here? This `path.decode()` frighten me a bit.
| |||||
if exclude_patterns | |||||
else accept_all_directories | |||||
) | |||||
object = Directory.from_disk(path=path, dir_filter=dir_filter).get_data() | |||||
return swhid(DIRECTORY, object) | return swhid(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 SWHID, origin_identifier | ||||
return str(SWHID(object_type="origin", object_id=origin_identifier({"url": url}))) | return str(SWHID(object_type="origin", object_id=origin_identifier({"url": url}))) | ||||
Show All 23 Lines | for ref, target in repo.refs.get_symrefs().items(): | ||||
"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="snapshot", object_id=snapshot_identifier(snapshot))) | ||||
def identify_object(obj_type, follow_symlinks, 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" | ||||
elif os.path.isdir(obj): | elif os.path.isdir(obj): | ||||
obj_type = "directory" | obj_type = "directory" | ||||
else: | else: | ||||
Show All 12 Lines | if obj == "-": | ||||
swhid = swhid_of_file_content(content) | swhid = swhid_of_file_content(content) | ||||
elif obj_type in ["content", "directory"]: | elif obj_type in ["content", "directory"]: | ||||
path = obj.encode(sys.getfilesystemencoding()) | path = obj.encode(sys.getfilesystemencoding()) | ||||
if follow_symlinks and os.path.islink(obj): | if follow_symlinks and os.path.islink(obj): | ||||
path = os.path.realpath(obj) | path = os.path.realpath(obj) | ||||
if obj_type == "content": | if obj_type == "content": | ||||
swhid = swhid_of_file(path) | swhid = swhid_of_file(path) | ||||
elif obj_type == "directory": | elif obj_type == "directory": | ||||
swhid = swhid_of_dir(path) | swhid = swhid_of_dir(path, exclude_patterns) | ||||
elif obj_type == "origin": | elif obj_type == "origin": | ||||
swhid = swhid_of_origin(obj) | swhid = swhid_of_origin(obj) | ||||
elif obj_type == "snapshot": | elif obj_type == "snapshot": | ||||
swhid = swhid_of_git_repo(obj) | swhid = swhid_of_git_repo(obj) | ||||
else: # shouldn't happen, due to option validation | else: # shouldn't happen, due to option validation | ||||
raise click.BadParameter("invalid object type: " + obj_type) | raise click.BadParameter("invalid object type: " + obj_type) | ||||
# note: we return original obj instead of path here, to preserve user-given | # note: we return original obj instead of path here, to preserve user-given | ||||
Show All 18 Lines | |||||
@click.option( | @click.option( | ||||
"--type", | "--type", | ||||
"-t", | "-t", | ||||
"obj_type", | "obj_type", | ||||
default="auto", | default="auto", | ||||
type=click.Choice(["auto", "content", "directory", "origin", "snapshot"]), | type=click.Choice(["auto", "content", "directory", "origin", "snapshot"]), | ||||
help="type of object to identify (default: auto)", | help="type of object to identify (default: auto)", | ||||
) | ) | ||||
@click.option( | @click.option( | ||||
Done Inline Actionssounds like a bit of a mouthful, can we just go for --ignore-dir instead? Also, the help string should clarify:
zack: sounds like a bit of a mouthful, can we just go for `--ignore-dir` instead?
Also, the help… | |||||
"--exclude", | |||||
"-x", | |||||
"exclude_patterns", | |||||
metavar="PATTERN", | |||||
Done Inline ActionsMinor nit/typo: it should be "globbing", but all in all I'd rather just not mention globbing if it's not supported. I suggest ignore directories by path name (can be repeated). The point about depth remains though. If it does match at any depth, I propose: ignore directories by local path name, matching at any depth (can be repeated). Otherwise it should be: ignore directories by absolute path name, starting from root path (can be repeated) zack: Minor nit/typo: it should be "globbing", but all in all I'd rather just not mention globbing if… | |||||
multiple=True, | |||||
Done Inline ActionsI have the same question as Zack, does this match at root level or at all level? marmoute: I have the same question as Zack, does this match at root level or at all level? | |||||
help="Exclude directories using glob patterns \ | |||||
(e.g., '*.git' to exclude all .git directories)", | |||||
) | |||||
@click.option( | |||||
"--verify", | "--verify", | ||||
"-v", | "-v", | ||||
metavar="SWHID", | metavar="SWHID", | ||||
type=SWHIDParamType(), | type=SWHIDParamType(), | ||||
help="reference identifier to be compared with computed one", | help="reference identifier to be compared with computed one", | ||||
) | ) | ||||
@click.argument("objects", nargs=-1, required=True) | @click.argument("objects", nargs=-1, required=True) | ||||
def identify(obj_type, verify, show_filename, follow_symlinks, objects): | def identify( | ||||
obj_type, verify, show_filename, follow_symlinks, objects, exclude_patterns, | |||||
): | |||||
"""Compute the Software Heritage persistent identifier (SWHID) for the given | """Compute the Software Heritage persistent identifier (SWHID) for the given | ||||
source code object(s). | source code object(s). | ||||
For more details about SWHIDs see: | For more details about SWHIDs see: | ||||
\b | \b | ||||
https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html | https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html | ||||
Show All 18 Lines | \b | ||||
swh:1:snp:510aa88bdc517345d258c1fc2babcd0e1f905e93 helloworld.git | swh:1:snp:510aa88bdc517345d258c1fc2babcd0e1f905e93 helloworld.git | ||||
""" # NoQA # overlong lines in shell examples are fine | """ # NoQA # overlong lines in shell examples are fine | ||||
from functools import partial | from functools import partial | ||||
if verify and len(objects) != 1: | if verify and len(objects) != 1: | ||||
raise click.BadParameter("verification requires a single object") | raise click.BadParameter("verification requires a single object") | ||||
results = map(partial(identify_object, obj_type, follow_symlinks), objects) | results = map( | ||||
partial(identify_object, obj_type, follow_symlinks, exclude_patterns), objects, | |||||
) | |||||
if verify: | if verify: | ||||
swhid = next(results)[1] | swhid = next(results)[1] | ||||
if str(verify) == swhid: | if str(verify) == swhid: | ||||
click.echo("SWHID match: %s" % swhid) | click.echo("SWHID match: %s" % swhid) | ||||
sys.exit(0) | sys.exit(0) | ||||
else: | else: | ||||
click.echo("SWHID mismatch: %s != %s" % (verify, swhid)) | click.echo("SWHID mismatch: %s != %s" % (verify, swhid)) | ||||
Show All 11 Lines |
(nits: maybe we could have a default value for this new arguments since if was not necessary in most usecase before)