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

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

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"  : False
        },
        "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
        }
    }
}

def getChromeVersionData(base_url, os):
    if not base_url.endswith("/"):
        url = base_url + "/"
    url += f"all.json?os={os}"

    response = urllib.request.urlopen(url)
    data = json.loads(response.read())
    return data[0]["versions"]

def getChromeChannelVersion(versions, channel):
    for item in versions:
        if item["channel"] == channel:
            return item["current_version"]
    return None

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 main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--commit', '-c', action='store_true')
    parser.add_argument('--dry-run', '-n', action='store_true')
    args = parser.parse_args()

    output = EOutput()

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

    versions = getChromeVersionData(base_url="https://omahaproxy.appspot.com",
                                    os="linux")

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

    for channel in channels:
        chrome_info[channel] = getChromeChannelVersion(versions=versions,
                                                       channel=channel)

    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:
            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)]
                elif not "_" in version:
                    pkg_data[category][channel]["version"] = version
            if pkg_data[category][channel]["version"] is None:
                output.ewarn("Couldn't determine tree version for "+
                             "{category}/{pkg}")

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

    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():
                pkg_data[category][channel]["bump"] = False
                ver_info = vercmp(chrome_info[channel],
                                  pkg_data[category][channel]["version"])
                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:
            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  = pkg_data[category][channel]["version"]
            output.einfo(f"\t{channel}\t{tversion}\t{uversion}" +
                         f"\t==> {'bump' if need_bump else 'no bump'}")

    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 not pkg_data[category][channel]["bump"]:
                continue
            uversion   = chrome_info[channel]
            tversion   = pkg_data[category][channel]["version"]
            major_bump = isMajorBump(uversion=uversion, tversion=tversion)
            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:
                prev_channel = getPrevChannel(channel=channel)
                prev_pkg     = pkg_data[category][prev_channel]["pkg"]
                prev_version = pkg_data[category][prev_channel]["version"]
                prev_suffix  = pkg_data[category][prev_channel]["suffix"]
                print(prev_pkg)
                if prev_suffix is not None:
                    prev_suffix = "_" + prev_suffix
                else:
                    prev_suffix = ""
                from_ebuild = os.path.join(repo_path,
                                           category,
                                           prev_pkg,
                                           prev_pkg + "-" +
                                           prev_version + prev_suffix +
                                           ".ebuild")
            else:
                from_ebuild = os.path.join(repo_path,
                                           category,
                                           pkg,
                                           pkg + "-" +
                                           tversion + suffix +
                                           ".ebuild")
            to_ebuild = os.path.join(repo_path,
                                     category,
                                     pkg,
                                     pkg + "-" +
                                     uversion + suffix +
                                     ".ebuild")

            shutil.copyfile(from_ebuild, to_ebuild)

            index.add(to_ebuild)
            if major_bump:
                old_ebuild = os.path.join(repo_path,
                                          category,
                                          pkg,
                                          pkg + "-" +
                                          tversion + suffix +
                                          ".ebuild")
                index.remove(old_ebuild, working_tree=True)
            else:
                index.remove(from_ebuild, working_tree=True)

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

            digestgen.digestgen(None, cfg, db)

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

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

if __name__ == "__main__":
    main()