Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 107 lines (88 sloc) 2.56 KB
#!/usr/bin/env ruby
# rf2gc.rb -- defines the RF2GC module
#
# To use, run the file via runner or console, and
# add/give the command:
#
# RF2GC.transfer_records
#
# transfer_records expects a file called rfdata.txt, containing lines
# like this:
#
# dblack@blah.com|asofasoinaosdfiansdofiansdelgoid|handle
#
# i.e., email address and 32-char MD5 password, separated by a pipe.
#
# Given such a file, it will go line by line and:
#
# * look for an existing Rubyforger or Gemcutter
# with that email, and skip if found
# * create a new Rubyforger record
# * create a new Gemcutter record with a random password
# * back out of both creations if either fails
#
# It logs progressively to rf2gclog.txt.
#
# Comment lines (#-based) and blank lines are permitted in the
# input file.
module RF2GC
RFFILE = "rf2gcdata.txt"
LOGFILE = File.join("log", "rf2gclog.txt")
EMAIL_RE = /[^@|]+@[^@.|]+(\.\w+)+/
PW_RE = /\w{32}/
HANDLE_RE = /\w+/
RECORD_RE = /\A#{EMAIL_RE}\|#{PW_RE}|#{HANDLE_RE}\Z/
IGNORE_RE = /^\s*(#.*?)?\Z/
def self.transfer_records
@log = File.open(LOGFILE, "a")
File.open(RFFILE) do |fh|
fh.each_with_index do |record,i|
next if ignore?(record)
unless well_formed?(record)
log("Bad record at line #{i}")
next
end
record.chomp!
email, password, handle = record.split('|')
rfer = Rubyforger.find_by_email(email)
gcer = User.find_by_email(email)
if rfer
log("Rubyforger exists: #{rfer.email}")
next
elsif gcer
log("Gemcutter account exists: #{gcer.email}, setting handle to: #{handle}")
gcer.update_attribute(:handle, handle)
next
end
rfer = Rubyforger.new(:email => email, :encrypted_password => password)
unless rfer.save
log("Couldn't save Rubyforger: #{rfer.email}")
next
end
user = User.new
user.email = email
user.password = ActiveSupport::SecureRandom.hex(16)
user.handle = handle
user.email_confirmed = true
unless user.save
log("Couldn't save user: #{user.email} --- #{user.errors.full_messages.join(' ')}")
Rubyforger.delete(rfer.id)
next
end
log("Created Rubyforger/User: #{email}/#{handle}")
end
end
@log.close
end
def self.well_formed?(record)
record =~ RECORD_RE
end
def self.ignore?(record)
record =~ IGNORE_RE
end
def self.log(msg)
puts msg
end
end
require 'config/environment'
RF2GC.transfer_records