Debian Codegraph @

utils/rch

raw
1 #!/usr/bin/ruby
2
3 =begin rdoc
4 This is a small helper script for keeping changelogs up to date.
5 It is copyright 2006 by Vincent Fourmond, and can be distributed under
6 the terms of the General Public License version 2.
7 =end
8
9
10 require 'tempfile'
11 # we need a decent parsing of command-line here...
12 # it is provided by optparse, which is quite cool...
13
14 require 'optparse'
15 require 'ostruct'
16 require 'etc'
17
18
19
20 # A class to describe and parse a changelog line
21 class CLLine
22 # The text of the line:
23 attr_accessor :line
24
25 # The type of the line:
26 # * :version (i.e. mkmf2 -- 0.1.2)
27 # * :blank
28 # * :name (describing the name [...])
29 # * :entry ( starting with ' * ')
30 # * :entry_cont (starting with enough blank space)
31 # * :sig (signature)
32 attr_accessor :kind
33
34 # Hash that can be used to store interesting informations.
35 attr_accessor :data
36
37 def initialize(line = "")
38 @line = line
39 @data = {}
40
41 # we parse the line here.
42 case line
43 when /^\s*(\w+)\s+\(\s*([\w.-]+)\s*\)\s*(?:\w+\s*)?$/
44 # this is a version line
45 @kind = :version
46 @data["package"] = $1
47 @data["version"] = $2
48 when /^\s+\*/
49 @kind = :entry
50 when /^\s*$/
51 @kind = :blank
52 when /^\s*\[\s*([^\]]+?)\s*\]/
53 @kind = :name
54 @data["name"] = $1
55 when /^\s--\s*((.*?)\s*<(.*)>)[\s,]\s([^<>]*?)\s*$/
56 @kind = :sig
57 @data["full name"] = $1
58 @data["name"] = $2
59 @data["email"] = $3
60 @data["date"] = $4
61 when /^\s\s+\S+/
62 @kind = :entry_cont
63 end
64 end
65
66 # A small wrapper around the data structure...
67 def [](str)
68 return @data[str]
69 end
70
71 # Changes the current entry into a proper signature...
72 def sign(email,date)
73 @kind = :sig
74 @line = " -- #{email} #{date}"
75 @data["full name"] = email
76 @data["date"] = date.chomp
77 if @data["full name"] =~ /^(.*?)\s*<(.*)>/
78 @data["name"] = $1
79 @data["email"] = $2
80 else
81 # we take the email address for the name
82 @data["name"] = @data["full name"]
83 end
84 end
85
86 end
87
88 # An section consists of a version, entries and a signature
89 class CLSection
90 # The CLLine object containing the version
91 attr_accessor :version
92
93 # The siganture
94 attr_accessor :signature
95
96 # The rest inbetween
97 attr_accessor :entries
98
99 # The names of the differents contributors. It is a hash where to a
100 # name corresponds the CLLine object where it is found.
101 attr_accessor :contributors
102
103 # This function takes an array of CLLines, removes blank lines
104 # then, takes everything between a version and a signature, removing them from
105 # the array. You'd better make a copy of the array if you need it otherwise.
106 def initialize(cllines)
107 # Stripping trailing blank lines.
108 while cllines[0] && cllines[0].kind == :blank
109 cllines.shift
110 end
111 if cllines[0].kind != :version
112 raise "Error parsing the changelog: version line expected, but got " +
113 cllines[0].inspect
114 end
115 @version = cllines.shift
116 @entries = []
117 @contributors = {}
118 while cllines[0].kind != :sig
119 @entries.push cllines.shift
120 if @entries.last.kind == :name
121 @contributors[@entries.last["name"]] = @entries.last
122 end
123 end
124 @signature = cllines.shift
125 # and removing all the blank lines after, to make sure nothing is left
126 # in the array if it is the end of the changelog.
127
128 while cllines[0] && cllines[0].kind == :blank
129 cllines.shift
130 end
131 end
132
133 # returns an array with the text lines of the section
134 def lines
135 lines = []
136 lines << @version.line
137 lines += @entries.collect { |e|
138 e.line
139 }
140 lines << @signature.line
141 end
142
143 # Returns the text for the section.
144 def to_s
145 return lines.join('')
146 end
147
148 def email_to_name(email)
149 email =~ /^\s*(.*?)\s*<(.*)>/
150 return $1
151 end
152
153 # returns the CLLine object corresponding to the last :entry of
154 # a contributor. You need to provide it's short name
155 def last_contributor_entry(name)
156 if ! @contributors.key?(name)
157 if name == sig_name
158 return last_entry
159 else
160 return nil
161 end
162 end
163 index = @entries.index(@contributors[name]) + 1
164 last = nil
165 while @entries[index] and @entries[index].kind == :blank
166 last = @entries[index]
167 index += 1
168 end
169 while @entries[index] and
170 (@entries[index].kind == :entry or
171 @entries[index].kind == :entry_cont)
172 last = @entries[index]
173 index += 1
174 end
175
176 # it can be the blank just after the name.
177 return last
178 end
179
180 def last_entry
181 entries = @entries.dup
182 while a = entries.pop
183 return a unless a.kind == :blank
184 end
185 return nil
186 end
187
188 def sig_name
189 return @signature["name"]
190 end
191
192 # adds a contributor to the list, and make the corresponding entries
193 def add_contributor(name)
194 return if @contributors.key?(name)
195
196 @contributors[name] = CLLine.new(" [#{name}]\n")
197
198 # we add the contributor at the end:
199 @entries.push @contributors[name]
200 @entries.push CLLine.new("\n")
201 end
202
203
204 # This functions adds a new entry to the section, with the given
205 # contributor name and returns an array containing:
206 # * the line number where the entry was added
207 # * an array containing the text lines
208 #
209 # The contributor name should include full email
210 def add_entry(contributor, entry = "")
211 # nil = ""
212 if entry.nil?
213 entry = ""
214 end
215 # we check if we need to add contributors name or not:
216 name = email_to_name(contributor)
217 new_entry = CLLine.new(" * #{entry}\n")
218 if name != sig_name
219 add_sig_name
220 add_contributor(name)
221 end
222 last_entry_index = @entries.index(last_contributor_entry(name))
223 # raise "Problem parsing the changelog ??" if last_entry_index.nil?
224 # no, this is not right: we want to be able to deal with almost empty
225 # Changelogs -- namely, the ones created by this very program ;-) !
226 if last_entry_index.nil?
227 last_entry_index = -1
228 # we add an initial \n:
229 new_entry.line = "\n#{new_entry.line}"
230 end
231 @entries[last_entry_index + 1, 0] = new_entry
232
233 # last thing: we need to update the signature:
234 @signature.sign(contributor, CLFile.date)
235 return [@entries.index(new_entry) + 2, lines]
236 end
237
238 # Adds a name entry in the beginning with the name of the signature
239 def add_sig_name
240 return if @contributors.key?(sig_name)
241 @contributors[sig_name] =
242 CLLine.new(" [#{sig_name}]\n")
243 @entries.unshift @contributors[sig_name]
244 @entries.unshift CLLine.new("\n")
245 end
246
247 # normalize makes sure that the entries start and end with a blank line, and
248 # that there are never more than one blank line in a row
249 def normalize
250 # to be written...
251 end
252 end
253
254 # The class describing the Changelog file.
255 class CLFile
256 # The lines of the file
257 attr_accessor :lines
258
259 # The sections of the file
260 attr_accessor :sections
261
262 # returns the date in the wanted format...
263 def CLFile.date
264 return `date`
265 end
266
267 # file can be either an IO object or a file name.
268 def initialize(file)
269 if file.is_a? String
270 file = File.open(file)
271 end
272
273 @lines = []
274 for line in file
275 @lines << CLLine.new(line)
276 end
277 @sections = []
278
279 # we make a copy to feed it to CLSection
280 lines = @lines.dup
281 while lines.length > 0
282 @sections.push CLSection.new(lines)
283 end
284 end
285
286 def latest_version
287 return @sections[0].version["version"]
288 end
289
290 # Creates a release of the given directory by parsing the changelog and
291 # creating an appropriately named archive. The directory name will be
292 # changed to match the _package_-_version_ convention.
293 #
294 # As is, it probably shouldn't be used directly, but rather through
295 # a slightly more advanced interface.
296 def CLFile.make_release(dir, changelog = "Changelog")
297 base_dir = File.dirname(dir)
298
299 ch = CLFile.new(File.join(dir, changelog))
300 target_name = "#{ch.package_name}-#{ch.latest_version}"
301 # First, we rename the directory to the correct name
302 File.rename(dir, File.join(base_dir, target_name))
303 # Then, we make an archive out of it:
304 cmd = "cd #{base_dir}; tar cvzf #{target_name}.tar.gz #{target_name}"
305 system cmd
306 # and that's done !!!
307 target_name
308 end
309
310 # This convenience function does the following:
311 # * exports the given module from the given repository with the given tag
312 # * looks for a Changelog file in it
313 # * calls make_release
314 # * and erases the exported directory
315
316 def CLFile.cvs_release(mod, tag, rep = nil, changelog = "Changelog")
317 # we first make up a temporary directory name...
318 export_dir = "biniou-temp"
319 cmd = "cvs " + if rep
320 " -d #{rep} "
321 else
322 ""
323 end +
324 "export -d #{export_dir} -r #{tag} #{mod}"
325 puts cmd
326 system cmd
327 tg = make_release(File.join(Dir::pwd, export_dir),
328 changelog)
329 end
330
331
332 # Increases a version number and returns it
333 def CLFile.increase_version(version)
334 return version.sub(/(\w+)\W*$/) do |str|
335 str.succ
336 end
337 end
338
339 def increase_version
340 return CLFile.increase_version(latest_version)
341 end
342
343 def package_name
344 return @sections[0].version["package"]
345 end
346
347 # This is the most interesting function: it adds a new entry, or prompts
348 # the user for it (via an external text editor). If version is :increase
349 # then the latest version is taken and increased automatically. The output
350 # is written to the file specified by target_file (which has to be an IO
351 # object -- and a real file in case of user interaction).
352 # This function returns true if the provided file has been modified in a
353 # reasonable way (that is: either we provided text, or the user did actually
354 # modify the file)
355 def add_new_entry(target_file,email,
356 version = nil, entry = nil,
357 editor = "nano",
358 verbose = false,
359 editor_line_syntax = "+%d")
360 if version.nil? or version == latest_version
361 line_number, lines = @sections[0].add_entry(email, entry)
362 text = lines.join('') + "\n" +
363 @sections[[email protected] - 1].collect do |v|
364 v.to_s
365 end.join("\n")
366 else
367 if version == :increase
368 version = increase_version
369 end
370 text = CLFile.create_new(email, version,
371 package_name,if entry
372 entry
373 else
374 ""
375 end
376 ) +
377 @sections.collect do |v|
378 v.to_s
379 end.join("\n")
380 line_number = 3
381 end
382 # now, text is the text of the changelog, and line_number
383 # the number of the line which the user should edit
384
385 target_file.print text
386 target_file.close
387 # closing the file so that its contents appear on
388 # the hard drive...
389 if(entry.nil?)
390 # interactive edition
391 time_before = File::mtime(target_file.path)
392 line_number_spec = sprintf(editor_line_syntax, line_number)
393 cmd = "#{editor} #{line_number_spec} #{target_file.path}"
394 puts cmd if verbose
395 system cmd
396 time_after = File::mtime(target_file.path)
397 return time_before < time_after
398 end
399 return true
400 end
401
402 # Creates a new changelog from scratch and writes it to filename,
403 # or returns the corresponding string.
404 def CLFile.create_new(email, version, package,
405 entry = nil,
406 filename = nil)
407 contents = "#{package} (#{version})\n\n" + if entry
408 " * #{entry}\n\n"
409 else
410 ""
411 end +
412 " -- #{email} #{CLFile.date}\n"
413 if filename
414 File.open(filename, "w") do |f|
415 f.print contents
416 end
417 end
418 return contents
419 end
420
421 end
422
423
424 # new generic structure:
425 options = OpenStruct.new
426 options.version = nil
427
428 if ENV.has_key? "RCH_EMAIL"
429 options.email = ENV["RCH_EMAIL"]
430 else
431 # Try to make up something from the etc gecos field
432 # and the environment variable EMAIL (or username@hostname if not defined)
433 email = ""
434 if ENV.has_key? "EMAIL"
435 email = ENV["EMAIL"]
436 else
437 # Make up one email, not nice-looking, but not too bad.
438 email = "#{Etc.getpwuid.name}@localhost"
439 end
440 options.email = "#{Etc.getpwuid.gecos.split(',')[0]} <#{email}>"
441 end
442 options.package = nil
443
444 # The editor
445 if ENV.has_key? "EDITOR"
446 options.editor = ENV["EDITOR"]
447 else
448 options.editor = "nano" # nano by default
449 end
450
451
452 # The editor's specification for a command-line
453 if ENV.has_key? "EDITOR_LINE_SPEC"
454 options.editor_line_spec = ENV["EDITOR_LINE_SPEC"]
455 else
456 options.editor_line_spec = "+%d" # works for nano, and for others as well
457 end
458
459
460 options.verbose = false
461
462 opts = OptionParser.new do |opts|
463 opts.banner = "$0 [options]"
464
465 opts.on("--version VERSION",
466 "Create a new entry for the version VERSION") do |version|
467 puts "Got version #{version}"
468 options.version = version
469 end
470
471 opts.on("-e","--entry ENTRY",
472 "Adds the entry ENTRY without prompting the user") do |entry|
473 options.entry = entry
474 end
475
476 opts.on("-i","--increase",
477 "Increase automatically the version number") do
478 options.version = :increase
479 end
480
481 opts.on("--email EMAIL",
482 "Sets the full email address to EMAIL") do |email|
483 options.email = email
484 end
485
486 opts.on("-p","--package PACKAGE",
487 "Sets the package name to PACKAGE") do |package|
488 options.package = package
489 end
490
491 opts.on("-c","--create",
492 "Creates a brand new changelog. Version and package " +
493 "have to be specified") do
494 options.create = true
495 end
496
497 opts.on("--editor EDITOR",
498 "Specifies your default editor for editing the changelog") do |e|
499 options.editor = e
500 end
501
502 opts.on("--line_spec SPEC",
503 "How rch should tell the editor which line to edit" +
504 " (like for sprintf)") do |l|
505 options.editor_line_spec = l
506 end
507
508 opts.on("--release",
509 "Make an archive of the current directory, and name it " +
510 " according to the changelog.") do
511 options.release = true
512 end
513
514 opts.on("--cvs MODULE",
515 "Does a cvs release -- you need to specify a " +
516 "tag and maybe a repository") do |mod|
517 options.cvs_release = true
518 options.module = mod
519 end
520
521 opts.on("--cvs-tag TAG",
522 "Does a cvs release") do |tag|
523 options.tag = tag
524 end
525
526 opts.on("-v","--verbose",
527 "Prints out a command before executing it") do
528 options.verbose = true
529 end
530
531
532 opts.on("--repository REP",
533 "Chooses the repository REP for --cvs") do |rep|
534 options.repository = rep
535 end
536
537 opts.on("--print-version",
538 "Only prints the latest version of the package and exit") do |rep|
539 options.print_version = rep
540 end
541
542
543 end
544
545 opts.parse!
546
547 file = "Changelog"
548 if $*[0]
549 file = $*[0]
550 end
551
552 if options.create
553 if options.version && options.package
554 CLFile.create_new(options.email, options.version,
555 options.package,nil,file)
556 else
557 puts "You should specify a package name and a version"
558 end
559 elsif options.cvs_release
560 CLFile.cvs_release(options.module, options.tag,
561 options.repository,
562 file)
563 elsif options.release
564 CLFile.make_release(Dir.pwd, file)
565 else
566 cl = CLFile.new(file)
567 if options.print_version
568 print cl.latest_version
569 exit
570 end
571
572 # basic changelog addition:
573 temp_file = Tempfile.new('rch_changelog')
574 if cl.add_new_entry(temp_file, options.email,
575 options.version, options.entry,
576 options.editor, options.verbose,
577 options.editor_line_spec)
578 cmd = "cp '#{temp_file.path}' '#{file}'"
579 puts cmd if options.verbose
580 system cmd
581 end
582 end
583
584