Commit a8cfd272270ec676cf6775c8838b48408478b987

Authored by Antonio Terceiro
1 parent a5c10ed7

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 46 end
47 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 66 for username in ARGV
  67 + Progress.instance.step
50 68 begin
51 69 User.transaction do
52 70 # guess environment
... ... @@ -64,17 +82,17 @@ for username in ARGV
64 82 person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))
65 83 person.preferred_domain = get_domain(domain_name)
66 84 user.save!
67   - puts "I: #{username} data imported"
  85 + Progress.instance.puts "I: #{username} data imported"
68 86  
69 87 # import articles
70 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 90 article = TinyMceArticle.new
73 91 article.from_xml(File.read(xml))
74 92 article.profile = person
75 93 if article.valid?
76 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 96 else
79 97 $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}"
80 98 next
... ... @@ -83,12 +101,12 @@ for username in ARGV
83 101 if article.is_homepage
84 102 person.home_page = article
85 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 105 end
88 106  
89 107 File.open("tmp/rewrite.txt", 'w+') do |file|
90 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 110 end
93 111  
94 112 # import attachments
... ... @@ -97,15 +115,16 @@ for username in ARGV
97 115 file = UploadedFile.new
98 116 file.from_xml(File.read(attachment_xml))
99 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 119 next
102 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 122 file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type)
105 123 file.parent = article
106 124 file.profile = person
  125 + Progress.instance.puts "I: Trying to save attachment \"#{file.filename}/#{file.slug}\""
107 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 129 file.reload
111 130 if file.image?
... ... @@ -131,7 +150,7 @@ for username in ARGV
131 150 block.title = data['menu']['title']
132 151 block.links = links
133 152 block.save!
134   - puts "imported links: #{links.inspect}"
  153 + Progress.instance.puts "imported links: #{links.inspect}"
135 154 end
136 155  
137 156 end
... ...