Skip to content
Snippets Groups Projects
Commit f5398909 authored by Stefano Zacchiroli's avatar Stefano Zacchiroli
Browse files

swh-identify: show filename in output (by default)

with explicit option to hide it

Closes T1133
parent edcd3659
No related branches found
No related tags found
No related merge requests found
......@@ -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__':
......
......@@ -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'])
......
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