Class: RubyForge

Inherits:
Object show all
Defined in:
lib/gems/rubyforge-1.0.1/lib/rubyforge.rb,
lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb,
lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb

Defined Under Namespace

Classes: Client, CookieManager

Constant Summary collapse

VERSION =

:stopdoc:

'1.0.1'
HOME =
ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")
RUBYFORGE_D =
File::join HOME, ".rubyforge"
CONFIG_F =
File::join RUBYFORGE_D, "user-config.yml"
File::join RUBYFORGE_D, "cookie.dat"
CONFIG =
YAML.load(config)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(userconfig = nil, autoconfig = nil, opts = nil) ⇒ RubyForge

Returns a new instance of RubyForge.



28
29
30
31
32
33
34
35
36
37
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 28

def initialize(userconfig=nil, autoconfig=nil, opts=nil)
  # def initialize(userconfig=CONFIG_F, opts={})
  @userconfig, @autoconfig = userconfig, autoconfig

  @autoconfig ||= CONFIG["rubyforge"].dup
  @userconfig.merge! opts if opts

  @client = nil
  @uri = nil
end

Instance Attribute Details

#autoconfigObject (readonly)

:startdoc:



26
27
28
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 26

def autoconfig
  @autoconfig
end

#userconfigObject (readonly)

:startdoc:



26
27
28
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 26

def userconfig
  @userconfig
end

Instance Method Details

#add_file(group_name, package_name, release_name, userfile) ⇒ Object

add a file to an existing release under the specified group_id, package_id, and release_id

example :

add_file("codeforpeople.com", "traits", "0.8.0", "traits-0.8.0.gem")
add_file("codeforpeople.com", "traits", "0.8.0", "traits-0.8.0.tgz")
add_file(1024, 1242, "0.8.0", "traits-0.8.0.gem")


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 330

def add_file(group_name, package_name, release_name, userfile)
  page         = '/frs/admin/editrelease.php'
  type_id      = @userconfig["type_id"]
  group_id     = lookup "group", group_name
  package_id   = lookup "package", package_name
  release_id   = (Integer === release_name) ? release_name : lookup("release", package_name)[release_name]
  processor_id = @userconfig["processor_id"]

  page = "/frs/admin/editrelease.php?group_id=#{group_id}&release_id=#{release_id}&package_id=#{package_id}"

  userfile = open userfile, 'rb'

  type_id ||= userfile.path[%r|\.[^\./]+$|]
  type_id = (lookup "type", type_id rescue lookup "type", ".oth")

  processor_id ||= "Any"
  processor_id = lookup "processor", processor_id

  form = {
    "step2"        => 1,
    "type_id"      => type_id,
    "processor_id" => processor_id,
    "userfile"     => userfile,
    "submit"       => "Add This File"
    }

  boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
  boundary = "multipart/form-data; boundary=___#{ boundary }___"

  run page, form, 'content-type' => boundary
end

#add_release(group_id, package_id, release_name, *files) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 248

def add_release(group_id, package_id, release_name, *files)
  userfile = files.shift
  page = "/frs/admin/qrs.php"

  group_id        = lookup "group", group_id
  package_id      = lookup "package", package_id
  userfile        = open userfile, 'rb'
  release_date    = @userconfig["release_date"]
  type_id         = @userconfig["type_id"]
  processor_id    = @userconfig["processor_id"]
  release_notes   = @userconfig["release_notes"]
  release_changes = @userconfig["release_changes"]
  preformatted    = @userconfig["preformatted"]

  release_date ||= Time.now.strftime("%Y-%m-%d %H:%M")

  type_id ||= userfile.path[%r|\.[^\./]+$|]
  type_id = (lookup "type", type_id rescue lookup "type", ".oth")

  processor_id ||= "Any"
  processor_id = lookup "processor", processor_id

  release_notes = IO::read(release_notes) if
    test(?e, release_notes) if release_notes

  release_changes = IO::read(release_changes) if
    test(?e, release_changes) if release_changes

  preformatted = preformatted ? 1 : 0

  form = {
    "group_id"        => group_id,
    "package_id"      => package_id,
    "release_name"    => release_name,
    "release_date"    => release_date,
    "type_id"         => type_id,
    "processor_id"    => processor_id,
    "release_notes"   => release_notes,
    "release_changes" => release_changes,
    "preformatted"    => preformatted,
    "userfile"        => userfile,
    "submit"          => "Release File"
  }

  boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
  boundary = "multipart/form-data; boundary=___#{ boundary }___"

  html = run(page, form, 'content-type' => boundary)
  raise "Invalid package_id #{package_id}" if html[/Invalid package_id/]
  raise "You have already released this version." if html[/That filename already exists in this project/]

  release_id = html[/release_id=\d+/][/\d+/].to_i rescue nil

  unless release_id then
    puts html if $DEBUG
    raise "Couldn't get release_id, upload failed\?"
  end

  puts "RELEASE ID = #{release_id}" if $DEBUG

  files.each do |file|
    add_file(group_id, package_id, release_id, file)
  end

  package_name = @autoconfig["package_ids"].invert[package_id]
  raise "unknown package name for #{package_id}" if package_name.nil?
  @autoconfig["release_ids"][package_name] ||= {}
  @autoconfig["release_ids"][package_name][release_name] = release_id
  save_autoconfig

  release_id
end

#clientObject



362
363
364
365
366
367
368
369
370
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 362

def client
  return @client if @client

  @client = RubyForge::Client::new ENV["HTTP_PROXY"]
  @client.debug_dev = STDERR if ENV["RUBYFORGE_DEBUG"] || ENV["DEBUG"] || $DEBUG
  @client.cookie_store = @userconfig["cookie_jar"]

  @client
end

#configure(opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 39

def configure opts = {}
  user_path        = CONFIG_F
  dir, file        = File.split(user_path)

  @userconfig      = if test(?e, user_path) then
                       YAML.load_file(user_path)
                     else
                       CONFIG
                     end.merge(opts)
  @autoconfig_path = File.join(dir, file.sub(/^user/, 'auto'))
  @autoconfig      = if test(?e, @autoconfig_path) then
                       YAML.load_file(@autoconfig_path)
                     else
                       CONFIG["rubyforge"].dup
                     end
  @autoconfig["type_ids"] = CONFIG['rubyforge']['type_ids'].dup

  raise "no <username>"   unless @userconfig["username"]
  raise "no <password>"   unless @userconfig["password"]
  raise "no <cookie_jar>" unless @userconfig["cookie_jar"]

  self
end

#create_package(group_id, package_name) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 186

def create_package(group_id, package_name)
  page = "/frs/admin/index.php"

  group_id = lookup "group", group_id
  is_private = @userconfig["is_private"]
  is_public = is_private ? 0 : 1

  form = {
    "func"         => "add_package",
    "group_id"     => group_id,
    "package_name" => package_name,
    "is_public"    => is_public,
    "submit"       => "Create This Package",
  }

  run page, form

  group_name = @autoconfig["group_ids"].invert[group_id]
  scrape_project(group_name)
end

#delete_package(group_id, package_id) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 225

def delete_package(group_id, package_id)
  page = "/frs/admin/index.php"

  group_id = lookup "group", group_id
  package_id = lookup "package", package_id

  form = {
    "func"        => "delete_package",
    "group_id"    => group_id,
    "package_id"  => package_id,
    "sure"        => "1",
    "really_sure" => "1",
    "submit"      => "Delete",
  }

  package_name = @autoconfig["package_ids"].invert[package_id]
  @autoconfig["package_ids"].delete package_name
  @autoconfig["release_ids"].delete package_name
  save_autoconfig

  run page, form
end

#loginObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 160

def 
  page = self.uri + "/account/login.php"
  page.scheme = 'https'
  page = URI.parse page.to_s # set SSL port correctly

  username = @userconfig["username"]
  password = @userconfig["password"]

  form = {
    "return_to"      => "",
    "form_loginname" => username,
    "form_pw"        => password,
    "login"          => "Login"
  }

  response = run page, form

  re = %r/personal\s+page\s+for:\s+#{ Regexp.escape username }/iom
  unless response =~ re
    warn("%s:%d: warning: potentially failed login using %s:%s" %
      [__FILE__,__LINE__,username,password]) unless $TESTING
  end

  response
end

#lookup(type, val) ⇒ Object

:nodoc:



392
393
394
395
396
397
398
399
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 392

def lookup(type, val) # :nodoc:
  unless Fixnum === val then
    key = val.to_s
    val = @autoconfig["#{type}_ids"][key]
    raise "no <#{type}_id> configured for <#{ key }>" unless val
  end
  val
end

#post_news(group_id, subject, body) ⇒ Object

Posts news item to group_id (can be name) with subject and body



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 210

def post_news(group_id, subject, body)
  page = "/news/submit.php"
  group_id = lookup "group", group_id

  form = {
    "group_id"     => group_id,
    "post_changes" => "y",
    "summary"      => subject,
    "details"      => body,
    "submit"       => "Submit",
  }

  run page, form
end

#run(page, form, extheader = {}) ⇒ Object

:nodoc:



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 372

def run(page, form, extheader={}) # :nodoc:
  uri = self.uri + page
  if $DEBUG then
    puts "client.post_content #{uri.inspect}, #{form.inspect}, #{extheader.inspect}"
  end

  response = client.post_content uri, form, extheader

  client.save_cookie_store

  if $DEBUG then
    response.sub!(/\A.*end tabGenerator -->/m, '')
    response.gsub!(/\t/, '  ')
    response.gsub!(/\n{3,}/, "\n\n")
    puts response
  end

  return response
end

#save_autoconfigObject



81
82
83
84
85
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 81

def save_autoconfig
  File.open(@autoconfig_path, "w") do |file|
    YAML.dump @autoconfig, file
  end
end

#scrape_configObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 87

def scrape_config
  username = @userconfig['username']

  %w(group package processor release).each do |type|
    @autoconfig["#{type}_ids"].clear
  end

  puts "Getting #{username}"
  html = URI.parse("http://rubyforge.org/users/#{username}/index.html").read

  projects = html.scan(%r%/projects/([^/]+)/%).flatten

  puts "Fetching #{projects.size} projects"
  projects.each do |project|
    next if project == "support"
    scrape_project(project)
  end
end

#scrape_project(project) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 106

def scrape_project(project)
  data = {
    "group_ids" => {},
    "package_ids" => {},
    "processor_ids" => Hash.new { |h,k| h[k] = {} },
    "release_ids" => Hash.new { |h,k| h[k] = {} },
  }

  puts "Updating #{project}"

  unless data["group_ids"].has_key? project then
    html = URI.parse("http://rubyforge.org/projects/#{project}/index.html").read
    group_id = html[/(frs|tracker|mail)\/\?group_id=\d+/][/\d+/].to_i
    data["group_ids"][project] = group_id
  end

  group_id = data["group_ids"][project]

  html = URI.parse("http://rubyforge.org/frs/?group_id=#{group_id}").read

  package = nil
  html.scan(/<h3>[^<]+|release_id=\d+">[^>]+|filemodule_id=\d+/).each do |s|
    case s
    when /<h3>([^<]+)/ then
      package = $1.strip
    when /release_id=(\d+)">([^<]+)/ then
      data["release_ids"][package][$2] = $1.to_i
    when /filemodule_id=(\d+)/ then
      data["package_ids"][package] = $1.to_i
    end
  end

  if not data['release_ids'][package].empty? and
     @autoconfig['processor_ids'].empty? then
    puts "Fetching processor ids"

    

    html = client.get_content "http://rubyforge.org/frs/admin/qrs.php?package=&group_id=#{group_id}"

    html =~ /<select name="processor_id">(.*?)<\/select>/m
    processors = $1
    processors.scan(/<option value="(\d{4})">([^<]+)/) do
      data["processor_ids"][$2] = $1.to_i
    end if processors
  end

  data.each do |key, val|
    @autoconfig[key].merge! val
  end

  save_autoconfig
end

#setupObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 67

def setup
  FileUtils::mkdir_p RUBYFORGE_D, :mode => 0700 unless test ?d, RUBYFORGE_D
  test ?e, CONFIG_F and FileUtils::mv CONFIG_F, "#{CONFIG_F}.bak"
  config = CONFIG.dup
  config.delete "rubyforge"

  open(CONFIG_F, "w") { |f|
    f.write YAML.dump(config)
  }
  FileUtils::touch COOKIE_F
  edit = (ENV["EDITOR"] || ENV["EDIT"] || "vi") + " '#{CONFIG_F}'"
  system edit or puts "edit '#{CONFIG_F}'"
end

#uriObject



63
64
65
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge.rb', line 63

def uri
  @uri ||= URI.parse @userconfig['uri']
end