source: trunk/bin/asap_update_data

Last change on this file was 3138, checked in by VincentMcIntyre, 5 years ago

Manpage for asap_update_data

  • Property svn:executable set to *
File size: 2.7 KB
Line 
1#!/usr/bin/env python
2from __future__ import print_function
3import os
4import sys
5import tempfile
6import urllib
7import errno
8import shutil
9import tarfile
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)
31
32# globals
33md5suff  = '.md5sum'
34name     = 'asap_data.tar.bz2'
35dataurl  = "ftp://ftp.atnf.csiro.au"
36datadir  = 'pub/software/asap/data'
37
38# use ASAPDATA if set - allows non-root update
39if os.environ.has_key("ASAPDATA"):
40    asapbase = os.environ["ASAPDATA"]
41else:
42    import asap
43    # get asap module path
44    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
71try:
72    fl = os.path.join(asapbase, name+md5suff)
73    data_md5 = file(fl)
74    ls = data_md5.readlines()[0]
75    data_md5.close()
76    md5old = ls.split()[0]
77except IOError:
78    md5old =''
79
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()
96else:
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 TracBrowser for help on using the repository browser.