]> git.decadent.org.uk Git - dak.git/blob - dak/edit_transitions.py
* dak/dak.py (init): Renamed check -> edit transitions
[dak.git] / dak / edit_transitions.py
1 #!/usr/bin/env python
2
3 # Edit and then check the release managers transition file for correctness
4 # and outdated transitions
5 # Copyright (C) 2008 Joerg Jaspert <joerg@debian.org>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 ################################################################################
22
23 # <elmo> if klecker.d.o died, I swear to god, I'm going to migrate to gentoo.
24
25 ################################################################################
26
27 import os, pg, sys, time
28 import apt_pkg
29 import daklib.database
30 import daklib.utils
31 import syck
32
33 # Globals
34 Cnf = None
35 Options = None
36 projectB = None
37
38 ################################################################################
39
40 def init():
41     global Cnf, Options, projectB
42
43     apt_pkg.init()
44
45     Cnf = daklib.utils.get_conf()
46
47     Arguments = [('h',"help","Edit-Transitions::Options::Help"),
48                  ('e',"edit","Edit-Transitions::Option::Edit"),
49                  ('c',"check","Edit-Transitions::Option::check"),
50                  ('n',"no-action","Edit-Transitions::Options::No-Action")]
51
52     for i in ["help", "no-action", "edit", "check"]:
53         if not Cnf.has_key("Edit-Transitions::Options::%s" % (i)):
54             Cnf["Edit-Transitions::Options::%s" % (i)] = ""
55
56     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
57
58     Options = Cnf.SubTree("Edit-Transitions::Options")
59
60     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
61     daklib.database.init(Cnf, projectB)
62     
63     if Options["help"]:
64         usage()
65
66 ################################################################################
67
68 def usage (exit_code=0):
69     print """Usage: edit_transitions [OPTION]...
70   Check the release managers transition file for correctness and outdated transitions
71   -h, --help                show this help and exit.
72   -e, --edit                edit the transitions file
73   -c, --check               check the transitions file, remove outdated entries
74   -n, --no-action           don't do anything
75
76   Called without an option this tool will check the transition file for outdated
77   transitions and remove them."""
78     sys.exit(exit_code)
79
80 ################################################################################
81
82 def lock_file(lockfile):
83     while retry < 10:
84         try:
85             lock_fd = os.open(lockfile, os.O_RDONLY | os.O_CREAT | os.O_EXCL)
86             retry = 10
87             except OSError, e:
88             if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EEXIST':
89                 retry += 1
90                 if (retry >= 10):
91                     daklib.utils.fubar("Couldn't obtain lock for %s." % (lockfile) )
92                 else:
93                     print("Unable to get lock for %s (try %d of 10)" % (lockfile, retry) )
94                     time.sleep(60)
95             else:
96                 raise
97
98
99 ################################################################################
100
101 def edit_transitions():
102     trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
103
104     tempfile = "./%s.transition.tmp" % (os.getpid() )
105
106     lockfile="%s.lock" % (tempfile)
107     lock_file(lockfile)
108
109     daklib.utils.copy(trans_file, tempfile )
110
111     editor = os.environ.get("EDITOR", "vi")
112
113     while True:
114         result = os.system("%s %s" % (editor, temp_file))
115         if result != 0:
116             os.unlink(tempfile)
117             os.unlink(lockfile)
118             daklib.utils.fubar("%s invocation failed for %s, not removing tempfile." % (editor, temp_file))
119     
120         # Now try to load the new file
121         test = load_transitions(tempfile)
122
123         if test = None:
124             # Edit is broken
125             prompt = "Broken edit: [E]dit again, Drop changes?"
126             while prompt.find(answer) == -1:
127                 answer = daklib.utils.our_raw_input(prompt)
128                 m = daklib.queue.re_default_answer.match(prompt)
129                 if answer == "":
130                     answer = m.group(1)
131                 answer = answer[:1].upper()
132
133             if answer == 'E':
134                 continue
135             elif answer == 'D':
136                 os.unlink(tempfile)
137                 os.unlink(lockfile)
138                 sys.exit(0)
139         else:
140             # No problems in loading the new file, jump out of the while loop
141             break
142
143     # We seem to be done and also have a working file. Copy over.
144     daklib.utils.copy(tempfile, trans_file)
145     os.unlink(tempfile)
146     os.unlink(lockfile)
147
148 ################################################################################
149
150 def load_transitions(trans_file):
151     # Parse the yaml file
152     sourcefile = file(trans_file, 'r')
153     sourcecontent = sourcefile.read()
154     try:
155         trans = syck.load(sourcecontent)
156     except error, msg:
157         # Someone fucked it up
158         print "ERROR: %s" % (msg)
159         return None
160     return trans
161
162 ################################################################################
163
164 def print_info(trans, source, expected, rm, reason, packages):
165         print """
166 Looking at transition: %s
167  Source:      %s
168  New Version: %s
169  Responsible: %s
170  Description: %s
171  Blocked Packages (total: %d): %s
172 """ % (trans, source, expected, rm, reason, len(packages), ", ".join(packages))
173         return
174
175 ################################################################################
176
177 def transition_info(transitions):
178     print "Currently defined transitions:"
179     for trans in transitions:
180         t = transitions[trans]
181         source = t["source"]
182         expected = t["new"]
183
184         # Will be None if nothing is in testing.
185         current = daklib.database.get_testing_version(source)
186
187         print_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
188
189         if current == None:
190             # No package in testing
191             print "Transition source %s not in testing, transition still ongoing." % (source)
192         else:
193             compare = apt_pkg.VersionCompare(current, expected)
194             print "Apt compare says: %s" % (compare)
195             if compare < 0:
196                 # This is still valid, the current version in database is older than
197                 # the new version we wait for
198                 print "This transition is still ongoing, we currently have version %s" % (current)
199             else:
200                 print "This transition is over, the target package reached testing, should be removed"
201                 print "%s wanted version: %s, has %s" % (source, expected, current)
202         print "-------------------------------------------------------------------------"
203
204 ################################################################################
205
206 def check_transitions(transitions):
207     to_dump = 0
208     to_remove = []
209     # Now look through all defined transitions
210     for trans in transitions:
211         t = transitions[trans]
212         source = t["source"]
213         expected = t["new"]
214
215         # Will be None if nothing is in testing.
216         current = daklib.database.get_testing_version(source)
217
218         print_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
219
220         if current == None:
221             # No package in testing
222             print "Transition source %s not in testing, transition still ongoing." % (source)
223         else:
224             compare = apt_pkg.VersionCompare(current, expected)
225             print "Apt compare says: %s" % (compare)
226             if compare < 0:
227                 # This is still valid, the current version in database is older than
228                 # the new version we wait for
229                 print "This transition is still ongoing, we currently have version %s" % (current)
230             else:
231                 print "REMOVE: This transition is over, the target package reached testing. REMOVE"
232                 print "%s wanted version: %s, has %s" % (source, expected, current)
233                 to_remove.append(trans)
234                 to_dump = 1
235         print "-------------------------------------------------------------------------"
236
237     if to_dump:
238         prompt = "Removing: "
239         for remove in to_remove:
240             prompt += remove
241             prompt += ","
242
243         prompt += " Commit Changes? (y/N)"
244
245         if Options["no-action"]:
246             answer="n"
247         else:
248             answer = daklib.utils.our_raw_input(prompt).lower()
249
250         if answer == "":
251             answer = "n"
252
253         if answer == 'n':
254             print "Not committing changes"
255             sys.exit(0)
256         elif answer == 'y':
257             print "Committing"
258             for remove in to_remove:
259                 del transitions[remove]
260             destfile = file(Cnf["Dinstall::Reject::ReleaseTransitions"], 'w')
261             syck.dump(transitions, destfile)
262             print "Done"
263         else:
264             print "WTF are you typing?"
265             sys.exit(0)
266
267
268 ################################################################################
269
270 def main():
271     global Cnf
272
273     init()
274     
275     # Only check if there is a file defined (and existant) with checks. It's a little bit
276     # specific to Debian, not much use for others, so return early there.
277     if not Cnf.has_key("Dinstall::Reject::ReleaseTransitions") or not os.path.exists("%s" % (Cnf["Dinstall::Reject::ReleaseTransitions"])):
278         daklib.utils.warn("Dinstall::Reject::ReleaseTransitions not defined or file %s not existant." %
279                           (Cnf["Dinstall::Reject::ReleaseTransitions"]))
280         sys.exit(1)
281     
282     # Parse the yaml file
283     transitions = load_transitions(Cnf["Dinstall::Reject::ReleaseTransitions"])
284     if transitions = None:
285         # Something very broken with the transitions, exit
286         daklib.utils.warn("Not doing any work, someone fucked up the transitions file outside our control")
287         sys.exit(2)
288
289     if Options["edit"]:
290         # Output information about the currently defined transitions.
291         transition_info(transitions)
292         daklib.utils.our_raw_input("Press a key to continue...")
293
294         # Lets edit the transitions file
295         edit_transitions(Cnf["Dinstall::Reject::ReleaseTransitions"])
296     elif Options["check"]:
297         # Check and remove outdated transitions
298         check_transitions(transitions, Cnf["Dinstall::Reject::ReleaseTransitions"])
299     else:
300         # Output information about the currently defined transitions.
301         transition_info(transitions)
302         daklib.utils.our_raw_input("Press a key to continue...")
303
304         # Nothing requested, doing nothing besides the above display of the transitions
305         sys.exit(0)
306     
307
308 ################################################################################
309
310 if __name__ == '__main__':
311     main()