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

Revision 365, 4.6 KB checked in by marcus, 12 years ago (diff)

r198 | walther | 2011-04-06 16:58:44 +0200 (Mi, 06 Apr 2011) | 2 lines

Minor fixes.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1#! /usr/bin/env python
2
3import os
4import sys
5import shutil
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 "="*70
37    print "  Setup program v0.1 for Seismic Handler (SH/SHM)"
38    print "="*70
39    print "\n This piece of software will try to setup the included source code.\n"
40
41def download(target, pkg, dbg):
42    print "Downloading %s..." % pkg
43
44    verbose = "-q "
45    if dbg:
46        verbose = ""
47        print downloads[pkg]
48
49    if downloads[pkg].split(":")[0].lower() in ["http", "ftp"]:
50        error = os.system("wget %s-P %s %s" % (verbose, target, downloads[pkg]))
51    else:
52        # try to copy file
53        error = True
54        try:
55            shutil.copy(downloads[pkg], target)
56            error = False
57        except IOError:
58            pass
59   
60    if error:
61        abort("Cannot access %s. Exit here." % pkg)
62
63def setup(**kwargs):
64    """
65    Setup Seismic Handler from source.
66    """
67    banner()
68    dbg = kwargs["debug"]
69
70    data = fetchinfo(debug=dbg)
71    if dbg:
72        print data
73
74    if not data["root"]:
75        res = query("You have no administrative rights. "
76                    "Perform user installation [Yn]? ")
77        if res.lower() == "n":
78            abort()
79
80    while 1:
81        target = query("Installation directory [%s, q = exit]: " % data["shcroot"])
82        if target.lower() == "q":
83            abort()
84
85        if not target:
86            target = data["shcroot"]
87
88        if not os.path.exists(target):
89            try:
90                os.mkdir(target)
91            except Exception, e:
92                if dbg:
93                    print e
94
95                print "Cannot create target directory: %s" % target
96                continue
97
98        break
99
100    if dbg:
101        print "Installation target: %s" % target
102
103    copy(os.path.join(data["pwd"], ".."), os.path.join(target, "sh"), dbg)
104   
105    for pkg in ["locsat", "fk"]:
106        downloads[pkg] = kwargs.get(pkg)
107        download(target, pkg, dbg)
108
109    exitcode = install(target, dbg)
110    if exitcode == 99:
111        abort("Installation aborted.")
112    elif exitcode:
113        abort("Installation errors occured. Please check output.")
114
115    customize(target, dbg)
116
117def customize(target, dbg):
118    """
119    Offers usage of new configuration files.
120
121    Save user defined configuration files, if choosen.
122    """
123
124    flist = ["STATINF.DAT", "sensitivities.txt", "filter_lookup.txt"]
125
126    print "\nSeismic Handler comes with updated station configuration files."
127    a = raw_input("Do you want to use them (backup will be saved) [Yn]? ")
128
129    if a == "n":
130        print "\nPlease check the following files manually (sh/inputs):"
131        for f in flist:
132            print f, "vs.", f+".dist"
133        return
134
135    for f in flist:
136        tname = os.path.join(target, "sh/inputs", f)
137        if os.path.exists(tname):
138            print "Backing up %s... (to %s)" % (f, f+".yours")
139            os.system("cp %s %s" % (tname, tname+".yours"))
140        os.system("cp %s %s" % (tname+".dist", tname))
141
142def install(target, dbg):
143    exitcode = \
144         os.system(os.path.join(target, "sh/setup/SHM-install.csh %s" % target))
145
146    return exitcode >> 8
147
148def copy(src, trt, dbg=False):
149    if dbg:
150        os.system("cp -rvd %s %s" % (src, trt))
151    else:
152        os.system("cp -rd %s %s" % (src, trt))
153
154def abort(text=None):
155    if not text:
156        text = "Installation aborted by user."
157
158    quit(text)
159
160def query(text):
161    """
162    Query user.
163    """
164    return raw_input(text)
165
166if __name__ == "__main__":
167    from optparse import OptionParser
168    parser = OptionParser()
169    parser.add_option("--debug", dest="debug", action="store_true",
170                      help="Turn on debug option.", default=False)
171
172    parser.add_option("--locsat", dest="locsat", action="store",
173                      help="Overwrite locsat installation source.",
174                      default=downloads["locsat"])
175
176    parser.add_option("--fk", dest="fk", action="store",
177                      help="Overwrite fk package installation source.",
178                      default=downloads["fk"])
179
180    (options, args) = parser.parse_args()
181
182    setup(**vars(options))
Note: See TracBrowser for help on using the repository browser.