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

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

r179 | walther | 2011-03-03 17:35:27 +0100 (Do, 03 Mär 2011) | 1 line

...

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