Commit a8cfd272270ec676cf6775c8838b48408478b987
1 parent
a5c10ed7
Exists in
master
and in
28 other branches
ActionItem305: changes in import
Showing
1 changed file
with
28 additions
and
9 deletions
Show diff stats
script/import-cooperation.net
| @@ -46,7 +46,25 @@ class FileData < StringIO | @@ -46,7 +46,25 @@ class FileData < StringIO | ||
| 46 | end | 46 | end |
| 47 | end | 47 | end |
| 48 | 48 | ||
| 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]) | ||
| 62 | + end | ||
| 63 | +end | ||
| 64 | + | ||
| 65 | +Progress.instance.start(ARGV.size) | ||
| 49 | for username in ARGV | 66 | for username in ARGV |
| 67 | + Progress.instance.step | ||
| 50 | begin | 68 | begin |
| 51 | User.transaction do | 69 | User.transaction do |
| 52 | # guess environment | 70 | # guess environment |
| @@ -64,17 +82,17 @@ for username in ARGV | @@ -64,17 +82,17 @@ for username in ARGV | ||
| 64 | person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) | 82 | person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) |
| 65 | person.preferred_domain = get_domain(domain_name) | 83 | person.preferred_domain = get_domain(domain_name) |
| 66 | user.save! | 84 | user.save! |
| 67 | - puts "I: #{username} data imported" | 85 | + Progress.instance.puts "I: #{username} data imported" |
| 68 | 86 | ||
| 69 | # import articles | 87 | # import articles |
| 70 | Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| | 88 | Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| |
| 71 | - puts "I: Trying to import #{username}'s article in #{xml} ..." | 89 | + Progress.instance.puts "I: Trying to import #{username}'s article in #{xml} ..." |
| 72 | article = TinyMceArticle.new | 90 | article = TinyMceArticle.new |
| 73 | article.from_xml(File.read(xml)) | 91 | article.from_xml(File.read(xml)) |
| 74 | article.profile = person | 92 | article.profile = person |
| 75 | if article.valid? | 93 | if article.valid? |
| 76 | article.save! | 94 | article.save! |
| 77 | - puts "I: #{username}'s article #{article.name.inspect} imported" | 95 | + Progress.instance.puts "I: #{username}'s article #{article.name.inspect} imported" |
| 78 | else | 96 | else |
| 79 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" | 97 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" |
| 80 | next | 98 | next |
| @@ -83,12 +101,12 @@ for username in ARGV | @@ -83,12 +101,12 @@ for username in ARGV | ||
| 83 | if article.is_homepage | 101 | if article.is_homepage |
| 84 | person.home_page = article | 102 | person.home_page = article |
| 85 | person.save! | 103 | person.save! |
| 86 | - puts "I: Article #{article.name.inspect} is #{username}'s homepage!" | 104 | + Progress.instance.puts "I: Article #{article.name.inspect} is #{username}'s homepage!" |
| 87 | end | 105 | end |
| 88 | 106 | ||
| 89 | File.open("tmp/rewrite.txt", 'w+') do |file| | 107 | File.open("tmp/rewrite.txt", 'w+') do |file| |
| 90 | file.puts("#{article.id} #{url_for(article.url)}") | 108 | file.puts("#{article.id} #{url_for(article.url)}") |
| 91 | - puts "I: Article with id = #{article.id} redirected to #{url_for(article.url)}" | 109 | + Progress.instance.puts "I: Article with id = #{article.id} redirected to #{url_for(article.url)}" |
| 92 | end | 110 | end |
| 93 | 111 | ||
| 94 | # import attachments | 112 | # import attachments |
| @@ -97,15 +115,16 @@ for username in ARGV | @@ -97,15 +115,16 @@ for username in ARGV | ||
| 97 | file = UploadedFile.new | 115 | file = UploadedFile.new |
| 98 | file.from_xml(File.read(attachment_xml)) | 116 | file.from_xml(File.read(attachment_xml)) |
| 99 | if !File.exist?(file.filesystem_location) | 117 | if !File.exist?(file.filesystem_location) |
| 100 | - puts "W: skipping attachment pointing to unexisting file" | 118 | + Progress.instance.puts "W: skipping attachment pointing to unexisting file" |
| 101 | next | 119 | next |
| 102 | end | 120 | end |
| 103 | - puts "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" | 121 | + Progress.instance.puts "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" |
| 104 | file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) | 122 | file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) |
| 105 | file.parent = article | 123 | file.parent = article |
| 106 | file.profile = person | 124 | file.profile = person |
| 125 | + Progress.instance.puts "I: Trying to save attachment \"#{file.filename}/#{file.slug}\"" | ||
| 107 | file.save! | 126 | file.save! |
| 108 | - puts "I: attachment added to article #{article.id}" | 127 | + Progress.instance.puts "I: attachment added to article #{article.id}" |
| 109 | 128 | ||
| 110 | file.reload | 129 | file.reload |
| 111 | if file.image? | 130 | if file.image? |
| @@ -131,7 +150,7 @@ for username in ARGV | @@ -131,7 +150,7 @@ for username in ARGV | ||
| 131 | block.title = data['menu']['title'] | 150 | block.title = data['menu']['title'] |
| 132 | block.links = links | 151 | block.links = links |
| 133 | block.save! | 152 | block.save! |
| 134 | - puts "imported links: #{links.inspect}" | 153 | + Progress.instance.puts "imported links: #{links.inspect}" |
| 135 | end | 154 | end |
| 136 | 155 | ||
| 137 | end | 156 | end |