File: Repository.py

package info (click to toggle)
debomatic 0.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: python: 1,904; sh: 192; makefile: 37
file content (148 lines) | stat: -rw-r--r-- 6,353 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
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
# Deb-o-Matic - Repository module
#
# Copyright (C) 2011-2025 Luca Falavigna
#
# Authors: Luca Falavigna <dktrkranz@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option), any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Generate local repository of built packages

import os
from fcntl import flock, LOCK_EX, LOCK_NB, LOCK_SH, LOCK_UN
from shutil import rmtree
from stat import S_IRWXU, S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH
from subprocess import Popen, PIPE
from tempfile import mkdtemp, mkstemp


class DebomaticModule_Repository:

    def __init__(self):
        self.af = '/usr/bin/apt-ftparchive'
        self.gpg = '/usr/bin/gpg'

    def pre_build(self, args):
        self.update_repository(args)

    def post_build(self, args):
        self.update_repository(args)

    def pre_chroot(self, args):
        if args.action:
            self.update_repository(args)

    class Lock:

        def __init__(self, distribution, architecture):
            self._file = ('/tmp/debomatic-%s-%s.apt.lock' %
                          (distribution, architecture))
            self._skip = False

        def __enter__(self):
            self._fd = open(self._file, 'w')
            try:
                flock(self._fd, LOCK_EX | LOCK_NB)
            except IOError:
                flock(self._fd, LOCK_SH)
                self._skip = True
            return self

        def __exit__(self, exc_type, exc_value, traceback):
            flock(self._fd, LOCK_UN)
            self._fd.close()

        def skip(self):
            return self._skip

    def update_repository(self, args):
        if args.opts.has_section('repository'):
            gpgkey = args.opts.get('repository', 'gpgkey')
            keyring = args.opts.get('repository', 'keyring')
        else:
            return
        if not os.access(self.af, os.X_OK):
            return
        if not os.access(self.gpg, os.X_OK):
            return
        distribution = os.path.basename(args.directory)
        archive = args.directory
        pool = os.path.join(archive, 'pool')
        distslink = os.path.join(archive, 'dists', distribution)
        if not os.path.islink(distslink):
            if not os.path.isdir(os.path.join(archive, 'dists')):
                os.makedirs(os.path.join(archive, 'dists'))
            tmpdir = mkdtemp(prefix='.', dir=os.path.join(archive, 'dists'))
            os.symlink(tmpdir, distslink)
        dists = mkdtemp(prefix='.', dir=os.path.join(archive, 'dists'))
        for arch in [a for a in (args.architecture,
                                 args.hostarchitecture) if a is not None]:
            packages = os.path.join(dists, 'main', 'binary-%s' % arch)
            packages_file = os.path.join(packages, 'Packages')
            arch_release_file = os.path.join(packages, 'Release')
            release_file = os.path.join(dists, 'Release')
            release_gpg = os.path.join(dists, 'Release.gpg')
            inrelease_gpg = os.path.join(dists, 'InRelease')
            for dir in (pool, packages):
                if not os.path.isdir(dir):
                    os.makedirs(dir)
            with self.Lock(distribution, arch) as lock:
                if not lock.skip():
                    with open(packages_file, 'w') as fd:
                        Popen([self.af, '-a', arch, 'packages', 'pool'],
                              stdout=fd, stderr=PIPE, cwd=archive).wait()
                    with open(arch_release_file, 'w') as fd:
                        fd.write('Origin: Deb-O-Matic\n')
                        fd.write('Label: Deb-O-Matic\n')
                        fd.write('Archive: %s\n' % distribution)
                        fd.write('Component: main\n')
                        fd.write('Architecture: %s\n' % arch)
                    with open(release_file, 'w') as fd:
                        afstring = 'APT::FTPArchive::Release::'
                        Popen([self.af, '-qq',
                               '-o', '%sOrigin=Deb-o-Matic' % afstring,
                               '-o', '%sLabel=Deb-o-Matic' % afstring,
                               '-o', '%sSuite=%s' % (afstring, distribution),
                               '-o', '%sArchitectures=%s' % (afstring, arch),
                               '-o', '%sComponents=main' % afstring,
                               'release',
                               'dists/%s' % os.path.basename(dists)],
                              stdout=fd, stderr=PIPE, cwd=archive).wait()
                    with open(release_file, 'r+') as fd:
                        data = fd.read()
                        fd.seek(0)
                        fd.write(data.replace('MD5Sum',
                                 'NotAutomatic: yes\nMD5Sum'))
                with open(release_gpg, 'w') as fd:
                    Popen([self.gpg, '--no-default-keyring', '--homedir',
                           keyring, '-u', gpgkey, '--yes', '-a', '-o', fd.name,
                           '-b', release_file], cwd=archive).wait()
                with open(inrelease_gpg, 'w') as fd:
                    Popen([self.gpg, '--no-default-keyring', '--homedir',
                           keyring, '-u', gpgkey, '--yes', '-a', '-o', fd.name,
                           '--clearsign', release_file], cwd=archive).wait()
        olddists = os.readlink(distslink)
        (tmp, tmplink) = mkstemp(prefix='.',
                                 dir=os.path.dirname(olddists))
        os.close(tmp)
        os.unlink(tmplink)
        os.symlink(dists, tmplink)
        os.chmod(dists, S_IRWXU | S_IRGRP | S_IXGRP |
                 S_IROTH | S_IXOTH)
        os.rename(tmplink, distslink)
        try:
            rmtree(olddists)
        except FileNotFoundError:
            pass