Coverage for /builds/kinetik161/ase/ase/cli/nomad.py: 24.32%

37 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +0000

1# Note: 

2# Try to avoid module level import statements here to reduce 

3# import time during CLI execution 

4 

5 

6class CLICommand: 

7 """Upload files to NOMAD. 

8 

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. 

12 

13 To get an authentication token, you create a Nomad repository account 

14 and use the 'Uploads' button on that page while logged in: 

15 

16 https://repository.nomad-coe.eu/ 

17 """ 

18 

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') 

31 

32 @staticmethod 

33 def run(args): 

34 import os 

35 import os.path as op 

36 import subprocess 

37 

38 dotase = op.expanduser('~/.ase') 

39 tokenfile = op.join(dotase, 'nomad-token') 

40 

41 if args.token: 

42 token = args.token 

43 if not args.no_save_token: 

44 if not op.isdir(dotase): 

45 os.mkdir(dotase) 

46 with open(tokenfile, 'w') as fd: 

47 print(token, file=fd) 

48 os.chmod(tokenfile, 0o600) 

49 print('Wrote token to', tokenfile) 

50 else: 

51 try: 

52 with open(tokenfile) as fd: 

53 token = fd.readline().strip() 

54 except OSError as err: # py2/3 discrepancy 

55 from ase.cli.main import CLIError 

56 msg = ('Could not find authentication token in {}. ' 

57 'Use the --token option to specify a token. ' 

58 'Original error: {}' 

59 .format(tokenfile, err)) 

60 raise CLIError(msg) 

61 

62 cmd = ('tar cf - {} | ' 

63 'curl -XPUT -# -HX-Token:{} ' 

64 '-N -F file=@- http://nomad-repository.eu:8000 | ' 

65 'xargs echo').format(' '.join(args.folders), token) 

66 

67 if not args.folders: 

68 print('No folders specified -- another job well done!') 

69 elif args.dry_run: 

70 print(cmd) 

71 else: 

72 print('Uploading {} folder{} ...' 

73 .format(len(args.folders), 

74 's' if len(args.folders) != 1 else '')) 

75 subprocess.check_call(cmd, shell=True)