Class: RubyForge

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyforge.rb

Constant Summary collapse

VERSION =

:stopdoc:

"0.3.0"
HOME =
ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")
RUBYFORGE_D =
File::join HOME, ".rubyforge"
CONFIG_F =
File::join RUBYFORGE_D, "config.yml"
File::join RUBYFORGE_D, "cookie.dat"
CONFIG =

We must use __FILE__ instead of DATA because this is now a library and DATA is relative to $0, not __FILE__.

File.read(__FILE__).split(/__END__/).last.gsub(/#\{(.*)\}/) { eval $1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = CONFIG_F, opts = {}) ⇒ RubyForge

Returns a new instance of RubyForge.



43
44
45
46
47
48
49
50
# File 'lib/rubyforge.rb', line 43

def initialize(config=CONFIG_F, opts={})
  @config = test(?e, config) ? IO::read(config) : CONFIG
  @config = YAML.load(@config).merge(opts)

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



38
39
40
# File 'lib/rubyforge.rb', line 38

def client
  @client
end

#configObject (readonly)

:startdoc:



41
42
43
# File 'lib/rubyforge.rb', line 41

def config
  @config
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")


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/rubyforge.rb', line 230

def add_file(group_name, package_name, release_name, userfile)
  page = '/frs/admin/editrelease.php'
  type_id = @config["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 = @config["processor_id"]

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

  userfile = open userfile

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

  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



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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rubyforge.rb', line 165

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
  release_date = @config["release_date"]
  type_id = @config["type_id"]
  processor_id = @config["processor_id"]
  release_notes = @config["release_notes"]
  release_changes = @config["release_changes"]
  preformatted = @config["preformatted"]

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

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

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

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

  release_changes = IO::read(release_changes) if release_changes and test(?e, 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)
  release_id = html[/release_id=\d+/][/\d+/].to_i
  puts "RELEASE ID = #{release_id}" if $DEBUG

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

  release_id
end

#create_package(group_id, package_name) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rubyforge.rb', line 111

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

  group_id = lookup "group", group_id
  is_private = @config["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
end

#delete_package(group_id, package_id) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rubyforge.rb', line 147

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",
  }

  run page, form
end

#loginObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubyforge.rb', line 61

def 
  page = "/account/login.php"

  username = @config["username"]
  password = @config["password"]

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

  run page, form
end

#lookup(type, val) ⇒ Object

:nodoc:



294
295
296
297
298
299
300
301
# File 'lib/rubyforge.rb', line 294

def lookup(type, val) # :nodoc:
  unless Fixnum === val then
    key = val.to_s
    val = @config["rubyforge"]["#{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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubyforge.rb', line 132

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:



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
# File 'lib/rubyforge.rb', line 262

def run(page, form, extheader={}) # :nodoc:
  client = HTTPAccess2::Client::new ENV["HTTP_PROXY"]
  client.debug_dev = STDERR if ENV["RUBYFORGE_DEBUG"] || ENV["DEBUG"]
  client.set_cookie_store @config["cookie_jar"]

  # HACK to fix http-access2 redirect bug/feature
  client.redirect_uri_callback = lambda do |res| 
    page = res.header['location'].first
    page =~ %r/http/ ? page : "#{ @config['uri'] }/#{ page }"
  end

  uri = File.join(@config['uri'], page)
  if $DEBUG then
    puts "client.post_content #{uri.inspect}, #{form.inspect}, #{extheader.inspect}"
  end

  response = client.post_content uri, form, extheader 

  @client = client if $TESTING

  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

#scrape_config(username) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubyforge.rb', line 77

def scrape_config(username)
  require 'open-uri'

  data = {
    "group_ids" => {},
    "package_ids" => {},
    "release_ids" => Hash.new { |h,k| h[k] = {} },
  }

  html = URI.parse("http://rubyforge.org/users/#{username}/index.html").read
  html.scan(%r%/projects/([^/]+)/%).flatten.each do |project|
    next if project == "support"
    html = URI.parse("http://rubyforge.org/projects/#{project}/index.html").read

    group_id = html[/(frs|tracker)\/\?group_id=\d+/][/\d+/].to_i
    data["group_ids"][project] = group_id

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

    package = nil
    html.scan(/<h3>\w+|release_id=\d+">[^>]+|filemodule_id=\d+/).each do |s|
      case s
      when /<h3>([\w-]+)/ then
        package = $1
      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
  end
  data
end

#setupObject



52
53
54
55
56
57
58
59
# File 'lib/rubyforge.rb', line 52

def setup
  FileUtils::mkdir_p RUBYFORGE_D unless test ?d, RUBYFORGE_D
  test ?e, CONFIG_F and FileUtils::mv CONFIG_F, "#{ CONFIG_F }.bak"    
  open(CONFIG_F, "w") { |f| f.write CONFIG }
  FileUtils::touch COOKIE_F
  edit = (ENV["EDITOR"] || ENV["EDIT"] || "vi") + " #{ CONFIG_F }"
  system edit or puts "edit #{ CONFIG_F }"
end