1 |
#!/usr/bin/python |
2 |
|
3 |
import os |
4 |
import os.path |
5 |
import sys |
6 |
import re |
7 |
import subprocess |
8 |
import urllib2 |
9 |
import urlparse |
10 |
import argparse |
11 |
from sgmllib import SGMLParser |
12 |
|
13 |
MEDIA="Core Release Source" |
14 |
URL="http://download.gnome.org/sources/" |
15 |
PKGROOT='~/pkgs' |
16 |
|
17 |
class urllister(SGMLParser): |
18 |
def reset(self): |
19 |
SGMLParser.reset(self) |
20 |
self.urls = [] |
21 |
|
22 |
def start_a(self, attrs): |
23 |
href = [v for k, v in attrs if k=='href'] |
24 |
if href: |
25 |
self.urls.extend(href) |
26 |
|
27 |
def get_upstream_names(): |
28 |
urlopen = urllib2.build_opener() |
29 |
|
30 |
good_dir = re.compile('^[-A-Za-z0-9_+.]+/$') |
31 |
|
32 |
# Get the files |
33 |
usock = urlopen.open(URL) |
34 |
parser = urllister() |
35 |
parser.feed(usock.read()) |
36 |
usock.close() |
37 |
parser.close() |
38 |
files = parser.urls |
39 |
|
40 |
tarballs = set([filename.replace('/', '') for filename in files if good_dir.search(filename)]) |
41 |
|
42 |
return tarballs |
43 |
|
44 |
def get_downstream_names(): |
45 |
re_file = re.compile(r'^(?P<module>.*?)[_-](?:(?P<oldversion>([0-9]+[\.])*[0-9]+)-)?(?P<version>([0-9]+[\.\-])*[0-9]+)\.(?P<format>(?:tar\.|diff\.)?[a-z][a-z0-9]*)$') |
46 |
|
47 |
p = subprocess.Popen(['urpmf', '--files', '.', "--media", MEDIA], stdout=subprocess.PIPE, close_fds=True) |
48 |
contents = p.stdout.read().strip("\n").splitlines() |
49 |
ecode = p.wait() |
50 |
if ecode != 0: |
51 |
sys.exit(1) |
52 |
|
53 |
FILES = {} |
54 |
TARBALLS = {} |
55 |
|
56 |
for line in contents: |
57 |
try: |
58 |
srpm, filename = line.split(":") |
59 |
except ValueError: |
60 |
print >>sys.stderr, line |
61 |
continue |
62 |
|
63 |
if '.tar' in filename: |
64 |
r = re_file.match(filename) |
65 |
if r: |
66 |
fileinfo = r.groupdict() |
67 |
module = fileinfo['module'] |
68 |
|
69 |
if module not in TARBALLS: |
70 |
TARBALLS[module] = set() |
71 |
TARBALLS[module].add(srpm) |
72 |
|
73 |
if srpm not in FILES: |
74 |
FILES[srpm] = set() |
75 |
FILES[srpm].add(filename) |
76 |
|
77 |
return TARBALLS, FILES |
78 |
|
79 |
def cmd_co(options, parser): |
80 |
upstream = get_upstream_names() |
81 |
downstream, downstream_files = get_downstream_names() |
82 |
|
83 |
cwd = os.path.expanduser(PKGROOT) |
84 |
|
85 |
matches = upstream & set(downstream.keys()) |
86 |
for module in matches: |
87 |
print module, "\t".join(downstream[module]) |
88 |
for package in downstream[module]: |
89 |
subprocess.call(['mgarepo', 'co', package], cwd=cwd) |
90 |
|
91 |
def cmd_ls(options, parser): |
92 |
upstream = get_upstream_names() |
93 |
downstream, downstream_files = get_downstream_names() |
94 |
|
95 |
matches = upstream & set(downstream.keys()) |
96 |
for module in matches: |
97 |
print "\n".join(downstream[module]) |
98 |
|
99 |
def cmd_patches(options, parser): |
100 |
upstream = get_upstream_names() |
101 |
downstream, downstream_files = get_downstream_names() |
102 |
|
103 |
path = os.path.expanduser(PKGROOT) |
104 |
|
105 |
matches = upstream & set(downstream.keys()) |
106 |
for module in sorted(matches): |
107 |
for srpm in downstream[module]: |
108 |
for filename in downstream_files[srpm]: |
109 |
if '.patch' in filename or '.diff' in filename: |
110 |
print "\t".join((module,srpm, os.path.join(path, srpm, "SOURCES", filename) if options.path else filename)) |
111 |
|
112 |
def main(): |
113 |
description = """Mageia GNOME commands.""" |
114 |
epilog="""Report bugs to Olav Vitters""" |
115 |
parser = argparse.ArgumentParser(description=description,epilog=epilog) |
116 |
|
117 |
# SUBPARSERS |
118 |
subparsers = parser.add_subparsers(title='subcommands') |
119 |
# install |
120 |
subparser = subparsers.add_parser('co', help='checkout all GNOME modules') |
121 |
subparser.set_defaults( |
122 |
func=cmd_co |
123 |
) |
124 |
|
125 |
subparser = subparsers.add_parser('packages', help='list all GNOME packages') |
126 |
subparser.set_defaults( |
127 |
func=cmd_ls |
128 |
) |
129 |
|
130 |
subparser = subparsers.add_parser('patches', help='list all GNOME patches') |
131 |
subparser.add_argument("-p", "--path", action="store_true", dest="path", |
132 |
help="Full path to patch") |
133 |
subparser.set_defaults( |
134 |
func=cmd_patches, path=False |
135 |
) |
136 |
|
137 |
|
138 |
if len(sys.argv) == 1: |
139 |
parser.print_help() |
140 |
sys.exit(2) |
141 |
|
142 |
options = parser.parse_args() |
143 |
|
144 |
try: |
145 |
options.func(options, parser) |
146 |
except KeyboardInterrupt: |
147 |
print('Interrupted') |
148 |
sys.exit(1) |
149 |
except EOFError: |
150 |
print('EOF') |
151 |
sys.exit(1) |
152 |
except IOError, e: |
153 |
if e.errno != errno.EPIPE: |
154 |
raise |
155 |
sys.exit(0) |
156 |
|
157 |
if __name__ == "__main__": |
158 |
main() |