134
143
for s in t.split():
135
if (len(out)-out.rfind("\n")) + len(s) > width:
144
if (len(out) - out.rfind("\n")) + len(s) > width:
136
145
out += "\n" + subsequent_indent
140
150
def twrap(s, **kwargs):
142
152
paras = s.split("\n")
143
153
for par in paras:
144
154
s = wrap(par, **kwargs)
149
" return list of loaded modules (or [] if lsmod is not found) "
152
if not os.path.exists("/sbin/lsmod"):
154
p=subprocess.Popen(["/sbin/lsmod"], stdout=subprocess.PIPE,
155
universal_newlines=True)
156
lines=p.communicate()[0].split("\n")
157
# remove heading line: "Modules Size Used by"
159
# add lines to list, skip empty lines
162
modules.append(line.split()[0])
160
" return list of loaded modules (or [] if lsmod is not found) "
163
if not os.path.exists("/sbin/lsmod"):
165
p = subprocess.Popen(["/sbin/lsmod"], stdout=subprocess.PIPE,
166
universal_newlines=True)
167
lines = p.communicate()[0].split("\n")
168
# remove heading line: "Modules Size Used by"
170
# add lines to list, skip empty lines
173
modules.append(line.split()[0])
165
177
def check_and_fix_xbit(path):
166
" check if a given binary has the executable bit and if not, add it"
167
if not os.path.exists(path):
169
mode = S_IMODE(os.stat(path)[ST_MODE])
170
if not ((mode & S_IXUSR) == S_IXUSR):
171
os.chmod(path, mode | S_IXUSR)
178
" check if a given binary has the executable bit and if not, add it"
179
if not os.path.exists(path):
181
mode = S_IMODE(os.stat(path)[ST_MODE])
182
if not ((mode & S_IXUSR) == S_IXUSR):
183
os.chmod(path, mode | S_IXUSR)
173
186
def country_mirror():
174
" helper to get the country mirror from the current locale "
175
# special cases go here
176
lang_mirror = { 'c' : '',
179
if not 'LANG' in os.environ:
187
" helper to get the country mirror from the current locale "
188
# special cases go here
189
lang_mirror = {'c': ''}
191
if not 'LANG' in os.environ:
193
lang = os.environ['LANG'].lower()
194
# check if it is a special case
195
if lang[:5] in lang_mirror:
196
return lang_mirror[lang[:5]]
197
# now check for the most comon form (en_US.UTF-8)
199
country = lang.split(".")[0].split("_")[1]
201
country = country.split("@")[0]
204
return lang[:2] + "."
181
lang = os.environ['LANG'].lower()
182
# check if it is a special case
183
if lang[:5] in lang_mirror:
184
return lang_mirror[lang[:5]]
185
# now check for the most comon form (en_US.UTF-8)
187
country = lang.split(".")[0].split("_")[1]
189
country = country.split("@")[0]
196
" return the codename of the current runing distro "
197
# support debug overwrite
198
dist = os.environ.get("META_RELEASE_FAKE_CODENAME")
200
logging.warn("using fake release name '%s' (because of META_RELEASE_FAKE_CODENAME environment) " % dist)
202
# then check the real one
203
from subprocess import Popen, PIPE
204
p = Popen(["lsb_release","-c","-s"], stdout=PIPE, universal_newlines=True)
207
sys.stderr.write("lsb_release returned exitcode: %i\n" % res)
208
return "unknown distribution"
209
dist = p.stdout.readline().strip()
213
def get_dist_description():
214
" return the description of the current runing distro "
215
# support debug overwrite
216
desc = os.environ.get("META_RELEASE_FAKE_DESCRIPTION")
218
logging.warn("using fake release description '%s' (because of META_RELEASE_FAKE_DESCRIPTION environment) " % desc)
220
# then check the real one
221
from subprocess import Popen, PIPE
222
p = Popen(["lsb_release","-d","-s"], stdout=PIPE, universal_newlines=True)
225
sys.stderr.write("lsb_release returned exitcode: %i\n" % res)
226
return "unknown distribution"
227
desc = p.stdout.readline().strip()
209
" return the codename of the current runing distro "
210
# support debug overwrite
211
dist = os.environ.get("META_RELEASE_FAKE_CODENAME")
213
logging.warn("using fake release name '%s' (because of "
214
"META_RELEASE_FAKE_CODENAME environment) " % dist)
216
# then check the real one
217
from subprocess import Popen, PIPE
218
p = Popen(["lsb_release", "-c", "-s"], stdout=PIPE,
219
universal_newlines=True)
222
sys.stderr.write("lsb_release returned exitcode: %i\n" % res)
223
return "unknown distribution"
224
dist = p.stdout.readline().strip()
229
def get_dist_version():
230
" return the version of the current running distro "
231
# support debug overwrite
232
desc = os.environ.get("META_RELEASE_FAKE_VERSION")
234
logging.warn("using fake release version '%s' (because of "
235
"META_RELEASE_FAKE_VERSION environment) " % desc)
237
# then check the real one
238
from subprocess import Popen, PIPE
239
p = Popen(["lsb_release", "-r", "-s"], stdout=PIPE,
240
universal_newlines=True)
243
sys.stderr.write("lsb_release returned exitcode: %i\n" % res)
244
return "unknown distribution"
245
desc = p.stdout.readline().strip()
231
250
class HeadRequest(Request):
232
251
def get_method(self):
235
255
def url_downloadable(uri, debug_func=None):
237
helper that checks if the given uri exists and is downloadable
238
(supports optional debug_func function handler to support
241
Supports http (via HEAD) and ftp (via size request)
245
debug_func("url_downloadable: %s" % uri)
246
(scheme, netloc, path, querry, fragment) = urlsplit(uri)
247
debug_func("s='%s' n='%s' p='%s' q='%s' f='%s'" % (scheme, netloc, path, querry, fragment))
250
http_file = urlopen(HeadRequest(uri))
252
if http_file.code == 200:
255
except Exception as e:
256
debug_func("error from httplib: '%s'" % e)
258
elif scheme == "ftp":
261
f = ftplib.FTP(netloc)
263
f.cwd(os.path.dirname(path))
264
size = f.size(os.path.basename(path))
267
debug_func("ftplib.size() returned: %s" % size)
270
except Exception as e:
272
debug_func("error from ftplib: '%s'" % e)
257
helper that checks if the given uri exists and is downloadable
258
(supports optional debug_func function handler to support
261
Supports http (via HEAD) and ftp (via size request)
265
debug_func("url_downloadable: %s" % uri)
266
(scheme, netloc, path, querry, fragment) = urlsplit(uri)
267
debug_func("s='%s' n='%s' p='%s' q='%s' f='%s'" % (scheme, netloc, path,
271
http_file = urlopen(HeadRequest(uri))
273
if http_file.code == 200:
276
except Exception as e:
277
debug_func("error from httplib: '%s'" % e)
279
elif scheme == "ftp":
282
f = ftplib.FTP(netloc)
284
f.cwd(os.path.dirname(path))
285
size = f.size(os.path.basename(path))
288
debug_func("ftplib.size() returned: %s" % size)
291
except Exception as e:
293
debug_func("error from ftplib: '%s'" % e)
276
298
def init_proxy(gsettings=None):
277
""" init proxy settings
279
* first check for http_proxy environment (always wins),
280
* then check the apt.conf http proxy,
281
* then look into synaptics conffile
282
* then into gconf (if gconfclient was supplied)
284
SYNAPTIC_CONF_FILE = "/root/.synaptic/synaptic.conf"
286
# generic apt config wins
287
if apt_pkg.config.find("Acquire::http::Proxy") != '':
288
proxy = apt_pkg.config.find("Acquire::http::Proxy")
290
elif os.path.exists(SYNAPTIC_CONF_FILE):
291
cnf = apt_pkg.Configuration()
292
apt_pkg.read_config_file(cnf, SYNAPTIC_CONF_FILE)
293
use_proxy = cnf.find_b("Synaptic::useProxy", False)
295
proxy_host = cnf.find("Synaptic::httpProxy")
296
proxy_port = str(cnf.find_i("Synaptic::httpProxyPort"))
297
if proxy_host and proxy_port:
298
proxy = "http://%s:%s/" % (proxy_host, proxy_port)
301
# try: # see LP: #281248
302
# if gconfclient.get_bool("/system/http_proxy/use_http_proxy"):
303
# host = gconfclient.get_string("/system/http_proxy/host")
304
# port = gconfclient.get_int("/system/http_proxy/port")
305
# use_auth = gconfclient.get_bool("/system/http_proxy/use_authentication")
308
# auth_user = gconfclient.get_string("/system/http_proxy/authentication_user")
309
# auth_pw = gconfclient.get_string("/system/http_proxy/authentication_password")
310
# proxy = "http://%s:%s@%s:%s/" % (auth_user,auth_pw,host, port)
312
# proxy = "http://%s:%s/" % (host, port)
313
# except Exception as e:
314
# print("error from gconf: %s" % e)
315
# if we have a proxy, set it
318
if not re.match("http://\w+", proxy):
319
print("proxy '%s' looks invalid" % proxy, file=sys.stderr)
321
proxy_support = ProxyHandler({"http":proxy})
322
opener = build_opener(proxy_support)
323
install_opener(opener)
324
os.putenv("http_proxy",proxy)
299
""" init proxy settings
301
* first check for http_proxy environment (always wins),
302
* then check the apt.conf http proxy,
303
* then look into synaptics conffile
304
* then into gconf (if gconfclient was supplied)
306
SYNAPTIC_CONF_FILE = "/root/.synaptic/synaptic.conf"
308
# generic apt config wins
309
if apt_pkg.config.find("Acquire::http::Proxy") != '':
310
proxy = apt_pkg.config.find("Acquire::http::Proxy")
312
elif os.path.exists(SYNAPTIC_CONF_FILE):
313
cnf = apt_pkg.Configuration()
314
apt_pkg.read_config_file(cnf, SYNAPTIC_CONF_FILE)
315
use_proxy = cnf.find_b("Synaptic::useProxy", False)
317
proxy_host = cnf.find("Synaptic::httpProxy")
318
proxy_port = str(cnf.find_i("Synaptic::httpProxyPort"))
319
if proxy_host and proxy_port:
320
proxy = "http://%s:%s/" % (proxy_host, proxy_port)
321
# if we have a proxy, set it
324
if not re.match("http://\w+", proxy):
325
print("proxy '%s' looks invalid" % proxy, file=sys.stderr)
327
proxy_support = ProxyHandler({"http": proxy})
328
opener = build_opener(proxy_support)
329
install_opener(opener)
330
os.putenv("http_proxy", proxy)
327
334
def on_battery():
329
Check via dbus if the system is running on battery.
330
This function is using UPower per default, if UPower is not
331
available it falls-back to DeviceKit.Power.
335
bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
336
Check via dbus if the system is running on battery.
337
This function is using UPower per default, if UPower is not
338
available it falls-back to DeviceKit.Power.
337
devobj = bus.get_object('org.freedesktop.UPower',
338
'/org/freedesktop/UPower')
339
dev = dbus.Interface(devobj, 'org.freedesktop.DBus.Properties')
340
return dev.Get('org.freedesktop.UPower', 'OnBattery')
341
except dbus.exceptions.DBusException as e:
342
if e._dbus_error_name != 'org.freedesktop.DBus.Error.ServiceUnknown':
344
devobj = bus.get_object('org.freedesktop.DeviceKit.Power',
345
'/org/freedesktop/DeviceKit/Power')
346
dev = dbus.Interface(devobj, "org.freedesktop.DBus.Properties")
347
return dev.Get("org.freedesktop.DeviceKit.Power", "on_battery")
348
except Exception as e:
350
#print("on_battery returned error: ", e, file=sys.stderr)
342
bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
344
devobj = bus.get_object('org.freedesktop.UPower',
345
'/org/freedesktop/UPower')
346
dev = dbus.Interface(devobj, 'org.freedesktop.DBus.Properties')
347
return dev.Get('org.freedesktop.UPower', 'OnBattery')
348
except dbus.exceptions.DBusException as e:
349
error_unknown = 'org.freedesktop.DBus.Error.ServiceUnknown'
350
if e._dbus_error_name != error_unknown:
352
devobj = bus.get_object('org.freedesktop.DeviceKit.Power',
353
'/org/freedesktop/DeviceKit/Power')
354
dev = dbus.Interface(devobj, "org.freedesktop.DBus.Properties")
355
return dev.Get("org.freedesktop.DeviceKit.Power", "on_battery")
356
except Exception as e:
358
#print("on_battery returned error: ", e, file=sys.stderr)
353
362
def inhibit_sleep():
355
Send a dbus signal to power-manager to not suspend
356
the system, using the freedesktop common interface
360
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
361
devobj = bus.get_object('org.freedesktop.PowerManagement',
362
'/org/freedesktop/PowerManagement/Inhibit')
363
dev = dbus.Interface(devobj, "org.freedesktop.PowerManagement.Inhibit")
364
cookie = dev.Inhibit('UpdateManager', 'Updating system')
367
#print("could not send the dbus Inhibit signal: %s" % e)
368
return (False, False)
364
Send a dbus signal to power-manager to not suspend
365
the system, using the freedesktop common interface
369
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
370
devobj = bus.get_object('org.freedesktop.PowerManagement',
371
'/org/freedesktop/PowerManagement/Inhibit')
372
dev = dbus.Interface(devobj, "org.freedesktop.PowerManagement.Inhibit")
373
cookie = dev.Inhibit('UpdateManager', 'Updating system')
376
#print("could not send the dbus Inhibit signal: %s" % e)
377
return (False, False)
370
380
def allow_sleep(dev, cookie):
371
"""Send a dbus signal to gnome-power-manager to allow a suspending
374
dev.UnInhibit(cookie)
375
except Exception as e:
376
print("could not send the dbus UnInhibit signal: %s" % e)
381
"""Send a dbus signal to gnome-power-manager to allow a suspending
384
dev.UnInhibit(cookie)
385
except Exception as e:
386
print("could not send the dbus UnInhibit signal: %s" % e)
379
389
def str_to_bool(str):
380
if str == "0" or str.upper() == "FALSE":
390
if str == "0" or str.upper() == "FALSE":
387
398
(locale_s, encoding) = locale.getdefaultlocale()
390
401
logging.exception("gedefaultlocale() failed")
393
405
def get_ubuntu_flavor():
394
406
""" try to guess the flavor based on the running desktop """
395
# this will (of course) not work in a server environment,
407
# this will (of course) not work in a server environment,
396
408
# but the main use case for this is to show the right
410
# TODO: actually examine which meta packages are installed, like
411
# DistUpgrade/DistUpgradeCache.py does and use that to choose a flavor.
398
412
denv = os.environ.get("DESKTOP_SESSION", "")
399
413
if "gnome" in denv: