File: build.py

package info (click to toggle)
debomatic 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 344 kB
  • ctags: 69
  • sloc: python: 642; sh: 67; makefile: 12
file content (146 lines) | stat: -rw-r--r-- 6,419 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
# Deb-o-Matic
#
# Copyright (C) 2007-2009 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
import sys
import threading
from re import findall
from Debomatic import gpg
from Debomatic import locks
from Debomatic import packages
from Debomatic import pbuilder
from Debomatic import Options
from Debomatic import packagequeue
from Debomatic import modules

def build_process():
    directory = Options.get('default', 'packagedir')
    configdir = Options.get('default', 'configdir')
    package = packages.select_package(directory)
    if package:
        distopts = parse_distribution_options(directory, configdir, package)
        try:
            fd = os.open(os.path.join(directory, package), os.O_RDONLY)
        except:
            print _('Unable to open %s') % os.path.join(directory, package)
            packages.del_package(package)
            sys.exit(-1)
        try:
            for entry in findall('\s\w{32}\s\d+\s\S+\s\S+\s(.*)', os.read(fd, os.fstat(fd).st_size)):
                packagequeue[package].append(os.path.join(directory, entry))
        except:
            print _('Bad .changes file: %s') % os.path.join(directory, package)
            sys.exit(-1)
        packagequeue[package].append(os.path.join(directory, package))
        os.close(fd)
        try:
            gpg.check_changes_signature(os.path.join(directory, package))
        except RuntimeError, error:
            packages.rm_package(package)
            print error
            sys.exit(-1)
	packages.fetch_missing_files(package, packagequeue[package], directory, distopts)
        distdir = os.path.join(directory, distopts['distribution'])
        try:
            pbuilder.setup_pbuilder(distdir, configdir, distopts)
        except RuntimeError, error:
            packages.del_package(package)
            print error
            sys.exit(-1)
        build_package(directory, os.path.join(configdir, distopts['distribution']), distdir, package, distopts)

def parse_distribution_options(packagedir, configdir, package):
    options = dict()
    try:
        fd = os.open(os.path.join(packagedir, package), os.O_RDONLY)
    except:
        print 'Unable to open %s' % os.path.join(packagedir, package)
        packages.del_package(package)
        sys.exit(-1)
    try:
        distro = findall('Distribution:\s+(\w+)', os.read(fd, os.fstat(fd).st_size))[0]
        options['distribution'] = distro.lower()
    except:
        print _('Bad .changes file: %s') % os.path.join(packagedir, package)
        packages.del_package(package)
        sys.exit(-1)
    os.close(fd)
    try:
        fd = os.open(os.path.join(configdir,options['distribution']), os.O_RDONLY)
    except:
        print _('Unable to open %s') % os.path.join(configdir, options['distribution'])
        packages.del_package(package)
        sys.exit(-1)
    conf = os.read(fd, os.fstat(fd).st_size)
    os.close(fd)
    if not len(findall('[^#]?MIRRORSITE="?(.*[^"])"?\n', conf)):
        print _('Please set DISTRIBUTION in %s') % os.path.join(configdir, options['distribution'])
        packages.del_package(package)
        sys.exit(-1)
    try:
        options['mirror'] = findall('[^#]?MIRRORSITE="?(.*[^"])"?\n', conf)[0]
    except:
        print _('Please set MIRRORSITE in %s') % os.path.join(configdir, options['distribution'])
        packages.del_package(package)
        sys.exit(-1)
    try:
        options['components'] = findall('[^#]?COMPONENTS="?(.*[^"])"?\n', conf)[0]
    except:
        print _('Please set COMPONENTS in %s') % os.path.join(configdir, options['distribution'])
        packages.del_package(package)
        sys.exit(-1)
    try:
        options['debootstrap'] = findall('[^#]?DEBOOTSTRAP="?(.*[^"])"?\n', conf)[0]
    except:
        print _('Please set DEBOOTSTRAP in %s') % os.path.join(configdir, options['distribution'])
        packages.del_package(package)
        sys.exit(-1)
    return options

def build_package(directory, configfile, distdir, package, distopts):
    mod_sys = modules.Module()
    if not locks.buildlock_acquire():
        packages.del_package(package)
        sys.exit(-1)
    dscfile = None
    if not os.path.exists(os.path.join(distdir, 'pool')):
        os.mkdir(os.path.join(distdir, 'pool'))
    for pkgfile in packagequeue[package]:
            if not dscfile:
                dscfile = findall('(.*\.dsc$)', pkgfile)
    try:
        packageversion = findall('.*/(.*).dsc$', dscfile[0])[0]
    except:
        packageversion = None
    if not os.path.exists(os.path.join(distdir, 'pool', packageversion)):
        os.mkdir(os.path.join(distdir, 'pool', packageversion))
    mod_sys.execute_hook('pre_build', { 'directory': distdir, 'package': packageversion, \
              'cfg': configfile, 'distribution': distopts['distribution'], 'dsc': dscfile[0]})
    base = '--basepath' if Options.get('default', 'builder') == 'cowbuilder' else '--basetgz'
    os.system('%(builder)s --build %(basetype)s %(directory)s/%(distribution)s \
              --override-config --configfile %(cfg)s --logfile %(directory)s/pool/%(package)s/%(package)s.buildlog \
              --buildplace %(directory)s/build --buildresult %(directory)s/pool/%(package)s \
              --aptcache %(directory)s/aptcache %(dsc)s >/dev/null 2>&1' \
              % { 'builder': Options.get('default', 'builder'), 'basetype': base, 'directory': distdir, \
              'package': packageversion, 'cfg': configfile, 'distribution': distopts['distribution'], 'dsc': dscfile[0]})
    mod_sys.execute_hook('post_build', { 'directory': distdir, 'package': packageversion, \
              'cfg': configfile, 'distribution': distopts['distribution'], 'dsc': dscfile[0]})
    packages.rm_package(package)
    locks.buildlock_release()