1 |
commit: c0c412ef02302ccf9e306d091d429e7df3d6278f |
2 |
Author: Alex Legler <alex <AT> a3li <DOT> li> |
3 |
AuthorDate: Sat Dec 26 17:42:30 2015 +0000 |
4 |
Commit: Alex Legler <a3li <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Dec 26 17:42:30 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/data/api.git/commit/?id=c0c412ef |
7 |
|
8 |
Add projects.xml generation script |
9 |
|
10 |
bin/projects-xml.rb | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 |
1 file changed, 70 insertions(+) |
12 |
|
13 |
diff --git a/bin/projects-xml.rb b/bin/projects-xml.rb |
14 |
new file mode 100755 |
15 |
index 0000000..328cc93 |
16 |
--- /dev/null |
17 |
+++ b/bin/projects-xml.rb |
18 |
@@ -0,0 +1,70 @@ |
19 |
+#!/usr/bin/env ruby |
20 |
+# Generates projects.xml as per GLEP-67 from projects.json via semantic-data-toolkit. |
21 |
+# The file is only touched if contents change. |
22 |
+# |
23 |
+# Usage: projects-xml.rb <file> |
24 |
+# |
25 |
+# Alex Legler <a3li@g.o> |
26 |
+ |
27 |
+require 'net/http' |
28 |
+require 'json' |
29 |
+require 'nokogiri' |
30 |
+ |
31 |
+PROJECTS_JSON = URI('https://wiki.gentoo.org/rdf/projects.json') |
32 |
+ |
33 |
+projects = begin |
34 |
+ JSON.parse(Net::HTTP.get(PROJECTS_JSON)) |
35 |
+rescue JSON::ParserError |
36 |
+ abort 'Cannot load projects.json.' |
37 |
+end |
38 |
+ |
39 |
+parent_map = Hash.new { |h, k| h[k] = [] } |
40 |
+ |
41 |
+projects.each_pair do |id, project| |
42 |
+ parent_map[project['parent']] << id if project.key? 'parent' |
43 |
+end |
44 |
+ |
45 |
+projects_xml = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| |
46 |
+ xml.doc.create_internal_subset('projects', nil, 'http://www.gentoo.org/dtd/projects.dtd') |
47 |
+ xml.projects do |
48 |
+ projects.each_pair do |id, project| |
49 |
+ xml.project do |
50 |
+ xml.email project['email'] |
51 |
+ xml.name project['name'] |
52 |
+ xml.url project['href'] |
53 |
+ xml.description project['description'] |
54 |
+ |
55 |
+ if parent_map.key? id |
56 |
+ parent_map[id].each do |subproject_id| |
57 |
+ attributes = { ref: projects[subproject_id]['email'] } |
58 |
+ attributes['inherit-members'] = '1' if projects[subproject_id]['propagates_members'] |
59 |
+ |
60 |
+ xml.subproject nil, attributes |
61 |
+ end |
62 |
+ end |
63 |
+ |
64 |
+ project['members'].sort { |a, b| a['nickname'].casecmp(b['nickname']) }.each do |member| |
65 |
+ attributes = {} |
66 |
+ attributes['is-lead'] = '1' if member['is_lead'] |
67 |
+ |
68 |
+ xml.member nil, attributes do |
69 |
+ xml.email member['email'] |
70 |
+ xml.name member['name'] |
71 |
+ xml.role member['role'] if member.key?('role') && !member['role'].empty? |
72 |
+ end |
73 |
+ end |
74 |
+ end |
75 |
+ end |
76 |
+ end |
77 |
+end |
78 |
+ |
79 |
+output_file = ARGV[0] |
80 |
+ |
81 |
+generated_xml = projects_xml.to_xml |
82 |
+current_xml = begin |
83 |
+ File.read(output_file) |
84 |
+rescue Errno::ENOENT |
85 |
+ '' |
86 |
+end |
87 |
+ |
88 |
+File.write(output_file, projects_xml.to_xml) unless generated_xml == current_xml |