aboutsummaryrefslogtreecommitdiff
blob: 431fe9ab711edc727c1a68e29904d0672c7be630 (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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# Copyright 2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import os
import io
from pathlib import Path

from portage import config as portage_config
from portage import auxdbkeys
from portage import _encodings
from portage import _unicode_encode
from portage import _parse_eapi_ebuild_head, eapi_is_supported
from portage.versions import cpv_getversion, pkgsplit, catpkgsplit
from portage import portdbapi

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 SKIPPED
from buildbot.plugins import steps

from buildbot_gentoo_ci.steps import master as master_steps
from buildbot_gentoo_ci.utils.use import getIUseValue

@defer.inlineCallbacks
def WriteTextToFile(path, text_list):
    separator = '\n'
    text_string = separator.join(text_list)
    with open(path, "a") as f:
        yield f.write(text_string)
        yield f.write(separator)
        yield f.close

def PersOutputOfEbuildSH(rc, stdout, stderr):
    metadata = None
    NoSplit = []
    NoSplit.append('DESCRIPTION')
    #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
    # should have 22 lines
    if len(auxdbkeys) != index -1:
        # number of lines is incorrect.
        return {
            'auxdb' : metadata
            }
    # split all keys to list instead of speces
    metadata = {}
    i = 1
    for key in auxdbkeys:
        if metadata_line_dict[i] == '':
            metadata[key] = None
        else:
            if ' ' in metadata_line_dict[i] and key not in NoSplit:
                metadata[key] = metadata_line_dict[i].split(' ')
            else:
                metadata[key] = []
                metadata[key].append(metadata_line_dict[i])
        i = i + 1
    return {
        'auxdb' : metadata
        }

class SetMakeProfile(BuildStep):

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

    def __init__(self, workdir=False, **kwargs):
        self.rootworkdir = workdir
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        portage_repos_path = self.getProperty('portage_repos_path')
        project_data = self.getProperty('project_data')
        profile_repository_data = yield self.gentooci.db.repositorys.getRepositoryByUuid(project_data['profile_repository_uuid'])
        makeprofiles_paths = []
        #NOTE: pkgcheck don't support make.profile as a dir
        # we only support one line in db
        makeprofiles_data = yield self.gentooci.db.projects.getAllProjectPortageByUuidAndDirectory(project_data['uuid'], 'make.profile')
        for makeprofile in makeprofiles_data:
            makeprofile_path = yield os.path.join(portage_repos_path, profile_repository_data['name'], 'profiles', makeprofile['value'], '')
        #    makeprofiles_paths.append('../../..' + makeprofile_path)
        #separator = '\n'
        #makeprofile_path_string = separator.join(makeprofiles_paths)
        # yield self.build.addStepsAfterCurrentStep([
        #    steps.StringDownload(makeprofile_path_string + separator,
        #                        workerdest="make.profile/parent",
        #                        workdir='/etc/portage/')
        #    ])
        #NOTE: pkgcheck profile link
        shell_commad_list = [
                    'ln',
                    '-s'
                    ]
        if self.rootworkdir:
            symlink_makeprofile_path = yield os.path.join(self.rootworkdir, 'etc/portage/make.profile')
        else:
            symlink_makeprofile_path = '/etc/portage/make.profile'
        shell_commad_list.append(makeprofile_path)
        shell_commad_list.append(symlink_makeprofile_path)
        yield self.build.addStepsAfterCurrentStep([
            steps.ShellCommand(
                        command=shell_commad_list,
                        workdir='/'
                )
            ])
        log = yield self.addLog('make.profile')
        yield log.addStdout('Profile path: ' + makeprofile_path + '\n')
        return SUCCESS

class SetReposConf(BuildStep):

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

    def __init__(self, workdir=False, **kwargs):
        self.rootworkdir = workdir
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        portage_repos_path = self.getProperty('portage_repos_path')
        project_data = self.getProperty('project_data')
        if self.rootworkdir:
            portage_etc_path = yield os.path.join(self.rootworkdir, 'etc/portage/')
        else:
            portage_etc_path = '/etc/portage/'
        # setup the default.conf
        repos_conf_data = yield self.gentooci.db.projects.getProjectPortageByUuidAndDirectory(project_data['uuid'], 'repos.conf')
        if repos_conf_data is None:
            print('Default repo is not set in repos.conf')
            return FAILURE
        log = yield self.addLog('repos.conf')
        # check if repos_conf_data['value'] is vaild repo name
        separator = '\n'
        default_conf = []
        default_conf.append('[DEFAULT]')
        default_conf.append('main-repo = ' + repos_conf_data['value'])
        default_conf.append('auto-sync = no')
        default_conf_string = separator.join(default_conf)
        yield self.build.addStepsAfterCurrentStep([
            steps.StringDownload(default_conf_string + separator,
                                workerdest="repos.conf/default.conf",
                                workdir=portage_etc_path)
            ])
        # display the default.conf
        yield log.addStdout('File: ' + 'default.conf' + '\n')
        for line in default_conf:
            yield log.addStdout(line + '\n')
        # add all repos that project have in projects_repositorys to repos.conf/reponame.conf
        projects_repositorys_data = yield self.gentooci.db.projects.getRepositorysByProjectUuid(project_data['uuid'])
        for project_repository_data in projects_repositorys_data:
            repository_data = yield self.gentooci.db.repositorys.getRepositoryByUuid(project_repository_data['repository_uuid'])
            repository_path = yield os.path.join(portage_repos_path, repository_data['name'])
            repository_conf = []
            repository_conf.append('[' + repository_data['name'] + ']')
            repository_conf.append('location = ' + repository_path)
            repository_conf.append('sync-uri = ' + repository_data['url'])
            repository_conf.append('sync-type = git')
            repository_conf.append('auto-sync = no')
            repository_conf_string = separator.join(repository_conf)
            filename = repository_data['name'] + '.conf'
            yield self.build.addStepsAfterCurrentStep([
                steps.StringDownload(repository_conf_string + separator,
                                workerdest='repos.conf/' + filename,
                                workdir=portage_etc_path)
                ])
            yield log.addStdout('File: ' + filename + '\n')
            for line in repository_conf:
                yield log.addStdout(line + '\n')
        return SUCCESS

class SetMakeConf(BuildStep):

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

    def __init__(self, workdir=False, **kwargs):
        self.rootworkdir = workdir
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def run(self):
        #FIXME: Make a dict before we pass it to the log
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        project_data = self.getProperty('project_data')
        makeconf_variables_data = yield self.gentooci.db.portages.getVariables()
        if self.rootworkdir:
            portage_etc_path = yield os.path.join(self.rootworkdir, 'etc/portage/')
        else:
            portage_etc_path = '/etc/portage/'
        separator1 = '\n'
        separator2 = ' '
        makeconf_list = []
        log = yield self.addLog('make.conf')
        for k in makeconf_variables_data:
            makeconf_variables_values_data = yield self.gentooci.db.projects.getProjectMakeConfById(project_data['uuid'], k['id'])
            makeconf_variable_list = []
            # CFLAGS
            if k['variable'] == 'CFLAGS' or k['variable'] == 'FCFLAGS':
                makeconf_variable_list.append('-O2')
                makeconf_variable_list.append('-pipe')
                makeconf_variable_list.append('-fno-diagnostics-color')
                #FIXME:
                # Depend on worker we may have to add a diffrent march
                makeconf_variable_list.append('-march=native')
            if k['variable'] == 'CXXFLAGS':
                makeconf_variable_list.append('${CFLAGS}')
            if k['variable'] == 'FFLAGS':
                makeconf_variable_list.append('${FCFLAGS}')
            # Add default setting if use_default
            if project_data['use_default']:
                default_project_data = yield self.gentooci.db.projects.getProjectByName(self.gentooci.config.project['project']['update_db'])
                default_makeconf_variables_values_data = yield self.gentooci.db.projects.getProjectMakeConfById(default_project_data['uuid'], k['id'])
                for v in default_makeconf_variables_values_data:
                    if v['build_id'] == 0:
                        makeconf_variable_list.append(v['value'])
            for v in makeconf_variables_values_data:
                if v['build_id'] == 0:
                    makeconf_variable_list.append(v['value'])
            #NOTE: set it by project
            #if k['variable'] == 'ACCEPT_LICENSE' and makeconf_variable_list != []:
            #    makeconf_variable_list.append('ACCEPT_LICENSE="*"')
            if makeconf_variable_list != []:
                makeconf_variable_string = k['variable'] + '="' + separator2.join(makeconf_variable_list) + '"'
                makeconf_list.append(makeconf_variable_string)
        # add hardcoded variables from config file
        config_makeconfig = self.gentooci.config.project['project']['config_makeconfig']
        for v in config_makeconfig:
            makeconf_list.append(v)
        # add ACCEPT_KEYWORDS from the project_data info
        keyword_data = yield self.gentooci.db.keywords.getKeywordById(project_data['keyword_id'])
        if project_data['status'] == 'unstable':
            makeconf_keyword = '~' + keyword_data['name']
        else:
            makeconf_keyword = keyword_data['name']
        makeconf_list.append('ACCEPT_KEYWORDS="' + makeconf_keyword + '"')
        makeconf_list.append('MAKEOPTS="-j14"')
        makeconf_string = separator1.join(makeconf_list)
        print(makeconf_string)
        yield self.build.addStepsAfterCurrentStep([
            steps.StringDownload(makeconf_string + separator1,
                                workerdest="make.conf",
                                workdir=portage_etc_path)
            ])
        # display the make.conf
        for line in makeconf_list:
            yield log.addStdout(line + '\n')
        return SUCCESS

class SetPackageDefault(BuildStep):

    name = 'SetPackageDefault'
    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']
        separator1 = '\n'
        separator2 = ' '
        log = yield self.addLog('package.*')
        self.aftersteps_list = []
        self.aftersteps_list.append(steps.MakeDirectory(dir='package.use',
                                workdir='/etc/portage/'))
        self.aftersteps_list.append(steps.MakeDirectory(dir='package.env',
                                workdir='/etc/portage/'))
        #FIXME: accept_keywords
        # add the needed package.* settings from db
        # add package use
        package_conf_use_list = []
        package_settings = yield self.gentooci.db.projects.getProjectPortagePackageByUuid(self.getProperty('project_data')['uuid'])
        for package_setting in package_settings:
            if package_setting['directory'] == 'use':
                package_conf_use_list.append(separator2.join(package_setting['package'],package_setting['value']))
        if self.getProperty('use_data') is not None:
            for k, v in self.getProperty('use_data').items():
                    for use, value in v.items():
                        if value:
                            package_conf_use_list.append(separator2.join([k, use]))
                        else:
                            package_conf_use_list.append(separator2.join([k, '-' + use]))
        if package_conf_use_list != []:
            package_conf_use_string = separator1.join(package_conf_use_list)
            self.aftersteps_list.append(
                        steps.StringDownload(package_conf_use_string + separator1,
                            workerdest='default.conf',
                            workdir='/etc/portage/package.use/'
                            )
                        )
            yield log.addStdout('File: ' + 'package.use/default.conf' + separator1)
            for line in package_conf_use_list:
                yield log.addStdout(line + separator1)
        # for test we need to add env and use
        #FIXME: check restrictions, test use mask and required use
        if self.getProperty('projectrepository_data')['test']:
            auxdb_iuses = yield self.gentooci.db.versions.getMetadataByUuidAndMatadata(self.getProperty("version_data")['uuid'], 'iuse')
            for auxdb_iuse in auxdb_iuses:
                iuse, status = getIUseValue(auxdb_iuse['value'])
                if iuse == 'test':
                    self.aftersteps_list.append(
                        steps.StringDownload(separator2.join(['=' + self.getProperty("cpv"),'test']) + separator1,
                            workerdest='test.conf',
                            workdir='/etc/portage/package.use/'
                            )
                        )
                    yield log.addStdout('File: ' + 'package.use/test.conf' + separator1)
                    yield log.addStdout(separator2.join(['=' + self.getProperty("cpv"),'test']) + separator1)
            self.aftersteps_list.append(
                        steps.StringDownload(separator2.join(['=' + self.getProperty("cpv"),'test.conf']) + separator1,
                            workerdest='test.conf',
                            workdir='/etc/portage/package.env/'
                            )
                        )
            yield log.addStdout('File: ' + 'package.env/test.conf' + separator1)
            yield log.addStdout(separator2.join(['=' + self.getProperty("cpv"),'test.conf']) + separator1)
        yield self.build.addStepsAfterCurrentStep(self.aftersteps_list)
        return SUCCESS

class SetEnvDefault(BuildStep):

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

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

    def getPortageEnv(self, portage_env_data, portage_env_dict = {}):
        for project_portage_env in portage_env_data:
            if not project_portage_env['name'] in portage_env_dict:
                portage_env_dict[project_portage_env['name']] = {
                    project_portage_env['makeconf_id'] : []
                    }
            if not project_portage_env['makeconf_id'] in portage_env_dict[project_portage_env['name']]:
                portage_env_dict[project_portage_env['name']][project_portage_env['makeconf_id']] = []
            portage_env_dict[project_portage_env['name']][project_portage_env['makeconf_id']].append(project_portage_env['value'])
        return portage_env_dict

    @defer.inlineCallbacks
    def run(self):
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        project_data = self.getProperty('project_data')
        default_project_data = yield self.gentooci.db.projects.getProjectByName(self.gentooci.config.project['project']['update_db'])
        aftersteps_list = []
        separator1 = '\n'
        separator2 = ' '
        log = yield self.addLog('env')
        # create the dir
        aftersteps_list.append(steps.MakeDirectory(dir='env',
                                workdir='/etc/portage/'))
        #FIXME:
        # add env settings from the db
        default_project_portage_env_data = yield self.gentooci.db.projects.getProjectPortageEnvByUuid(default_project_data['uuid'])
        project_portage_env_data = yield self.gentooci.db.projects.getProjectPortageEnvByUuid(project_data['uuid'])
        project_portage_env_dict = yield self.getPortageEnv(default_project_portage_env_data, portage_env_dict = {})
        project_portage_env_dict = yield self.getPortageEnv(project_portage_env_data, portage_env_dict = project_portage_env_dict)
        print(project_portage_env_dict)
        for k, v in project_portage_env_dict.items():
            env_strings = []
            for a, b in v.items():
                variable_data = yield self.gentooci.db.portages.getVariableById(a)
                env_variable_string = variable_data['variable'] + '="' + separator2.join(b) + '"'
                env_strings.append(env_variable_string)
            yield self.build.addStepsAfterCurrentStep([
            steps.StringDownload(separator1.join(env_strings) + separator1,
                                workerdest=k + '.conf',
                                workdir='/etc/portage/env/')
            ])
            yield log.addStdout('File: ' + k + '.conf' + '\n')
            for line in env_strings:
                yield log.addStdout(line + '\n')
        yield self.build.addStepsAfterCurrentStep(aftersteps_list)
        return SUCCESS

class CheckPathLocal(BuildStep):

    name = 'CheckPathLocal'
    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.portage_path = yield os.path.join('etc', 'portage')
        self.profile_path = yield os.path.join(self.portage_path, 'make.profile')
        self.repos_path = yield os.path.join(self.portage_path, 'repos.conf')
        self.repository_basedir_db = yield os.path.join(self.master.basedir, 'repositorys')
        self.build_repository_basedir_db = yield os.path.join(self.getProperty("builddir"), 'repositorys')
        #print(self.repository_basedir_db)
        log = yield self.addLog('CheckPathLocal')
        #print(os.getcwd())
        print(self.getProperty("builddir"))
        #yield os.chdir(self.getProperty("builddir"))
        #print(os.getcwd())
        for x in [
                self.portage_path,
                self.profile_path,
                self.repos_path,
                ]:
            check_dir = yield os.path.join(self.getProperty("builddir"), x)
            if not Path(check_dir).is_dir():
                yield Path(check_dir).mkdir(parents=True)
                yield log.addStdout(' '.join(['Makeing missing dir', x]))
        if not Path(self.build_repository_basedir_db).is_dir():
            yield Path(self.build_repository_basedir_db).symlink_to(self.repository_basedir_db)
            yield log.addStdout(' '.join(['Makeing missing link', 'repositorys', 'to', self.repository_basedir_db]))
        return SUCCESS

class SetMakeProfileLocal(BuildStep):

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

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

    @defer.inlineCallbacks
    def run(self):
        parent_path = yield os.path.join('etc','portage', 'make.profile', 'parent')
        if os.path.isfile(parent_path):
            return SKIPPED
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.profile_repository_data = yield self.gentooci.db.repositorys.getRepositoryByUuid(self.getProperty('project_data')['profile_repository_uuid'])
        self.repository_basedir = 'repositorys'
        makeprofiles_paths = []
        makeprofiles_data = yield self.gentooci.db.projects.getAllProjectPortageByUuidAndDirectory(self.getProperty('project_data')['uuid'], 'make.profile')
        for makeprofile in makeprofiles_data:
            makeprofile_path = yield os.path.join(self.repository_basedir, self.profile_repository_data['name'], 'profiles', makeprofile['value'], '')
            makeprofiles_paths.append('../../../' + makeprofile_path)
        yield WriteTextToFile(parent_path, makeprofiles_paths)
        return SUCCESS

class SetReposConfLocal(BuildStep):

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

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

    @defer.inlineCallbacks
    def run(self):
        repos_conf_path = yield os.path.join('etc', 'portage', 'repos.conf')
        repos_conf_default_path = yield os.path.join(repos_conf_path, 'default.conf')
        self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        self.repository_basedir_db = yield os.path.join(self.master.basedir, 'repositorys')
        if not os.path.isfile(repos_conf_default_path):
            # setup the default.conf
            repos_conf_data = yield self.gentooci.db.projects.getProjectPortageByUuidAndDirectory(self.getProperty('project_data')['uuid'], 'repos.conf')
            if repos_conf_data is None:
                print('Default repo is not set in repos.conf')
                return FAILURE
            default_conf = []
            default_conf.append('[DEFAULT]')
            default_conf.append('main-repo = ' + repos_conf_data['value'])
            default_conf.append('auto-sync = no')
            yield WriteTextToFile(repos_conf_default_path, default_conf)
        repos_conf_repository_path = yield os.path.join(repos_conf_path, self.getProperty("repository_data")['name'] + '.conf')
        if not os.path.isfile(repos_conf_repository_path):
            repository_path = yield os.path.join(self.repository_basedir_db, self.getProperty("repository_data")['name'])
            repository_conf = []
            repository_conf.append('[' + self.getProperty("repository_data")['name'] + ']')
            repository_conf.append('location = ' + repository_path)
            repository_conf.append('sync-uri = ' + self.getProperty("repository_data")['url'])
            repository_conf.append('sync-type = git')
            repository_conf.append('auto-sync = no')
            yield WriteTextToFile(repos_conf_repository_path, repository_conf)
        return SUCCESS

class SetMakeConfLocal(BuildStep):

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

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

    @defer.inlineCallbacks
    def run(self):
        make_conf_path = yield os.path.join('etc', 'portage', 'make.conf')
        if os.path.isfile(make_conf_path):
            return SUCCESS
        makeconf_list = []
        makeconf_list.append('CFLAGS=""')
        makeconf_list.append('CXXFLAGS=""')
        makeconf_list.append('ACCEPT_LICENSE="*"')
        makeconf_list.append('USE=""')
        makeconf_list.append('ACCEPT_KEYWORDS="~amd64 amd64"')
        makeconf_list.append('EMERGE_DEFAULT_OPTS=""')
        makeconf_list.append('ABI_X86="32 64"')
        makeconf_list.append('FEATURES="sandbox"')
        yield WriteTextToFile(make_conf_path, makeconf_list)
        return SUCCESS

class SetEnvForEbuildSH(BuildStep):

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

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

    def getEapiFromFile(self):
        with io.open(_unicode_encode(self.getProperty("ebuild_file"),
            encoding=_encodings['fs'], errors='strict'),
            mode='r', encoding=_encodings['repo.content'],
            errors='replace') as f:
            _eapi, _eapi_lineno = _parse_eapi_ebuild_head(f)

        return _eapi

    @defer.inlineCallbacks
    def run(self):
        addStepEbuildSH = []
        ebuild_commands = []
        ebuild_env = {}
        config_root = yield os.path.join(self.getProperty("builddir"), '')
        mysettings = yield portage_config(config_root = config_root)

        #Get EAPI from file and add it to env
        eapi = yield self.getEapiFromFile()
        print(eapi)
        if eapi is None or not eapi_is_supported(eapi):
            print('invalid eapi')
            eapi = '0'
        print(eapi_is_supported(eapi))
        ebuild_env['EAPI'] = eapi

        #FIXME: check manifest on ebuild_file

        #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]
        portage_bin_path = mysettings["PORTAGE_BIN_PATH"]
        ebuild_sh_path = yield os.path.join(portage_bin_path, 'ebuild.sh')
        #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(mysettings["PORTAGE_TMPDIR"], 'portage', category, ebuild_env['PF'], 'work')
        ebuild_env['PORTAGE_ECLASS_LOCATIONS'] = self.getProperty("repository_path")

        #FIXME: use sandbox if in FEATURES
        ebuild_commands.append(ebuild_sh_path)
        ebuild_commands.append('depend')

        addStepEbuildSH.append(master_steps.MasterSetPropertyFromCommand(
                                                            name = 'RunEbuildSH',
                                                            haltOnFailure = True,
                                                            flunkOnFailure = True,
                                                            command=ebuild_commands,
                                                            env=ebuild_env,
                                                            workdir=self.getProperty("builddir"),
                                                            strip=False,
                                                            extract_fn=PersOutputOfEbuildSH
                                                            ))
        yield self.build.addStepsAfterCurrentStep(addStepEbuildSH)
        return SUCCESS

class GetAuxMetadata(BuildStep):

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

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

    #@defer.inlineCallbacks
    def thd_getAuxDbKeys(self):
        auxdbs = self.myportdb.aux_get(self.getProperty("cpv"), auxdbkeys, myrepo=self.getProperty("repository_data")['name'])
        return auxdbs

    @defer.inlineCallbacks
    def run(self):
        config_root = yield os.path.join(self.getProperty("builddir"), '')
        # setup mysettings and myportdb
        mysettings = yield portage_config(config_root = config_root)
        self.myportdb = yield portdbapi(mysettings=mysettings)
        auxdbs = yield self.thd_getAuxDbKeys()
        metadata = None
        NoSplit = []
        NoSplit.append('DESCRIPTION')
        # should have 22 lines
        if len(auxdbkeys) != len(auxdbs) or not isinstance(auxdbs, list):
            # number of lines is incorrect or not a list.
            print("Lines don't match or not a list")
            yield self.myportdb.close_caches()
            yield portdbapi.portdbapi_instances.remove(self.myportdb)
            #self.setProperty('auxdb', metadata, 'auxdb')
            return FAILURE
        # split all keys to list instead of speces
        metadata = {}
        i = 0
        for key in auxdbkeys:
            if auxdbs[i] == '':
                metadata[key] = None
            else:
                if ' ' in auxdbs[i] and key not in NoSplit:
                    metadata[key] = auxdbs[i].split(' ')
                else:
                    metadata[key] = []
                    metadata[key].append(auxdbs[i])
            i = i + 1
        self.setProperty('auxdb', metadata, 'auxdb')
        yield self.myportdb.close_caches()
        yield portdbapi.portdbapi_instances.remove(self.myportdb)
        return SUCCESS