From c62f13fd55739b73245cbfa75f0325df4de12957 Mon Sep 17 00:00:00 2001
From: Valentin Lorentz <vlorentz@softwareheritage.org>
Date: Thu, 8 Apr 2021 18:55:01 +0200
Subject: [PATCH] swh-identify: Hide tracebacks if Click or Dulwich is not
 installed

And show nice human-readable errors instead
---
 swh/model/cli.py | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/swh/model/cli.py b/swh/model/cli.py
index 312a0247..effdf8a8 100644
--- a/swh/model/cli.py
+++ b/swh/model/cli.py
@@ -9,7 +9,15 @@ from typing import Dict, List, Optional
 
 # WARNING: do not import unnecessary things here to keep cli startup time under
 # control
-import click
+try:
+    import click
+except ImportError:
+    print(
+        "Cannot run swh-identify; the Click package is not installed."
+        "Please install 'swh.model[cli]' for full functionality.",
+        file=sys.stderr,
+    )
+    exit(1)
 
 try:
     from swh.core.cli import swh as swh_cli_group
@@ -101,7 +109,13 @@ def swhid_of_origin(url):
 
 
 def swhid_of_git_repo(path) -> CoreSWHID:
-    import dulwich.repo
+    try:
+        import dulwich.repo
+    except ImportError:
+        raise click.ClickException(
+            "Cannot compute snapshot identifier; the Dulwich package is not installed. "
+            "Please install 'swh.model[cli]' for full functionality.",
+        )
 
     from swh.model import hashutil
     from swh.model.identifiers import snapshot_identifier
-- 
GitLab