-
Notifications
You must be signed in to change notification settings - Fork 96
/
get_exp_data.py
executable file
·72 lines (53 loc) · 1.98 KB
/
get_exp_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import re
import argparse
import tempfile
import subprocess as sp
base_url = 'https://climate-cms.nci.org.au/repository/entry/{}/Data+Repository/Other+Data+at+NCI/MOM+Test+Data/'
def get_file_list(verbose=False):
tmp_f = tempfile.NamedTemporaryFile()
out = sp.check_output(['wget', '-O', tmp_f.name, base_url.format('show')],
stderr=sp.STDOUT)
if verbose:
print(out, file=sys.stderr)
filenames = re.findall('MOM Test Data/(.+?\.tar\.gz)', tmp_f.read())
tmp_f.close()
assert(filenames != [])
return filenames
def main():
parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='?', default=None, help="""
Name of the file to get.""")
parser.add_argument('--list', action='store_true', default=False,
help="List all available files.")
parser.add_argument('--verbose', action='store_true', default=False,
help='Verbose output')
args = parser.parse_args()
if args.list:
available_files = get_file_list(args.verbose)
print('\n'.join(available_files))
return 0
if args.filename is None:
parser.print_help()
return 1
# Set up file source and destination
my_dir = os.path.dirname(os.path.realpath(__file__))
dest_dir = os.path.join(my_dir, 'archives')
dest = os.path.join(dest_dir, args.filename)
src = base_url.format('get') + args.filename
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
if os.path.exists(dest):
print('Error: destination {} already exists.'.format(dest))
return 1
ret = sp.call(['wget', '-P', dest_dir, src])
if ret != 0:
print('Error: wget of {} failed. Does it exist?'.format(args.filename),
file=sys.stderr)
parser.print_help()
return ret
if __name__ == '__main__':
sys.exit(main())