Changeset 3138 for trunk/bin


Ignore:
Timestamp:
03/05/19 20:52:06 (6 years ago)
Author:
VincentMcIntyre
Message:

Manpage for asap_update_data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/asap_update_data

    r2792 r3138  
    11#!/usr/bin/env python
     2from __future__ import print_function
    23import os
     4import sys
     5import tempfile
     6import urllib
     7import errno
     8import shutil
    39import tarfile
    4 import shutil
    5 from ftplib import FTP
     10
     11def errormsg(args, **kwargs):
     12    print(*args, file=sys.stderr, **kwargs)
     13
     14def 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
     25def 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)
    631
    732# 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]
     33md5suff  = '.md5sum'
     34name     = 'asap_data.tar.bz2'
     35dataurl  = "ftp://ftp.atnf.csiro.au"
     36datadir  = 'pub/software/asap/data'
    2237
    2338# use ASAPDATA if set - allows non-root update
     
    2843    # get asap module path
    2944    asapbase = asap.__path__[0]
     45
     46print('asapbase is ' + asapbase)
     47# check we have write permission to asapbase before continuing
     48if 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
     53saved_umask = os.umask(0077)
     54
     55tmpdir  = tempfile.mkdtemp()
     56md5path = os.path.join(tmpdir, name + md5suff)
     57
     58print("Checking if an update is required.")
     59url = dataurl + '/' + datadir + '/' + name + md5suff
     60try:
     61    urllib.urlretrieve (url, md5path)
     62    md5file = file(md5path)
     63    md5new = md5file.readlines()[0].split()[0]
     64except IOError as e:
     65    errormsg(['Download failed\n','IOError %s' % e.errno])
     66    cleanup([md5path,tmpdir])
     67    exit()
     68else:
     69    print('Downloaded checksum file to ' + md5path)
     70
    3071try:
    3172    fl = os.path.join(asapbase, name+md5suff)
     
    3778    md5old =''
    3879
    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)
     80if md5new == md5old:
     81    print("""Data already at latest available version.
     82If you still get errors running asap, please report this.""")
     83    cleanup([md5path,tmpdir])
     84    exit()
     85
     86print("Update required. Downloading asap data archive....")
     87url = dataurl + '/' + datadir + '/' + name
     88tarpath = os.path.join(tmpdir, name)
     89urllib.urlcleanup()
     90try:
     91     urllib.urlretrieve (url, tarpath)
     92except IOError as e:
     93    errormsg(['Download failed\n','IOError %s' % e.errno])
     94    cleanup([tarpath,md5path,tmpdir])
     95    exit()
    5096else:
    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
     99print("Extracting data archive in %s.." % asapbase)
     100os.umask(saved_umask)
     101shutil.copy(md5path, fl)
     102shutil.copy(tarpath, os.path.join(asapbase, name))
     103os.chdir(asapbase)
     104tf = tarfile.TarFile.bz2open(name)
     105for member in tf.getmembers():
     106    tf.extract(member)
     107
     108cleanup([tarpath,md5path,tmpdir])
     109exit()
     110
Note: See TracChangeset for help on using the changeset viewer.