Changeset 3138 for trunk/bin/asap_update_data
- Timestamp:
- 03/05/19 20:52:06 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bin/asap_update_data
r2792 r3138 1 1 #!/usr/bin/env python 2 from __future__ import print_function 2 3 import os 4 import sys 5 import tempfile 6 import urllib 7 import errno 8 import shutil 3 9 import tarfile 4 import shutil 5 from ftplib import FTP 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) 6 31 7 32 # 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] 33 md5suff = '.md5sum' 34 name = 'asap_data.tar.bz2' 35 dataurl = "ftp://ftp.atnf.csiro.au" 36 datadir = 'pub/software/asap/data' 22 37 23 38 # use ASAPDATA if set - allows non-root update … … 28 43 # get asap module path 29 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 30 71 try: 31 72 fl = os.path.join(asapbase, name+md5suff) … … 37 78 md5old ='' 38 79 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 os.remove(tmpdata) 49 os.remove(tmpmd5) 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() 50 96 else: 51 print """Data already at latest available version. 52 If you still get errors running asap, please report this.""" 53 ftp.quit() 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
Note:
See TracChangeset
for help on using the changeset viewer.