Revisão | f28d000e44207c4dca490e954eda1e08cfc381e3 (tree) |
---|---|
Hora | 2018-08-20 18:52:46 |
Autor | Tatsuki Sugiura <sugi@nemu...> |
Commiter | Tatsuki Sugiura |
Update to compat API change (no digest infomation when file uploaded).
@@ -165,6 +165,13 @@ module OSDN | ||
165 | 165 | logger.warn "Failed to load variables from #{path}; #{e.message}" |
166 | 166 | end |
167 | 167 | end |
168 | + # Fix key conflicts | |
169 | + if vars['local_file_info'] | |
170 | + vars['local_file_info'].each do |fname, finfo| | |
171 | + finfo.has_key?('size') or next | |
172 | + finfo['filesize'] = finfo.delete('size') | |
173 | + end | |
174 | + end | |
168 | 175 | logger.debug "Variables: #{vars.inspect}" |
169 | 176 | Hashie::Mash.new(vars) |
170 | 177 | end |
@@ -1,5 +1,7 @@ | ||
1 | 1 | module OSDN; module CLI; module Command |
2 | 2 | class FrsUpload < Base |
3 | + attr_accessor :target_proj, :visibility, :force_digest, :dry_run, :show_progress | |
4 | + | |
3 | 5 | def help |
4 | 6 | puts "#{$0} frs_upload [opts] [target_dir]" |
5 | 7 | puts "Options:" |
@@ -164,67 +166,31 @@ module OSDN; module CLI; module Command | ||
164 | 166 | return false |
165 | 167 | end |
166 | 168 | |
167 | - vars = load_variables(rdir) | |
168 | - digests = nil | |
169 | - if !@force_digest && vars.local_file_info && | |
170 | - vars.local_file_info[file.basename.to_s] | |
171 | - finfo = vars.local_file_info[file.basename.to_s] | |
172 | - if finfo[:size] == file.size && finfo.mtime == file.mtime | |
173 | - digests = vars.local_file_info[file.basename.to_s].digests | |
174 | - end | |
169 | + filecmd = Relfile.new logger | |
170 | + [:target_proj, :visibility, :force_digest, :show_progress].each do |opt| | |
171 | + filecmd.send "#{opt}=", send(opt) | |
175 | 172 | end |
173 | + filecmd.target_package = target_package(rdir) | |
174 | + filecmd.target_release = target_release(rdir) | |
175 | + filecmd.calc_file_digest(file) | |
176 | + | |
177 | + vars = load_variables(rdir) | |
178 | + digests = vars.local_file_info[file.basename.to_s].digests | |
176 | 179 | |
177 | - unless digests | |
178 | - logger.info "Calculating digest for #{file}..." | |
179 | - digests = { | |
180 | - sha256: hexdigest(Digest::SHA256, file), | |
181 | - sha1: hexdigest(Digest::SHA1, file), | |
182 | - md5: hexdigest(Digest::MD5, file), | |
183 | - } | |
184 | - update_variables rdir, {local_file_info: {file.basename.to_s => {digests: digests, mtime: file.mtime, size: file.size}}} | |
185 | - end | |
186 | 180 | if remote_f = rinfo.files.find { |f| f.name == file.basename.to_s } |
187 | - if digests.find { |type, dig| dig != remote_f.send("digest_#{type}") } | |
181 | + if remote_f.size != file.size || digests.find { |type, dig| rd = remote_f.send("digest_#{type}"); rd && rd != '' && dig != rd } | |
188 | 182 | logger.error "#{file} was changed from remote file! Please delete remote file before uploading new one." |
189 | 183 | end |
190 | 184 | logger.info "Skip already uploaded file '#{file}'" |
191 | 185 | return |
192 | 186 | end |
193 | 187 | |
188 | + finfo = {} | |
194 | 189 | logger.info "Uploading file #{file} (#{file.size} bytes)" |
195 | 190 | if @dry_run |
196 | 191 | finfo = Hashie::Mash.new id: '(dry-run)', url: '(dry-run)' |
197 | 192 | else |
198 | - logger.level <= Logger::INFO && @show_progress != false || @show_progress and | |
199 | - OSDN::CLI._show_progress = true | |
200 | - fio = file.open | |
201 | - logger.info "Starting upload #{file}..." | |
202 | - max_upload_tries = 5 | |
203 | - upload_tries = 0 | |
204 | - begin | |
205 | - upload_tries += 1 | |
206 | - finfo = api.create_release_file target_proj, target_package(rdir), target_release(rdir), fio, visibility: @visibility | |
207 | - rescue OSDNClient::ApiError => e | |
208 | - if max_upload_tries - upload_tries <= 0 | |
209 | - logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!" | |
210 | - raise e | |
211 | - elsif [0, 100, 502].member?(e.code.to_i) | |
212 | - fio.rewind | |
213 | - logger.error "Upload error (#{e.code} #{e.message}), retrying (#{upload_tries}/#{max_upload_tries})..." | |
214 | - sleep 10 | |
215 | - retry | |
216 | - else | |
217 | - raise e | |
218 | - end | |
219 | - ensure | |
220 | - OSDN::CLI._show_progress = false | |
221 | - fio.close | |
222 | - end | |
223 | - if digests.find { |type, dig| dig != finfo.send("digest_#{type}") } | |
224 | - logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check." | |
225 | - else | |
226 | - logger.info "Upload complete." | |
227 | - end | |
193 | + finfo = filecmd.create_one file | |
228 | 194 | end |
229 | 195 | $stdout.puts "New file '#{file}' has been uploaded; #{finfo.url}" |
230 | 196 | end |
@@ -3,6 +3,8 @@ require 'pathname' | ||
3 | 3 | |
4 | 4 | module OSDN; module CLI; module Command |
5 | 5 | class Relfile < FrsBase |
6 | + attr_accessor :target_proj, :target_package, :target_release, :visibility, :force_digest, :show_progress | |
7 | + | |
6 | 8 | def help |
7 | 9 | puts "#{$0} relfile [opts] [list]" |
8 | 10 | puts "#{$0} relfile [opts] create <target-file> [target-files...]" |
@@ -93,16 +95,14 @@ module OSDN; module CLI; module Command | ||
93 | 95 | end |
94 | 96 | end |
95 | 97 | |
96 | - def create_one(filename) | |
98 | + def calc_file_digest(filename) | |
97 | 99 | file = Pathname('.') + filename |
98 | - logger.debug "Calculating digest for #{file}..." | |
99 | - | |
100 | 100 | vars = load_variables(file.dirname) |
101 | 101 | digests = nil |
102 | 102 | if !@force_digest && vars.local_file_info && |
103 | - vars.local_file_info[file.basename.to_s] | |
103 | + vars.local_file_info[file.basename.to_s] | |
104 | 104 | finfo = vars.local_file_info[file.basename.to_s] |
105 | - if finfo[:size] == file.size && finfo.mtime == file.mtime | |
105 | + if finfo.filesize == file.size && finfo.mtime == file.mtime | |
106 | 106 | digests = vars.local_file_info[file.basename.to_s].digests |
107 | 107 | end |
108 | 108 | end |
@@ -114,19 +114,24 @@ module OSDN; module CLI; module Command | ||
114 | 114 | sha1: hexdigest(Digest::SHA1, file), |
115 | 115 | md5: hexdigest(Digest::MD5, file), |
116 | 116 | } |
117 | - update_variables file.dirname, {local_file_info: {file.basename.to_s => {digests: digests, mtime: file.mtime, size: file.size}}} | |
117 | + update_variables file.dirname, {local_file_info: {file.basename.to_s => {digests: digests, mtime: file.mtime, filesize: file.size}}} | |
118 | 118 | end |
119 | + end | |
119 | 120 | |
121 | + def create_one(filename) | |
122 | + file = Pathname('.') + filename | |
123 | + calc_file_digest file | |
124 | + vars = load_variables(file.dirname) | |
120 | 125 | fio = file.open |
121 | 126 | logger.level <= Logger::INFO && @show_progress != false || @show_progress and |
122 | 127 | OSDN::CLI._show_progress = true |
123 | - logger.info "Starting upload #{file}..." | |
128 | + logger.info "Starting upload #{file} (#{file.size} bytes)..." | |
124 | 129 | max_upload_tries = 5 |
125 | 130 | upload_tries = 0 |
126 | - f = nil | |
131 | + remote_file = nil | |
127 | 132 | begin |
128 | 133 | upload_tries += 1 |
129 | - f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility | |
134 | + remote_file = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility | |
130 | 135 | rescue OSDNClient::ApiError => e |
131 | 136 | if max_upload_tries - upload_tries <= 0 |
132 | 137 | logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!" |
@@ -144,12 +149,15 @@ module OSDN; module CLI; module Command | ||
144 | 149 | OSDN::CLI._show_progress = false |
145 | 150 | end |
146 | 151 | |
147 | - if digests.find { |type, dig| dig != f.send("digest_#{type}") } | |
148 | - logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check." | |
152 | + if vars.local_file_info[file.basename.to_s].digests.find { |type, dig| rd = remote_file.send("digest_#{type}"); rd && rd != '' && rd != dig } | |
153 | + logger.error "File digest mismatch! Uploaded file #{file} may be broken! Please check." | |
154 | + elsif file.size != remote_file.size | |
155 | + logger.error "File size mismatch! Uploaded file #{file} may be broken! Please check." | |
149 | 156 | else |
150 | 157 | logger.info "Upload complete." |
151 | 158 | end |
152 | - puts format_file(f) | |
159 | + puts format_file(remote_file) | |
160 | + remote_file | |
153 | 161 | end |
154 | 162 | |
155 | 163 | def update |