Add ipfs command

This commit is contained in:
Ales (Shagi) Zabala Alava 2020-11-06 15:45:30 +01:00
parent 376f8be080
commit d7f9017abd
2 changed files with 69 additions and 1 deletions

View File

@ -1,7 +1,7 @@
import argparse import argparse
import sys import sys
from . import run, build, thumbnail, rsync from . import run, build, thumbnail, rsync, ipfs
def main(): def main():
@ -24,6 +24,7 @@ def main():
run, run,
thumbnail, thumbnail,
rsync, rsync,
ipfs,
] ]
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()

67
negromate/web/commands/ipfs.py Executable file
View File

@ -0,0 +1,67 @@
from pathlib import Path
import subprocess
import urllib.request
name = 'ipfs'
help_text = 'Upload the web to IPFS'
def options(parser):
parser.add_argument(
'song_folder', type=Path,
help="Folder with the song database.")
parser.add_argument(
'-a', '--api', default='http://localhost:5001',
help="IPFS API server, defaults to http://localhost:5001.")
parser.add_argument(
'-p', '--pinfile', default='~/.negromate/ipfs.hash', type=Path,
help="file to store the current ipfs hash, defaults to ~/.negromate/ipfs.")
def run(args):
# add to local
command = [
"ipfs",
"add",
"--recursive",
"--quieter",
args.song_folder,
]
final_hash = subprocess.check_output(command).decode('utf-8').strip()
# pin in server
url = "{}/api/v0/pin/add?arg={}&progress=false".format(
args.api,
final_hash,
)
urllib.request.urlopen(url)
# read previous hash and update value
pinfile = args.pinfile.expanduser()
if pinfile.exists():
with pinfile.open() as f:
previous_hash = f.read()
else:
if not pinfile.parent.exists():
pinfile.parent.mkdir()
previous_hash = None
with pinfile.open('w') as f:
f.write(final_hash)
if previous_hash is not None:
# remove previous pin on local
command = [
'ipfs',
'pin',
'rm',
previous_hash,
]
subprocess.check_call(command)
# remove previous pin on server
url = "{}/api/v0/pin/rm?arg={}".format(
args.api,
previous_hash,
)
urllib.request.urlopen(url)