aboutsummaryrefslogtreecommitdiff
blob: 2e445c3e53a86f0a8696fab71aeaf110c68a99da (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
""" Filling script templates """

import random
import os
import sys

from .usecombis import findUseFlagCombis
from .tinderbox import stablerdeps
from .tool import unique

#### USE-COMBIS ########

def useCombiTestString(pack, config):
    """ Build with diffent useflag combis """
    try:
        usesnippetfile=open(config['template-dir'] + "use-snippet", 'r')
    except IOError:
        print("use-snippet not found in " + config['template-dir'])
        sys.exit(1)
    s = "" # This will contain the resulting string
    usesnippet = usesnippetfile.read()
    usesnippet = usesnippet.replace("@@CPV@@", pack.packageString() )
    usesnippet = usesnippet.replace("@@EMERGEOPTS@@", config['emergeopts'])
    usecombis = findUseFlagCombis (pack, config)
    for uc in usecombis:
        localsnippet = usesnippet.replace("@@USE@@", uc)
        localsnippet = localsnippet.replace("@@FEATURES@@", "")
        s = s + localsnippet
    # In the end we test once with tests and users flags
    localsnippet = usesnippet.replace("@@USE@@", "")
    localsnippet = localsnippet.replace("@@FEATURES@@", "FEATURES=\"${FEATURES} test\"")
    s = s + localsnippet
    return s

def writeusecombiscript(job, config):
    # job is a tatt job object
    # config is a tatt configuration
    try:
        useheaderfile=open(config['template-dir'] + "use-header", 'r')
    except IOError:
        print("use-header not found in " + config['template-dir'])
        sys.exit(1)
    useheader=useheaderfile.read().replace("@@JOB@@", job.name)
    outfilename = (job.name + "-useflags.sh")
    reportname = (job.name + ".report")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    outfile = open(outfilename, 'w')
    outfile.write(useheader)
    for p in job.packageList:
        outfile.write("# Code for " + p.packageCatName() + "\n")
        outfile.write(useCombiTestString(p, config).replace("@@REPORTFILE@@",reportname))
    # Note: fchmod needs the filedescriptor which is an internal
    # integer retrieved by fileno().
    os.fchmod(outfile.fileno(), 0o744)  # rwxr--r--
    outfile.close()

######################################

############ RDEPS ################
def rdepTestString(rdep, config):
    try:
        rdepsnippetfile=open(config['template-dir'] + "revdep-snippet", 'r')
    except IOError:
        print("revdep-snippet not found in " + config['template-dir'])
        sys.exit(1)
    rdepsnippet=rdepsnippetfile.read()
    snip = rdepsnippet.replace("@@FEATURES@@", "FEATURES=\"${FEATURES} test\"")
    ustring = "USE=\'" + " ".join([st for st in rdep[1] if not st[0] == "!"]) + " "
    ustring = ustring + " ".join(["-" + st[1:] for st in rdep[1] if st[0] == "!"]) + "\'"
    snip = snip.replace("@@USE@@", ustring)
    snip = snip.replace("@@CPV@@", rdep[0] )
    snip = snip.replace("@@EMERGEOPTS@@", config['emergeopts'])
    return snip

def writerdepscript(job, config):
    # Populate the list of rdeps
    rdeps = []
    for p in job.packageList:
        rdeps = rdeps + stablerdeps (p, config)
    if len(rdeps) == 0:
        print("No stable rdeps for " + job.name)
        return

    # If there are rdeps, write the script
    try:
        rdepheaderfile=open(config['template-dir'] + "revdep-header", 'r')
    except IOError:
        print("revdep-header not found in " + config['template-dir'])
        sys.exit(1)
    rdepheader=rdepheaderfile.read().replace("@@JOB@@", job.name)
    outfilename = (job.name + "-rdeps.sh")
    reportname = (job.name + ".report")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    outfile = open(outfilename,'w')
    outfile.write(rdepheader)

    for r in rdeps:
        # Todo: remove duplicates
        localsnippet = rdepTestString (r, config)
        outfile.write(localsnippet.replace("@@REPORTFILE@@", reportname))
    os.fchmod(outfile.fileno(), 0o744)
    outfile.close()


#######Write report script############
def writesucessreportscript (job, config):
    outfilename = (job.name + "-success.sh")
    reportname = (job.name + ".report")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    try:
        updatebugtemplate=open(config['template-dir'] + "updatebug", 'r')
    except IOError:
        print("updatebug not found in " + config['template-dir'])
        sys.exit(1)
    updatebug=updatebugtemplate.read().replace("@@ARCH@@", config['arch'])
    updatebug=updatebug.replace("@@BUG@@", job.bugnumber)
    outfile = open(outfilename,'w')
    outfile.write(updatebug)
    os.fchmod(outfile.fileno(), 0o744)
    outfile.close()
    print("Success Report script written to " + outfilename)


####### Write the commit script #########
def writecommitscript (job, config):
    try:
        commitheaderfile=open(config['template-dir'] + "commit-header", 'r')
        commitsnippetfile=open(config['template-dir'] + "commit-snippet", 'r')
        commitsnippetfile2=open(config['template-dir'] + "commit-snippet-2", 'r')
        commitfooterfile=open(config['template-dir'] + "commit-footer", 'r')
    except IOError:
        print("Some commit template not found in " + config['template-dir'])
        sys.exit(1)
    csnippet = commitsnippetfile.read().replace("@@JOB@@", job.name)
    csnippet2 = commitsnippetfile2.read().replace("@@JOB@@", job.name)
    outfilename = (job.name + "-commit.sh")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    outfile = open(outfilename,'w')
    cheader = commitheaderfile.read().replace("@@JOB@@", job.name)
    cheader = cheader.replace("@@REPODIR@@", config['repodir'])
    outfile.write (cheader)
    # Here's a catch: If there are multiple versions of the same package to be
    # stabilized, then we want only one keywording block and one commit block
    # for them.  Therefore we split up the loop by sorting job.packlist
    # accordingly, saving them in a hash-table with the package names as keys
    # and the packages as values.
    packageHash = dict();
    for pack in job.packageList:
        if pack.packageCatName() in packageHash:
            packageHash[pack.packageCatName()] = packageHash[pack.packageCatName()] + [pack]
        else:
            packageHash[pack.packageCatName()] = [pack]
    # First round (ekeyword)
    for pack in packageHash.keys():
        s = csnippet.replace("@@BUG@@", job.bugnumber)
        s = s.replace("@@ARCH@@", config['arch'])
        if job.type=="stable":
            newkeyword=config['arch']
        elif job.type=="keyword":
            newkeyword="~"+config['arch']
        else:
            print ("No job type? Can't continue. This is a bug")
            sys.exit(1)
        s = s.replace("@@NEWKEYWORD@@", newkeyword)
        # Prepare a list of ebuild names strings
        ebuilds = [p.packageName()+"-"+p.packageVersion()+".ebuild" for p in packageHash[pack]]
        s = s.replace("@@EBUILD@@", " ".join(ebuilds))
        s = s.replace("@@CP@@", pack)
        outfile.write(s)
    # Second round: repoman -d full checks and commit, should be done once per
    # key of packageHash
    for pack in packageHash.keys():
        s = csnippet2.replace("@@BUG@@", job.bugnumber)
        s = s.replace("@@ARCH@@", config['arch'])
        s = s.replace("@@NEWKEYWORD@@", newkeyword)
        # Prepare a list of ebuild names strings
        ebuilds = [p.packageName()+"-"+p.packageVersion()+".ebuild" for p in packageHash[pack]]
        s = s.replace("@@EBUILD@@", " ".join(ebuilds))
        s = s.replace("@@CP@@", pack)
        outfile.write(s)
    # Footer (committing)
    outfile.write (commitfooterfile.read().replace("@@ARCH@@", config['arch']).replace("@@BUG@@", job.bugnumber))
    os.fchmod(outfile.fileno(), 0o744)
    outfile.close()
    print("Commit script written to " + outfilename)


######## Write clean-up script ##############
def writeCleanUpScript (job, config):
    try:
        cleanUpTemplate=open(config['template-dir'] + "cleanup", 'r')
    except IOError:
        print("Clean-Up template not found in" + config['template-dir'])
        print("No clean-up script written")
        return
    script = cleanUpTemplate.read().replace("@@JOB@@", job.name)
    script = script.replace("@@CPV@@", job.name)
    script = script.replace("@@KEYWORDFILE@@", config['unmaskfile'])
    outfilename = (job.name + "-cleanup.sh")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    outfile = open(outfilename,'w')
    outfile.write(script)
    os.fchmod(outfile.fileno(), 0o744)
    outfile.close()