Skip to content
Snippets Groups Projects
Commit 7404486d authored by David Douard's avatar David Douard
Browse files

cli: speedup the `swh` cli command startup time

move most import statements in functions.

Related to T2575.
parent 12fe1f78
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2018-2019 The Software Heritage developers
# Copyright (C) 2018-2020 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
# WARNING: do not import unnecessary things here to keep cli startup time under
# control
import click
import dulwich.repo
import os
import sys
from functools import partial
from urllib.parse import urlparse
from swh.model import hashutil
from swh.model.identifiers import (
origin_identifier,
snapshot_identifier,
parse_swhid,
swhid,
SWHID,
CONTENT,
DIRECTORY,
)
from swh.model.exceptions import ValidationError
from swh.model.from_disk import Content, Directory
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
......@@ -41,6 +26,9 @@ class SWHIDParamType(click.ParamType):
name = "persistent identifier"
def convert(self, value, param, ctx):
from swh.model.exceptions import ValidationError
from swh.model.identifiers import parse_swhid
try:
parse_swhid(value)
return value # return as string, as we need just that
......@@ -49,26 +37,42 @@ class SWHIDParamType(click.ParamType):
def swhid_of_file(path):
from swh.model.from_disk import Content
from swh.model.identifiers import swhid, CONTENT
object = Content.from_file(path=path).get_data()
return swhid(CONTENT, object)
def swhid_of_file_content(data):
from swh.model.from_disk import Content
from swh.model.identifiers import swhid, CONTENT
object = Content.from_bytes(mode=644, data=data).get_data()
return swhid(CONTENT, object)
def swhid_of_dir(path):
from swh.model.from_disk import Directory
from swh.model.identifiers import swhid, DIRECTORY
object = Directory.from_disk(path=path).get_data()
return swhid(DIRECTORY, object)
def swhid_of_origin(url):
swhid = SWHID(object_type="origin", object_id=origin_identifier({"url": url}))
return str(swhid)
from swh.model.identifiers import origin_identifier
from swh.model.identifiers import SWHID
return str(SWHID(object_type="origin", object_id=origin_identifier({"url": url})))
def swhid_of_git_repo(path):
import dulwich.repo
from swh.model import hashutil
from swh.model.identifiers import snapshot_identifier
from swh.model.identifiers import SWHID
repo = dulwich.repo.Repo(path)
branches = {}
......@@ -90,11 +94,12 @@ def swhid_of_git_repo(path):
snapshot = {"branches": branches}
swhid = SWHID(object_type="snapshot", object_id=snapshot_identifier(snapshot))
return str(swhid)
return str(SWHID(object_type="snapshot", object_id=snapshot_identifier(snapshot)))
def identify_object(obj_type, follow_symlinks, obj):
from urllib.parse import urlparse
if obj_type == "auto":
if obj == "-" or os.path.isfile(obj):
obj_type = "content"
......@@ -194,6 +199,7 @@ def identify(obj_type, verify, show_filename, follow_symlinks, objects):
swh:1:snp:510aa88bdc517345d258c1fc2babcd0e1f905e93 helloworld.git
""" # NoQA # overlong lines in shell examples are fine
from functools import partial
if verify and len(objects) != 1:
raise click.BadParameter("verification requires a single object")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment