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