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

import random
import os
import portage
import sys

from .gentooPackage import gentooPackage as gP
from .usecombis import findUseFlagCombis
from .tinderbox import stablerdeps
from .tool import unique
from portage.dep import dep_getkey

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

def scriptTemplate(job, config, filename):
    """ open snippet file and replace common placeholders """
    try:
        snippetfile=open(config['template-dir'] + filename, 'r')
    except IOError:
        print("template " + filename + " not found in " + config['template-dir'])
        sys.exit(1)

    reportname = job.name + ".report"
    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)

    snippet = snippetfile.read()
    snippet = snippet.replace("@@EMERGEOPTS@@", config['emergeopts'])
    if job.bugnumber:
        snippet = snippet.replace("@@BUG@@", job.bugnumber)
    else:
        snippet = snippet.replace("@@BUG@@", '')
    snippet = snippet.replace("@@JOB@@", job.name)
    snippet = snippet.replace("@@ARCH@@", config['arch'])
    snippet = snippet.replace("@@REPODIR@@", config['repodir'])
    snippet = snippet.replace("@@REPORTFILE@@", reportname)
    snippet = snippet.replace("@@BUILDLOGDIR@@", config['buildlogdir'])
    snippet = snippet.replace("@@NEWKEYWORD@@", newkeyword)
    snippet = snippet.replace("@@TEMPLATEDIR@@", config['template-dir'])
    return snippet

def useCombiTestString(job, pack, config, port):
    """ Build with diffent useflag combis """
    usesnippet = scriptTemplate(job, config, "use-snippet")
    usesnippet = usesnippet.replace("@@CPV@@", pack.packageString() )

    # test once with tests and users flags
    # do this first to trigger bugs in some packages where the test suite relies on
    # the package being already installed
    usetestsnippet = scriptTemplate(job, config, "use-test-snippet")
    usetestsnippet = usetestsnippet.replace("@@CPV@@", pack.packageString() )
    s = usetestsnippet.replace("@@USE@@", "")

    usecombis = findUseFlagCombis (pack, config, port)
    for uc in usecombis:
        localsnippet = usesnippet.replace("@@USE@@", uc)
        s = s + localsnippet
    return s

def writeusecombiscript(job, config):
    # job is a tatt job object
    # config is a tatt configuration
    useheader = scriptTemplate(job, config, "use-header")
    if os.path.exists(config['template-dir'] + "use-loop"):
        useloop = scriptTemplate(job, config, "use-loop")
    else:
        useloop = "@@LOOP_BODY@@"

    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)
    port = portage.db[portage.root]["porttree"].dbapi
    for p in job.packageList:
        loop = useloop.replace("@@LOOP_BODY@@", useCombiTestString(job, p, config, port))
        loop = loop.replace("@@CPV@@", p.packageString())
        outfile.write(loop)
    if os.path.exists(config['template-dir'] + "use-footer"):
        footer = scriptTemplate(job, config, "use-footer")
        outfile.write(footer)
    # 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(job, rdep, config):
    snip = scriptTemplate(job, config, "revdep-snippet")

    uflags = []
    for st in rdep[1]:
        st = st.strip()
        if len(st) == 0:
            continue
        if st[0] == "!":
            uflags.append("-" + st[1:])
        else:
            uflags.append(st)
    ustring = "USE=\'" + " ".join(uflags) + "\'"
    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
    # while at it also create a list of only the package names
    rdeps = []
    pkgs = []
    for p in job.packageList:
        atom = p.packageCatName()
        pkgs.append(atom)
        rdeps = rdeps + stablerdeps (atom, config)
    if len(rdeps) == 0:
        print("No stable rdeps for " + job.name)
        return

    # now clean the list
    # first find all those entries that have no useflags and main packages of this job
    for i in range(len(rdeps) - 1, 0, -1):
        r = rdeps[i]
        hasU = False
        for st in r[1]:
            if len(st.strip()) > 0:
                hasU = True
                break
        if hasU:
            continue
        if r[0] in pkgs:
            rdeps.pop(i)

    # If there are rdeps, write the script
    rdepheader = scriptTemplate(job, config, "revdep-header")
    outfilename = (job.name + "-rdeps.sh")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    outfile = open(outfilename,'w')
    outfile.write(rdepheader)

    for r in rdeps:
        # Todo: remove duplicates
        outfile.write(rdepTestString(job, r, config))
    os.fchmod(outfile.fileno(), 0o744)
    outfile.close()


#######Write report script############
def writesucessreportscript (job, config):
    outfilename = (job.name + "-success.sh")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    updatebug = scriptTemplate(job, config, "updatebug")
    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):
    cheader = scriptTemplate(job, config, "commit-header")
    csnippet = scriptTemplate(job, config, "commit-snippet")
    csnippet2 = scriptTemplate(job, config, "commit-snippet-2")
    cfooter = scriptTemplate(job, config, "commit-footer")

    outfilename = (job.name + "-commit.sh")
    if os.path.isfile(outfilename):
        print("WARNING: Will overwrite " + outfilename)
    outfile = open(outfilename,'w')
    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():
        # Prepare a list of ebuild names strings
        ebuilds = [p.packageName()+"-"+p.packageVersion()+".ebuild" for p in packageHash[pack]]
        s = csnippet.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():
        # Prepare a list of ebuild names strings
        ebuilds = [p.packageName()+"-"+p.packageVersion()+".ebuild" for p in packageHash[pack]]
        s = csnippet2.replace("@@EBUILD@@", " ".join(ebuilds))
        s = s.replace("@@CP@@", pack)
        outfile.write(s)
    # Footer (committing)
    outfile.write(cfooter)
    os.fchmod(outfile.fileno(), 0o744)
    outfile.close()
    print("Commit script written to " + outfilename)


######## Write clean-up script ##############
def writeCleanUpScript (job, config, unmaskname):
    script = scriptTemplate(job, config, "cleanup")
    script = script.replace("@@KEYWORDFILE@@", unmaskname)
    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()