| 1 |
"""Auxillary methods for the UDD"""
|
| 2 |
|
| 3 |
import yaml
|
| 4 |
import sys
|
| 5 |
import psycopg2
|
| 6 |
from os import path
|
| 7 |
import fcntl
|
| 8 |
|
| 9 |
# If debug is something that evaluates to True, then print_debug actually prints something
|
| 10 |
debug = 0
|
| 11 |
|
| 12 |
def quote(s):
|
| 13 |
"Quote a string for SQL"
|
| 14 |
return "'" + s.replace("\\", "\\\\").replace("'", "\\'") + "'"
|
| 15 |
|
| 16 |
def null_or_quote(dict, key):
|
| 17 |
"If key is an element of dict, return it quoted. Return NULL otherwise"
|
| 18 |
if key in dict:
|
| 19 |
return quote(dict[key])
|
| 20 |
else:
|
| 21 |
return 'NULL'
|
| 22 |
|
| 23 |
class ConfigException(Exception):
|
| 24 |
def __init__(self, message):
|
| 25 |
Exception(self)
|
| 26 |
self.message = message
|
| 27 |
|
| 28 |
def __str__(self):
|
| 29 |
return "ConfigException: " + self.message
|
| 30 |
|
| 31 |
def open_connection(config):
|
| 32 |
"""Open the connection to the database and return it"""
|
| 33 |
if 'dbport' in config['general']:
|
| 34 |
p = " port=" + str(config['general']['dbport'])
|
| 35 |
else:
|
| 36 |
p = ""
|
| 37 |
return psycopg2.connect("dbname=" + config['general']['dbname'] + p)
|
| 38 |
|
| 39 |
__locks = {}
|
| 40 |
def lock(config, source):
|
| 41 |
lock_dir = config['general']['lock-dir']
|
| 42 |
lock_path = path.join(lock_dir, source)
|
| 43 |
f = file(lock_path, "w+")
|
| 44 |
__locks[lock_path] = f
|
| 45 |
try:
|
| 46 |
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
| 47 |
except IOError:
|
| 48 |
print source+": lockfile found, exiting."
|
| 49 |
exit(1)
|
| 50 |
|
| 51 |
def unlock(config, source):
|
| 52 |
lock_dir = config['general']['lock-dir']
|
| 53 |
lock_path = path.join(lock_dir, source)
|
| 54 |
if lock_path in __locks:
|
| 55 |
f = file(lock_path)
|
| 56 |
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
|
| 57 |
del __locks[lock_path]
|
| 58 |
|
| 59 |
def load_config(str):
|
| 60 |
"""Load and check configuration from the string"""
|
| 61 |
config = yaml.load(str)
|
| 62 |
if not 'general' in config:
|
| 63 |
raise ConfigException('general section not specified')
|
| 64 |
|
| 65 |
general = config['general']
|
| 66 |
for k in ['dbname', 'archs', 'types', 'lock-dir']:
|
| 67 |
if not k in general:
|
| 68 |
raise ConfigException(k + ' not specified in node "general"')
|
| 69 |
if not 'debug' in general:
|
| 70 |
general['debug'] = 0
|
| 71 |
|
| 72 |
# Check that the source-entries are well-formed
|
| 73 |
for name in config:
|
| 74 |
if name == 'general':
|
| 75 |
continue
|
| 76 |
|
| 77 |
src = config[name]
|
| 78 |
if not 'type' in src:
|
| 79 |
raise ConfigException('type not specified for "%s"' % name)
|
| 80 |
if src['type'] not in general['types']:
|
| 81 |
raise ConfigException('Type of %s not specified in types' % name)
|
| 82 |
|
| 83 |
return config
|
| 84 |
|
| 85 |
def print_debug(*args):
|
| 86 |
"Print arguments to stdout if debug is set to something that evaluates to true"
|
| 87 |
if debug:
|
| 88 |
sys.stdout.write(*args)
|
| 89 |
sys.stdout.write("\n")
|