Commit 99a5c48c1b8450d8e4508598431eefef7d16d5d8
1 parent
02159428
Exists in
master
and in
29 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 | 47 | end |
| 48 | 48 | |
| 49 | 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 | 65 | end |
| 63 | 66 | end |
| 64 | 67 | |
| 65 | 68 | FORBIDDEN_LOGINS = %w[ |
| 66 | 69 | info |
| 67 | 70 | ] |
| 68 | -Progress.instance.start(ARGV.size) | |
| 71 | +Progress.start(ARGV.size) | |
| 69 | 72 | for username in ARGV |
| 70 | - Progress.instance.step | |
| 73 | + Progress.step | |
| 71 | 74 | begin |
| 72 | 75 | User.transaction do |
| 73 | 76 | # guess environment |
| ... | ... | @@ -89,17 +92,17 @@ for username in ARGV |
| 89 | 92 | person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) |
| 90 | 93 | person.preferred_domain = get_domain(domain_name) |
| 91 | 94 | user.save! |
| 92 | - Progress.instance.puts "I: #{username} data imported" | |
| 95 | + Progress.say "I: #{username} data imported" | |
| 93 | 96 | |
| 94 | 97 | # import articles |
| 95 | 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 | 100 | article = TinyMceArticle.new |
| 98 | 101 | article.from_xml(File.read(xml)) |
| 99 | 102 | article.profile = person |
| 100 | 103 | if article.valid? |
| 101 | 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 | 106 | else |
| 104 | 107 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" |
| 105 | 108 | next |
| ... | ... | @@ -108,12 +111,12 @@ for username in ARGV |
| 108 | 111 | if article.is_homepage |
| 109 | 112 | person.home_page = article |
| 110 | 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 | 115 | end |
| 113 | 116 | |
| 114 | 117 | File.open("tmp/rewrite.txt", 'w+') do |file| |
| 115 | 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 | 120 | end |
| 118 | 121 | |
| 119 | 122 | # import attachments |
| ... | ... | @@ -122,16 +125,16 @@ for username in ARGV |
| 122 | 125 | file = UploadedFile.new |
| 123 | 126 | file.from_xml(File.read(attachment_xml)) |
| 124 | 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 | 129 | next |
| 127 | 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 | 132 | file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) |
| 130 | 133 | file.parent = article |
| 131 | 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 | 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 | 139 | file.reload |
| 137 | 140 | if file.image? |
| ... | ... | @@ -158,7 +161,7 @@ for username in ARGV |
| 158 | 161 | block.title = data['menu']['title'] |
| 159 | 162 | block.links = links |
| 160 | 163 | block.save! |
| 161 | - Progress.instance.puts "imported links: #{links.inspect}" | |
| 164 | + Progress.say "imported links: #{links.inspect}" | |
| 162 | 165 | end |
| 163 | 166 | |
| 164 | 167 | end |
| ... | ... | @@ -170,4 +173,5 @@ for username in ARGV |
| 170 | 173 | $stderr.puts "==================================" |
| 171 | 174 | $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." |
| 172 | 175 | end |
| 176 | + Progress.step_done | |
| 173 | 177 | end | ... | ... |