moved scripts to bin
This commit is contained in:
parent
b402de77a1
commit
88f83f0742
12 changed files with 138 additions and 207 deletions
20
bin/couchdir
Executable file
20
bin/couchdir
Executable file
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
unless ARGV.length >= 2
|
||||||
|
puts "usage: couchdir path/to/directory db-name [docid]"
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'rubygems'
|
||||||
|
require 'couchrest'
|
||||||
|
|
||||||
|
dirname = ARGV[0]
|
||||||
|
dbname = ARGV[1]
|
||||||
|
docid = ARGV[2]
|
||||||
|
|
||||||
|
puts "Create attachments for the files in #{dirname} in database #{dbname}."
|
||||||
|
|
||||||
|
fm = CouchRest::FileManager.new(dbname)
|
||||||
|
fm.loud = true
|
||||||
|
puts "Pushing views from directory #{dirname} to database #{fm.db}"
|
||||||
|
fm.push_directory(dirname, docid)
|
|
@ -15,7 +15,9 @@ if ARGV.length == 1
|
||||||
case command
|
case command
|
||||||
when "generate"
|
when "generate"
|
||||||
puts <<-GEN
|
puts <<-GEN
|
||||||
|
Usage: couchview generate directory design1 design2 design3 ...
|
||||||
|
|
||||||
|
Couchview will create directories and example views for the design documents you specify.
|
||||||
|
|
||||||
GEN
|
GEN
|
||||||
when "push"
|
when "push"
|
||||||
|
@ -100,61 +102,10 @@ require 'couchrest'
|
||||||
if command == 'push'
|
if command == 'push'
|
||||||
dirname = ARGV[1]
|
dirname = ARGV[1]
|
||||||
dbname = ARGV[2]
|
dbname = ARGV[2]
|
||||||
puts "Pushing views from directory #{dirname} to database #{dbname}"
|
|
||||||
fm = CouchRest::FileManager.new(dbname)
|
fm = CouchRest::FileManager.new(dbname)
|
||||||
# fm.loud = true
|
fm.loud = true
|
||||||
|
puts "Pushing views from directory #{dirname} to database #{fm.db}"
|
||||||
fm.push_views(dirname)
|
fm.push_views(dirname)
|
||||||
elsif command == 'pull'
|
elsif command == 'generate'
|
||||||
end
|
puts "Under construction ;)"
|
||||||
|
|
||||||
|
|
||||||
__END__
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
dbname = ARGV[1]
|
|
||||||
dirname = ARGV[2] || "views"
|
|
||||||
|
|
||||||
puts "Running #{command} on #{dbname} from directory #{dirname}."
|
|
||||||
|
|
||||||
require 'rubygems'
|
|
||||||
require 'couchrest'
|
|
||||||
|
|
||||||
case command
|
|
||||||
when "push" # files to views
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
when "pull" # views to files
|
|
||||||
ds = db.documents(:startkey => '_design/', :endkey => '_design/ZZZZZZZZZ')
|
|
||||||
ds['rows'].collect{|r|r['id']}.each do |id|
|
|
||||||
puts directory = id.split('/').last
|
|
||||||
FileUtils.mkdir_p(File.join("views",directory))
|
|
||||||
views = db.get(id)['views']
|
|
||||||
|
|
||||||
vgroups = views.keys.group_by{|k|k.sub(/\-(map|reduce)$/,'')}
|
|
||||||
vgroups.each do|g,vs|
|
|
||||||
mapname = vs.find {|v|views[v]["map"]}
|
|
||||||
if mapname
|
|
||||||
# save map
|
|
||||||
mapfunc = views[mapname]["map"]
|
|
||||||
mapfile = File.join(dirname,directory,"#{g}-map.js") # todo support non-js views
|
|
||||||
File.open(mapfile,'w') do |f|
|
|
||||||
f.write mapfunc
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
reducename = vs.find {|v|views[v]["reduce"]}
|
|
||||||
if reducename
|
|
||||||
# save reduce
|
|
||||||
reducefunc = views[reducename]["reduce"]
|
|
||||||
reducefile = File.join(dirname,directory,"#{g}-reduce.js") # todo support non-js views
|
|
||||||
File.open(reducefile,'w') do |f|
|
|
||||||
f.write reducefunc
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
|
@ -1,7 +1,7 @@
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = "couchrest"
|
s.name = "couchrest"
|
||||||
s.version = "0.8.4"
|
s.version = "0.8.9"
|
||||||
s.date = "2008-06-20"
|
s.date = "2008-08-03"
|
||||||
s.summary = "Lean and RESTful interface to CouchDB."
|
s.summary = "Lean and RESTful interface to CouchDB."
|
||||||
s.email = "jchris@grabb.it"
|
s.email = "jchris@grabb.it"
|
||||||
s.homepage = "http://github.com/jchris/couchrest"
|
s.homepage = "http://github.com/jchris/couchrest"
|
||||||
|
@ -9,13 +9,17 @@ Gem::Specification.new do |s|
|
||||||
s.has_rdoc = false
|
s.has_rdoc = false
|
||||||
s.authors = ["J. Chris Anderson"]
|
s.authors = ["J. Chris Anderson"]
|
||||||
s.files = %w{
|
s.files = %w{
|
||||||
lib/couchrest.rb lib/database.rb lib/pager.rb lib/file_manager.rb
|
lib/couchrest.rb
|
||||||
|
lib/couch_rest.rb lib/database.rb lib/pager.rb lib/file_manager.rb
|
||||||
Rakefile README
|
Rakefile README
|
||||||
script/couchdir script/couchview
|
bin/couchdir bin/couchview
|
||||||
spec/couchrest_spec.rb spec/database_spec.rb spec/pager_spec.rb spec/file_manager_spec.rb
|
spec/couchrest_spec.rb spec/database_spec.rb spec/pager_spec.rb spec/file_manager_spec.rb
|
||||||
spec/spec_helper.rb
|
spec/spec_helper.rb
|
||||||
}
|
}
|
||||||
s.require_path = "lib"
|
s.require_path = "lib"
|
||||||
|
s.bindir = 'bin'
|
||||||
|
s.executables << 'couchview'
|
||||||
|
s.executables << 'couchdir'
|
||||||
s.add_dependency("json", [">= 1.1.2"])
|
s.add_dependency("json", [">= 1.1.2"])
|
||||||
s.add_dependency("rest-client", [">= 0.5"])
|
s.add_dependency("rest-client", [">= 0.5"])
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
require "rubygems"
|
|
||||||
require 'json'
|
|
||||||
require 'rest_client'
|
|
||||||
|
|
||||||
require File.dirname(__FILE__) + '/lib/couchrest'
|
|
||||||
require File.dirname(__FILE__) + '/lib/database'
|
|
||||||
require File.dirname(__FILE__) + '/lib/pager'
|
|
||||||
require File.dirname(__FILE__) + '/lib/file_manager'
|
|
||||||
|
|
62
lib/couch_rest.rb
Normal file
62
lib/couch_rest.rb
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
class CouchRest
|
||||||
|
attr_accessor :uri
|
||||||
|
def initialize server = 'http://localhost:5984'
|
||||||
|
@uri = server
|
||||||
|
end
|
||||||
|
|
||||||
|
# list all databases on the server
|
||||||
|
def databases
|
||||||
|
CouchRest.get "#{@uri}/_all_dbs"
|
||||||
|
end
|
||||||
|
|
||||||
|
def database name
|
||||||
|
CouchRest::Database.new(@uri, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
# get the welcome message
|
||||||
|
def info
|
||||||
|
CouchRest.get "#{@uri}/"
|
||||||
|
end
|
||||||
|
|
||||||
|
# restart the couchdb instance
|
||||||
|
def restart!
|
||||||
|
CouchRest.post "#{@uri}/_restart"
|
||||||
|
end
|
||||||
|
|
||||||
|
# create a database
|
||||||
|
def create_db name
|
||||||
|
CouchRest.put "#{@uri}/#{name}"
|
||||||
|
database name
|
||||||
|
end
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def put uri, doc = nil
|
||||||
|
payload = doc.to_json if doc
|
||||||
|
JSON.parse(RestClient.put(uri, payload))
|
||||||
|
end
|
||||||
|
|
||||||
|
def get uri
|
||||||
|
JSON.parse(RestClient.get(uri), :max_nesting => false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def post uri, doc = nil
|
||||||
|
payload = doc.to_json if doc
|
||||||
|
JSON.parse(RestClient.post(uri, payload))
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete uri
|
||||||
|
JSON.parse(RestClient.delete(uri))
|
||||||
|
end
|
||||||
|
|
||||||
|
def paramify_url url, params = nil
|
||||||
|
if params
|
||||||
|
query = params.collect do |k,v|
|
||||||
|
v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
|
||||||
|
"#{k}=#{CGI.escape(v.to_s)}"
|
||||||
|
end.join("&")
|
||||||
|
url = "#{url}?#{query}"
|
||||||
|
end
|
||||||
|
url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,62 +1,10 @@
|
||||||
class CouchRest
|
require "rubygems"
|
||||||
attr_accessor :uri
|
require 'json'
|
||||||
def initialize server = 'http://localhost:5984'
|
require 'rest_client'
|
||||||
@uri = server
|
|
||||||
end
|
|
||||||
|
|
||||||
# list all databases on the server
|
require File.dirname(__FILE__) + '/couch_rest'
|
||||||
def databases
|
require File.dirname(__FILE__) + '/database'
|
||||||
CouchRest.get "#{@uri}/_all_dbs"
|
require File.dirname(__FILE__) + '/pager'
|
||||||
end
|
require File.dirname(__FILE__) + '/file_manager'
|
||||||
|
|
||||||
def database name
|
|
||||||
CouchRest::Database.new(@uri, name)
|
|
||||||
end
|
|
||||||
|
|
||||||
# get the welcome message
|
|
||||||
def info
|
|
||||||
CouchRest.get "#{@uri}/"
|
|
||||||
end
|
|
||||||
|
|
||||||
# restart the couchdb instance
|
|
||||||
def restart!
|
|
||||||
CouchRest.post "#{@uri}/_restart"
|
|
||||||
end
|
|
||||||
|
|
||||||
# create a database
|
|
||||||
def create_db name
|
|
||||||
CouchRest.put "#{@uri}/#{name}"
|
|
||||||
database name
|
|
||||||
end
|
|
||||||
|
|
||||||
class << self
|
|
||||||
def put uri, doc = nil
|
|
||||||
payload = doc.to_json if doc
|
|
||||||
JSON.parse(RestClient.put(uri, payload))
|
|
||||||
end
|
|
||||||
|
|
||||||
def get uri
|
|
||||||
JSON.parse(RestClient.get(uri), :max_nesting => false)
|
|
||||||
end
|
|
||||||
|
|
||||||
def post uri, doc = nil
|
|
||||||
payload = doc.to_json if doc
|
|
||||||
JSON.parse(RestClient.post(uri, payload))
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete uri
|
|
||||||
JSON.parse(RestClient.delete(uri))
|
|
||||||
end
|
|
||||||
|
|
||||||
def paramify_url url, params = nil
|
|
||||||
if params
|
|
||||||
query = params.collect do |k,v|
|
|
||||||
v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
|
|
||||||
"#{k}=#{CGI.escape(v.to_s)}"
|
|
||||||
end.join("&")
|
|
||||||
url = "#{url}?#{query}"
|
|
||||||
end
|
|
||||||
url
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -10,6 +10,10 @@ class CouchRest
|
||||||
@root = "#{host}/#{name}"
|
@root = "#{host}/#{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@root
|
||||||
|
end
|
||||||
|
|
||||||
def documents params = nil
|
def documents params = nil
|
||||||
url = CouchRest.paramify_url "#{@root}/_all_docs", params
|
url = CouchRest.paramify_url "#{@root}/_all_docs", params
|
||||||
CouchRest.get url
|
CouchRest.get url
|
||||||
|
|
|
@ -3,6 +3,7 @@ require 'digest/md5'
|
||||||
class CouchRest
|
class CouchRest
|
||||||
class FileManager
|
class FileManager
|
||||||
attr_reader :db
|
attr_reader :db
|
||||||
|
attr_accessor :loud
|
||||||
|
|
||||||
LANGS = {"rb" => "ruby", "js" => "javascript"}
|
LANGS = {"rb" => "ruby", "js" => "javascript"}
|
||||||
MIMES = {
|
MIMES = {
|
||||||
|
@ -41,16 +42,18 @@ class CouchRest
|
||||||
doc = @db.get(docid) rescue nil
|
doc = @db.get(docid) rescue nil
|
||||||
|
|
||||||
unless doc
|
unless doc
|
||||||
# puts "creating public"
|
say "creating #{docid}"
|
||||||
@db.save({"_id" => docid, "_attachments" => @attachments, "signatures" => @signatures})
|
@db.save({"_id" => docid, "_attachments" => @attachments, "signatures" => @signatures})
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
# remove deleted docs
|
# remove deleted docs
|
||||||
to_be_removed = doc["signatures"].keys.select{|d| !couch["public"].collect{|p| p.keys.first}.include?(d) }
|
to_be_removed = doc["signatures"].keys.select do |d|
|
||||||
|
!pushfiles.collect{|p| p.keys.first}.include?(d)
|
||||||
|
end
|
||||||
|
|
||||||
to_be_removed.each do |p|
|
to_be_removed.each do |p|
|
||||||
# puts "deleting #{p}"
|
say "deleting #{p}"
|
||||||
doc["signatures"].delete(p)
|
doc["signatures"].delete(p)
|
||||||
doc["_attachments"].delete(p)
|
doc["_attachments"].delete(p)
|
||||||
end
|
end
|
||||||
|
@ -58,9 +61,9 @@ class CouchRest
|
||||||
# update existing docs:
|
# update existing docs:
|
||||||
doc["signatures"].each do |path, sig|
|
doc["signatures"].each do |path, sig|
|
||||||
if (@signatures[path] == sig)
|
if (@signatures[path] == sig)
|
||||||
# puts "no change to #{path}. skipping..."
|
say "no change to #{path}. skipping..."
|
||||||
else
|
else
|
||||||
# puts "replacing #{path}"
|
say "replacing #{path}"
|
||||||
doc["signatures"][path] = md5(@attachments[path]["data"])
|
doc["signatures"][path] = md5(@attachments[path]["data"])
|
||||||
doc["_attachments"][path].delete("stub")
|
doc["_attachments"][path].delete("stub")
|
||||||
doc["_attachments"][path].delete("length")
|
doc["_attachments"][path].delete("length")
|
||||||
|
@ -71,10 +74,10 @@ class CouchRest
|
||||||
end
|
end
|
||||||
|
|
||||||
# add in new files
|
# add in new files
|
||||||
new_files = couch["public"].select{|d| !doc["signatures"].keys.include?( d.keys.first) }
|
new_files = pushfiles.select{|d| !doc["signatures"].keys.include?( d.keys.first) }
|
||||||
|
|
||||||
new_files.each do |f|
|
new_files.each do |f|
|
||||||
# puts "creating #{f}"
|
say "creating #{f}"
|
||||||
path = f.keys.first
|
path = f.keys.first
|
||||||
content = f.values.first
|
content = f.values.first
|
||||||
doc["signatures"][path] = md5(content)
|
doc["signatures"][path] = md5(content)
|
||||||
|
@ -88,15 +91,16 @@ class CouchRest
|
||||||
begin
|
begin
|
||||||
@db.save(doc)
|
@db.save(doc)
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
# puts e.message
|
say e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def push_views(view_dir)
|
def push_views(view_dir)
|
||||||
designs = {}
|
designs = {}
|
||||||
|
|
||||||
Dir["#{view_dir}/**/*.*"].collect do |design_doc|
|
Dir["#{view_dir}/**/*.*"].each do |design_doc|
|
||||||
design_doc_parts = design_doc.split('/')
|
design_doc_parts = design_doc.split('/')
|
||||||
|
next if /^lib\..*$/.match design_doc_parts.last
|
||||||
pre_normalized_view_name = design_doc_parts.last.split("-")
|
pre_normalized_view_name = design_doc_parts.last.split("-")
|
||||||
view_name = pre_normalized_view_name[0..pre_normalized_view_name.length-2].join("-")
|
view_name = pre_normalized_view_name[0..pre_normalized_view_name.length-2].join("-")
|
||||||
|
|
||||||
|
@ -150,6 +154,10 @@ class CouchRest
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def say words
|
||||||
|
puts words if @loud
|
||||||
|
end
|
||||||
|
|
||||||
def md5 string
|
def md5 string
|
||||||
Digest::MD5.hexdigest(string)
|
Digest::MD5.hexdigest(string)
|
||||||
end
|
end
|
||||||
|
@ -165,16 +173,17 @@ class CouchRest
|
||||||
|
|
||||||
if existing
|
if existing
|
||||||
updated = fields.merge({"_id" => id, "_rev" => existing["_rev"]})
|
updated = fields.merge({"_id" => id, "_rev" => existing["_rev"]})
|
||||||
|
if existing != updated
|
||||||
|
say "replacing #{id}"
|
||||||
|
db.save(updated)
|
||||||
|
else
|
||||||
|
say "skipping #{id}"
|
||||||
|
end
|
||||||
else
|
else
|
||||||
# puts "saving #{id}"
|
say "creating #{id}"
|
||||||
db.save(fields.merge({"_id" => id}))
|
db.save(fields.merge({"_id" => id}))
|
||||||
end
|
end
|
||||||
|
|
||||||
if existing != updated
|
|
||||||
# puts "replacing #{id}"
|
|
||||||
db.save(updated)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
unless ARGV.length >= 2
|
|
||||||
puts "usage: couchdir path/to/directory db-name"
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
|
|
||||||
dirname = ARGV[0].sub(/\/$/,'')
|
|
||||||
dbname = ARGV[1]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
puts "Shoving #{dirname} into #{dbname}."
|
|
||||||
|
|
||||||
require File.expand_path(File.dirname(__FILE__)) + '/../couchrest'
|
|
||||||
require 'fileutils'
|
|
||||||
|
|
||||||
cr = CouchRest.new("http://localhost:5984")
|
|
||||||
@db = cr.database(dbname)
|
|
||||||
|
|
||||||
@content_types = {
|
|
||||||
"html" => "text/html",
|
|
||||||
"htm" => "text/html",
|
|
||||||
"png" => "image/png",
|
|
||||||
"css" => "text/css"
|
|
||||||
}
|
|
||||||
|
|
||||||
files = Dir.glob(File.join(dirname,"**","*"))
|
|
||||||
attachments = {}
|
|
||||||
files.each do |filename|
|
|
||||||
content = open(filename).read
|
|
||||||
aname = filename.split('/')
|
|
||||||
aname.shift
|
|
||||||
aname = aname.join('/')
|
|
||||||
attachments[aname] = {
|
|
||||||
"data" => content,
|
|
||||||
"content_type" => @content_types[aname.split('.').last]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
puts attachments.keys.inspect
|
|
||||||
|
|
||||||
begin
|
|
||||||
doc = @db.get(dirname)
|
|
||||||
rescue RestClient::Request::RequestFailed
|
|
||||||
doc = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
# puts "get: #{doc.inspect}"
|
|
||||||
|
|
||||||
if doc
|
|
||||||
doc["_attachments"] = attachments
|
|
||||||
else
|
|
||||||
doc = {
|
|
||||||
"_id" => dirname,
|
|
||||||
"_attachments" => attachments
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# puts "saving: #{doc.inspect}"
|
|
||||||
@db.save(doc)
|
|
||||||
puts "saved"
|
|
|
@ -51,6 +51,10 @@ describe CouchRest::FileManager, "pushing views" do
|
||||||
@design["views"]["only-map"]["map"].should include("globalLib")
|
@design["views"]["only-map"]["map"].should include("globalLib")
|
||||||
@design["views"]["only-map"]["map"].should include("justThisView")
|
@design["views"]["only-map"]["map"].should include("justThisView")
|
||||||
end
|
end
|
||||||
|
it "should not create extra design docs" do
|
||||||
|
docs = @db.documents(:startkey => '_design', :endkey => '_design/ZZZZZZ')
|
||||||
|
docs['total_rows'].should == 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe CouchRest::FileManager, "pushing a directory with id" do
|
describe CouchRest::FileManager, "pushing a directory with id" do
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
require File.dirname(__FILE__) + '/../couchrest'
|
require File.dirname(__FILE__) + '/../lib/couchrest'
|
||||||
|
|
||||||
COUCHHOST = "http://localhost:5984"
|
COUCHHOST = "http://localhost:5984"
|
||||||
TESTDB = 'couchrest-test'
|
TESTDB = 'couchrest-test'
|
Loading…
Add table
Reference in a new issue