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