summaryrefslogtreecommitdiff
blob: b3f82a5597734abbf2dde9a1abb7310e9b5d511e (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
#!/usr/bin/env python

from __future__ import print_function

import argparse
from contextlib import closing
from debian import deb822
from glob import glob
import os
import os.path
import portage
import shutil
import subprocess
import sys
try:
    from urllib.request import urlopen
except ImportError:
    from urllib2 import urlopen

ARCHIVE = 'https://dl.google.com/linux/chrome/deb'
DIST = 'stable'
COMP = 'main'
ARCH = 'amd64'

PORTDIR = portage.settings.repositories['gentoo'].location

PKGMAP = {
    'www-client/google-chrome': {
        '*.ebuild': ('_p', 'google-chrome-stable'),
    },
    'www-client/google-chrome-beta': {
        '*.ebuild': ('_p', 'google-chrome-beta'),
    },
    'www-client/google-chrome-unstable': {
        '*.ebuild': ('_p', 'google-chrome-unstable'),
    },
    'www-plugins/chrome-binary-plugins': {
        '*_p*.ebuild': ('_p', 'google-chrome-stable'),
        '*_beta*.ebuild': ('_beta', 'google-chrome-beta'),
        '*_alpha*.ebuild': ('_alpha', 'google-chrome-unstable'),
    },
}

ARGS = None

def get_deb_release(archive, dist):
    url = '%s/dists/%s/Release' % (archive, dist)
    with closing(urlopen(url)) as fp:
        return deb822.Release(fp)

def get_deb_packages(archive, dist, comp, arch):
    url = '%s/dists/%s/%s/binary-%s/Packages' % (archive, dist, comp, arch)
    with closing(urlopen(url)) as fp:
        return list(deb822.Packages.iter_paragraphs(fp))

def ebuild_pvr(pn, ebuild):
    return ebuild[len(pn) + 1 : -7]

def ebuild_pv(pn, ebuild):
    return ebuild_pvr(pn, ebuild).split('-r')[0]

def ebuild_version(pn, ebuild):
    return ebuild_pv(pn, ebuild).split('_')[0]

def new_ebuild(pn, version, sep, revision):
    return pn + '-' + version + sep + revision + '.ebuild'

def copy_ebuild(src, dest):
    print('cp ' + src + ' ' + dest)
    if not ARGS.dry_run:
        shutil.copyfile(src, dest)
    print('git add ' + dest)
    if not ARGS.dry_run:
        subprocess.check_call(['git', 'add', dest])

def remove_ebuild(ebuild):
    print('git rm ' + ebuild)
    if not ARGS.dry_run:
        subprocess.check_call(['git', 'rm',  ebuild])

def sync_ebuilds(pkg, debs):
    os.chdir(os.path.join(PORTDIR, pkg))
    pn = pkg.split('/')[1]
    changed = False

    for pattern in PKGMAP[pkg]:
        (sep, name) = PKGMAP[pkg][pattern]
        ebuilds = sorted(glob(pattern), reverse=True)

        for deb in debs:
            if deb['Package'] != name:
                continue

            (version, revision) = deb['Version'].split('-')
            found = False
            for ebuild in ebuilds:
                if version == ebuild_version(pn, ebuild):
                    found = True
                    break
            if not found:
                copy_ebuild(ebuilds[0], new_ebuild(pn, version, sep, revision))
                changed = True

        for ebuild in ebuilds:
            found = False
            for deb in debs:
                if deb['Package'] != name:
                    continue

                (version, revision) = deb['Version'].split('-')
                if version == ebuild_version(pn, ebuild):
                    found = True
                    break
            if not found:
                remove_ebuild(ebuild)
                changed = True

    if changed:
        if ARGS.commit:
            print('repoman commit')
            if not ARGS.dry_run:
                subprocess.check_call(['repoman', 'commit', '-S', '-m', pkg + ': automated update'])
        else:
            print('repoman manifest')
            if not ARGS.dry_run:
                subprocess.check_call(['repoman', 'manifest'])

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

    debs = get_deb_packages(ARCHIVE, DIST, COMP, ARCH)
    for pkg in PKGMAP:
        sync_ebuilds(pkg, debs)

if __name__ == '__main__':
    main()