[IMP] various code improvements in runbot

This commit is contained in:
Gery Debongnie 2014-06-26 11:07:15 +02:00
parent 6cb6bdd4ab
commit 30a36aa0f6

View File

@ -35,63 +35,62 @@ _logger = logging.getLogger(__name__)
#---------------------------------------------------------- #----------------------------------------------------------
def log(*l, **kw): def log(*l, **kw):
out = [] out = [i if isinstance(i, basestring) else repr(i) for i in l] + \
for i in l: ["%s=%r" % (k, v) for k, v in kw.items()]
if not isinstance(i, basestring):
i = repr(i)
out.append(i)
out += ["%s=%r" % (k, v) for k, v in kw.items()]
_logger.debug(' '.join(out)) _logger.debug(' '.join(out))
def dashes(s): def dashes(string):
"""Sanitize the input string"""
for i in '~":\'': for i in '~":\'':
s = s.replace(i, "") string = string.replace(i, "")
for i in '/_. ': for i in '/_. ':
s = s.replace(i, "-") string = string.replace(i, "-")
return s return string
def mkdirs(dirs): def mkdirs(dirs):
for i in dirs: for d in dirs:
if not os.path.exists(i): if not os.path.exists(d):
os.makedirs(i) os.makedirs(d)
def grep(filename, s): def grep(filename, string):
if os.path.isfile(filename): if os.path.isfile(filename):
return open(filename).read().find(s) != -1 return open(filename).read().find(string) != -1
return False return False
_re_error = r'^(?:\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d{3} \d+ (?:ERROR|CRITICAL) )|(?:Traceback \(most recent call last\):)$' _re_error = r'^(?:\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d{3} \d+ (?:ERROR|CRITICAL) )|(?:Traceback \(most recent call last\):)$'
_re_warning = r'^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d{3} \d+ WARNING ' _re_warning = r'^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d{3} \d+ WARNING '
def rfind(filename, patern): def rfind(filename, pattern):
"""Determine in something in filename matches the pattern"""
if os.path.isfile(filename): if os.path.isfile(filename):
p = re.compile(patern, re.M) regexp = re.compile(pattern, re.M)
with open(filename, 'r') as f: with open(filename, 'r') as f:
if p.findall(f.read()): if regexp.findall(f.read()):
return True return True
return False return False
def lock(name): def lock(filename):
fd = os.open(name, os.O_CREAT | os.O_RDWR, 0600) fd = os.open(filename, os.O_CREAT | os.O_RDWR, 0600)
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
def locked(name): def locked(filename):
r = False result = False
try: try:
fd = os.open(name, os.O_CREAT | os.O_RDWR, 0600) fd = os.open(filename, os.O_CREAT | os.O_RDWR, 0600)
try: try:
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError: except IOError:
r = True result = True
os.close(fd) os.close(fd)
except OSError: except OSError:
r = False result = False
return r return result
def nowait(): def nowait():
signal.signal(signal.SIGCHLD, signal.SIG_IGN) signal.signal(signal.SIGCHLD, signal.SIG_IGN)
def run(l, env=None): def run(l, env=None):
"""Run a command described by l in environment env"""
log("run", l) log("run", l)
env = dict(os.environ, **env) if env else None env = dict(os.environ, **env) if env else None
if isinstance(l, list): if isinstance(l, list):
@ -111,14 +110,16 @@ def run(l, env=None):
def now(): def now():
return time.strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT) return time.strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)
def dt2time(dt): def dt2time(datetime):
return time.mktime(time.strptime(dt, openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)) """Convert datetime to time"""
return time.mktime(time.strptime(datetime, openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT))
def s2human(t): def s2human(time):
for m,u in [(86400,'d'),(3600,'h'),(60,'m')]: """Convert a time in second into an human readable string"""
if t>=m: for delay, desc in [(86400,'d'),(3600,'h'),(60,'m')]:
return str(int(t/m))+u if time >= delay:
return str(int(t))+"s" return str(int(time / delay)) + desc
return str(int(time)) + "s"
#---------------------------------------------------------- #----------------------------------------------------------
# RunBot Models # RunBot Models
@ -129,14 +130,14 @@ class runbot_repo(osv.osv):
_order = 'name' _order = 'name'
def _get_path(self, cr, uid, ids, field_name, arg, context=None): def _get_path(self, cr, uid, ids, field_name, arg, context=None):
wd = self.root(cr, uid) root = self.root(cr, uid)
r = {} result = {}
for repo in self.browse(cr, uid, ids, context=context): for repo in self.browse(cr, uid, ids, context=context):
name = repo.name name = repo.name
for i in '@:/': for i in '@:/':
name = name.replace(i, '_') name = name.replace(i, '_')
r[repo.id] = os.path.join(wd, 'repo', name) result[repo.id] = os.path.join(root, 'repo', name)
return r return result
def _get_base(self, cr, uid, ids, field_name, arg, context=None): def _get_base(self, cr, uid, ids, field_name, arg, context=None):
r = {} r = {}
@ -170,9 +171,9 @@ class runbot_repo(osv.osv):
return domain return domain
def root(self, cr, uid, context=None): def root(self, cr, uid, context=None):
"""Return root directory of repository"""
default = os.path.join(os.path.dirname(__file__), 'static') default = os.path.join(os.path.dirname(__file__), 'static')
root = self.pool.get('ir.config_parameter').get_param(cr, uid, 'runbot.root', default) return self.pool.get('ir.config_parameter').get_param(cr, uid, 'runbot.root', default)
return root
def git(self, cr, uid, ids, cmd, context=None): def git(self, cr, uid, ids, cmd, context=None):
for repo in self.browse(cr, uid, ids, context=context): for repo in self.browse(cr, uid, ids, context=context):