diff --git a/iscc_sdk/ipfs.py b/iscc_sdk/ipfs.py index dac908c..e2cf533 100644 --- a/iscc_sdk/ipfs.py +++ b/iscc_sdk/ipfs.py @@ -1,9 +1,7 @@ """IPFS wrapper""" import sys from os.path import basename - import iscc_sdk as idk -import subprocess __all__ = [ @@ -24,11 +22,11 @@ def ipfs_cidv1(fp, wrap=False): :return: IPFS CIDv1 of the file :rtype: str """ - cmd = [idk.ipfs_bin(), "add", "--only-hash", "--cid-version=1", "--offline", "--quieter"] + args = ["add", "--only-hash", "--cid-version=1", "--offline", "--quieter"] if wrap: - cmd.append("--wrap-with-directory") - cmd.append(fp) - result = subprocess.run(cmd, capture_output=True, check=True) + args.append("--wrap-with-directory") + args.append(fp) + result = idk.ipfs_run(args) cid = result.stdout.decode(sys.stdout.encoding).strip() if wrap: cid += f"/{basename(fp)}" @@ -44,8 +42,7 @@ def ipfs_cidv1_base16(fp): :return: IPFS CIDv1 of the file in base16 (hex) :rtype: str """ - cmd = [ - idk.ipfs_bin(), + args = [ "add", "--only-hash", "--cid-version=1", @@ -54,6 +51,5 @@ def ipfs_cidv1_base16(fp): "--cid-base=base16", fp, ] - - result = subprocess.run(cmd, capture_output=True, check=True) + result = idk.ipfs_run(args) return result.stdout.decode(sys.stdout.encoding).strip() diff --git a/iscc_sdk/tools.py b/iscc_sdk/tools.py index 1f95840..5875dc7 100644 --- a/iscc_sdk/tools.py +++ b/iscc_sdk/tools.py @@ -7,6 +7,7 @@ import zipfile from pathlib import Path from platform import system, architecture +from typing import List from urllib.parse import urlparse from urllib.request import urlretrieve from blake3 import blake3 @@ -25,8 +26,7 @@ "fpcalc_install", "exiv2_bin", "exiv2json_bin", - "ipfs_bin", - "ipfs_install", + "ipfs_run", "java_bin", "tika_bin", ] @@ -196,6 +196,17 @@ def ipfs_version_info(): # pragma: no cover return "IPFS not installed" +def ipfs_run(args: List[str]): + cmd = [ipfs_bin()] + args + try: + result = subprocess.run(cmd, capture_output=True, check=True) + except FileNotFoundError: # pragma: no cover + print("IPFS not found - installing ...") + ipfs_install() + result = subprocess.run(cmd, capture_output=True, check=True) + return result + + ######################################################################################## # Exiv2 # ######################################################################################## diff --git a/tests/test_ipfs.py b/tests/test_ipfs.py index 915151b..ee0cfff 100644 --- a/tests/test_ipfs.py +++ b/tests/test_ipfs.py @@ -2,12 +2,10 @@ def test_ipfs_cidv1(jpg_file): - idk.ipfs_install() assert idk.ipfs_cidv1(jpg_file) == "bafkreibpvnkawhupqto4zouc3nve5rjyahddf33agrksajkdos3q2ud4iq" def test_ipfs_cidv1_wrap(jpg_file): - idk.ipfs_install() assert ( idk.ipfs_cidv1(jpg_file, wrap=True) == "bafybeicg5clgwhge6eyzzvuvtsch6eenqjhc2osm42qmuza6xbrgui3kpy/img.jpg" @@ -15,7 +13,6 @@ def test_ipfs_cidv1_wrap(jpg_file): def test_ipfs_cidv1_base16(jpg_file): - idk.ipfs_install() assert ( idk.ipfs_cidv1_base16(jpg_file) == "f015512202fab540b1e8f84ddccba82db6a4ec53801c632ef60345520254374b70d507c44"