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

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

r192 | walther | 2011-03-31 17:30:28 +0200 (Do, 31 Mär 2011) | 4 lines

  • locsat and fk source may be defined at command line
  • replacing makedepend by gccmakedep (bug not fixed for 5 years in Xorg)
  • added include to sysbase.h for proper macro definition
  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[329]1#! /usr/bin/env python
2
3import os
4import sys
[358]5import shutil
[329]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
[338]24
[329]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():
[343]36    print "="*70
[338]37    print "  Setup program v0.1 for Seismic Handler (SH/SHM)"
[343]38    print "="*70
[329]39    print "\n This piece of software will try to setup the included source code.\n"
40
[343]41def download(target, pkg, dbg):
[332]42    print "Downloading %s..." % pkg
[329]43
[358]44    verbose = ""
45    if dbg:
46        verbose = "-q "
47        print downloads[pkg]
[343]48
[358]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)
[343]62
[329]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
[338]94
[329]95                print "Cannot create target directory: %s" % target
96                continue
97
98        break
99
[332]100    if dbg:
101        print "Installation target: %s" % target
102
[331]103    copy(os.path.join(data["pwd"], ".."), os.path.join(target, "sh"), dbg)
[358]104   
105    for pkg in ["locsat", "fk"]:
106        downloads[pkg] = kwargs.get(pkg)
107        download(target, pkg, dbg)
[329]108
[343]109    exitcode = install(target, dbg)
110    if exitcode == 99:
111        abort("Installation aborted.")
112    elif exitcode:
113        abort("Installation errors occured. Please check output.")
[331]114
[343]115    customize(target, dbg)
116
117def customize(target, dbg):
[339]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
[340]126    print "\nSeismic Handler comes with updated station configuration files."
[339]127    a = raw_input("Do you want to use them (backup will be saved) [Yn]? ")
128
129    if a == "n":
[340]130        print "\nPlease check the following files manually (sh/inputs):"
[339]131        for f in flist:
132            print f, "vs.", f+".dist"
133        return
134
135    for f in flist:
[340]136        tname = os.path.join(target, "sh/inputs", f)
[339]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
[343]142def install(target, dbg):
143    exitcode = \
144         os.system(os.path.join(target, "sh/setup/SHM-install.csh %s" % target))
[331]145
[343]146    return exitcode >> 8
147
[329]148def copy(src, trt, dbg=False):
149    if dbg:
[332]150        os.system("cp -rvd %s %s" % (src, trt))
[329]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
[358]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
[329]180    (options, args) = parser.parse_args()
181
182    setup(**vars(options))
Note: See TracBrowser for help on using the repository browser.