File: source_package.py

package info (click to toggle)
ubuntu-dev-tools 0.206
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,172 kB
  • sloc: python: 9,120; sh: 1,304; perl: 135; makefile: 10
file content (471 lines) | stat: -rw-r--r-- 17,692 bytes parent folder | download | duplicates (2)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#
# source_package.py - Internal helper class for sponsor-patch
#
# Copyright (C) 2011, Benjamin Drung <bdrung@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import logging
import os
import re
import subprocess
import sys

import debian.changelog
import debian.deb822

from ubuntutools.question import Question, YesNoQuestion
from ubuntutools.sponsor_patch.question import (
    ask_for_ignoring_or_fixing,
    ask_for_manual_fixing,
    user_abort,
)

Logger = logging.getLogger(__name__)


def _get_series(launchpad):
    """Returns a tuple with the development and list of supported series."""
    ubuntu = launchpad.distributions["ubuntu"]
    devel_series = ubuntu.current_series.name
    supported_series = [
        series.name for series in ubuntu.series if series.active and series.name != devel_series
    ]
    return (devel_series, supported_series)


def strip_epoch(version):
    """Removes the epoch from a Debian version string.

    strip_epoch(1:1.52-1) will return "1.52-1" and strip_epoch(1.1.3-1) will
    return "1.1.3-1".
    """

    parts = version.full_version.split(":")
    if len(parts) > 1:
        del parts[0]
    version_without_epoch = ":".join(parts)
    return version_without_epoch


class SourcePackage:
    """This class represents a source package."""

    def __init__(self, package, builder, workdir, branch):
        self._package = package
        self._builder = builder
        self._workdir = workdir
        self._branch = branch
        self._changelog = None
        self._version = None
        self._build_log = None

    def ack_sync(self, upload, task, launchpad):
        """Acknowledge a sync request and subscribe ubuntu-archive."""

        if upload == "ubuntu":
            self._print_logs()
            question = Question(["yes", "edit", "no"])
            answer = question.ask("Do you want to acknowledge the sync request", "no")
            if answer == "edit":
                return False
            if answer == "no":
                user_abort()

            bug = task.bug
            task.status = "Confirmed"
            if task.importance == "Undecided":
                task.importance = "Wishlist"
            task.lp_save()
            Logger.debug("Set bug #%i status to Confirmed.", bug.id)

            msg = "Sync request ACK'd."
            if self._build_log:
                msg = (
                    f"{self._package} {self._version} builds"
                    f" on {self._builder.get_architecture()}. {msg}"
                )
            bug.newMessage(content=msg, subject="sponsor-patch")
            Logger.debug("Acknowledged sync request bug #%i.", bug.id)

            bug.subscribe(person=launchpad.people["ubuntu-archive"])
            Logger.debug("Subscribed ubuntu-archive to bug #%i.", bug.id)

            bug.subscribe(person=launchpad.me)
            Logger.debug("Subscribed me to bug #%i.", bug.id)

            sponsorsteam = launchpad.people["ubuntu-sponsors"]
            for sub in bug.subscriptions:
                if sub.person == sponsorsteam and sub.canBeUnsubscribedByUser():
                    bug.unsubscribe(person=launchpad.people["ubuntu-sponsors"])
                    Logger.debug("Unsubscribed ubuntu-sponsors from bug #%i.", bug.id)
                elif sub.person == sponsorsteam:
                    Logger.debug("Couldn't unsubscribe ubuntu-sponsors from bug #%i.", bug.id)

            Logger.info("Successfully acknowledged sync request bug #%i.", bug.id)
        else:
            Logger.error(
                "Sync requests can only be acknowledged when the upload target is Ubuntu."
            )
            sys.exit(1)
        return True

    def ask_and_upload(self, upload):
        """Ask the user before uploading the source package.

        Returns true if the source package is uploaded successfully. Returns
        false if the user wants to change something.
        """

        # Upload package
        if upload:
            self._print_logs()
            if upload == "ubuntu":
                target = "the official Ubuntu archive"
            else:
                target = upload
            question = Question(["yes", "edit", "no"])
            answer = question.ask(f"Do you want to upload the package to {target}", "no")
            if answer == "edit":
                return False
            if answer == "no":
                user_abort()
            cmd = ["dput", "--force", upload, self._changes_file]
            Logger.debug(" ".join(cmd))
            if subprocess.call(cmd) != 0:
                Logger.error(
                    "Upload of %s to %s failed.", os.path.basename(self._changes_file), upload
                )
                sys.exit(1)

            # Push the branch if the package is uploaded to the Ubuntu archive.
            if upload == "ubuntu" and self._branch:
                cmd = ["debcommit"]
                Logger.debug(" ".join(cmd))
                if subprocess.call(cmd) != 0:
                    Logger.error("Bzr commit failed.")
                    sys.exit(1)
                cmd = ["bzr", "mark-uploaded"]
                Logger.debug(" ".join(cmd))
                if subprocess.call(cmd) != 0:
                    Logger.error("Bzr tagging failed.")
                    sys.exit(1)
                cmd = ["bzr", "push", ":parent"]
                Logger.debug(" ".join(cmd))
                if subprocess.call(cmd) != 0:
                    Logger.error("Bzr push failed.")
                    sys.exit(1)
        return True

    def build(self, update, dist=None):
        """Tries to build the package.

        Returns true if the package was built successfully. Returns false
        if the user wants to change something.
        """

        if dist is None:
            dist = re.sub("-.*$", "", self._changelog.distributions)
        build_name = (
            f"{self._package}_{strip_epoch(self._version)}"
            f"_{self._builder.get_architecture()}.build"
        )
        self._build_log = os.path.join(self._buildresult, build_name)

        successful_built = False
        while not successful_built:
            if update:
                ret = self._builder.update(dist)
                if ret != 0:
                    ask_for_manual_fixing()
                    break
                # We want to update the build environment only once, but not
                # after every manual fix.
                update = False

            # build package
            result = self._builder.build(self._dsc_file, dist, self._buildresult)
            if result != 0:
                question = Question(["yes", "update", "retry", "no"])
                answer = question.ask("Do you want to resolve this issue manually", "yes")
                if answer == "yes":
                    break
                if answer == "update":
                    update = True
                    continue
                if answer == "retry":
                    continue
                user_abort()
            successful_built = True
        if not successful_built:
            # We want to do a manual fix if the build failed.
            return False
        return True

    @property
    def _buildresult(self):
        """Returns the directory for the build result."""
        return os.path.join(self._workdir, "buildresult")

    def build_source(self, keyid, upload, previous_version):
        """Tries to build the source package.

        Returns true if the source package was built successfully. Returns false
        if the user wants to change something.
        """

        if self._branch:
            cmd = ["bzr", "builddeb", "--builder=debuild", "-S", "--", "--no-lintian", "-nc"]
        else:
            cmd = ["debuild", "--no-lintian", "-nc", "-S"]
        cmd.append("-v" + previous_version.full_version)
        if (
            previous_version.upstream_version == self._changelog.upstream_version
            and upload == "ubuntu"
        ):
            # FIXME: Add proper check that catches cases like changed
            # compression (.tar.gz -> tar.bz2) and multiple orig source tarballs
            cmd.append("-sd")
        else:
            cmd.append("-sa")
        if keyid is not None:
            cmd += ["-k" + keyid]
        env = os.environ
        if upload == "ubuntu":
            env["DEB_VENDOR"] = "Ubuntu"
        Logger.debug(" ".join(cmd))
        if subprocess.call(cmd, env=env) != 0:
            Logger.error("Failed to build source tarball.")
            # TODO: Add a "retry" option
            ask_for_manual_fixing()
            return False
        return True

    @property
    def _changes_file(self):
        """Returns the file name of the .changes file."""
        return os.path.join(
            self._workdir, f"{self._package}_{strip_epoch(self._version)}_source.changes"
        )

    def check_target(self, upload, launchpad):
        """Make sure that the target is correct.

        Returns true if the target is correct. Returns false if the user
        wants to change something.
        """

        (devel_series, supported_series) = _get_series(launchpad)

        if upload == "ubuntu":
            allowed = (
                supported_series + [s + "-proposed" for s in supported_series] + [devel_series]
            )
            if self._changelog.distributions not in allowed:
                Logger.error(
                    "%s is not an allowed series. It needs to be one of %s.",
                    self._changelog.distributions,
                    ", ".join(allowed),
                )
                return ask_for_ignoring_or_fixing()
        elif upload and upload.startswith("ppa/"):
            allowed = supported_series + [devel_series]
            if self._changelog.distributions not in allowed:
                Logger.error(
                    "%s is not an allowed series. It needs to be one of %s.",
                    self._changelog.distributions,
                    ", ".join(allowed),
                )
                return ask_for_ignoring_or_fixing()
        return True

    def check_version(self, previous_version):
        """Check if the version of the package is greater than the given one.

        Return true if the version of the package is newer. Returns false
        if the user wants to change something.
        """

        if self._version <= previous_version:
            Logger.error(
                "The version %s is not greater than the already available %s.",
                self._version,
                previous_version,
            )
            return ask_for_ignoring_or_fixing()
        return True

    def check_sync_request_version(self, bug_number, task):
        """Check if the downloaded version of the package is mentioned in the
        bug title."""

        if not task.title_contains(self._version):
            print(f"Bug #{bug_number} title: {task.get_bug_title()}")
            msg = f"Is {self._package} {self._version} the version that should be synced"
            answer = YesNoQuestion().ask(msg, "no")
            if answer == "no":
                user_abort()

    @property
    def _debdiff_filename(self):
        """Returns the file name of the .debdiff file."""
        debdiff_name = f"{self._package}_{strip_epoch(self._version)}.debdiff"
        return os.path.join(self._workdir, debdiff_name)

    @property
    def _dsc_file(self):
        """Returns the file name of the .dsc file."""
        return os.path.join(self._workdir, f"{self._package}_{strip_epoch(self._version)}.dsc")

    def generate_debdiff(self, dsc_file):
        """Generates a debdiff between the given .dsc file and this source
        package."""

        assert os.path.isfile(dsc_file), f"{dsc_file} does not exist."
        assert os.path.isfile(self._dsc_file), f"{self._dsc_file} does not exist."
        cmd = ["debdiff", dsc_file, self._dsc_file]
        if not Logger.isEnabledFor(logging.DEBUG):
            cmd.insert(1, "-q")
        Logger.debug("%s > %s", " ".join(cmd), self._debdiff_filename)
        with open(self._debdiff_filename, "w", encoding="utf-8") as debdiff_file:
            debdiff = subprocess.run(cmd, check=False, stdout=debdiff_file)
            assert debdiff.returncode in (0, 1)

    def is_fixed(self, lp_bug):
        """Make sure that the given Launchpad bug is closed.

        Returns true if the bug is closed. Returns false if the user wants to
        change something.
        """

        assert os.path.isfile(self._changes_file), f"{self._changes_file} does not exist."
        changes = debian.deb822.Changes(open(self._changes_file, encoding="utf-8"))
        fixed_bugs = []
        if "Launchpad-Bugs-Fixed" in changes:
            fixed_bugs = changes["Launchpad-Bugs-Fixed"].split(" ")
        fixed_bugs = [int(bug) for bug in fixed_bugs]

        # Check if the given bug is marked as duplicate. In this case get the
        # master bug.
        while lp_bug.duplicate_of:
            lp_bug = lp_bug.duplicate_of

        if lp_bug.id not in fixed_bugs:
            Logger.error("Launchpad bug #%i is not closed by new version.", lp_bug.id)
            return ask_for_ignoring_or_fixing()
        return True

    def _print_logs(self):
        """Print things that should be checked before uploading a package."""

        lintian_filename = self._run_lintian()
        print(f"\nPlease check {self._package} {self._version} carefully:")
        if os.path.isfile(self._debdiff_filename):
            print("file://" + self._debdiff_filename)
        print("file://" + lintian_filename)
        if self._build_log:
            print("file://" + self._build_log)

    def reload_changelog(self):
        """Reloads debian/changelog and updates the version.

        Returns true if the changelog was reloaded successfully. Returns false
        if the user wants to correct a broken changelog.
        """

        # Check the changelog
        self._changelog = debian.changelog.Changelog()
        try:
            self._changelog.parse_changelog(
                open("debian/changelog", encoding="utf-8"), max_blocks=1, strict=True
            )
        except debian.changelog.ChangelogParseError as error:
            Logger.error("The changelog entry doesn't validate: %s", str(error))
            ask_for_manual_fixing()
            return False

        # Get new version of package
        try:
            self._version = self._changelog.get_version()
        except IndexError:
            Logger.error(
                "Debian package version could not be determined. "
                "debian/changelog is probably malformed."
            )
            ask_for_manual_fixing()
            return False

        return True

    def _run_lintian(self):
        """Runs lintian on either the source or binary changes file.

        Returns the filename of the created lintian output file.
        """

        # Determine whether to use the source or binary build for lintian
        if self._build_log:
            build_changes = (
                self._package
                + "_"
                + strip_epoch(self._version)
                + "_"
                + self._builder.get_architecture()
                + ".changes"
            )
            changes_for_lintian = os.path.join(self._buildresult, build_changes)
        else:
            changes_for_lintian = self._changes_file

        # Check lintian
        assert os.path.isfile(changes_for_lintian), f"{changes_for_lintian} does not exist."
        cmd = ["lintian", "-IE", "--pedantic", "-q", "--profile", "ubuntu", changes_for_lintian]
        lintian_filename = os.path.join(
            self._workdir, self._package + "_" + strip_epoch(self._version) + ".lintian"
        )
        Logger.debug("%s > %s", " ".join(cmd), lintian_filename)
        report = subprocess.check_output(cmd, encoding="utf-8")

        # write lintian report file
        lintian_file = open(lintian_filename, "w", encoding="utf-8")
        lintian_file.writelines(report)
        lintian_file.close()

        return lintian_filename

    def sync(self, upload, series, bug_number, requester):
        """Does a sync of the source package."""

        if upload == "ubuntu":
            cmd = [
                "syncpackage",
                self._package,
                "-b",
                str(bug_number),
                "-f",
                "-s",
                requester,
                "-V",
                str(self._version),
                "-d",
                series,
            ]
            Logger.debug(" ".join(cmd))
            if subprocess.call(cmd) != 0:
                Logger.error("Syncing of %s %s failed.", self._package, str(self._version))
                sys.exit(1)
        else:
            # FIXME: Support this use case!
            Logger.error("Uploading a synced package other than to Ubuntu is not supported yet!")
            sys.exit(1)
        return True