Commit 4dee0c16 authored by Julien Muchembled's avatar Julien Muchembled

cli: add necessary options to upload a pypi-index file

The commands remain difficult to use. Maybe we should move some slapos.buildout
code here so that the required information can be computed automatically.
parent 1fa91b70
...@@ -348,24 +348,26 @@ class NetworkcacheException(Exception): ...@@ -348,24 +348,26 @@ class NetworkcacheException(Exception):
DirectoryNotFound = UploadError = NetworkcacheException # BBB DirectoryNotFound = UploadError = NetworkcacheException # BBB
def _newArgumentParser(): def _newArgumentParser(url_help):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('slapos_config', type=argparse.FileType('r'), 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('--suffix-key', default='')
parser.add_argument('--url', required=True, help=url_help
+ " The key will be concatenation of PREFIX_KEY, md5(URL) and SUFFIX_KEY.")
return parser return parser
def cmd_upload(*args): def cmd_upload(*args):
parser = _newArgumentParser() parser = _newArgumentParser("Upload data pointed to by this argument,"
parser.add_argument('--prefix-key', default='', " unless --file is specified. If argument is not a local path, contents"
help="Key will be concatenation of PREFIX_KEY and md5(URL).") " is first downloaded in a temporary file.")
parser.add_argument('--url', required=True,
help="Upload data pointed to by this argument, unless --file is specified."
" If argument is not a local path, contents is first downloaded"
" in a temporary file.")
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('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.")
args = parser.parse_args(args or sys.argv[1:]) args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.slapos_config) nc = NetworkcacheClient(args.config)
f = None f = None
try: try:
if args.file: if args.file:
...@@ -375,18 +377,15 @@ def cmd_upload(*args): ...@@ -375,18 +377,15 @@ def cmd_upload(*args):
else: else:
f = urllib2.urlopen(args.url) f = urllib2.urlopen(args.url)
urlmd5 = hashlib.md5(args.url).hexdigest() urlmd5 = hashlib.md5(args.url).hexdigest()
nc.upload(f, args.prefix_key + urlmd5, urlmd5=urlmd5, nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, urlmd5=urlmd5,
file_name=os.path.basename(args.url)) file_name=os.path.basename(args.url) or "file",
**dict(x.split('=', 1) for x in args.meta))
finally: finally:
f is None or f.close() f is None or f.close()
def cmd_download(*args): def cmd_download(*args):
parser = _newArgumentParser() parser = _newArgumentParser("URL of data to download.")
parser.add_argument('--prefix-key', default='',
help="Key will be concatenation of PREFIX_KEY and md5(URL).")
parser.add_argument('--url', required=True,
help="URL of data to download.")
args = parser.parse_args(args or sys.argv[1:]) args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.slapos_config) nc = NetworkcacheClient(args.config)
key = args.prefix_key + hashlib.md5(args.url).hexdigest() key = args.prefix_key + hashlib.md5(args.url).hexdigest() + args.suffix_key
shutil.copyfileobj(nc.download(nc.select(key).next()['sha512']), sys.stdout) shutil.copyfileobj(nc.download(nc.select(key).next()['sha512']), sys.stdout)
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