source: SH_SHM/trunk/setup/setup.py @ 339

Revision 339, 3.7 KB checked in by marcus, 12 years ago (diff)

r178 | walther | 2011-03-03 16:51:56 +0100 (Do, 03 Mär 2011) | 1 line

Backup and replacement of existing station configuration.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1import os.path
2#! /usr/bin/env python
3
4import os
5import sys
6
7default_location = "/usr/local/sh"
8downloads = {
9    "locsat": "ftp://ftp.szgrf.bgr.de/pub/software/locsat-linux.tar.gz",
10    "fk": "ftp://ftp.szgrf.bgr.de/pub/software/fk-linux.tar.gz"
11}
12
13def fetchinfo(**kwargs):
14    """
15    Fetch information about user etc.
16    """
17    dbg = kwargs["debug"]
18
19    home = os.getenv("HOME")
20    pwd = os.path.realpath(os.path.split(sys.argv[0])[0])
21    tmp = os.getenv("TEMP")
22    if not tmp:
23        tmp = pwd
24
25    uid = os.getuid()
26    root = uid == 0
27    if root:
28        shcroot = default_location
29    else:
30        shcroot = os.path.join(home, "sh")
31
32    del kwargs
33    return locals()
34
35def banner():
36    print "="*78
37    print "  Setup program v0.1 for Seismic Handler (SH/SHM)"
38    print "="*78
39    print "\n This piece of software will try to setup the included source code.\n"
40
41def download(target, pkg):
42    print "Downloading %s..." % pkg
43    os.system("wget -P %s %s" % (target, downloads[pkg]))
44
45def setup(**kwargs):
46    """
47    Setup Seismic Handler from source.
48    """
49    banner()
50    dbg = kwargs["debug"]
51
52    data = fetchinfo(debug=dbg)
53    if dbg:
54        print data
55
56    if not data["root"]:
57        res = query("You have no administrative rights. "
58                    "Perform user installation [Yn]? ")
59        if res.lower() == "n":
60            abort()
61
62    while 1:
63        target = query("Installation directory [%s, q = exit]: " % data["shcroot"])
64        if target.lower() == "q":
65            abort()
66
67        if not target:
68            target = data["shcroot"]
69
70        if not os.path.exists(target):
71            try:
72                os.mkdir(target)
73            except Exception, e:
74                if dbg:
75                    print e
76
77                print "Cannot create target directory: %s" % target
78                continue
79
80        break
81
82    if dbg:
83        print "Installation target: %s" % target
84
85    copy(os.path.join(data["pwd"], ".."), os.path.join(target, "sh"), dbg)
86    download(target, "locsat")
87    download(target, "fk")
88
89    install(target)
90    customize(target)
91
92def customize(target):
93    """
94    Offers usage of new configuration files.
95
96    Save user defined configuration files, if choosen.
97    """
98
99    flist = ["STATINF.DAT", "sensitivities.txt", "filter_lookup.txt"]
100
101    print "A backup of your shm-config.txt has been saved under"
102    print " %s/sh/inputs/shm-config.txt.yours" % target
103
104    print "Seismic Handler comes with updated station configuration files."
105    a = raw_input("Do you want to use them (backup will be saved) [Yn]? ")
106
107    if a == "n":
108        print "Please check the following files manually (sh/inputs):"
109        for f in flist:
110            print f, "vs.", f+".dist"
111        return
112
113    for f in flist:
114        tname = os.pathjoin(target, f)
115        if os.path.exists(tname):
116            print "Backing up %s... (to %s)" % (f, f+".yours")
117            os.system("cp %s %s" % (tname, tname+".yours"))
118        os.system("cp %s %s" % (tname+".dist", tname))
119
120def install(target):
121    os.system(os.path.join(target, "sh/setup/SHM-install.csh %s" % target))
122
123def copy(src, trt, dbg=False):
124    if dbg:
125        os.system("cp -rvd %s %s" % (src, trt))
126    else:
127        os.system("cp -rd %s %s" % (src, trt))
128
129def abort(text=None):
130    if not text:
131        text = "Installation aborted by user."
132
133    quit(text)
134
135def query(text):
136    """
137    Query user.
138    """
139    return raw_input(text)
140
141if __name__ == "__main__":
142    from optparse import OptionParser
143    parser = OptionParser()
144    parser.add_option("--debug", dest="debug", action="store_true",
145                      help="Turn on debug option.", default=False)
146
147    (options, args) = parser.parse_args()
148
149    setup(**vars(options))
Note: See TracBrowser for help on using the repository browser.