File: mock.py

package info (click to toggle)
jhbuild 3.38.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,640 kB
  • sloc: python: 11,621; sh: 4,548; makefile: 204; ansic: 19; sed: 16
file content (211 lines) | stat: -rw-r--r-- 6,695 bytes parent folder | download | duplicates (3)
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
# jhbuild - a tool to ease building collections of source packages
# Copyright (C) 2001-2006  James Henstridge
# Copyright (C) 2007-2008  Frederic Peters
#
#   mock.py: mock objects for unit testing
#
# 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 2 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import time
import os
import tempfile

import jhbuild.frontends.buildscript
import jhbuild.versioncontrol
import jhbuild.errors
import jhbuild.config
from jhbuild.utils import _

class Config(jhbuild.config.Config):
    buildroot = tempfile.mkdtemp(prefix='jhbuild-tests-')
    builddir_pattern = '%s'
    use_lib64 = False
    noxvfb = True

    force_policy = False
    build_policy = 'all'

    nonetwork = False
    nobuild = False
    makeclean = False
    makecheck = False
    makedist = False
    makedistcheck = False
    makedistclean = False
    nopoison = False
    makecheck_advisory = False
    module_makecheck = {}
    module_nopoison = {}
    noinstall = False
    forcecheck = False
    partial_build = True
    autogenargs = ''
    module_autogenargs = {}
    module_extra_env = {}
    makeargs = ''
    module_makeargs = {}
    build_targets = ['install']
    exit_on_error = False
    disable_Werror = False

    min_age = None

    prefix = os.path.join(buildroot, 'prefix')
    top_builddir = os.path.join(buildroot, '_jhbuild')

    def __init__(self):
        pass

class PackageEntry:
    def __init__(self, package, version, manifest,
                 metadata):
        self.package = package # string
        self.version = version # string
        self.manifest = manifest # list of strings
        self.metadata = metadata # hash of string to value

class PackageDB:
    time_delta = 0

    def __init__(self, uptodate = False):
        self.force_uptodate = uptodate
        self.entries = {}

    def check(self, package, version=None):
        if self.force_uptodate:
            return self.force_uptodate
        entry = self.entries.get(package)
        if not entry:
            return None
        return entry.version == version

    def add(self, package, version, manifest, configure_cmd=None):
        entry = PackageEntry(package, version, [], {})
        entry.metadata['installed-date'] = time.time()+self.time_delta
        self.entries[package] = entry

    def remove(self, package):
        del self.entries[package]

    def installdate(self, package):
        entry = self.entries.get(package)
        if entry is None:
            return None
        return entry.metadata['installed-date']

    def get(self, package):
        '''Return entry if package is installed, otherwise return None.'''
        return self.entries.get(package)

class BuildScript(jhbuild.frontends.buildscript.BuildScript):
    execute_is_failure = False

    def __init__(self, config, module_list, moduleset):
        self.config = config
        self.modulelist = module_list
        self.moduleset = moduleset
        self.packagedb = moduleset.packagedb
        self.actions = []
    
    def set_action(self, action, module, module_num=-1, action_target=None):
        self.actions.append('%s:%s' % (module.name, action))

    def execute(self, command, hint=None, cwd=None, extra_env=None):
        if self.execute_is_failure:
            raise jhbuild.errors.CommandError('Mock command asked to fail')

    def message(self, msg, module_num = -1):
        pass
    
    def handle_error(self, module, state, nextstate, error, altstates):
        self.actions[-1] = self.actions[-1] + ' [error]'
        return 'fail'

class MockModule(jhbuild.modtypes.Package):
    PHASE_FORCE_CHECKOUT = 'force-checkout'
    PHASE_CHECKOUT       = 'checkout'
    PHASE_CLEAN          = 'clean'
    PHASE_DISTCLEAN      = 'distclean'
    PHASE_CONFIGURE      = 'configure'
    PHASE_BUILD          = 'build'
    PHASE_CHECK          = 'check'
    PHASE_DIST           = 'dist'
    PHASE_INSTALL        = 'install'

    def do_checkout(self, buildscript):
        buildscript.set_action(_('Checking out'), self)
        if self.check_build_policy(buildscript) == self.PHASE_DONE:
            raise jhbuild.errors.SkipToEnd()
    do_checkout.error_phases = [PHASE_FORCE_CHECKOUT]

    def skip_checkout(self, buildscript, last_phase):
        # skip the checkout stage if the nonetwork flag is set
        if not self.branch.may_checkout(buildscript):
            if self.check_build_policy(buildscript) == self.PHASE_DONE:
                raise jhbuild.errors.SkipToEnd()
            return True
        return False

    def do_clean(self, buildscript):
        buildscript.set_action(_('Cleaning'), self)
    do_clean.depends = [PHASE_CONFIGURE]
    do_clean.error_phases = [PHASE_FORCE_CHECKOUT, PHASE_CONFIGURE]

    def do_configure(self, buildscript):
        buildscript.set_action(_('Configuring'), self)
    do_configure.depends = [PHASE_CHECKOUT]

    def do_build(self, buildscript):
        buildscript.set_action(_('Building'), self)
    do_build.depends = [PHASE_CONFIGURE]
    do_build.error_phases = [PHASE_FORCE_CHECKOUT, PHASE_CONFIGURE,
                             PHASE_CLEAN, PHASE_DISTCLEAN]

    def do_install(self, buildscript):
        buildscript.set_action(_('Installing'), self)
        buildscript.moduleset.packagedb.add(self.name, self.get_revision(), None)
    do_install.depends = [PHASE_BUILD]

    def do_check(self, buildscript):
        buildscript.set_action(_('Checking'), self)
    do_check.depends = [PHASE_BUILD]
    do_check.error_phases = [PHASE_CONFIGURE]


class Branch(jhbuild.versioncontrol.Branch):
    def __init__(self, tmpdir):
        self._tmpdir = tmpdir

    @property
    def srcdir(self):
        return self._tmpdir

    @property
    def checkoutdir(self):
        return self._tmpdir

    def checkout(self, buildscript):
        pass

    def may_checkout(self, buildscript):
        if buildscript.config.nonetwork:
            return False
        return True

    def tree_id(self):
        return 'foo'

def raise_command_error(*args):
    raise jhbuild.errors.CommandError('Mock Command Error Exception')