File: example_package.py

package info (click to toggle)
ubuntu-dev-tools 0.208
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,172 kB
  • sloc: python: 9,118; sh: 1,330; perl: 135; makefile: 11
file content (92 lines) | stat: -rw-r--r-- 3,035 bytes parent folder | download
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
# example_package.py - Creates an example package
#
# Copyright (C) 2010-2011, Stefano Rivera <stefanor@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 os
import subprocess
import tempfile
from pathlib import Path

from ubuntutools.version import Version


class ExamplePackage:
    def __init__(self, source="example", version="1.0-1", destdir="test-data"):
        self.source = source
        self.version = Version(version)
        self.destdir = Path(destdir)

        self.env = dict(os.environ)
        self.env["DEBFULLNAME"] = "Example"
        self.env["DEBEMAIL"] = "example@example.net"

    @property
    def orig(self):
        return self.destdir / f"{self.source}_{self.version.upstream_version}.orig.tar.xz"

    @property
    def debian(self):
        return self.destdir / f"{self.source}_{self.version}.debian.tar.xz"

    @property
    def dsc(self):
        return self.destdir / f"{self.source}_{self.version}.dsc"

    @property
    def dirname(self):
        return f"{self.source}-{self.version.upstream_version}"

    @property
    def content_filename(self):
        return "content"

    @property
    def content_text(self):
        return "my content"

    def create(self):
        with tempfile.TemporaryDirectory() as tmpdir:
            self._create(Path(tmpdir))

    def _create(self, directory: Path) -> None:
        pkgdir = directory / self.dirname
        pkgdir.mkdir()
        (pkgdir / self.content_filename).write_text(self.content_text)

        # run dh_make to create orig tarball
        subprocess.run(
            "dh_make -sy --createorig".split(),
            check=True,
            env=self.env,
            cwd=str(pkgdir),
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )

        # run dpkg-source -b to create debian tar and dsc
        subprocess.run(
            f"dpkg-source -b {self.dirname}".split(),
            check=True,
            env=self.env,
            cwd=str(directory),
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )

        # move tarballs and dsc to destdir
        self.destdir.mkdir(parents=True, exist_ok=True)
        (directory / self.orig.name).rename(self.orig)
        (directory / self.debian.name).rename(self.debian)
        (directory / self.dsc.name).rename(self.dsc)