1 | #!/usr/bin/env python
|
---|
2 | import urllib2
|
---|
3 | import os
|
---|
4 | import tarfile
|
---|
5 |
|
---|
6 | import shutil
|
---|
7 |
|
---|
8 | name = 'asap_data.tar.bz2'
|
---|
9 | dataurl = "http://www.atnf.csiro.au/pub/software/asap/data/"
|
---|
10 | tmpdata = '/tmp/asap_data.tar.bz2'
|
---|
11 | tmpmd5 = tmpdata+'.md5sum'
|
---|
12 |
|
---|
13 | print "Checking if an update is required."
|
---|
14 | md5req = urllib2.Request(dataurl+name+'.md5sum')
|
---|
15 | response = urllib2.urlopen(md5req)
|
---|
16 | md5file = file(tmpmd5,'w')
|
---|
17 | md5file.write(response.read())
|
---|
18 | md5file.close()
|
---|
19 | md5file = file(tmpmd5)
|
---|
20 | md5new = md5file.readlines()[0].split()[0]
|
---|
21 |
|
---|
22 | # use ASAPDATA if set - allows non-root update
|
---|
23 | if os.environ.has_key("ASAPDATA"):
|
---|
24 | asapbase = os.environ["ASAPDATA"]
|
---|
25 | else:
|
---|
26 | import asap
|
---|
27 | # get asap module path
|
---|
28 | asapbase = asap.__path__[0]
|
---|
29 | try:
|
---|
30 | fl = os.path.join(asapbase, name+".md5sum")
|
---|
31 | data_md5 = file(fl)
|
---|
32 | ls = data_md5.readlines()[0]
|
---|
33 | data_md5.close()
|
---|
34 | md5old = ls.split()[0]
|
---|
35 | except IOError:
|
---|
36 | md5old =''
|
---|
37 |
|
---|
38 | if md5new != md5old:
|
---|
39 | dreq = urllib2.Request(dataurl+name)
|
---|
40 | src = urllib2.urlopen(dreq)
|
---|
41 | print "Update required. Downloading asap data archive...."
|
---|
42 | dst = open(tmpdata, 'w')
|
---|
43 | dst.write(src.read())
|
---|
44 | dst.close()
|
---|
45 | os.chdir(asapbase)
|
---|
46 | tf = tarfile.TarFile.bz2open(tmpdata)
|
---|
47 | print "Extracting data archive in %s.." % asapbase
|
---|
48 | for member in tf.getmembers():
|
---|
49 | tf.extract(member)
|
---|
50 | shutil.copy(tmpmd5, asapbase)
|
---|
51 | else:
|
---|
52 | print """Data already at latest available version.
|
---|
53 | If you still get errors running asap, please report this."""
|
---|