1 | #!/usr/bin/env python
|
---|
2 | import urllib2
|
---|
3 | import os
|
---|
4 | import tarfile
|
---|
5 |
|
---|
6 | import asap
|
---|
7 | import shutil
|
---|
8 |
|
---|
9 | name = 'asap_data.tar.bz2'
|
---|
10 | dataurl = "http://www.atnf.csiro.au/pub/software/asap/data/"
|
---|
11 | tmpdata = '/tmp/asap_data.tar.bz2'
|
---|
12 | tmpmd5 = tmpdata+'.md5sum'
|
---|
13 |
|
---|
14 | print "Checking if an update is required."
|
---|
15 | md5req = urllib2.Request(dataurl+name+'.md5sum')
|
---|
16 | response = urllib2.urlopen(md5req)
|
---|
17 | md5file = file(tmpmd5,'w')
|
---|
18 | md5file.write(response.read())
|
---|
19 | md5file.close()
|
---|
20 | md5file = file(tmpmd5)
|
---|
21 | md5new = md5file.readlines()[0].split()[0]
|
---|
22 |
|
---|
23 | # get asap module path
|
---|
24 | asapbase = asap.__path__[0]
|
---|
25 | # use ASAPDATA if set - allows non-root update
|
---|
26 | if os.environ.has_key("ASAPDATA"):
|
---|
27 | asapbase = os.environ["ASAPDATA"]
|
---|
28 |
|
---|
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."""
|
---|