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
Renaud Boyer
swh-model
Commits
f5398909
Commit
f5398909
authored
6 years ago
by
Stefano Zacchiroli
Browse files
Options
Downloads
Patches
Plain Diff
swh-identify: show filename in output (by default)
with explicit option to hide it Closes T1133
parent
edcd3659
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/model/cli.py
+9
-2
9 additions, 2 deletions
swh/model/cli.py
swh/model/tests/test_cli.py
+32
-2
32 additions, 2 deletions
swh/model/tests/test_cli.py
with
41 additions
and
4 deletions
swh/model/cli.py
+
9
−
2
View file @
f5398909
...
...
@@ -4,6 +4,7 @@
# See top-level LICENSE file for more information
import
click
import
locale
import
os
import
sys
...
...
@@ -39,10 +40,12 @@ def pid_of_dir(path):
help
=
'
type of object to identify (default: auto)
'
)
@click.option
(
'
--verify
'
,
'
-v
'
,
metavar
=
'
PID
'
,
type
=
PidParamType
(),
help
=
'
reference identifier to be compared with computed one
'
)
@click.option
(
'
--filename/--no-filename
'
,
'
show_filename
'
,
default
=
True
,
help
=
'
show/hide file name (default: show)
'
)
@click.argument
(
'
object
'
,
type
=
click
.
Path
(
exists
=
True
,
readable
=
True
,
allow_dash
=
True
,
path_type
=
bytes
))
def
identify
(
type
,
verify
,
object
):
def
identify
(
type
,
verify
,
show_filename
,
object
):
"""
Compute the Software Heritage persistent identifier (PID) for a given
source code object.
...
...
@@ -88,7 +91,11 @@ def identify(type, verify, object):
click
.
echo
(
'
PID mismatch: %s != %s
'
%
(
verify
,
pid
))
sys
.
exit
(
1
)
else
:
click
.
echo
(
pid
)
msg
=
pid
if
show_filename
:
encoding
=
locale
.
getpreferredencoding
(
do_setlocale
=
False
)
msg
=
'
%s
\t
%s
'
%
(
pid
,
object
.
decode
(
encoding
))
click
.
echo
(
msg
)
if
__name__
==
'
__main__
'
:
...
...
This diff is collapsed.
Click to expand it.
swh/model/tests/test_cli.py
+
32
−
2
View file @
f5398909
...
...
@@ -23,6 +23,7 @@ class TestIdentify(DataMixin, unittest.TestCase):
self
.
runner
=
CliRunner
()
def
test_content_id
(
self
):
"""
identify file content
"""
self
.
make_contents
(
self
.
tmpdir_name
)
for
filename
,
content
in
self
.
contents
.
items
():
path
=
os
.
path
.
join
(
self
.
tmpdir_name
,
filename
)
...
...
@@ -30,20 +31,48 @@ class TestIdentify(DataMixin, unittest.TestCase):
[
'
--type
'
,
'
content
'
,
path
])
self
.
assertEqual
(
result
.
exit_code
,
0
)
self
.
assertEqual
(
result
.
output
.
rstrip
()
,
self
.
assertEqual
(
result
.
output
.
split
()[
0
]
,
'
swh:1:cnt:
'
+
hash_to_hex
(
content
[
'
sha1_git
'
]))
def
test_directory_id
(
self
):
"""
identify an entire directory
"""
self
.
make_from_tarball
(
self
.
tmpdir_name
)
path
=
os
.
path
.
join
(
self
.
tmpdir_name
,
b
'
sample-folder
'
)
result
=
self
.
runner
.
invoke
(
cli
.
identify
,
[
'
--type
'
,
'
directory
'
,
path
])
self
.
assertEqual
(
result
.
exit_code
,
0
)
self
.
assertEqual
(
result
.
output
.
rstrip
()
,
self
.
assertEqual
(
result
.
output
.
split
()[
0
]
,
'
swh:1:dir:e8b0f1466af8608c8a3fb9879db172b887e80759
'
)
def
test_show_filename
(
self
):
"""
filename is shown by default
"""
self
.
make_contents
(
self
.
tmpdir_name
)
for
filename
,
content
in
self
.
contents
.
items
():
path
=
os
.
path
.
join
(
self
.
tmpdir_name
,
filename
)
result
=
self
.
runner
.
invoke
(
cli
.
identify
,
[
'
--type
'
,
'
content
'
,
path
])
self
.
assertEqual
(
result
.
exit_code
,
0
)
self
.
assertEqual
(
result
.
output
.
rstrip
(),
'
swh:1:cnt:%s
\t
%s
'
%
(
hash_to_hex
(
content
[
'
sha1_git
'
]),
path
.
decode
()))
def
test_hide_filename
(
self
):
"""
filename is hidden upon request
"""
self
.
make_contents
(
self
.
tmpdir_name
)
for
filename
,
content
in
self
.
contents
.
items
():
path
=
os
.
path
.
join
(
self
.
tmpdir_name
,
filename
)
result
=
self
.
runner
.
invoke
(
cli
.
identify
,
[
'
--type
'
,
'
content
'
,
'
--no-filename
'
,
path
])
self
.
assertEqual
(
result
.
exit_code
,
0
)
self
.
assertEqual
(
result
.
output
.
rstrip
(),
'
swh:1:cnt:
'
+
hash_to_hex
(
content
[
'
sha1_git
'
]))
def
test_auto_id
(
self
):
"""
automatic object type: file or directory, depending on argument
"""
with
tempfile
.
NamedTemporaryFile
(
prefix
=
'
swh.model.cli
'
)
as
f
:
result
=
self
.
runner
.
invoke
(
cli
.
identify
,
[
f
.
name
])
self
.
assertEqual
(
result
.
exit_code
,
0
)
...
...
@@ -55,6 +84,7 @@ class TestIdentify(DataMixin, unittest.TestCase):
self
.
assertRegex
(
result
.
output
,
r
'
^swh:\d+:dir:
'
)
def
test_verify_content
(
self
):
"""
identifier verification
"""
self
.
make_contents
(
self
.
tmpdir_name
)
for
filename
,
content
in
self
.
contents
.
items
():
expected_id
=
'
swh:1:cnt:
'
+
hash_to_hex
(
content
[
'
sha1_git
'
])
...
...
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