Skip to content

Commit

Permalink
add IPFS lazy install
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed May 8, 2022
1 parent 4b18fe9 commit 176a871
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
16 changes: 6 additions & 10 deletions iscc_sdk/ipfs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""IPFS wrapper"""
import sys
from os.path import basename

import iscc_sdk as idk
import subprocess


__all__ = [
Expand 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)}"
Expand All @@ -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",
Expand All @@ -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()
15 changes: 13 additions & 2 deletions iscc_sdk/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,8 +26,7 @@
"fpcalc_install",
"exiv2_bin",
"exiv2json_bin",
"ipfs_bin",
"ipfs_install",
"ipfs_run",
"java_bin",
"tika_bin",
]
Expand Down Expand Up @@ -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 #
########################################################################################
Expand Down
3 changes: 0 additions & 3 deletions tests/test_ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@


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"
)


def test_ipfs_cidv1_base16(jpg_file):
idk.ipfs_install()
assert (
idk.ipfs_cidv1_base16(jpg_file)
== "f015512202fab540b1e8f84ddccba82db6a4ec53801c632ef60345520254374b70d507c44"
Expand Down

0 comments on commit 176a871

Please sign in to comment.