e350638d301044e0d73434bd9a6509b56d479e59
[packages.grml.org.git] / update.rb
1 #!/usr/bin/env ruby
2 require 'git'
3 require 'builder'
4 require 'erb'
5 require 'yaml'
6
7 def build_used_package_list(source_files)
8         source_files.map do |source_file|
9                 File.read(source_file).split("\n").reject do |l|
10                         l.strip.start_with?('#') or l.strip == ""
11                 end
12         end.flatten
13 end
14
15 used_packages = {
16         :full => build_used_package_list(['grml-live/etc/grml/fai/config/package_config/GRMLBASE', 'grml-live/etc/grml/fai/config/package_config/GRML_FULL']),
17 }
18
19 def build_package_list(packages, used)
20         data = {}
21         packages.each do |pkg|
22                 next if not File.exists?(File.join(pkg, 'debian'))
23                 begin
24                         g = Git.open(working_dir = pkg)
25                         #g.pull(Git::Repo, Git::Branch) # fetch and a merge
26                 rescue ArgumentError
27                         g = Git.bare(working_dir = pkg)
28                 end
29
30                 current_head = g.gcommit('HEAD')
31                 next if not current_head.parent
32
33                 p = {
34                         :head_is_tagged => false,
35                         :used => {},
36                         :name => pkg,
37                         :version => nil,
38                         :has_tags => false,
39                 }
40                 used.each do |dist,l|
41                         p[:used][dist] = l.include?(pkg)
42                 end
43
44                 for tag in g.tags.reverse
45                         p[:has_tags] = true
46                         t = g.gcommit(tag.name)
47                         next if not t.parent
48                         #$stderr.puts "#{pkg}: Checking tag #{tag.name}: tag parent: #{t.parent.sha} HEAD: #{current_head.parent.sha}"
49                         if t.parent.sha === current_head.parent.sha
50                                 p[:head_is_tagged] = true
51                                 p[:version] = tag.name
52                                 break
53                         end
54                 end
55
56                 data[pkg] = p
57         end
58         data.values.sort { |x,y| x[:name] <=> y[:name] }
59 end
60
61 packages = build_package_list(ARGV, used_packages)
62
63 template = ERB.new <<-EOF
64 <!doctype html>
65 <head>
66   <meta charset="utf-8">
67   <title>Grml.org Package Index</title>
68   <link rel="stylesheet" href="style.css">
69 </head>
70 <body>
71   <header>
72   <h1>All Grml packages</h1>
73   <p>Last update: <%= Time.now.to_s %></p>
74   </header>
75   <div id="main">
76   <table>
77   <tr>
78   <th>Package</th><th>Git</th><th>Download</th><th>Fresh?</th><th>In FULL?</th>
79   </tr>
80   <% packages.each do |p| %>
81   <tr>
82   <td><%= p[:name] %></td>
83   <td class="git"><a href="http://git.grml.org/?p=<%= p[:name] %>.git;a=summary">Git</a></td>
84   <td class="download">
85     <% if p[:has_tags] %>
86     <a href="http://deb.grml.org/pool/main/<%= p[:name][0..0] %>/<%= p[:name] %>/">Download</a>
87     <% end %>
88   </td>
89   <% if p[:head_is_tagged] %>
90   <td class="ok">Version <%= p[:version] %></td>
91   <% else %>
92   <td class="error <% if p[:used][:full] %>important<% end %>">Untagged changes</td>
93   <% end %>
94   <td class="installed"><%= p[:used][:full] ? "Yes" : "No" %></td>
95   </tr>
96   <% end %>
97   </table>
98   </div>
99   <footer>
100   </footer>
101 </body>
102 </html>
103 EOF
104 File.open('index.html.new','w') do |f|
105   f.write template.result(binding)
106 end
107 File.open('packages.yaml.new','w') do |f|
108   f.write packages.to_yaml
109 end
110
111 FileUtils.mv 'index.html.new', 'index.html'
112 FileUtils.mv 'packages.yaml.new', 'packages.yaml'
113