Commit 99a5c48c1b8450d8e4508598431eefef7d16d5d8
1 parent
02159428
Exists in
master
and in
28 other branches
ActionItem305: cleaning the console output
Showing
1 changed file
with
28 additions
and
24 deletions
Show diff stats
script/import-cooperation.net
@@ -47,27 +47,30 @@ class FileData < StringIO | @@ -47,27 +47,30 @@ class FileData < StringIO | ||
47 | end | 47 | end |
48 | 48 | ||
49 | class Progress | 49 | class Progress |
50 | - def self.instance | ||
51 | - @instance ||= self.new | ||
52 | - end | ||
53 | - def start(n) | ||
54 | - @total = n | ||
55 | - @current = 0 | ||
56 | - end | ||
57 | - def step | ||
58 | - @current += 1 | ||
59 | - end | ||
60 | - def puts(msg) | ||
61 | - Kernel.puts("[%d/%d] %s" % [@current, @total, msg]) | 50 | + DEBUG = false |
51 | + class << self | ||
52 | + def start(n) | ||
53 | + @total = n | ||
54 | + @current = 0 | ||
55 | + end | ||
56 | + def step | ||
57 | + @current += 1 | ||
58 | + end | ||
59 | + def step_done | ||
60 | + say('=> done', true) | ||
61 | + end | ||
62 | + def say(msg, force = Progress::DEBUG) | ||
63 | + puts("[%d/%d] %s" % [@current, @total, msg]) if force | ||
64 | + end | ||
62 | end | 65 | end |
63 | end | 66 | end |
64 | 67 | ||
65 | FORBIDDEN_LOGINS = %w[ | 68 | FORBIDDEN_LOGINS = %w[ |
66 | info | 69 | info |
67 | ] | 70 | ] |
68 | -Progress.instance.start(ARGV.size) | 71 | +Progress.start(ARGV.size) |
69 | for username in ARGV | 72 | for username in ARGV |
70 | - Progress.instance.step | 73 | + Progress.step |
71 | begin | 74 | begin |
72 | User.transaction do | 75 | User.transaction do |
73 | # guess environment | 76 | # guess environment |
@@ -89,17 +92,17 @@ for username in ARGV | @@ -89,17 +92,17 @@ for username in ARGV | ||
89 | person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) | 92 | person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) |
90 | person.preferred_domain = get_domain(domain_name) | 93 | person.preferred_domain = get_domain(domain_name) |
91 | user.save! | 94 | user.save! |
92 | - Progress.instance.puts "I: #{username} data imported" | 95 | + Progress.say "I: #{username} data imported" |
93 | 96 | ||
94 | # import articles | 97 | # import articles |
95 | Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| | 98 | Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| |
96 | - Progress.instance.puts "I: Trying to import #{username}'s article in #{xml} ..." | 99 | + Progress.say "I: Trying to import #{username}'s article in #{xml} ..." |
97 | article = TinyMceArticle.new | 100 | article = TinyMceArticle.new |
98 | article.from_xml(File.read(xml)) | 101 | article.from_xml(File.read(xml)) |
99 | article.profile = person | 102 | article.profile = person |
100 | if article.valid? | 103 | if article.valid? |
101 | article.save! | 104 | article.save! |
102 | - Progress.instance.puts "I: #{username}'s article #{article.name.inspect} imported" | 105 | + Progress.say "I: #{username}'s article #{article.name.inspect} imported" |
103 | else | 106 | else |
104 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" | 107 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" |
105 | next | 108 | next |
@@ -108,12 +111,12 @@ for username in ARGV | @@ -108,12 +111,12 @@ for username in ARGV | ||
108 | if article.is_homepage | 111 | if article.is_homepage |
109 | person.home_page = article | 112 | person.home_page = article |
110 | person.save! | 113 | person.save! |
111 | - Progress.instance.puts "I: Article #{article.name.inspect} is #{username}'s homepage!" | 114 | + Progress.say "I: Article #{article.name.inspect} is #{username}'s homepage!" |
112 | end | 115 | end |
113 | 116 | ||
114 | File.open("tmp/rewrite.txt", 'w+') do |file| | 117 | File.open("tmp/rewrite.txt", 'w+') do |file| |
115 | file.puts("#{article.id} #{url_for(article.url)}") | 118 | file.puts("#{article.id} #{url_for(article.url)}") |
116 | - Progress.instance.puts "I: Article with id = #{article.id} redirected to #{url_for(article.url)}" | 119 | + Progress.say "I: Article with id = #{article.id} redirected to #{url_for(article.url)}" |
117 | end | 120 | end |
118 | 121 | ||
119 | # import attachments | 122 | # import attachments |
@@ -122,16 +125,16 @@ for username in ARGV | @@ -122,16 +125,16 @@ for username in ARGV | ||
122 | file = UploadedFile.new | 125 | file = UploadedFile.new |
123 | file.from_xml(File.read(attachment_xml)) | 126 | file.from_xml(File.read(attachment_xml)) |
124 | if !File.exist?(file.filesystem_location) | 127 | if !File.exist?(file.filesystem_location) |
125 | - Progress.instance.puts "W: skipping attachment pointing to unexisting file" | 128 | + Progress.say "W: skipping attachment pointing to unexisting file" |
126 | next | 129 | next |
127 | end | 130 | end |
128 | - Progress.instance.puts "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" | 131 | + Progress.say "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" |
129 | file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) | 132 | file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) |
130 | file.parent = article | 133 | file.parent = article |
131 | file.profile = person | 134 | file.profile = person |
132 | - Progress.instance.puts "I: Trying to save attachment \"#{file.filename}/#{file.slug}\"" | 135 | + Progress.say "I: Trying to save attachment \"#{file.filename}/#{file.slug}\"" |
133 | file.save! | 136 | file.save! |
134 | - Progress.instance.puts "I: attachment added to article #{article.id}" | 137 | + Progress.say "I: attachment added to article #{article.id}" |
135 | 138 | ||
136 | file.reload | 139 | file.reload |
137 | if file.image? | 140 | if file.image? |
@@ -158,7 +161,7 @@ for username in ARGV | @@ -158,7 +161,7 @@ for username in ARGV | ||
158 | block.title = data['menu']['title'] | 161 | block.title = data['menu']['title'] |
159 | block.links = links | 162 | block.links = links |
160 | block.save! | 163 | block.save! |
161 | - Progress.instance.puts "imported links: #{links.inspect}" | 164 | + Progress.say "imported links: #{links.inspect}" |
162 | end | 165 | end |
163 | 166 | ||
164 | end | 167 | end |
@@ -170,4 +173,5 @@ for username in ARGV | @@ -170,4 +173,5 @@ for username in ARGV | ||
170 | $stderr.puts "==================================" | 173 | $stderr.puts "==================================" |
171 | $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." | 174 | $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." |
172 | end | 175 | end |
176 | + Progress.step_done | ||
173 | end | 177 | end |