2012-01-11 18:03:27 +00:00
|
|
|
import codecs
|
2012-01-11 16:52:29 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import textwrap
|
2008-05-01 07:54:28 +00:00
|
|
|
|
2012-01-11 16:52:29 +00:00
|
|
|
|
2009-04-12 10:27:59 +00:00
|
|
|
class Templates(object):
|
2012-01-11 16:52:29 +00:00
|
|
|
def __init__(self, dirs=["debian/templates"]):
|
2008-05-01 07:54:28 +00:00
|
|
|
self.dirs = dirs
|
|
|
|
|
2009-04-12 10:27:59 +00:00
|
|
|
self._cache = {}
|
2008-05-01 07:54:28 +00:00
|
|
|
|
2009-04-12 10:27:59 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
ret = self.get(key)
|
|
|
|
if ret is not None:
|
|
|
|
return ret
|
|
|
|
raise KeyError(key)
|
2008-05-01 07:54:28 +00:00
|
|
|
|
|
|
|
def _read(self, name):
|
|
|
|
prefix, id = name.split('.', 1)
|
|
|
|
|
2013-08-17 11:18:34 +00:00
|
|
|
for suffix in ['.in', '']:
|
|
|
|
for dir in self.dirs:
|
|
|
|
filename = "%s/%s%s" % (dir, name, suffix)
|
|
|
|
if os.path.exists(filename):
|
|
|
|
f = codecs.open(filename, 'r', 'utf-8')
|
|
|
|
if prefix == 'control':
|
|
|
|
return read_control(f)
|
2015-08-11 21:42:35 +00:00
|
|
|
if prefix == 'tests-control':
|
|
|
|
return read_tests_control(f)
|
2013-08-17 11:18:34 +00:00
|
|
|
return f.read()
|
2008-05-01 07:54:28 +00:00
|
|
|
|
2009-04-12 10:27:59 +00:00
|
|
|
def get(self, key, default=None):
|
|
|
|
if key in self._cache:
|
|
|
|
return self._cache[key]
|
|
|
|
|
|
|
|
value = self._cache.setdefault(key, self._read(key))
|
2008-05-01 07:54:28 +00:00
|
|
|
if value is None:
|
|
|
|
return default
|
|
|
|
return value
|
|
|
|
|
2012-01-11 16:52:29 +00:00
|
|
|
|
2011-09-03 20:53:59 +00:00
|
|
|
def read_control(f):
|
|
|
|
from .debian import Package
|
2015-08-11 21:25:51 +00:00
|
|
|
return _read_rfc822(f, Package)
|
2011-09-03 20:53:59 +00:00
|
|
|
|
2018-10-01 15:22:49 +00:00
|
|
|
|
2015-08-11 21:42:35 +00:00
|
|
|
def read_tests_control(f):
|
|
|
|
from .debian import TestsControl
|
|
|
|
return _read_rfc822(f, TestsControl)
|
|
|
|
|
2018-10-01 15:22:49 +00:00
|
|
|
|
2015-08-11 21:25:51 +00:00
|
|
|
def _read_rfc822(f, cls):
|
2011-09-03 20:53:59 +00:00
|
|
|
entries = []
|
2011-09-03 20:55:12 +00:00
|
|
|
eof = False
|
2011-09-03 20:53:59 +00:00
|
|
|
|
2011-09-03 20:55:12 +00:00
|
|
|
while not eof:
|
2015-08-11 21:25:51 +00:00
|
|
|
e = cls()
|
2011-09-03 20:53:59 +00:00
|
|
|
last = None
|
|
|
|
lines = []
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if not line:
|
2011-09-03 20:55:12 +00:00
|
|
|
eof = True
|
2011-09-03 20:53:59 +00:00
|
|
|
break
|
2016-05-14 18:54:34 +00:00
|
|
|
# Strip comments rather than trying to preserve them
|
|
|
|
if line[0] == '#':
|
|
|
|
continue
|
2011-09-03 20:53:59 +00:00
|
|
|
line = line.strip('\n')
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
if line[0] in ' \t':
|
|
|
|
if not last:
|
2018-10-01 15:22:49 +00:00
|
|
|
raise ValueError(
|
|
|
|
'Continuation line seen before first header')
|
2011-09-03 20:53:59 +00:00
|
|
|
lines.append(line.lstrip())
|
|
|
|
continue
|
|
|
|
if last:
|
2015-08-29 20:48:39 +00:00
|
|
|
e[last] = '\n'.join(lines)
|
2011-09-03 20:53:59 +00:00
|
|
|
i = line.find(':')
|
|
|
|
if i < 0:
|
2018-10-01 15:22:49 +00:00
|
|
|
raise ValueError(u"Not a header, not a continuation: ``%s''" %
|
|
|
|
line)
|
2011-09-03 20:53:59 +00:00
|
|
|
last = line[:i]
|
2012-01-11 16:52:29 +00:00
|
|
|
lines = [line[i + 1:].lstrip()]
|
2011-09-03 20:53:59 +00:00
|
|
|
if last:
|
|
|
|
e[last] = '\n'.join(lines)
|
2011-09-03 20:55:12 +00:00
|
|
|
if e:
|
|
|
|
entries.append(e)
|
2011-09-03 20:53:59 +00:00
|
|
|
|
|
|
|
return entries
|
|
|
|
|
2012-01-11 16:52:29 +00:00
|
|
|
|
2008-05-01 07:54:28 +00:00
|
|
|
class TextWrapper(textwrap.TextWrapper):
|
|
|
|
wordsep_re = re.compile(
|
|
|
|
r'(\s+|' # any whitespace
|
|
|
|
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
|