Gentoo Archives: gentoo-commits

From: Agostino Sarubbo <ago@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] dev/ago:master commit in: script/
Date: Sun, 30 Dec 2012 17:13:51
Message-Id: 1356887582.c5696e8abc894929fc3066ff7ae39f4cab84cb1b.ago@gentoo
1 commit: c5696e8abc894929fc3066ff7ae39f4cab84cb1b
2 Author: Agostino Sarubbo <ago <AT> gentoo <DOT> org>
3 AuthorDate: Sun Dec 30 17:13:02 2012 +0000
4 Commit: Agostino Sarubbo <ago <AT> gentoo <DOT> org>
5 CommitDate: Sun Dec 30 17:13:02 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=dev/ago.git;a=commit;h=c5696e8a
7
8 Add batch-pretend.py
9
10 ---
11 script/batch-pretend.py | 165 +++++++++++++++++++++++++++++++++++++++++++++++
12 1 files changed, 165 insertions(+), 0 deletions(-)
13
14 diff --git a/script/batch-pretend.py b/script/batch-pretend.py
15 new file mode 100755
16 index 0000000..ee7e0f4
17 --- /dev/null
18 +++ b/script/batch-pretend.py
19 @@ -0,0 +1,165 @@
20 +#!/usr/bin/env python
21 +# Copyright 2011 Gentoo Foundation
22 +# Distributed under the terms of the GNU General Public License v2
23 +
24 +import glob
25 +import itertools
26 +import optparse
27 +import os
28 +import pickle
29 +import re
30 +import shutil
31 +import subprocess
32 +import sys
33 +
34 +import bugz.bugzilla
35 +import portage.versions
36 +
37 +BUG_REGEX = re.compile("[Bb]ug #?(\d+)")
38 +
39 +def print_and_log(message, log):
40 + try:
41 + print message
42 + log.write(message + '\n')
43 + finally:
44 + log.flush()
45 +
46 +def run_command(args, cwd, log):
47 + try:
48 + message = "Running %r in %s...\n" % (args, cwd)
49 + sys.stdout.write(message)
50 + log.write(message)
51 +
52 + cmd = subprocess.Popen(args, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
53 + output = cmd.communicate()[0]
54 + log.write("Finished with exit code %d\n" % cmd.returncode)
55 + log.write(output)
56 + return (cmd.returncode, output)
57 + finally:
58 + log.flush()
59 +
60 +def save_state(done_bugs):
61 + with open('batch-stabilize.state', 'w') as state_file:
62 + pickle.dump(done_bugs, state_file)
63 +
64 +class MyBugz(bugz.bugzilla.Bugz):
65 + def get_input(self, prompt):
66 + return raw_input(prompt)
67 +
68 +if __name__ == "__main__":
69 + parser = optparse.OptionParser()
70 + parser.add_option("--arch", dest="arch", help="Gentoo arch to use, e.g. x86, amd64, ...")
71 + parser.add_option("-i", "--input", dest="input_filename", default="package.keywords", help="Input filename for generated package.keywords file [default=%default]")
72 + parser.add_option("--repo", dest="repo", help="Path to portage CVS repository")
73 + parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False)
74 +
75 + (options, args) = parser.parse_args()
76 + if not options.arch:
77 + parser.error("--arch option is required")
78 + if not options.input_filename:
79 + parser.error("--input option is required")
80 + if not options.repo:
81 + parser.error("--repo option is required")
82 + if args:
83 + parser.error("unrecognized command-line args")
84 +
85 + done_bugs = []
86 + if os.path.exists('batch-stabilize.state'):
87 + with open('batch-stabilize.state', 'r') as state_file:
88 + done_bugs = pickle.load(state_file)
89 +
90 + url = 'https://bugs.gentoo.org'
91 + print 'You may be prompted for your Gentoo Bugzilla username and password (%s).' % url
92 + bugzilla = MyBugz(url)
93 + bugzilla.auth()
94 +
95 + with open(options.input_filename, "r") as input_file:
96 + stabilization_dict = {}
97 + bug_id = -1
98 + for line in input_file:
99 + line = line.strip()
100 +
101 + # Skip empty/whitespace lines.
102 + if not line:
103 + continue
104 +
105 + if line.startswith("#"):
106 + match = BUG_REGEX.search(line, re.IGNORECASE)
107 + if not match:
108 + print 'Ignoring comment line [%s]...' % line
109 + continue
110 + else:
111 + bug_id = int(match.group(1))
112 + continue
113 +
114 + if bug_id == -1:
115 + print 'Could not recognize bug id'
116 + sys.exit(1)
117 +
118 + # Drop the leading '='.
119 + cpv = line[1:]
120 +
121 + p = portage.versions.catsplit(cpv)[1]
122 + pn = portage.versions.pkgsplit(cpv)[0]
123 + ebuild_name = p + ".ebuild"
124 + if bug_id not in stabilization_dict:
125 + stabilization_dict[bug_id] = []
126 + stabilization_dict[bug_id].append((pn, ebuild_name))
127 +
128 + # Sanity check.
129 + success = True
130 + for bug_id in stabilization_dict:
131 + for (pn, ebuild_name) in stabilization_dict[bug_id]:
132 + ebuild_path = os.path.join(options.repo, pn, ebuild_name)
133 + if not os.path.exists(ebuild_path):
134 + print '%s: file does not exist' % ebuild_path
135 + success = False
136 + if not success:
137 + print 'Sanity check failed. Please make sure your CVS repo is up to date (cvs up).'
138 + sys.exit(1)
139 +
140 + with open('batch-stabilize.log', 'a') as log_file:
141 + for bug_id in stabilization_dict:
142 + if bug_id in done_bugs:
143 + print_and_log('Skipping bug #%d because it is marked as done.' % bug_id, log_file)
144 + continue
145 +
146 + print_and_log('Working on bug %d...' % bug_id, log_file)
147 + commit_message = "Stable for %s, wrt bug #%d" % (options.arch, bug_id)
148 + for (pn, ebuild_name) in stabilization_dict[bug_id]:
149 + cvs_path = os.path.join(options.repo, pn)
150 + print_and_log('Working in %s...' % cvs_path, log_file)
151 +
152 + # Remove whole directory to prevent problems with conflicts.
153 + if os.path.exists(cvs_path):
154 + try:
155 + shutil.rmtree(cvs_path)
156 + except OSError:
157 + print '!!! rmtree %s failed' % cvs_path
158 + sys.exit(1)
159 +
160 + if run_command(["cvs", "up", pn], options.repo, log_file)[0] != 0:
161 + print '!!! cvs up failed'
162 + sys.exit(1)
163 + if run_command(["ekeyword", options.arch, ebuild_name], cvs_path, log_file)[0] != 0:
164 + print '!!! ekeyword failed'
165 + sys.exit(1)
166 + if run_command(["repoman", "manifest"], cvs_path, log_file)[0] != 0:
167 + print '!!! repoman manifest failed'
168 + sys.exit(1)
169 + for (pn, ebuild_name) in stabilization_dict[bug_id]:
170 + cvs_path = os.path.join(options.repo, pn)
171 + print_and_log('Working in %s...' % cvs_path, log_file)
172 + return_code, output = run_command(["cvs", "diff"], cvs_path, log_file)
173 + # It seems that cvs diff returns 1 if there are differences.
174 + if return_code == 0 and not output:
175 + print_and_log('Seems already keyworded, skipping.', log_file)
176 + done_bugs.append(bug_id)
177 + save_state(done_bugs)
178 + continue
179 + if run_command(["repoman", "full"], cvs_path, log_file)[0] != 0:
180 + os.chdir(cvs_path)
181 + os.system("repoman full")
182 + sys.exit(1)
183 + if os.path.exists('batch-stabilize.state'):
184 + os.remove('batch-stabilize.state')