1 | #!/usr/bin/env python
|
---|
2 | from __future__ import print_function
|
---|
3 | import os
|
---|
4 | import sys
|
---|
5 | import tempfile
|
---|
6 | import urllib
|
---|
7 | import errno
|
---|
8 | import shutil
|
---|
9 | import tarfile
|
---|
10 |
|
---|
11 | def errormsg(args, **kwargs):
|
---|
12 | print(*args, file=sys.stderr, **kwargs)
|
---|
13 |
|
---|
14 | def isWritable(path):
|
---|
15 | try:
|
---|
16 | testfile = tempfile.TemporaryFile(dir = path)
|
---|
17 | testfile.close()
|
---|
18 | except OSError as e:
|
---|
19 | if e.errno == errno.EACCES: # 13
|
---|
20 | return False
|
---|
21 | e.filename = path
|
---|
22 | raise
|
---|
23 | return True
|
---|
24 |
|
---|
25 | def cleanup(inlist):
|
---|
26 | for item in inlist:
|
---|
27 | if os.path.isfile(item):
|
---|
28 | os.remove(item)
|
---|
29 | if os.path.isdir(item):
|
---|
30 | os.rmdir(item)
|
---|
31 |
|
---|
32 | # globals
|
---|
33 | md5suff = '.md5sum'
|
---|
34 | name = 'asap_data.tar.bz2'
|
---|
35 | dataurl = "ftp://ftp.atnf.csiro.au"
|
---|
36 | datadir = 'pub/software/asap/data'
|
---|
37 |
|
---|
38 | # use ASAPDATA if set - allows non-root update
|
---|
39 | if os.environ.has_key("ASAPDATA"):
|
---|
40 | asapbase = os.environ["ASAPDATA"]
|
---|
41 | else:
|
---|
42 | import asap
|
---|
43 | # get asap module path
|
---|
44 | asapbase = asap.__path__[0]
|
---|
45 |
|
---|
46 | print('asapbase is ' + asapbase)
|
---|
47 | # check we have write permission to asapbase before continuing
|
---|
48 | if not isWritable(asapbase):
|
---|
49 | errormsg(['No write access to %s, aborting ' % asapbase])
|
---|
50 | exit()
|
---|
51 |
|
---|
52 | # Ensure the file is read/write by the creator only
|
---|
53 | saved_umask = os.umask(0077)
|
---|
54 |
|
---|
55 | tmpdir = tempfile.mkdtemp()
|
---|
56 | md5path = os.path.join(tmpdir, name + md5suff)
|
---|
57 |
|
---|
58 | print("Checking if an update is required.")
|
---|
59 | url = dataurl + '/' + datadir + '/' + name + md5suff
|
---|
60 | try:
|
---|
61 | urllib.urlretrieve (url, md5path)
|
---|
62 | md5file = file(md5path)
|
---|
63 | md5new = md5file.readlines()[0].split()[0]
|
---|
64 | except IOError as e:
|
---|
65 | errormsg(['Download failed\n','IOError %s' % e.errno])
|
---|
66 | cleanup([md5path,tmpdir])
|
---|
67 | exit()
|
---|
68 | else:
|
---|
69 | print('Downloaded checksum file to ' + md5path)
|
---|
70 |
|
---|
71 | try:
|
---|
72 | fl = os.path.join(asapbase, name+md5suff)
|
---|
73 | data_md5 = file(fl)
|
---|
74 | ls = data_md5.readlines()[0]
|
---|
75 | data_md5.close()
|
---|
76 | md5old = ls.split()[0]
|
---|
77 | except IOError:
|
---|
78 | md5old =''
|
---|
79 |
|
---|
80 | if md5new == md5old:
|
---|
81 | print("""Data already at latest available version.
|
---|
82 | If you still get errors running asap, please report this.""")
|
---|
83 | cleanup([md5path,tmpdir])
|
---|
84 | exit()
|
---|
85 |
|
---|
86 | print("Update required. Downloading asap data archive....")
|
---|
87 | url = dataurl + '/' + datadir + '/' + name
|
---|
88 | tarpath = os.path.join(tmpdir, name)
|
---|
89 | urllib.urlcleanup()
|
---|
90 | try:
|
---|
91 | urllib.urlretrieve (url, tarpath)
|
---|
92 | except IOError as e:
|
---|
93 | errormsg(['Download failed\n','IOError %s' % e.errno])
|
---|
94 | cleanup([tarpath,md5path,tmpdir])
|
---|
95 | exit()
|
---|
96 | else:
|
---|
97 | print('Downloaded tar file to ' + tarpath)
|
---|
98 |
|
---|
99 | print("Extracting data archive in %s.." % asapbase)
|
---|
100 | os.umask(saved_umask)
|
---|
101 | shutil.copy(md5path, fl)
|
---|
102 | shutil.copy(tarpath, os.path.join(asapbase, name))
|
---|
103 | os.chdir(asapbase)
|
---|
104 | tf = tarfile.TarFile.bz2open(name)
|
---|
105 | for member in tf.getmembers():
|
---|
106 | tf.extract(member)
|
---|
107 |
|
---|
108 | cleanup([tarpath,md5path,tmpdir])
|
---|
109 | exit()
|
---|
110 |
|
---|