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

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

parent c9337a3b
...@@ -486,8 +486,10 @@ def _newArgumentParser(url_help): ...@@ -486,8 +486,10 @@ def _newArgumentParser(url_help):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--config', type=argparse.FileType('r'), required=True, parser.add_argument('--config', type=argparse.FileType('r'), required=True,
help='SlapOS configuration file.') help='SlapOS configuration file.')
parser.add_argument('--prefix-key', default='') parser.add_argument('--prefix-key', default='',
parser.add_argument('--suffix-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) parser.add_argument('--url', help=url_help)
return parser return parser
...@@ -498,6 +500,8 @@ def cmd_upload(*args): ...@@ -498,6 +500,8 @@ def cmd_upload(*args):
"%s If not given, the uploaded data is not indexed." % key_help) "%s If not given, the uploaded data is not indexed." % key_help)
parser.add_argument('--file', parser.add_argument('--file',
help="Upload the contents of this file, overriding --url") 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', parser.add_argument('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.") help="Extra metadata.")
args = parser.parse_args(args or sys.argv[1:]) args = parser.parse_args(args or sys.argv[1:])
...@@ -506,7 +510,7 @@ def cmd_upload(*args): ...@@ -506,7 +510,7 @@ def cmd_upload(*args):
try: try:
if args.file: if args.file:
f = open(args.file, 'rb') f = open(args.file, 'rb')
if not args.url: if not args.url and not args.id:
nc.upload(f) nc.upload(f)
return return
elif args.url: elif args.url:
...@@ -514,21 +518,31 @@ def cmd_upload(*args): ...@@ -514,21 +518,31 @@ def cmd_upload(*args):
else: else:
parser.error('either --file or --url is required') parser.error('either --file or --url is required')
kw = dict(x.split('=', 1) for x in args.meta) kw = dict(x.split('=', 1) for x in args.meta)
kw.setdefault('url', args.url) if args.id:
urlmd5 = hashlib.md5(args.url.encode()).hexdigest() kw.setdefault('id', args.id)
nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, **kw) 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: finally:
f is None or f.close() f is None or f.close()
def cmd_download(*args): def cmd_download(*args):
parser = _newArgumentParser("URL of data to download." + key_help) 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', parser.add_argument('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.") help="Extra metadata.")
args = parser.parse_args(args or sys.argv[1:]) args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.config) nc = NetworkcacheClient(args.config)
kw = dict(x.split('=', 1) for x in args.meta) kw = dict(x.split('=', 1) for x in args.meta)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest() if args.id:
key = args.prefix_key + urlmd5 + args.suffix_key key = args.id
else:
urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
key = args.prefix_key + urlmd5 + args.suffix_key
f = sys.stdout f = sys.stdout
shutil.copyfileobj(nc.download(next(nc.select(key, kw))['sha512']), shutil.copyfileobj(nc.download(next(nc.select(key, kw))['sha512']),
getattr(f, 'buffer', f)) 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