File: commands.py

package info (click to toggle)
debomatic 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 716 kB
  • ctags: 153
  • sloc: python: 1,653; sh: 74; makefile: 46
file content (196 lines) | stat: -rw-r--r-- 8,287 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
# Deb-o-Matic
#
# Copyright (C) 2007-2014 Luca Falavigna
#
# Author: 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; version 3 of the License.
#
# 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.

import os
from ast import literal_eval
from glob import glob
from logging import debug, error, info
from re import findall, sub
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError

from .build import Build
from .gpg import GPG


class Command():

    def __init__(self, opts, pool, commandfile):
        (self.opts, self.rtopts, self.conffile) = opts
        self.pool = pool
        self.configdir = self.opts.get('default', 'configdir')
        self.packagedir = self.opts.get('default', 'packagedir')
        self.cmdfile = os.path.join(self.packagedir, commandfile)

    def fetch_dsc(self):
        parms = {}
        self.data = None
        conf = {'mirror': ('[^#]?MIRRORSITE="?(.*[^"])"?\n', 'MIRRORSITE'),
                'components': ('[^#]?COMPONENTS="?(.*[^"])"?\n', 'COMPONENTS')}
        try:
            with open(self.originconf, 'r') as fd:
                data = fd.read()
        except IOError:
            error(_('Unable to open %s') % self.originconf)
            return
        for elem in conf:
            try:
                parms[elem] = findall(conf[elem][0], data)[0]
            except IndexError:
                error(_('Please set %(parm)s in %s(conf)s') %
                      {'parm': conf[elem][0], 'conf': self.originconf})
                return
        for component in parms['components'].split():
            request = Request('%s/pool/%s/%s/%s/%s' %
                              (parms['mirror'], component,
                               findall('^lib\S|^\S', self.package)[0],
                               self.package, self.dscname))
            debug(_('Requesting URL %s') % request.get_full_url())
            try:
                debug(_('Downloading missing %s') % self.dscname)
                self.data = urlopen(request).read()
                break
            except (HTTPError, URLError):
                error(_('Unable to fetch %s') %
                      '_'.join((self.package, self.version)))

    def mangle_version(self, version):
        self.version = sub('^\d+\:', '', version)

    def map_distribution(self):
        self.rtopts.read(self.conffile)
        if self.rtopts.has_option('runtime', 'mapper'):
            try:
                mapper = literal_eval(self.rtopts.get('runtime', 'mapper'))
            except SyntaxError:
                pass
            else:
                if isinstance(mapper, dict):
                    if self.target in mapper:
                        debug(_('%(mapped)s mapped as %(mapper)s') %
                              {'mapped': self.target,
                               'mapper': mapper[self.target]})
                        self.target = mapper[self.target]
                    if self.origin in mapper:
                        debug(_('%(mapped)s mapped as %(mapper)s') %
                              {'mapped': self.origin,
                               'mapper': mapper[self.origin]})
                        self.origin = mapper[self.origin]

    def process_command(self):
        info(_('Processing %s') % os.path.basename(self.cmdfile))
        try:
            with GPG(self.opts, self.cmdfile) as gpg:
                try:
                    self.uploader = gpg.check()
                except RuntimeError:
                    os.remove(self.cmdfile)
                    error(gpg.error())
                    return
        except IOError:
            return
        with open(self.cmdfile, 'r') as fd:
            cmd = fd.read()
        os.remove(self.cmdfile)
        cmd_builddep = findall('\s?builddep\s+(\S+)_(\S+) (\S+) (.*)', cmd)
        cmd_rm = findall('\s?rm\s+(.*)', cmd)
        cmd_rebuild = findall('\s?rebuild\s+(\S+)_(\S+) (\S+) ?(\S*)', cmd)
        cmd_porter = findall('\s?porter\s+(\S+)_(\S+) (\S+) (.*)', cmd)
        if cmd_builddep:
            self.process_builddep(cmd_builddep)
        if cmd_rm:
            self.process_rm(cmd_rm)
        if cmd_porter:
            self.process_porter(cmd_porter)
        if cmd_rebuild:
            self.process_rebuild(cmd_rebuild)

    def process_builddep(self, packages):
        debug(_('Performing a package rebuild with extra dependencies'))
        for package in packages:
            self.package = package[0]
            self.mangle_version(package[1])
            self.dscname = '%s_%s.dsc' % (self.package, self.version)
            self.target = package[2]
            self.origin = None
            self.extrabd = package[3]
            self.map_distribution()
            self.originconf = os.path.join(self.configdir, self.target)
            self.fetch_dsc()
            if self.data:
                dsc = os.path.join(self.packagedir, self.dscname)
                with open(dsc, 'wb') as fd:
                    fd.write(self.data)
                b = Build((self.opts, self.rtopts, self.conffile), dsc=dsc,
                          distribution=self.target, extrabd=self.extrabd,
                          uploader=self.uploader)
                if self.pool.schedule(b.build):
                    debug(_('Thread for %s scheduled') % os.path.basename(dsc))

    def process_porter(self, packages):
        debug(_('Performing a porter build'))
        for package in packages:
            self.package = package[0]
            self.mangle_version(package[1])
            self.dscname = '%s_%s.dsc' % (self.package, self.version)
            self.target = package[2]
            self.origin = None
            self.debopts = package[3]
            self.map_distribution()
            self.originconf = os.path.join(self.configdir, self.target)
            self.fetch_dsc()
            if self.data:
                dsc = os.path.join(self.packagedir, self.dscname)
                with open(dsc, 'wb') as fd:
                    fd.write(self.data)
                b = Build((self.opts, self.rtopts, self.conffile), dsc=dsc,
                          distribution=self.target, debopts=self.debopts,
                          uploader=self.uploader)
                if self.pool.schedule(b.build):
                    debug(_('Thread for %s scheduled') % os.path.basename(dsc))

    def process_rebuild(self, packages):
        debug(_('Performing a package rebuild'))
        for package in packages:
            self.package = package[0]
            self.mangle_version(package[1])
            self.dscname = '%s_%s.dsc' % (self.package, self.version)
            self.target = package[2]
            self.origin = package[3] if package[3] else package[2]
            self.map_distribution()
            self.originconf = os.path.join(self.configdir, self.origin)
            self.fetch_dsc()
            if self.data:
                dsc = os.path.join(self.packagedir, self.dscname)
                with open(dsc, 'wb') as fd:
                    fd.write(self.data)
                b = Build((self.opts, self.rtopts, self.conffile), dsc=dsc,
                          distribution=self.target, origin=self.origin,
                          uploader=self.uploader)
                if self.pool.schedule(b.build):
                    debug(_('Thread for %s scheduled') % os.path.basename(dsc))

    def process_rm(self, filesets):
        debug(_('Removing files'))
        for files in filesets:
            for pattern in files.split():
                pattern = os.path.basename(pattern)
                for absfile in glob(os.path.join(self.packagedir, pattern)):
                    debug(_('Removing %s') % pattern)
                    os.remove(absfile)