"""
A more interactive version of vp_upload
"""
#Note that the imports are done as late as possible to improve the speed of
#the command line.
import sys
import pathlib
import os
import logging
import pygments
from prompt_toolkit.shortcuts import prompt
from reportengine import colors
from validphys.promptutils import confirm
log = logging.getLogger()
log.setLevel(logging.INFO)
log.addHandler(colors.ColorHandler())
[docs]
def handle_single_file(filename):
import tempfile
out = pathlib.Path(tempfile.mkdtemp(prefix='vp-upload'))
filename = pathlib.Path(filename)
p = out / filename.name
p.symlink_to(filename.absolute())
return out, filename.name
[docs]
def edit_settings(d):
title = d.get('title', '')
author = d.get('author', '')
keywords = d.get('keywords', '')
d['title'] = prompt('title: ', default=title)
if not author:
try:
import pwd
except ImportError:
pass
else:
author = pwd.getpwuid(os.getuid())[4]
d['author'] = prompt("author: ", default=author)
kwinp = prompt("keywords: ", default=','.join(keywords))
d['keywords'] = [k.strip() for k in kwinp.split(',') if k]
[docs]
def main():
import argparse
parser = argparse.ArgumentParser(description="Upload output to the NNPDF server.")
parser.add_argument("output", help="Folder to upload.")
args = parser.parse_args()
output = pathlib.Path(args.output)
upload_output = output
from validphys import uploadutils
if not output.is_dir():
if output.is_file():
upargs = handle_single_file(output)
upload_output = upargs[0]
uploader = uploadutils.ReportFileUploader()
else:
if not output.exists():
log.error(f"No such file or directory: {output}")
else:
log.errorr(f"{output} is not a file or a directory")
sys.exit(1)
else:
uploader = uploadutils.ReportUploader()
upargs = output
try:
with uploader.upload_or_exit_context(upargs):
handle_meta_interactive(upload_output)
except (KeyboardInterrupt, EOFError):
print(colors.t.bold_red("\nInterrupted by user. Exiting."), file=sys.stderr)
exit(1)
if __name__ == '__main__':
main()