~ubuntu-langpack/langpack-o-matic/main

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