Skip to content

Instantly share code, notes, and snippets.

@fogti
Created September 9, 2020 01:57
Show Gist options
  • Save fogti/9fec20f7e6a6864d12a4a254e4521016 to your computer and use it in GitHub Desktop.
Save fogti/9fec20f7e6a6864d12a4a254e4521016 to your computer and use it in GitHub Desktop.
insert a distfile into a "filename-hash BLAKE2B 8" distfile tree
#!/usr/bin/python3
import filecmp
import hashlib
import os
import shutil
import sys
args = sys.argv[1:]
if not args:
# nothing to do
sys.exit(0)
do_delete = False
if args[0] == '--delete':
do_delete = True
args.pop(0)
distdir = os.path.realpath(os.path.dirname(sys.argv[0]))
for i in args:
if os.path.islink(i) or (not os.path.isfile(i)):
print('{}: file not found'.format(i))
continue
rlp = os.path.realpath(i)
bn = os.path.basename(rlp)
hashpart = hashlib.blake2b(bn.encode('utf-8')).hexdigest()[:2]
trg = os.path.realpath(os.path.join(distdir, hashpart, bn))
del bn
msg = ''
if not hashpart:
msg = '!!!!'
elif trg == rlp:
msg = 'nodo'
elif os.path.isfile(trg):
msg = '....'
if filecmp.cmp(rlp, trg):
os.remove(rlp)
else:
msg = '.dlt'
else:
try:
if do_delete:
shutil.move(rlp, trg)
else:
try:
os.link(rlp, trg)
except:
shutil.copy2(rlp, trg)
msg = ' OK '
except:
msg = 'FAIL'
print('{} {} {}'.format(msg, hashpart, rlp))
[structure]
0=filename-hash BLAKE2B 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment