Skip to content

Instantly share code, notes, and snippets.

View dakrone's full-sized avatar

Lee Hinman dakrone

View GitHub Profile
; The execution macro
(defmacro exec
"Execute a command on the shell, passing to the given function
the lazy sequence of lines read as output, and the rest of arguments."
[cmd pred & args]
`(with-open [br# (BufferedReader. (InputStreamReader. (.getInputStream (.exec (Runtime/getRuntime) ~cmd))))]
(~pred (line-seq br#) ~@args)))
; This works
(defn get-groups
(ns timetest
(:use clojure.contrib.shell-out))
; Execute and return a list of lines, think of it as a helper for (sh)
(defn sh-lines
"Given a program and a list of args, run it and return an array
of the lines for stdout"
[& body]
(.split (:out (apply sh :return-map true body)) "\n"))
@dakrone
dakrone / trapr
Created June 9, 2009 16:35
Nesting traps that untrap in Ruby
#!/usr/bin/env ruby
class Object
def trapr_wrap(signal, newproc, function, *args)
#puts "Trapping #{signal} with #{newproc.inspect}"
oldproc = trap "#{signal}", newproc
self.send("#{function}", *args)
ensure
#puts "Reseting trap. #{signal} -> #{oldproc.inspect}"
trap "#{signal}", oldproc
#!/usr/bin/env ruby
#
# forkoff proc test
#
require 'forkoff'
require 'pp'
a = []
snip from spec file:
KNOWN_STATS = [SPEC_EXAMPLE_FILE, 0.570645834224529, 0.586986749301636, 3.40291187981663, 3.70043971814109, 22]
it 'should return the known statistics about a.rb' do
stats = @c.process_directory(SPEC_FILE_DIR)[SPEC_EXAMPLE_BASE][0]
stats.should == KNOWN_STATS
end
output:
FORKIFY_DEBUG = false
require 'pp'
require 'rinda/tuplespace'
module Enumerable
#
# Forkify will process _block_'s actions using processes. If no number of processes is
# given, the default of 5 will be used. If there are less than _procs_ number of items
# Stripped down version of the parent's job
pts = Rinda::TupleSpace.new
items.each_with_index { |item, index|
pts.write([:enum, index, item])
}
provider = nil
provider = DRb.start_service('druby://127.0.0.1:53421', pts)
# Stripped down Child
pid = fork
unless pid
DRb.start_service
# fetch the TupleSpace over DRb using a proxy object
ts = Rinda::TupleSpaceProxy.new(DRbObject.new_with_uri('druby://127.0.0.1:53421'))
loop do
# break if no more items in the queue
[hinmanm@Xanadu:~]% rvm-update
sh: bash -l -c 'cd /Library/Ruby/Gems/1.8/gems/rvm-0.0.26 && ./scripts/rvm-update && source ~/.rvm/scripts/rvm': No such file or directory
[hinmanm@Xanadu:~]% cd /Library/Ruby/Gems/1.8/gems/rvm-0.0.26 && ./scripts/rvm-update && source ~/.rvm/scripts/rvm
<i> Updating rvm source ~/.rvm/scripts/rvm ...
<w> Ensuring that rvm script location in /Users/hinmanm/.zshrc is scripts/rvm not bin/rvm
<i> There have been a great many updates since previous releases, please:
[2:hinmanm@Xanadu:~]% ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-darwin10.0.0]
[2:hinmanm@Xanadu:~]% sudo gem install duration
Successfully installed duration-0.1.0
1 gem installed
Installing ri documentation for duration-0.1.0...
Updating class cache with 3078 classes...
Installing RDoc documentation for duration-0.1.0...
[2:hinmanm@Xanadu:~]% irb
1.9.1 >> require 'duration'