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

Revision 520, 5.2 KB checked in by marcus, 11 years ago (diff)

r293 | walther | 2012-01-26 17:22:13 +0100 (Do, 26. Jan 2012) | 2 Zeilen

  • enable $HOME installation by default
  • 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, "bin")
31
32    del kwargs
33    return locals()
34
35def banner():
36    print "="*70
37    print "  Setup program 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 are not root (so maybe you haven't administrative rights).\n"
76                    "Perform user installation [Yn]? ")
77        if res.lower() == "n":
78            abort()
79
80    # override?
81    data["shcroot"] = kwargs.get("target", False) or data["shcroot"]
82
83    while 1:
84        target = query("Installation directory [%s, q = exit]: " % data["shcroot"])
85        if target.lower() == "q":
86            abort()
87
88        if not target:
89            target = data["shcroot"]
90
91        if not os.path.exists(target):
92            try:
93                os.mkdir(target)
94            except Exception, e:
95                if dbg:
96                    print e
97
98                print "Cannot create target directory: %s" % target
99                continue
100
101        break
102
103    if dbg:
104        print "Installation target: %s" % target
105
106    copy(os.path.join(data["pwd"], ".."), os.path.join(target, "sh"), dbg)
107
108    for pkg in ["locsat", "fk"]:
109        downloads[pkg] = kwargs.get(pkg)
110        download(target, pkg, dbg)
111
112    exitcode = install(target, dbg)
113    if exitcode == 99:
114        abort("Installation aborted.")
115    elif exitcode:
116        abort("Installation errors occured. Please check output.")
117
118    customize(target, dbg)
119
120def customize(target, dbg):
121    """
122    Offers usage of new configuration files.
123
124    Save user defined configuration files, if choosen.
125    """
126
127    flist = ["STATINF.DAT", "sensitivities.txt", "filter_lookup.txt"]
128
129    print "\nSeismic Handler comes with updated station configuration files."
130    a = raw_input("Do you want to use them (backup will be saved) [Yn]? ")
131
132    if a == "n":
133        print "\nPlease check the following files manually (sh/inputs):"
134        for f in flist:
135            print f, "vs.", f+".dist"
136        return
137
138    for f in flist:
139        tname = os.path.join(target, "sh/inputs", f)
140        if os.path.exists(tname):
141            print "Backing up %s... (to %s)" % (f, f+".yours")
142            os.system("cp %s %s" % (tname, tname+".yours"))
143        os.system("cp %s %s" % (tname+".dist", tname))
144
145def install(target, dbg):
146    exitcode = \
147         os.system(os.path.join(target, "sh/setup/SHM-install.csh %s" % target))
148
149    return exitcode >> 8
150
151def copy(src, trt, dbg=False):
152    if dbg:
153        os.system("cp -rvd %s %s" % (src, trt))
154    else:
155        os.system("cp -rd %s %s" % (src, trt))
156
157    # in fact only necessary in root install mode
158    os.chmod(trt, 0755)
159
160def abort(text=None):
161    if not text:
162        text = "Installation aborted by user."
163
164    quit(text)
165
166def query(text):
167    """
168    Query user.
169    """
170    return raw_input(text)
171
172if __name__ == "__main__":
173    from sys import version_info
174    if version_info < (2, 5):
175        import warnings
176        warnings.warn("Your installed python version is too old. This "
177                      "software is tested on python 2.5 and higher only!")
178
179    from optparse import OptionParser
180    parser = OptionParser()
181    parser.add_option("--debug", dest="debug", action="store_true",
182                      help="Turn on debug option.", default=False)
183
184    parser.add_option("--locsat", dest="locsat", action="store",
185                      help="Overwrite locsat installation source.",
186                      default=downloads["locsat"])
187
188    parser.add_option("--fk", dest="fk", action="store",
189                      help="Overwrite fk package installation source.",
190                      default=downloads["fk"])
191
192    parser.add_option("--target", dest="target", action="store",
193                      help="Override default install target.",
194                      default=None)
195
196    (options, args) = parser.parse_args()
197
198    setup(**vars(options))
Note: See TracBrowser for help on using the repository browser.