Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import os
2import os.path as op
3import subprocess
6class CLICommand:
7 """Upload files to NOMAD.
9 Upload all data within specified folders to the Nomad repository
10 using authentication token given by the --token option or,
11 if no token is given, the token stored in ~/.ase/nomad-token.
13 To get an authentication token, you create a Nomad repository account
14 and use the 'Uploads' button on that page while logged in:
16 https://repository.nomad-coe.eu/
17 """
19 @staticmethod
20 def add_arguments(parser):
21 parser.add_argument('folders', nargs='*', metavar='folder')
22 parser.add_argument('-t', '--token',
23 help='Use given authentication token and save '
24 'it to ~/.ase/nomad-token unless '
25 '--no-save-token')
26 parser.add_argument('-n', '--no-save-token', action='store_true',
27 help='do not save the token if given')
28 parser.add_argument('-0', '--dry-run', action='store_true',
29 help='print command that would upload files '
30 'without uploading anything')
32 @staticmethod
33 def run(args):
34 dotase = op.expanduser('~/.ase')
35 tokenfile = op.join(dotase, 'nomad-token')
37 if args.token:
38 token = args.token
39 if not args.no_save_token:
40 if not op.isdir(dotase):
41 os.mkdir(dotase)
42 with open(tokenfile, 'w') as fd:
43 print(token, file=fd)
44 os.chmod(tokenfile, 0o600)
45 print('Wrote token to', tokenfile)
46 else:
47 try:
48 with open(tokenfile) as fd:
49 token = fd.readline().strip()
50 except OSError as err: # py2/3 discrepancy
51 from ase.cli.main import CLIError
52 msg = ('Could not find authentication token in {}. '
53 'Use the --token option to specify a token. '
54 'Original error: {}'
55 .format(tokenfile, err))
56 raise CLIError(msg)
58 cmd = ('tar cf - {} | '
59 'curl -XPUT -# -HX-Token:{} '
60 '-N -F file=@- http://nomad-repository.eu:8000 | '
61 'xargs echo').format(' '.join(args.folders), token)
63 if not args.folders:
64 print('No folders specified -- another job well done!')
65 elif args.dry_run:
66 print(cmd)
67 else:
68 print('Uploading {} folder{} ...'
69 .format(len(args.folders),
70 's' if len(args.folders) != 1 else ''))
71 subprocess.check_call(cmd, shell=True)