aboutsummaryrefslogtreecommitdiff
blob: c6f333e8cf96bd61cd3f96234d82a0726733117d (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# Copyright 2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import os

from portage.versions import cpv_getversion, pkgsplit, catpkgsplit
from portage import _pms_eapi_re, eapi_is_supported, auxdbkeys

from twisted.internet import defer
from twisted.python import log

from buildbot.process.buildstep import BuildStep
from buildbot.process.results import SUCCESS
from buildbot.process.results import FAILURE
from buildbot.process.results import WARNINGS
from buildbot.process.results import SKIPPED
from buildbot.process import remotecommand
from buildbot.plugins import steps

def GetPythonVersion(pyprop):
    py_list = pyprop.replace(' ', '').replace('\n ', '').split('.')
    return '.'.join([py_list[0], py_list[1]]).lower()

def PersOutputOfGetEapi(rc, stdout, stderr):
    eapi = None
    # split the lines
    for line in stdout.split('\n'):
        if line.startswith('EAPI'):
            print(line[-1])
            m = _pms_eapi_re.match(line)
            if m is not None:
                eapi = m.group(2)
                print(eapi)
    if eapi is None or not eapi_is_supported(eapi):
        print('ERROR: invalid eapi or not found')
        eapi = False
    else:
        print(eapi)
    return {
        'eapi' : eapi
        }

def PersOutputOfGetAuxdb(rc, stdout, stderr):
    metadata = None
    NoSplit = ['DESCRIPTION']
    ignore_list = ['SRC_URI']
    #make dict of the stout
    index = 1
    metadata_line_dict = {}
    for text_line in stdout.splitlines():
        metadata_line_dict[index] = text_line
        index = index + 1
    if not stderr == '':
        print('stderr')
    # should have 22 lines
    if len(auxdbkeys) != index -1:
        # number of lines is incorrect.
        print('ERROR: Number of lines is incorrect')
        print(metadata_line_dict)
        return {
            'auxdb' : metadata
            }
    # split all keys to list instead of speces
    metadata = {}
    i = 1
    for key in auxdbkeys:
        if metadata_line_dict[i][-1] == '=' or key in ignore_list:
            metadata[key] = False
        else:
            if ' ' in metadata_line_dict[i] and key not in NoSplit:
                metadata[key] = metadata_line_dict[i].replace(key + '=', '').split(' ')
            else:
                metadata[key] = []
                metadata[key].append(metadata_line_dict[i].replace(key + '=', ''))
        i = i + 1
    return {
        'auxdb' : metadata
        }

class SetEnvForGetAuxdb(BuildStep):

    name = 'SetEnvForGetAuxdb'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        ebuild_commands = []
        ebuild_env = {}
        if not self.getProperty("eapi"):
            return FAILURE
        #Setup ENV
        category = yield catpkgsplit(self.getProperty("cpv"))[0]
        package = yield catpkgsplit(self.getProperty("cpv"))[1]
        version = yield catpkgsplit(self.getProperty("cpv"))[2]
        revision = yield catpkgsplit(self.getProperty("cpv"))[3]
        python_version = GetPythonVersion(self.getProperty("python_version"))
        portage_bin_path = yield os.path.join('/usr/lib/portage', python_version)
        ebuild_sh_bin = 'ebuild.sh'
        ebuild_sh_path = yield os.path.join(portage_bin_path, ebuild_sh_bin)
        ebuild_env['EAPI'] = self.getProperty("eapi")
        #ebuild_env['PORTAGE_DEBUG'] = '1'
        ebuild_env['EBUILD_PHASE'] = 'depend'
        ebuild_env['CATEGORY'] = category
        ebuild_env['P'] = package + '-' + version
        ebuild_env['PN'] = package
        ebuild_env['PR'] = revision
        ebuild_env['PV'] = version
        if revision == 'r0':
            ebuild_env['PF'] = ebuild_env['P']
            ebuild_env['PVR'] = version
        else:
            ebuild_env['PF'] = ebuild_env['P'] + '-' + revision
            ebuild_env['PVR'] = version + '-' + revision
        ebuild_env['PORTAGE_BIN_PATH'] = portage_bin_path
        ebuild_env['EBUILD'] = self.getProperty("ebuild_file")
        ebuild_env['PORTAGE_PIPE_FD'] = '1'
        ebuild_env['WORKDIR'] = yield os.path.join('/var/tmp/portage', 'portage', category, ebuild_env['PF'], 'work')
        #FIXME: get a list of repository_path
        ebuild_env['PORTAGE_ECLASS_LOCATIONS'] = self.getProperty("repository_path")
        yield self.build.addStepsAfterCurrentStep([steps.SetPropertyFromCommand(
                                                            name = 'RunGetAuxdb',
                                                            haltOnFailure = True,
                                                            flunkOnFailure = True,
                                                            command=[ebuild_sh_path, 'depend'],
                                                            env=ebuild_env,
                                                            workdir=self.getProperty("builddir"),
                                                            strip=False,
                                                            extract_fn=PersOutputOfGetAuxdb
                                                            )])
        return SUCCESS

class AddVersion(BuildStep):
    
    name = 'AddVersion'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.version_data = {}
        self.version_data['name'] = self.getProperty("version")
        self.version_data['package_uuid'] = self.getProperty("package_data")['uuid']
        self.version_data['file_hash'] = 'None'
        self.version_data['commit_id'] = self.getProperty("commit_id")
        self.version_data['uuid'] = yield self.gentooci.db.versions.addVersion(
                                            self.version_data['name'],
                                            self.version_data['package_uuid'],
                                            self.version_data['file_hash'],
                                            self.version_data['commit_id']
                                            )
        print(self.version_data)
        self.setProperty("version_data", self.version_data, 'version_data')
        return SUCCESS

class GetCommitdata(BuildStep):

    name = 'GetCommitdata'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    #@defer.inlineCallbacks
    def run(self):
        print(self.getProperty("change_data"))
        self.setProperty('commit_id', self.getProperty("change_data")['revision'], 'commit_id')
        return SUCCESS

#FIXME: use versions_metadata table
class AddVersionKeyword(BuildStep):

    name = 'AddVersionKeyword'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    
    @defer.inlineCallbacks
    def add_keyword_data(self, keyword):
        keyword_data = {}
        keyword_data['keyword'] = keyword
        keyword_data['id'] = yield self.gentooci.db.keywords.addKeyword(keyword_data['keyword'])
        return keyword_data
    
    @defer.inlineCallbacks
    def check_keyword_data(self, keyword):
        keyword_data = yield self.gentooci.db.keywords.getKeywordByName(keyword)
        if keyword_data is None:
            keyword_data = yield self.add_keyword_data(keyword)
        return keyword_data['id']

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.version_keyword_dict = {}
        auxdb = self.getProperty("auxdb")['KEYWORDS']
        if not auxdb or not isinstance(auxdb, list):
            self.version_keyword_dict = None
            self.setProperty('version_keyword_dict', self.version_keyword_dict, 'version_keyword_dict')
            return SUCCESS
        print(auxdb)
        for keyword in auxdb:
            status = 'stable'
            if keyword[0] in ["~"]:
                keyword = keyword[1:]
                status = 'unstable'
            elif keyword[0] in ["-"]:
                keyword = keyword[1:]
                status = 'negative'
            elif keyword[0] in ["*"]:
                keyword = keyword[1:]
                status = 'all'
            version_keyword_data = {}
            version_keyword_data['version_uuid'] = self.getProperty("version_data")['uuid']
            version_keyword_data['keyword_id'] = yield self.check_keyword_data(keyword)
            version_keyword_data['status'] = status
            version_keyword_data['uuid'] = yield self.gentooci.db.versions.addKeyword(
                                                version_keyword_data['version_uuid'],
                                                version_keyword_data['keyword_id'],
                                                version_keyword_data['status'])
            self.version_keyword_dict[keyword] = version_keyword_data
        self.setProperty('version_keyword_dict', self.version_keyword_dict, 'version_keyword_dict')
        return SUCCESS

class AddVersionRestrictions(BuildStep):

    name = 'AddVersionRestrictions'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        auxdb = self.getProperty("auxdb")['RESTRICT']
        if not auxdb or not isinstance(auxdb, list):
            return SKIPPED
        for restrict in auxdb:
            version_metadata_data = {}
            version_metadata_data['version_uuid'] = self.getProperty("version_data")['uuid']
            version_metadata_data['metadata'] = 'restrict'
            version_metadata_data['value'] = restrict
            version_metadata_data['id'] = yield self.gentooci.db.versions.addMetadata(
                                                version_metadata_data['version_uuid'],
                                                version_metadata_data['metadata'],
                                                version_metadata_data['value'])
        return SUCCESS

class AddVersionIUse(BuildStep):

    name = 'AddVersionIUse'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        auxdb = self.getProperty("auxdb")['IUSE']
        if not auxdb or not isinstance(auxdb, list):
            return SKIPPED
        for iuse in auxdb:
            version_metadata_data = {}
            version_metadata_data['version_uuid'] = self.getProperty("version_data")['uuid']
            version_metadata_data['metadata'] = 'iuse'
            version_metadata_data['value'] = iuse
            version_metadata_data['id'] = yield self.gentooci.db.versions.addMetadata(
                                                version_metadata_data['version_uuid'],
                                                version_metadata_data['metadata'],
                                                version_metadata_data['value'])
        return SUCCESS

class CheckPath(BuildStep):

    name = 'CheckPath'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.repository_basedir = yield os.path.join(self.getProperty("worker_basedir"), self.getProperty('portage_repos_path'))
        self.repository_path = yield os.path.join(self.repository_basedir, self.getProperty("repository_data")['name'])
        self.cp_path = yield pkgsplit(self.getProperty("cpv"))[0]
        self.file_name = yield self.getProperty("package_data")['name'] + '-' + self.getProperty("version") + '.ebuild'
        self.ebuild_file = yield os.path.join(self.repository_path, self.cp_path, self.file_name)
        print(self.ebuild_file)
        cmd = remotecommand.RemoteCommand('stat', {'file': self.ebuild_file})
        yield self.runCommand(cmd)
        if cmd.didFail():
            self.ebuild_file = None
        #self.ebuild_file_hash = None
        self.setProperty('ebuild_file', self.ebuild_file, 'ebuild_file')
        #self.setProperty('ebuild_file_hash', self.ebuild_file_hash, 'ebuild_file_hash')
        self.setProperty('repository_path', self.repository_path, 'repository_path')
        return SUCCESS

class TriggerBuildCheck(BuildStep):

    name = 'TriggerBuildCheck'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        yield self.build.addStepsAfterCurrentStep([steps.Trigger(
                schedulerNames=['build_request_data'],
                        waitForFinish=False,
                        updateSourceStamp=False,
                        set_properties={
                            'cpv' : self.getProperty("cpv"),
                            'version_data' : self.getProperty("version_data"),
                            'version_keyword_dict' : self.getProperty('version_keyword_dict'),
                            'repository_data' : self.getProperty("repository_data"),
                        }
                )])
        return SUCCESS

class DeleteOldVersion(BuildStep):

    name = 'DeleteOldVersion'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        yield self.gentooci.db.versions.delVersion(self.getProperty("old_version_data")['uuid'])
        return SUCCESS

class SetupPropertys(BuildStep):

    name = 'Setup propertys for checking V'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        if self.gentooci.config.project['worker_config']['basedir'] is None:
            worker_basedir = self.getProperty('builddir').split('/builds')[0]
        else:
            worker_basedir = self.gentooci.config.project['worker_config']['basedir']
        self.setProperty('worker_basedir', worker_basedir, 'worker_basedir')
        portage_repos_path = os.path.join(worker_basedir, self.gentooci.config.project['worker_config']['repository_basedir'])
        self.setProperty('portage_repos_path', portage_repos_path, 'portage_repos_path')
        print(self.getProperty("cpv"))
        self.version = yield cpv_getversion(self.getProperty("cpv"))
        print(self.version)
        self.old_version_data = yield self.gentooci.db.versions.getVersionByName(self.version, self.getProperty("package_data")['uuid'])
        print(self.old_version_data)
        self.setProperty("old_version_data", self.old_version_data, 'old_version_data')
        self.setProperty("version", self.version, 'version')
        return SUCCESS

class CheckEAPI(BuildStep):

    name = 'CheckEAPI'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    #@defer.inlineCallbacks
    def run(self):
        # Run head 30 if we did't find it on head 10
        if self.getProperty("eapi"):
            return SKIPPED
        self.build.addStepsAfterCurrentStep([
                steps.SetPropertyFromCommand(
                                            name = 'RunGetEAPI',
                                            haltOnFailure = True,
                                            flunkOnFailure = True,
                                            command=['head', '-n','30', self.getProperty("ebuild_file")],
                                            strip=False,
                                            extract_fn=PersOutputOfGetEapi
                                            )
            ])
        return SUCCESS

class SetupStepsForCheckV(BuildStep):
    
    name = 'Setup steps for Checking V'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        addStepVData = []
        print(self.getProperty("ebuild_file"))
        print(self.getProperty("old_version_data"))
        #print(self.getProperty("ebuild_file_hash"))
        if self.getProperty("ebuild_file") is None:
            addStepVData.append(TriggerBuildCheck())
            if self.getProperty("old_version_data") is None:
                return WARNINGS
            else:
                addStepVData.append(DeleteOldVersion())
        else:
            if self.getProperty("old_version_data") is not None:
                addStepVData.append(DeleteOldVersion())
            # get ebuild aux metadata
            addStepVData.append(steps.SetPropertyFromCommand(
                                                            name = 'RunGetEAPI',
                                                            haltOnFailure = True,
                                                            flunkOnFailure = True,
                                                            command=['head', '-n', '10', self.getProperty("ebuild_file")],
                                                            strip=False,
                                                            extract_fn=PersOutputOfGetEapi
                                                            ))
            addStepVData.append(CheckEAPI())
            addStepVData.append(steps.SetPropertyFromCommand(
                                                            name = 'GetPythonVersion',
                                                            haltOnFailure = True,
                                                            flunkOnFailure = True,
                                                            command=['python', '-V'],
                                                            strip=False,
                                                            property="python_version"
                                                            ))
            addStepVData.append(SetEnvForGetAuxdb())
            # get commit data
            addStepVData.append(GetCommitdata())
            # add version to db
            addStepVData.append(AddVersion())
            # add metadata to db
            addStepVData.append(AddVersionKeyword())
            addStepVData.append(AddVersionRestrictions())
            addStepVData.append(AddVersionIUse())
            # Trigger build_request_check
            addStepVData.append(TriggerBuildCheck())
        yield self.build.addStepsAfterCurrentStep(addStepVData)
        return SUCCESS