2010-05-02 00:03:45 +02:00
|
|
|
import sys, os
|
|
|
|
|
2010-05-02 06:52:06 +02:00
|
|
|
logprefix = ''
|
2010-05-02 08:14:20 +02:00
|
|
|
verbose = 0
|
2010-05-02 06:52:06 +02:00
|
|
|
|
2010-05-02 00:03:45 +02:00
|
|
|
def log(s):
|
2010-05-16 23:55:46 +02:00
|
|
|
try:
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.write(logprefix + s)
|
|
|
|
sys.stderr.flush()
|
|
|
|
except IOError:
|
|
|
|
# this could happen if stderr gets forcibly disconnected, eg. because
|
|
|
|
# our tty closes. That sucks, but it's no reason to abort the program.
|
|
|
|
pass
|
2010-05-02 08:14:20 +02:00
|
|
|
|
|
|
|
def debug1(s):
|
|
|
|
if verbose >= 1:
|
|
|
|
log(s)
|
|
|
|
|
|
|
|
def debug2(s):
|
|
|
|
if verbose >= 2:
|
|
|
|
log(s)
|
2010-05-02 08:23:42 +02:00
|
|
|
|
2010-05-08 07:30:34 +02:00
|
|
|
def debug3(s):
|
|
|
|
if verbose >= 3:
|
|
|
|
log(s)
|
|
|
|
|
2010-05-02 08:23:42 +02:00
|
|
|
|
|
|
|
class Fatal(Exception):
|
|
|
|
pass
|
2010-10-01 23:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def list_contains_any(l, sub):
|
|
|
|
for i in sub:
|
|
|
|
if i in l:
|
|
|
|
return True
|
|
|
|
return False
|
2011-01-26 11:15:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def resolvconf_nameservers():
|
|
|
|
l = []
|
|
|
|
for line in open('/etc/resolv.conf'):
|
|
|
|
words = line.lower().split()
|
|
|
|
if len(words) >= 2 and words[0] == 'nameserver':
|
|
|
|
l.append(words[1])
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
|
|
|
def resolvconf_random_nameserver():
|
|
|
|
l = resolvconf_nameservers()
|
|
|
|
if l:
|
|
|
|
if len(l) > 1:
|
|
|
|
# don't import this unless we really need it
|
|
|
|
import random
|
|
|
|
random.shuffle(l)
|
|
|
|
return l[0]
|
|
|
|
else:
|
|
|
|
return '127.0.0.1'
|
|
|
|
|
|
|
|
|