1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# this is part of langpack-o-matic, by Martin Pitt <martin.pitt@canonical.com>
# (C) 2005, 2010 Canonical Ltd.
import os
import gzip
import shutil
import logging
import macros
def fcontents(path):
f = open(path)
contents = f.read()
f.close()
return contents
def flines(path):
f = open(path)
lines = f.readlines()
f.close()
return lines
def make_pkg(skeleton, path, lpmacros, extra_tar=None):
'''Create a new source package from a skeleton for a specific locale and
class and subsitute all macros using the given LangpackMacros object.
If the package already exists, then it is updated. The "cls" parameter
specifies the desired package class (kde, gnome, or empty string for the
"all other stuff" language pack). The source package path is appended to
"updated-packages" (if not already present).
If a file name extra_tar is given, the file is installed into
data/extra.tar.
'''
if os.path.isdir(path):
logging.debug('Updating source package %s from skeleton %s', path, skeleton)
_update_pkg(skeleton, path, lpmacros)
else:
logging.debug('Creating source package %s from skeleton %s', path, skeleton)
_create_pkg(skeleton, path, lpmacros)
# add package to updated-packages
if not os.path.exists('updated-packages') or \
path not in [l.strip() for l in flines('updated-packages')]:
with open('updated-packages', 'a') as f:
f.write(path + '\n')
# Refresh upgrade notes
notedir = os.path.join(path, 'debian', 'upgrade-notes')
if os.path.isdir(notedir) and not lpmacros['CLASS']:
for f in os.listdir('upgrade-notes'):
# ignore .arch-ids and other stuff
if f[0] == '.':
continue
# copy file, substitute macros
content = fcontents(os.path.join('upgrade-notes', f))
content = lpmacros.subst_string(content)
f = open(os.path.join(notedir, f), 'w')
f.write(content)
f.close()
# Install extra tarball
if extra_tar:
target = os.path.join(path, 'data', 'extra.tar')
if extra_tar.endswith('.gz'):
tardata = gzip.open(extra_tar).read()
else:
tardata = fcontents(extra_tar)
f = open(target, 'w')
f.write(tardata)
f.close()
def _create_pkg(skel, dest, lpmacros):
# copy skel files, omitting hidden files
assert os.path.isdir(skel)
for path, dirs, files in os.walk(skel):
if os.path.basename(path)[0] == '.':
continue
destdir = os.path.join(dest, os.path.sep.join(path.split(os.path.sep)[1:]))
os.makedirs(destdir)
for f in files:
if f[0] == '.':
continue
# copy file, substitute macros
content = fcontents(os.path.join(path, f))
content = lpmacros.subst_string(content)
f = open(os.path.join(destdir, f), 'w')
f.write(content)
f.close()
def _update_pkg(skel, dest, lpmacros):
assert os.path.isdir(skel)
# copy control files (but changelog) again, in case they have changed
srcdir = os.path.join(skel, 'debian')
destdir = os.path.join(dest, 'debian')
# clean up old data to avoid file conflicts from files which moved to a
# different package
if 'custom' not in skel:
shutil.rmtree(os.path.join(dest, 'data'))
for f in os.listdir(srcdir):
if f[0] == '.' or f == 'changelog':
continue
# copy file, substitute macros
src = os.path.join(srcdir, f)
if os.path.isfile(src):
content = fcontents(src)
content = lpmacros.subst_string(content)
f = open(os.path.join(destdir, f), 'w')
f.write(content)
f.close()
# call dch to update changelog
cwd = os.getcwd()
os.chdir(dest)
result = os.spawnlpe(os.P_WAIT, 'dch', 'dch', '--force-distribution',
'--force-bad-version', '-v',
'1:%s+%s' % (lpmacros['RELEASEVERSION'], lpmacros['TIMESTAMP']),
'--urgency=low', '-p', '-D', lpmacros['RELEASE'],
'Automatic update to latest translation data.',
{'DEBEMAIL': lpmacros['UPLOADER']})
os.chdir(cwd)
if result != 0:
raise Exception('dch failed')
def get_pkg_version(path):
'''Return the version of a package.'''
ch = open(os.path.join(path, 'debian', 'changelog'))
l = ch.readline()
ch.close()
return l[l.index('(') + 1:l.index(')')].strip()
if __name__ == '__main__':
import tempfile
import unittest
import re
class _T(unittest.TestCase):
def setUp(self):
self.workdir = tempfile.mkdtemp()
self.macros = macros.LangpackMacros('ubuntu', 'de_CH', 'gnome', 'trusty', '20100528')
self.pkg = os.path.join(self.workdir, 'mypkg')
try:
os.unlink('updated-packages')
except OSError:
pass
def tearDown(self):
shutil.rmtree(self.workdir)
try:
os.unlink('updated-packages')
except OSError:
pass
def test_create(self):
'''create new package'''
make_pkg('skel-base', self.pkg, self.macros)
self.assertEqual(fcontents('updated-packages').strip(), self.pkg)
self.assertTrue(os.path.isfile(os.path.join(self.pkg, 'COPYING')))
self.assertTrue(os.path.isfile(os.path.join(self.pkg, 'debian', 'rules')))
# check changelog
changelog = flines(os.path.join(self.pkg, 'debian', 'changelog'))
self.assertTrue(changelog[0].startswith('language-pack-gnome-de-base (1:14.04+20100528) trusty'), changelog[0])
self.assertTrue('Initial Release' in changelog[2])
# check control file
control = fcontents(os.path.join(self.pkg, 'debian', 'control'))
self.assertTrue(control.startswith('Source: language-pack-gnome-de-base'))
self.assertTrue('Package: language-pack-gnome-de-base' in control)
self.assertTrue(re.search('^Description:.*GNOME.*German', control, re.M))
def test_update(self):
'''update existing package'''
make_pkg('skel-update', self.pkg, self.macros)
self.macros = macros.LangpackMacros('ubuntu', 'de_CH', 'gnome', 'trusty', '20100529')
make_pkg('skel-update', self.pkg, self.macros)
self.assertEqual(fcontents('updated-packages').strip(), self.pkg)
self.assertTrue(os.path.isfile(os.path.join(self.pkg, 'COPYING')))
self.assertTrue(os.path.isfile(os.path.join(self.pkg, 'debian', 'rules')))
# check changelog
changelog = flines(os.path.join(self.pkg, 'debian', 'changelog'))
self.assertTrue(changelog[0].startswith('language-pack-gnome-de (1:14.04+20100529) trusty'), changelog[0])
self.assertTrue('update' in changelog[2])
# old changelog entries still present
self.assertTrue(changelog[6].startswith('language-pack-gnome-de (1:14.04+20100528) trusty'))
# check control file
control = fcontents(os.path.join(self.pkg, 'debian', 'control'))
self.assertTrue(control.startswith('Source: language-pack-gnome-de'))
self.assertTrue('Package: language-pack-gnome-de' in control)
self.assertTrue(re.search('^Description:.*GNOME.*German', control, re.M))
def test_extra_tarball(self):
'''package with extra tarball'''
make_pkg('skel-base', self.pkg, self.macros, 'extra-files/kde-de.tar')
self.assertEqual(
fcontents(os.path.join(self.pkg, 'data', 'extra.tar')),
fcontents('extra-files/kde-de.tar'))
unittest.main()
|