1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | import os |
---|
4 | import sys |
---|
5 | import shutil |
---|
6 | |
---|
7 | default_location = "/usr/local/sh" |
---|
8 | downloads = { |
---|
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 | |
---|
13 | def 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 | |
---|
35 | def 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 | |
---|
41 | def 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 | |
---|
63 | def 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 | |
---|
117 | def 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 | |
---|
142 | def 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 | |
---|
148 | def 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 | |
---|
154 | # in fact only necessary in root install mode |
---|
155 | os.chmod(trt, 0755) |
---|
156 | |
---|
157 | def abort(text=None): |
---|
158 | if not text: |
---|
159 | text = "Installation aborted by user." |
---|
160 | |
---|
161 | quit(text) |
---|
162 | |
---|
163 | def query(text): |
---|
164 | """ |
---|
165 | Query user. |
---|
166 | """ |
---|
167 | return raw_input(text) |
---|
168 | |
---|
169 | if __name__ == "__main__": |
---|
170 | from optparse import OptionParser |
---|
171 | parser = OptionParser() |
---|
172 | parser.add_option("--debug", dest="debug", action="store_true", |
---|
173 | help="Turn on debug option.", default=False) |
---|
174 | |
---|
175 | parser.add_option("--locsat", dest="locsat", action="store", |
---|
176 | help="Overwrite locsat installation source.", |
---|
177 | default=downloads["locsat"]) |
---|
178 | |
---|
179 | parser.add_option("--fk", dest="fk", action="store", |
---|
180 | help="Overwrite fk package installation source.", |
---|
181 | default=downloads["fk"]) |
---|
182 | |
---|
183 | (options, args) = parser.parse_args() |
---|
184 | |
---|
185 | setup(**vars(options)) |
---|