Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-model
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nicolas Dandrimont
swh-model
Commits
7404486d
Commit
7404486d
authored
4 years ago
by
David Douard
Browse files
Options
Downloads
Patches
Plain Diff
cli: speedup the `swh` cli command startup time
move most import statements in functions. Related to T2575.
parent
12fe1f78
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
swh/model/cli.py
+28
-22
28 additions, 22 deletions
swh/model/cli.py
with
28 additions
and
22 deletions
swh/model/cli.py
+
28
−
22
View file @
7404486d
# Copyright (C) 2018-20
19
The Software Heritage developers
# Copyright (C) 2018-20
20
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
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment