summaryrefslogtreecommitdiff
blob: fa132e8cb23ef05c94ec31a6e45c144689860e0f (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
#!/bin/env python3

import argparse
import json
import os
import shutil
import sys
import urllib.request
import subprocess

from portage.dbapi.porttree import portdbapi
from portage.versions import *
from portage.package.ebuild import digestgen, config
from portage.output import EOutput

from git import Repo

channels = ["stable", "beta", "dev"]
pkg_data = \
{
    "www-client" :
    {
        "stable" :
        {
            "pkg"     : "google-chrome",
            "suffix"  : None,
            "version" : None,
            "bump"    : False,
            "stable"  : True
        },
        "beta"   :
        {
            "pkg"     : "google-chrome-beta",
            "suffix"  : None,
            "version" : None,
            "bump"    : False,
            "stable"  : False
        },
        "dev"    :
        {
            "pkg"     : "google-chrome-unstable",
            "suffix"  : None,
            "version" : None,
            "bump"    : False,
            "stable"  : False
        }
    },
    "www-plugins" :
    {
        "stable" :
        {
            "pkg"     : "chrome-binary-plugins",
            "suffix"  : None,
            "version" : None,
            "bump"    : False,
            "stable"  : True
        },
        "beta" :
        {
            "pkg"     : "chrome-binary-plugins",
            "suffix"  : "beta",
            "version" : None,
            "bump"    : False,
            "stable"  : False
        },
        "dev"  :
        {
            "pkg"     : "chrome-binary-plugins",
            "suffix"  : "alpha",
            "version" : None,
            "bump"    : False,
            "stable"  : False
        }
    },
    # This will be parsed last so we can take advantage of google chrome stable queries
    "www-apps":
    {
        "stable":
        {
            "pkg": "chromedriver-bin",
            "suffix": None,
            "version": None,
            "bump": False,
            "stable": True
        }
    },
}

def getChromeVersionData(base_url, os, channel):
    if not base_url.endswith("/"):
        url = base_url + "/"
    url += f"{os}/channels/{channel}/versions/all/releases?filter=endtime=1970-01-01T00:00:00Z"
    response = urllib.request.urlopen(url)
    data = json.loads(response.read())
    return data["releases"][0]["version"]

def isMajorBump(uversion, tversion):
    uv_list = uversion.split('.')
    tv_list = tversion.split('.')
    if int(uv_list[0]) > int(tv_list[0]):
        return True
    return False

def getPrevChannel(channel):
    channel_list = channels + [channels[len(channels) - 1]]
    for i in range(0, len(channel_list) - 1):
        if channel_list[i] == channel:
            return channel_list[i + 1]
    raise ValueError(f"Unknown channel \"{channel}\".")

def getEbuildVersion(version):
    if version[1] == "r0":
        return version[0]
    return f"{version[0]}-{version[1]}"

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--dry-run', '-n', action='store_true')
    args = parser.parse_args()

    output = EOutput()

    output.einfo("Fetching upstream version information ...")

    chrome_info = {}
    for channel in channels:
        chrome_info[channel] = None

    for channel in channels:
        version = getChromeVersionData(base_url="https://versionhistory.googleapis.com/v1/chrome/platforms",
                                       os="linux",
                                       channel=channel)
        chrome_info[channel] = version

    output.einfo("Looking up Chrome version information in tree ...")

    db = portdbapi()
    repo_path = db.getRepositoryPath(repository_id="gentoo")
    for category in pkg_data.keys():
        for channel in channels:
            # We only care about chromedriver that matches the stable version of google-chrome
            if category == "www-apps" and channel != "stable":
                continue
            pkg  = pkg_data[category][channel]["pkg"]
            cpvs = db.cp_list(mycp=f"{category}/{pkg}", mytree=repo_path)
            pkg_data[category][channel]["version"] = None
            for cpv in cpvs:
                (cp, version, rev) = pkgsplit(mypkg=cpv)
                suffix = pkg_data[category][channel]["suffix"]
                if suffix is not None:
                    suffix = "_" + suffix
                    if version.endswith(suffix):
                        pkg_data[category][channel]["version"] = (version[:-len(suffix)],
                                                                  rev)
                elif not "_" in version:
                    pkg_data[category][channel]["version"] = (version, rev)
            if pkg_data[category][channel]["version"] is None:
                output.ewarn("Couldn't determine tree version for "+
                             "{category}/{pkg}")

    output.einfo("Comparing Chrome version information...")

    for channel in channels:
        if chrome_info[channel] is None:
            output.ewarn(f"Upstream version unknown for channel \"{channel}\".")
        else:
            for category in pkg_data.keys():
                # chromedriver-bin is basically a shim for google-chrome stable for version purposes
                if category == "www-apps":
                    if channel == "stable":
                        pkg_data[category][channel]["bump"] = pkg_data["www-client"][channel]["bump"]
                        pkg_data[category][channel]["version"] = pkg_data["www-client"][channel]["version"]
                    else:
                        continue

                else:
                    pkg_data[category][channel]["bump"] = False

                ver_info = vercmp(chrome_info[channel],
                                pkg_data[category][channel]["version"][0])
                if ver_info is None:
                    output.ewarn("Cannot determine new version for " +
                                 f"channel \"{channel}\" of " +
                                 f"{category}/" +
                                 f"{pkg_data[category][channel]['pkg']}.")
                elif ver_info > 0:
                    pkg_data[category][channel]["bump"] = True
                elif ver_info < 0:
                    output.ewarn("Upstream reverted bump for " +
                                 f"channel \"{channel}\" of " +
                                 f"{category}/" +
                                 f"{pkg_data[category][channel]['pkg']}.")

    for category in pkg_data.keys():
        for channel in channels:
            if category == "www-apps" and channel != "stable":
                continue
            pkg = pkg_data[category][channel]["pkg"]
            output.einfo(f"{category}/{pkg} version information:")
            need_bump = pkg_data[category][channel]["bump"]
            uversion  = chrome_info[channel]
            tversion  = getEbuildVersion(pkg_data[category][channel]["version"])
            output.einfo(f"\t{channel}\t{tversion}\t{uversion}" +
                         f"\t==> {'bump' if need_bump else 'no bump'}")

    if not args.dry_run:
        repo = Repo(repo_path)
        if repo.is_dirty():
            output.eerror("Git Repository is dirty, can't continue.")
            sys.exit(1)

        index = repo.index

    for channel in channels:
        for category in pkg_data.keys():
            if category == "www-apps" and channel != "stable":
                continue
            if not pkg_data[category][channel]["bump"]:
                continue
            uversion   = chrome_info[channel]
            tversion   = getEbuildVersion(pkg_data[category][channel]["version"])
            major_bump = isMajorBump(uversion=uversion,
                                     tversion=pkg_data[category][channel]["version"][0])
            pkg        = pkg_data[category][channel]["pkg"]
            suffix     = pkg_data[category][channel]["suffix"]
            if suffix is not None:
                suffix = "_" + suffix
            else:
                suffix = ""
            output.einfo(f"Bumping {category}/{pkg} ...")
            if major_bump:
                if category != "www-apps":
                    prev_channel = getPrevChannel(channel=channel)
                    prev_pkg     = pkg_data[category][prev_channel]["pkg"]
                    prev_version = getEbuildVersion(pkg_data[category][prev_channel]["version"])
                    prev_suffix  = pkg_data[category][prev_channel]["suffix"]
                else:
                    # Grab the details for google-chrome; we never have a suffix
                    prev_pkg     = pkg_data["www-apps"]["stable"]["pkg"]
                    prev_version = getEbuildVersion(pkg_data["www-client"]["stable"]["version"])
                    prev_suffix  = None
                if prev_suffix is not None:
                    prev_suffix = "_" + prev_suffix
                else:
                    prev_suffix = ""
                from_ebuild = os.path.join(category,
                                           prev_pkg,
                                           prev_pkg + "-" +
                                           prev_version + prev_suffix +
                                           ".ebuild")
            else:
                from_ebuild = os.path.join(category,
                                           pkg,
                                           pkg + "-" +
                                           tversion + suffix +
                                           ".ebuild")
            to_ebuild = os.path.join(category,
                                     pkg,
                                     pkg + "-" +
                                     uversion + suffix +
                                     ".ebuild")

            if args.dry_run:
                print(f"cp {from_ebuild} {to_ebuild}")
                if not major_bump:
                    print(f"git rm {from_ebuild}")
            else:
                from_ebuild = os.path.join(repo_path, from_ebuild)
                shutil.copyfile(from_ebuild,
                                os.path.join(repo_path, to_ebuild))
                if not major_bump:
                    index.remove(from_ebuild, working_tree=True)

            if major_bump:
                old_ebuild = os.path.join(category,
                                          pkg,
                                          pkg + "-" +
                                          tversion + suffix +
                                          ".ebuild")
                if args.dry_run:
                    print(f"git rm {old_ebuild}")
                else:
                    index.remove(os.path.join(repo_path, old_ebuild),
                                 working_tree=True)
                if pkg_data[category][channel]["stable"]:
                    if args.dry_run:
                        print(f"ekeyword amd64 {to_ebuild}")
                    else:
                        subprocess.run(["ekeyword", "amd64",
                                        os.path.join(repo_path, to_ebuild)])

            if args.dry_run:
                print(f"git add {to_ebuild}")
            else:
                to_ebuild = os.path.join(repo_path, to_ebuild)
                index.add(to_ebuild)

            to_path = os.path.dirname(to_ebuild)
            cfg = config.config()
            cfg["O"] = to_path

            if args.dry_run:
                print(f"git add {os.path.join(to_path, 'Manifest')}")
                print("git commit -m",
                      f"\"{category}/{pkg}: automated update",
                      f"({uversion}{suffix})",
                      "-s -S\"")
            else:
                digestgen.digestgen(None, cfg, db)

                index.add(os.path.join(to_path, "Manifest"))

                repo.git.commit("-m",
                                f"{category}/{pkg}: automated update ({uversion}{suffix})",
                                "-s", "-S")

if __name__ == "__main__":
    main()