export.rake
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'csv'
namespace :export do
namespace :catalog do
desc "Export all softwares to CSV"
task :csv => :environment do
Environment.all.each do |env|
if env.plugin_enabled?("MpogSoftware") or env.plugin_enabled?("SoftwareCommunitiesPlugin")
softwares_to_csv
categories_to_csv
software_categories_to_csv
compress_files
end
end
end
end
def softwares_to_csv
print "Exporting softwares to softwares.csv: "
CSV.open('/tmp/softwares.csv', 'w') do |csv|
csv << [
"id",
"community_id",
"identifier",
"name",
"finality",
"acronym",
"created_at",
"image_filename",
"home_page_name",
"home_page_slug",
"home_page_path",
"home_page_body",
"home_page_abstract",
"home_page_published_at"
]
SoftwareInfo.all.each do |software|
if software.community
begin
csv << [
software.id,
software.community.id,
software.community.identifier,
software.community.name,
software.finality,
software.acronym,
software.community.created_at,
(software.community.image.nil? ? nil : software.community.image.filename),
(software.community.home_page.nil? ? nil : software.community.home_page.name),
(software.community.home_page.nil? ? nil : software.community.home_page.slug),
(software.community.home_page.nil? ? nil : software.community.home_page.path),
(software.community.home_page.nil? ? nil : software.community.home_page.body),
(software.community.home_page.nil? ? nil : software.community.home_page.abstract),
(software.community.home_page.nil? ? nil : software.community.home_page.published_at),
]
print '.'
rescue
print 'F'
end
end
end
end
print "\n"
end
def categories_to_csv
print "Exporting categories to categories.csv: "
CSV.open('/tmp/categories.csv', 'w') do |csv|
csv << [
"id",
"name",
"path",
]
Category.all.each do |category|
begin
csv << [
category.id,
category.name,
category.path,
]
print '.'
rescue
print 'F'
end
end
end
print "\n"
end
def software_categories_to_csv
print "Exporting software and categories relation to software_categories.csv: "
CSV.open('/tmp/software_categories.csv', 'w') do |csv|
csv << [
"software_id",
"category_id"
]
SoftwareInfo.all.each do |software|
if software.community
software.community.categories.each do |category|
begin
csv << [
software.id,
category.id
]
print '.'
rescue
print 'F'
end
end
end
end
end
print "\n"
end
def compress_files
`cd /tmp/ && tar -zcvf software_catalog_csvs.tar.gz softwares.csv categories.csv software_categories.csv`
`cd /tmp/ && rm softwares.csv categories.csv software_categories.csv`
end
end