Commit c0b9a92e authored by Ophélie Gagnard's avatar Ophélie Gagnard

New --id command-line option for download & upload

parent c9337a3b
Pipeline #24986 passed with stage
in 0 seconds
......@@ -486,8 +486,10 @@ def _newArgumentParser(url_help):
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=argparse.FileType('r'), required=True,
help='SlapOS configuration file.')
parser.add_argument('--prefix-key', default='')
parser.add_argument('--suffix-key', default='')
parser.add_argument('--prefix-key', default='',
help="Prefix used for the shadir URL, not a cryptografic key.")
parser.add_argument('--suffix-key', default='',
help="Suffix used for the shadir URL, not a cryptografic key.")
parser.add_argument('--url', help=url_help)
return parser
......@@ -498,6 +500,8 @@ def cmd_upload(*args):
"%s If not given, the uploaded data is not indexed." % key_help)
parser.add_argument('--file',
help="Upload the contents of this file, overriding --url")
parser.add_argument('--id',
help="Identifier used for the shadir URL. Overriding --prefix-key, --suffix-key and --url")
parser.add_argument('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.")
args = parser.parse_args(args or sys.argv[1:])
......@@ -506,7 +510,7 @@ def cmd_upload(*args):
try:
if args.file:
f = open(args.file, 'rb')
if not args.url:
if not args.url and not args.id:
nc.upload(f)
return
elif args.url:
......@@ -514,21 +518,31 @@ def cmd_upload(*args):
else:
parser.error('either --file or --url is required')
kw = dict(x.split('=', 1) for x in args.meta)
kw.setdefault('url', args.url)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, **kw)
if args.id:
kw.setdefault('id', args.id)
key = args.id
else:
kw.setdefault('url', args.url)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
key = args.prefix_key + urlmd5 + args.suffix_key
nc.upload(f, key, **kw)
finally:
f is None or f.close()
def cmd_download(*args):
parser = _newArgumentParser("URL of data to download." + key_help)
parser.add_argument('--id',
help="Identifier of the shadir URL, overriding --prefix-key and --suffix-key.")
parser.add_argument('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.")
args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.config)
kw = dict(x.split('=', 1) for x in args.meta)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
key = args.prefix_key + urlmd5 + args.suffix_key
if args.id:
key = args.id
else:
urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
key = args.prefix_key + urlmd5 + args.suffix_key
f = sys.stdout
shutil.copyfileobj(nc.download(next(nc.select(key, kw))['sha512']),
getattr(f, 'buffer', f))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment