diff --git a/swh/model/cli.py b/swh/model/cli.py index 5996d19..6df219e 100644 --- a/swh/model/cli.py +++ b/swh/model/cli.py @@ -1,95 +1,102 @@ # Copyright (C) 2018 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 import click +import locale import os import sys from swh.model import identifiers as pids from swh.model.exceptions import ValidationError from swh.model.from_disk import Content, Directory class PidParamType(click.ParamType): name = 'persistent identifier' def convert(self, value, param, ctx): try: pids.parse_persistent_identifier(value) return value # return as string, as we need just that except ValidationError as e: self.fail('%s is not a valid PID. %s.' % (value, e), param, ctx) def pid_of_file(path): object = Content.from_file(path=path).get_data() return pids.persistent_identifier(pids.CONTENT, object) def pid_of_dir(path): object = Directory.from_disk(path=path).get_data() return pids.persistent_identifier(pids.DIRECTORY, object) @click.command() @click.option('--type', '-t', default='auto', type=click.Choice(['auto', 'content', 'directory']), 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. For more details about Software Heritage PIDs see: \b https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html \b Examples: \b $ swh-identify /usr/src/linux/kernel/ swh:1:dir:f9f858a48d663b3809c9e2f336412717496202ab \b $ swh-identify /usr/src/linux/kernel/sched/deadline.c swh:1:cnt:57b939c81bce5d06fa587df8915f05affbe22b82 """ if type == 'auto': if os.path.isfile(object): type = 'content' elif os.path.isdir(object): type = 'directory' else: # shouldn't happen, due to path validation raise click.BadParameter('%s is neither a file nor a directory' % object) pid = None if type == 'content': pid = pid_of_file(object) elif type == 'directory': pid = pid_of_dir(object) else: # shouldn't happen, due to option validation raise click.BadParameter('invalid object type: ' + type) if verify: if verify == pid: click.echo('PID match: %s' % pid) sys.exit(0) else: 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__': identify() diff --git a/swh/model/tests/test_cli.py b/swh/model/tests/test_cli.py index 054cc0c..d8e68b9 100644 --- a/swh/model/tests/test_cli.py +++ b/swh/model/tests/test_cli.py @@ -1,73 +1,103 @@ # Copyright (C) 2018 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 import os import tempfile import unittest from click.testing import CliRunner from nose.plugins.attrib import attr from swh.model import cli from swh.model.tests.test_from_disk import DataMixin from swh.model.hashutil import hash_to_hex @attr('fs') class TestIdentify(DataMixin, unittest.TestCase): def setUp(self): super().setUp() 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) result = self.runner.invoke(cli.identify, ['--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) self.assertRegex(result.output, r'^swh:\d+:cnt:') with tempfile.TemporaryDirectory(prefix='swh.model.cli') as dirname: result = self.runner.invoke(cli.identify, [dirname]) self.assertEqual(result.exit_code, 0) 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']) # match path = os.path.join(self.tmpdir_name, filename) result = self.runner.invoke(cli.identify, ['--verify', expected_id, path]) self.assertEqual(result.exit_code, 0) # mismatch with open(path, 'a') as f: f.write('trailing garbage to make verification fail') result = self.runner.invoke(cli.identify, ['--verify', expected_id, path]) self.assertEqual(result.exit_code, 1)