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

« back to all changes in this revision

Viewing changes to lib/makepkg.py

  • Committer: Sebastien Bacher
  • Date: 2021-11-22 16:04:05 UTC
  • mfrom: (594.2.1 langpack-o-matic)
  • Revision ID: seb128@ubuntu.com-20211122160405-17hx41i98tvg459q
The repository has been moved to git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# this is part of langpack-o-matic, by Martin Pitt <martin.pitt@canonical.com>
2
 
 
3
 
# (C) 2005, 2010 Canonical Ltd.
4
 
 
5
 
import os
6
 
import gzip
7
 
import shutil
8
 
import logging
9
 
 
10
 
import macros
11
 
 
12
 
 
13
 
def fcontents(path):
14
 
    f = open(path)
15
 
    contents = f.read()
16
 
    f.close()
17
 
    return contents
18
 
 
19
 
 
20
 
def flines(path):
21
 
    f = open(path)
22
 
    lines = f.readlines()
23
 
    f.close()
24
 
    return lines
25
 
 
26
 
 
27
 
def make_pkg(skeleton, path, lpmacros, extra_tar=None):
28
 
    '''Create a new source package from a skeleton for a specific locale and
29
 
    class and subsitute all macros using the given LangpackMacros object.
30
 
 
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).
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):
40
 
        logging.debug('Updating source package %s from skeleton %s', path, skeleton)
41
 
        _update_pkg(skeleton, path, lpmacros)
42
 
    else:
43
 
        logging.debug('Creating source package %s from skeleton %s', path, skeleton)
44
 
        _create_pkg(skeleton, path, lpmacros)
45
 
 
46
 
    # add package to updated-packages
47
 
    if not os.path.exists('updated-packages') or \
48
 
            path not in [l.strip() for l in flines('updated-packages')]:
49
 
        with open('updated-packages', 'a') as f:
50
 
            f.write(path + '\n')
51
 
 
52
 
    # Refresh upgrade notes
53
 
    notedir = os.path.join(path, 'debian', 'upgrade-notes')
54
 
    if os.path.isdir(notedir) and not lpmacros['CLASS']:
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
60
 
            content = fcontents(os.path.join('upgrade-notes', f))
61
 
            content = lpmacros.subst_string(content)
62
 
            f = open(os.path.join(notedir, f), 'w')
63
 
            f.write(content)
64
 
            f.close()
65
 
 
66
 
    # Install extra tarball
67
 
    if extra_tar:
68
 
        target = os.path.join(path, 'data', 'extra.tar')
69
 
        if extra_tar.endswith('.gz'):
70
 
            tardata = gzip.open(extra_tar).read()
71
 
        else:
72
 
            tardata = fcontents(extra_tar)
73
 
        f = open(target, 'w')
74
 
        f.write(tardata)
75
 
        f.close()
76
 
 
77
 
 
78
 
def _create_pkg(skel, dest, lpmacros):
79
 
    # copy skel files, omitting hidden files
80
 
    assert os.path.isdir(skel)
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
90
 
            content = fcontents(os.path.join(path, f))
91
 
            content = lpmacros.subst_string(content)
92
 
            f = open(os.path.join(destdir, f), 'w')
93
 
            f.write(content)
94
 
            f.close()
95
 
 
96
 
 
97
 
def _update_pkg(skel, dest, lpmacros):
98
 
    assert os.path.isdir(skel)
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')
102
 
 
103
 
    # clean up old data to avoid file conflicts from files which moved to a
104
 
    # different package
105
 
    if 'custom' not in skel:
106
 
        shutil.rmtree(os.path.join(dest, 'data'))
107
 
 
108
 
    for f in os.listdir(srcdir):
109
 
        if f[0] == '.' or f == 'changelog':
110
 
            continue
111
 
 
112
 
        # copy file, substitute macros
113
 
        src = os.path.join(srcdir, f)
114
 
        if os.path.isfile(src):
115
 
            content = fcontents(src)
116
 
            content = lpmacros.subst_string(content)
117
 
            f = open(os.path.join(destdir, f), 'w')
118
 
            f.write(content)
119
 
            f.close()
120
 
 
121
 
    # call dch to update changelog
122
 
    cwd = os.getcwd()
123
 
    os.chdir(dest)
124
 
    result = os.spawnlpe(os.P_WAIT, 'dch', 'dch', '--force-distribution',
125
 
                         '--force-bad-version', '-v',
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']})
130
 
    os.chdir(cwd)
131
 
 
132
 
    if result != 0:
133
 
        raise Exception('dch failed')
134
 
 
135
 
 
136
 
def get_pkg_version(path):
137
 
    '''Return the version of a package.'''
138
 
 
139
 
    ch = open(os.path.join(path, 'debian', 'changelog'))
140
 
    l = ch.readline()
141
 
    ch.close()
142
 
    return l[l.index('(') + 1:l.index(')')].strip()
143
 
 
144
 
 
145
 
if __name__ == '__main__':
146
 
    import tempfile
147
 
    import unittest
148
 
    import re
149
 
 
150
 
    class _T(unittest.TestCase):
151
 
        def setUp(self):
152
 
            self.workdir = tempfile.mkdtemp()
153
 
            self.macros = macros.LangpackMacros('ubuntu', 'de_CH', 'gnome', 'trusty', '20100528')
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
 
 
170
 
            make_pkg('skel-base', self.pkg, self.macros)
171
 
 
172
 
            self.assertEqual(fcontents('updated-packages').strip(), self.pkg)
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')))
175
 
 
176
 
            # check changelog
177
 
            changelog = flines(os.path.join(self.pkg, 'debian', 'changelog'))
178
 
            self.assertTrue(changelog[0].startswith('language-pack-gnome-de-base (1:14.04+20100528) trusty'), changelog[0])
179
 
            self.assertTrue('Initial Release' in changelog[2])
180
 
 
181
 
            # check control file
182
 
            control = fcontents(os.path.join(self.pkg, 'debian', 'control'))
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))
186
 
 
187
 
        def test_update(self):
188
 
            '''update existing package'''
189
 
 
190
 
            make_pkg('skel-update', self.pkg, self.macros)
191
 
            self.macros = macros.LangpackMacros('ubuntu', 'de_CH', 'gnome', 'trusty', '20100529')
192
 
            make_pkg('skel-update', self.pkg, self.macros)
193
 
            self.assertEqual(fcontents('updated-packages').strip(), self.pkg)
194
 
 
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')))
197
 
 
198
 
            # check changelog
199
 
            changelog = flines(os.path.join(self.pkg, 'debian', 'changelog'))
200
 
            self.assertTrue(changelog[0].startswith('language-pack-gnome-de (1:14.04+20100529) trusty'), changelog[0])
201
 
            self.assertTrue('update' in changelog[2])
202
 
            # old changelog entries still present
203
 
            self.assertTrue(changelog[6].startswith('language-pack-gnome-de (1:14.04+20100528) trusty'))
204
 
 
205
 
            # check control file
206
 
            control = fcontents(os.path.join(self.pkg, 'debian', 'control'))
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))
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')
215
 
            self.assertEqual(
216
 
                fcontents(os.path.join(self.pkg, 'data', 'extra.tar')),
217
 
                fcontents('extra-files/kde-de.tar'))
218
 
 
219
 
    unittest.main()