]> git.decadent.org.uk Git - dak.git/blob - dak/edit_transitions.py
merge with extensions.py update
[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::Options::Edit"),
49                  ('c',"check","Edit-Transitions::Options::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     retry = 0
84     while retry < 10:
85         try:
86             lock_fd = os.open(lockfile, os.O_RDONLY | os.O_CREAT | os.O_EXCL)
87             retry = 10
88         except OSError, e:
89             if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EEXIST':
90                 retry += 1
91                 if (retry >= 10):
92                     daklib.utils.fubar("Couldn't obtain lock for %s." % (lockfile) )
93                 else:
94                     print("Unable to get lock for %s (try %d of 10)" % (lockfile, retry) )
95                     time.sleep(60)
96             else:
97                 raise
98
99
100 ################################################################################
101
102 def edit_transitions():
103     trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
104
105     tempfile = "./%s.transition.tmp" % (os.getpid() )
106
107     lockfile="%s.lock" % (tempfile)
108     lock_file(lockfile)
109
110     daklib.utils.copy(trans_file, tempfile)
111
112     editor = os.environ.get("EDITOR", "vi")
113
114     while True:
115         result = os.system("%s %s" % (editor, tempfile))
116         if result != 0:
117             os.unlink(tempfile)
118             os.unlink(lockfile)
119             daklib.utils.fubar("%s invocation failed for %s, not removing tempfile." % (editor, tempfile))
120     
121         # Now try to load the new file
122         test = load_transitions(tempfile)
123
124         if test == None:
125             # Edit is broken
126             answer = "XXX"
127             prompt = "Broken edit: [E]dit again, Drop changes?"
128
129             while prompt.find(answer) == -1:
130                 answer = daklib.utils.our_raw_input(prompt)
131                 if answer == "":
132                     answer = "E"
133                 answer = answer[:1].upper()
134
135             if answer == 'E':
136                 continue
137             elif answer == 'D':
138                 os.unlink(tempfile)
139                 os.unlink(lockfile)
140                 print "OK, discarding changes"
141                 sys.exit(0)
142         else:
143             # No problems in loading the new file, jump out of the while loop
144             break
145
146     # We seem to be done and also have a working file. Copy over.
147     daklib.utils.copy(tempfile, trans_file, True)
148     os.unlink(tempfile)
149     os.unlink(lockfile)
150
151     # Before we finish print out transition info again
152     print "\n\n------------------------------------------------------------------------"
153     print "Edit done, file saved, currently defined transitions:\n"
154     transitions = load_transitions(Cnf["Dinstall::Reject::ReleaseTransitions"])
155     transition_info(transitions)
156
157 ################################################################################
158
159 def load_transitions(trans_file):
160     # Parse the yaml file
161     sourcefile = file(trans_file, 'r')
162     sourcecontent = sourcefile.read()
163     try:
164         trans = syck.load(sourcecontent)
165     except syck.error, msg:
166         # Someone fucked it up
167         print "ERROR: %s" % (msg)
168         return None
169     return trans
170
171 ################################################################################
172
173 def print_info(trans, source, expected, rm, reason, packages):
174         print """
175 Looking at transition: %s
176  Source:      %s
177  New Version: %s
178  Responsible: %s
179  Description: %s
180  Blocked Packages (total: %d): %s
181 """ % (trans, source, expected, rm, reason, len(packages), ", ".join(packages))
182         return
183
184 ################################################################################
185
186 def transition_info(transitions):
187     for trans in transitions:
188         t = transitions[trans]
189         source = t["source"]
190         expected = t["new"]
191
192         # Will be None if nothing is in testing.
193         current = daklib.database.get_suite_version(source, "testing")
194
195         print_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
196
197         if current == None:
198             # No package in testing
199             print "Transition source %s not in testing, transition still ongoing." % (source)
200         else:
201             compare = apt_pkg.VersionCompare(current, expected)
202             print "Apt compare says: %s" % (compare)
203             if compare < 0:
204                 # This is still valid, the current version in database is older than
205                 # the new version we wait for
206                 print "This transition is still ongoing, we currently have version %s" % (current)
207             else:
208                 print "This transition is over, the target package reached testing, should be removed"
209                 print "%s wanted version: %s, has %s" % (source, expected, current)
210         print "-------------------------------------------------------------------------"
211
212 ################################################################################
213
214 def check_transitions(transitions):
215     to_dump = 0
216     to_remove = []
217     # Now look through all defined transitions
218     for trans in transitions:
219         t = transitions[trans]
220         source = t["source"]
221         expected = t["new"]
222
223         # Will be None if nothing is in testing.
224         current = daklib.database.get_suite_version(source, "testing")
225
226         print_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
227
228         if current == None:
229             # No package in testing
230             print "Transition source %s not in testing, transition still ongoing." % (source)
231         else:
232             compare = apt_pkg.VersionCompare(current, expected)
233             print "Apt compare says: %s" % (compare)
234             if compare < 0:
235                 # This is still valid, the current version in database is older than
236                 # the new version we wait for
237                 print "This transition is still ongoing, we currently have version %s" % (current)
238             else:
239                 print "REMOVE: This transition is over, the target package reached testing. REMOVE"
240                 print "%s wanted version: %s, has %s" % (source, expected, current)
241                 to_remove.append(trans)
242                 to_dump = 1
243         print "-------------------------------------------------------------------------"
244
245     if to_dump:
246         prompt = "Removing: "
247         for remove in to_remove:
248             prompt += remove
249             prompt += ","
250
251         prompt += " Commit Changes? (y/N)"
252         answer = ""
253
254         if Options["no-action"]:
255             answer="n"
256         else:
257             answer = daklib.utils.our_raw_input(prompt).lower()
258
259         if answer == "":
260             answer = "n"
261
262         if answer == 'n':
263             print "Not committing changes"
264             sys.exit(0)
265         elif answer == 'y':
266             print "Committing"
267             for remove in to_remove:
268                 del transitions[remove]
269             destfile = file(Cnf["Dinstall::Reject::ReleaseTransitions"], 'w')
270             syck.dump(transitions, destfile)
271             print "Done"
272         else:
273             print "WTF are you typing?"
274             sys.exit(0)
275
276
277 ################################################################################
278
279 def main():
280     global Cnf
281
282     init()
283     
284     # Only check if there is a file defined (and existant) with checks. It's a little bit
285     # specific to Debian, not much use for others, so return early there.
286     if not Cnf.has_key("Dinstall::Reject::ReleaseTransitions") or not os.path.exists("%s" % (Cnf["Dinstall::Reject::ReleaseTransitions"])):
287         daklib.utils.warn("Dinstall::Reject::ReleaseTransitions not defined or file %s not existant." %
288                           (Cnf["Dinstall::Reject::ReleaseTransitions"]))
289         sys.exit(1)
290     
291     # Parse the yaml file
292     transitions = load_transitions(Cnf["Dinstall::Reject::ReleaseTransitions"])
293     if transitions == None:
294         # Something very broken with the transitions, exit
295         daklib.utils.warn("Not doing any work, someone fucked up the transitions file outside our control")
296         sys.exit(2)
297
298     if Options["edit"]:
299         # Output information about the currently defined transitions.
300         print "Currently defined transitions:"
301         transition_info(transitions)
302         daklib.utils.our_raw_input("Press enter to continue...")
303
304         # Lets edit the transitions file
305         edit_transitions()
306     elif Options["check"]:
307         # Check and remove outdated transitions
308         check_transitions(transitions)
309     else:
310         # Output information about the currently defined transitions.
311         transition_info(transitions)
312
313         # Nothing requested, doing nothing besides the above display of the transitions
314         sys.exit(0)
315     
316
317 ################################################################################
318
319 if __name__ == '__main__':
320     main()