Class: OSDN::CLI::Command::FrsUpload

Inherits:
Base
  • Object
show all
Defined in:
lib/osdn/cli/command/frs_upload.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#credential, #format, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#credential_path, #initialize, #load_credential, #load_variables, #set_client_token, #set_credential, #update_token, #update_variables, #write_credential, #write_variables

Constructor Details

This class inherits a constructor from OSDN::CLI::Command::Base

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



3
4
5
# File 'lib/osdn/cli/command/frs_upload.rb', line 3

def dry_run
  @dry_run
end

#force_digestObject

Returns the value of attribute force_digest.



3
4
5
# File 'lib/osdn/cli/command/frs_upload.rb', line 3

def force_digest
  @force_digest
end

#show_progressObject

Returns the value of attribute show_progress.



3
4
5
# File 'lib/osdn/cli/command/frs_upload.rb', line 3

def show_progress
  @show_progress
end

#target_proj=(value) ⇒ Object

Sets the attribute target_proj

Parameters:

  • value

    the value to set the attribute target_proj to.



3
4
5
# File 'lib/osdn/cli/command/frs_upload.rb', line 3

def target_proj=(value)
  @target_proj = value
end

#visibilityObject

Returns the value of attribute visibility.



3
4
5
# File 'lib/osdn/cli/command/frs_upload.rb', line 3

def visibility
  @visibility
end

Class Method Details

.descriptionObject



88
89
90
# File 'lib/osdn/cli/command/frs_upload.rb', line 88

def self.description
  "Upload local file tree and create package/release implicitly."
end

Instance Method Details

#helpObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/osdn/cli/command/frs_upload.rb', line 5

def help
  puts "#{$0} frs_upload [opts] [target_dir]"
  puts "Options:"
  puts "  -n --dry-run               Do noting (use with global -v to inspect)"
  puts "  -p --project=<project>     Target project (numeric id or name)"
  puts "     --package=<package-id>  Target package (numeric id)"
  puts "     --release=<release-id>  Target release (numeric id)"
  puts "  -v --visibility=<public|private|hidden>"
  puts "                             Default visibility for newly created items"
  puts "      --force-digest         Calc local file digest forcely"
  puts "      --progress             Force to show upload progress"
  puts "      --no-progress          Force to hide upload progress"
  puts "      --bwlimit=RATE         Limit bandwidth (in KB)"
end

#process_file(file, rdir, rinfo) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/osdn/cli/command/frs_upload.rb', line 163

def process_file(file, rdir, rinfo)
  if file.directory?
    logger.error "Skip direcotry #{file}"
    return false
  end

  filecmd = Relfile.new logger
  [:target_proj, :visibility, :force_digest, :show_progress].each do |opt|
    filecmd.send "#{opt}=", send(opt)
  end
  filecmd.target_package = target_package(rdir)
  filecmd.target_release = target_release(rdir)
  filecmd.calc_file_digest(file)

  vars = load_variables(rdir)
  digests = vars.local_file_info[file.basename.to_s].digests

  if remote_f = rinfo.files.find { |f| f.name == file.basename.to_s }
    if remote_f.size != file.size || digests.find { |type, dig| rd = remote_f.send("digest_#{type}"); rd && rd != '' && dig != rd }
      logger.error "#{file} was changed from remote file! Please delete remote file before uploading new one."
    end
    logger.info "Skip already uploaded file '#{file}'"
    return
  end

  finfo = {}
  logger.info "Uploading file #{file} (#{file.size} bytes)"
  if @dry_run
    finfo = Hashie::Mash.new id: '(dry-run)', url: '(dry-run)'
  else
    finfo = filecmd.create_one file
  end
  $stdout.puts "New file '#{file}' has been uploaded; #{finfo.url}"
end

#process_package(pdir) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/osdn/cli/command/frs_upload.rb', line 92

def process_package(pdir)
  if cur_pkgid = load_variables(pdir).package_id
    # check package existance on server
    begin
      api.get_package target_proj, target_package(pdir)
    rescue OSDNClient::ApiError => e
      begin
        err = JSON.parse(e.response_body)
      rescue
        raise e
      end
      if err['status'] == 404
        logger.warn "Package ##{cur_pkgid} has been deleted on server and local directory '#{pdir}' remains. You can delete the local directory or delete '#{pdir}/.osdn.vars' file to create a package again with new ID."
        return false
      end
      raise e
    end
  else
    logger.info "Createing new package '#{pdir.basename}'"
    if @dry_run
      pinfo = Hashie::Mash.new id: '(dry-run)', name: pdir.basename, url: '(dry-run)'
    else
      pinfo = api.create_package target_proj, pdir.basename, visibility: @visibility
      update_variables pdir, package_id: pinfo.id
    end
    $stdout.puts "New package '#{pinfo.name}' has been created; #{pinfo.url}"
  end

  Pathname.glob(pdir + '*').sort.each do |rdir|
    process_release(rdir)
  end
end

#process_release(rdir) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/osdn/cli/command/frs_upload.rb', line 125

def process_release(rdir)
  if !rdir.directory?
    logger.warn "Skip normal file '#{rdir}' in release level"
    return false
  end

  vars = load_variables(rdir)
  rinfo = nil
  if vars.release_id
    begin
      rinfo = api.get_release target_proj, target_package(rdir), target_release(rdir)
    rescue OSDNClient::ApiError => e
      begin
        err = JSON.parse(e.response_body)
      rescue
        raise e
      end
      if err['status'] == 404
        logger.warn "Release ##{vars.release_id} has been deleted on server and local directory '#{rdir}' remains. You can delete the local directory or delete '#{rdir}/.osdn.vars' file to create a release again with new ID."
        return false
      end
      raise e
    end
  else
    logger.info "Createing new release '#{rdir.basename}'"
    if @dry_run
      rinfo = Hashie::Mash.new id: '(dry-run)', name: rdir.basename, url: '(dry-run)', files: []
    else
      rinfo = api.create_release target_proj, target_package(rdir), rdir.basename, visibility: @visibility
      update_variables rdir, release_id: rinfo.id
    end
    $stdout.puts "New release '#{rinfo.name}' has been created; #{rinfo.url}"
  end
  Pathname.glob(rdir + '*').sort.each do |file|
    process_file(file, rdir, rinfo)
  end
end

#process_targetObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osdn/cli/command/frs_upload.rb', line 70

def process_target
  proj_info = api.get_project target_proj # check project existance

  vars = load_variables(@target_dir)
  parent_vars = load_variables(@target_dir.parent)

  if @target_release || vars.release_id ||
     parent_vars.package_id && !vars.release_id # new release case...
    process_release(@target_dir)
  elsif @target_package || vars.package_id
    process_package(@target_dir)
  else
    Pathname.glob(@target_dir+'*').sort.each do |pdir|
      process_package(pdir)
    end
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/osdn/cli/command/frs_upload.rb', line 20

def run
  update_token
  opts = GetoptLong.new(
    [ '--dry-run', '-n', GetoptLong::NO_ARGUMENT ],
    [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--package', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--release', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--visibility', '-v', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--force-digest', GetoptLong::NO_ARGUMENT],
    [ '--progress', GetoptLong::NO_ARGUMENT],
    [ '--no-progress', GetoptLong::NO_ARGUMENT],
    [ '--bwlimit', GetoptLong::REQUIRED_ARGUMENT ],
  )
  opts.each do |opt, arg|
    case opt
    when '--project'
      arg.empty? or
        @target_proj = arg
    when '--release'
      arg.empty? or
        @target_release = arg
    when '--package'
      arg.empty? or
        @target_package = arg
    when '--visibility'
      unless %w(public private hidden).member?(arg)
        logger.fatal "Invalid visibility status: #{arg}"
        exit
      end
      @visibility = arg
    when '--force-digest'
      @force_digest = true
    when '--dry-run'
      @dry_run = true
    when '--progress'
      @show_progress = true
    when '--no-progress'
      @show_progress = false
    when '--bwlimit'
      arg.to_i != 0 and
        OSDN::CLI._rate_limit = arg.to_i * 1024
    end
  end

  (ARGV.empty? ? ['.'] : ARGV).each do |d|
    @target_dir = Pathname.new(d)
    process_target
  end
end