ckuru-tools 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ['ckuru-tools'].each do |lib|
4
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', lib))
5
+ end
6
+
7
+ require 'ruby-debug'
8
+
9
+ emacs_trace do
10
+ response = CkuruTools::ArgsProcessor.new(:args => ["-flag","value","Required_value+"]).parse
11
+ puts response.inspect
12
+
13
+ end
14
+
15
+ # EOF
data/bin/ckuru-tools ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(
4
+ File.join(File.dirname(__FILE__), '..', 'lib', 'ckuru-tools'))
5
+
6
+ # Put your code here
7
+
8
+ # EOF
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(
4
+ File.join(File.dirname(__FILE__), '..', 'lib', 'ckuru-tools'))
5
+
6
+ # Put your code here
7
+
8
+ args = CkuruTools::ArgsProcessor(:args => ["-table"
9
+
10
+ # EOF
data/lib/args.rb ADDED
@@ -0,0 +1,123 @@
1
+ require 'rubygems'
2
+ require 'ckuru-tools'
3
+ require 'getoptlong'
4
+
5
+ class String
6
+ def stripdashes
7
+ gsub(/-/,'')
8
+ end
9
+ def stripplusses
10
+ gsub(/\+/,'')
11
+ end
12
+
13
+ def String.code_append(old,new)
14
+ old += ";" if old and old.length > 0
15
+ old += new
16
+ end
17
+ end
18
+
19
+ module CkuruTools
20
+ class ArgParsed < HashInitializerClass
21
+ attr_accessor :spec, :type, :long, :short, :required
22
+ end
23
+
24
+ class ArgsProcessor < HashInitializerClass
25
+ attr_accessor :args
26
+ attr_accessor :noparse
27
+
28
+ #
29
+ # A simple, terse, and concise wrapper around GetoptLong command line parsing.
30
+ #
31
+ # Lets start with an example:
32
+ #
33
+ # args = CkuruTools::ArgsProcessor.new(:args => ["-flag","value","Required_value+"])
34
+ #
35
+ # response = args.parse
36
+ #
37
+ # Now if the script where to be called with
38
+ #
39
+ # myscript -f -v VALUE --Required_value required
40
+ #
41
+ # response => {:flag => true, :value => "VALUE", :Required_value => "required"}
42
+ #
43
+ # Defaults:
44
+ # -h : always generates help (do not use this flag)
45
+ # -d : Interface into CkuruTools::Debug (increments debugging level); (do not override this flag)
46
+ #
47
+ #
48
+ def initialize(h)
49
+ super h
50
+ end
51
+
52
+ def parse
53
+ argsparse, argshash = [], {}
54
+
55
+ argsparse.push(["--debug","-d",GetoptLong::NO_ARGUMENT],
56
+ ["--help","-h",GetoptLong::NO_ARGUMENT])
57
+
58
+ args.each do |arg|
59
+ if matchdata = arg.match(/-(([a-zA-Z0-9])[a-zA-Z0-9]+)/)
60
+ short = "-#{matchdata[2]}"
61
+ long = "-#{arg}"
62
+ argsparse.push([long,short,GetoptLong::NO_ARGUMENT])
63
+ argshash[long] = ArgParsed.new(:spec => arg,
64
+ :type => :flag,
65
+ :long => long,
66
+ :short => short,
67
+ :required => false)
68
+ elsif matchdata = arg.match(/(([a-zA-Z0-9])[a-zA-Z0-9]+)/)
69
+ short = "-#{matchdata[2]}"
70
+ long = "--#{arg.stripplusses}"
71
+
72
+ argsparse.push([long,short,GetoptLong::REQUIRED_ARGUMENT])
73
+ argshash[long] = ArgParsed.new(:spec => arg,
74
+ :type => :value,
75
+ :long => long,
76
+ :short => short,
77
+ :required => arg.match(/\+/) ? true : false)
78
+ else
79
+ raise "unrecognized arg spec #{arg}"
80
+ end
81
+ end
82
+
83
+ ret = {}
84
+ opts = GetoptLong.new *argsparse
85
+ opts.each do |opt, arg|
86
+ if opt == '--debug'
87
+ CkuruTools::Debug.instance.level = CkuruTools::Debug.instance.level + 1
88
+ ckebug 1, "incrementing debug level"
89
+ elsif opt == '--help'
90
+ puts "Options are:"
91
+ argshash.keys.each do |a|
92
+ puts "\t#{argshash[a].long},#{argshash[a].short}" + (argshash[a].required ? " (required)" : '')
93
+ end
94
+ exit 0
95
+ else
96
+ ckebug 1, "parsing #{opt}, #{arg}"
97
+ case argshash[opt].type
98
+ when :flag
99
+ ret[argshash[opt].long.stripdashes.to_sym] = true
100
+ when :value
101
+ ret[argshash[opt].long.stripdashes.to_sym] = arg
102
+ end
103
+ end
104
+ end
105
+ missing_required = []
106
+ argshash.keys.each do |a|
107
+ if argshash[a].required
108
+ missing_required.push a unless ret.has_key? a.stripdashes.to_sym
109
+ end
110
+ end
111
+ ckebug 1, ret.inspect
112
+ if missing_required.length > 0
113
+ missing_required.each do |m|
114
+ ckebug 0, "#{argshash[m].long}(#{argshash[m].short}) is required"
115
+ end
116
+ raise "missing required values"
117
+ end
118
+ ret
119
+ end
120
+ end
121
+ end
122
+
123
+
@@ -0,0 +1,238 @@
1
+ # $Id$
2
+
3
+ # Equivalent to a header guard in C/C++
4
+ # Used to prevent the class/module from being loaded more than once
5
+ unless defined? CkuruTools
6
+
7
+ module CkuruTools
8
+
9
+ # :stopdoc:
10
+ VERSION = '1.0.0'
11
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
12
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
13
+ # :startdoc:
14
+
15
+ # Returns the version string for the library.
16
+ #
17
+ def self.version
18
+ VERSION
19
+ end
20
+
21
+ # Returns the library path for the module. If any arguments are given,
22
+ # they will be joined to the end of the libray path using
23
+ # <tt>File.join</tt>.
24
+ #
25
+ def self.libpath( *args )
26
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
27
+ end
28
+
29
+ # Returns the lpath for the module. If any arguments are given,
30
+ # they will be joined to the end of the path using
31
+ # <tt>File.join</tt>.
32
+ #
33
+ def self.path( *args )
34
+ args.empty? ? PATH : ::File.join(PATH, *args)
35
+ end
36
+
37
+ # Utility method used to rquire all files ending in .rb that lie in the
38
+ # directory below this file that has the same name as the filename passed
39
+ # in. Optionally, a specific _directory_ name can be passed in such that
40
+ # the _filename_ does not have to be equivalent to the directory.
41
+ #
42
+ def self.require_all_libs_relative_to( fname, dir = nil )
43
+ dir ||= ::File.basename(fname, '.*')
44
+ search_me = ::File.expand_path(
45
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
46
+
47
+ Dir.glob(search_me).sort.each {|rb| require rb}
48
+ end
49
+
50
+ #
51
+ # extending HashInitalizerClass enables you to get Object.new(hash) functionality
52
+ # where each key will dispatch to an object setter method.
53
+ #
54
+ # Usage:
55
+ #
56
+ # class MyClass < HashInitalizerClass
57
+ # ..attr_accessor :foo
58
+ # end
59
+ #
60
+ # m = MyClass(:foo => "bar")
61
+ # m.foo
62
+ # => "bar"
63
+ #
64
+ #
65
+
66
+ class HashInitializerClass
67
+ def initialize(h)
68
+ h.keys.each do |k|
69
+ self.send("#{k}=",h[k])
70
+ end
71
+
72
+ yield self if block_given?
73
+ end
74
+ end
75
+
76
+ end # module CkuruTools
77
+
78
+ CkuruTools.require_all_libs_relative_to __FILE__
79
+ CkuruTools.require_all_libs_relative_to CkuruTools.libpath
80
+
81
+ class Object
82
+
83
+ def _require ; each {|r| require r } ; end
84
+
85
+ # def to_hash
86
+ # ret = Hash.new
87
+ # each do |elem|
88
+ # ret[elem] = true
89
+ # end
90
+ # ret
91
+ # end
92
+
93
+ def docmd(cmd,dir=nil)
94
+ ret = docmdi(cmd,dir)
95
+ if ret.exitstatus != 0
96
+ raise "cmd #{cmd} exitstatus #{ret.exitstatus} : #{ret}"
97
+ end
98
+ ret
99
+ end
100
+
101
+ def docmdi(cmd,dir=nil)
102
+ if dir
103
+ unless Dir.chdir(dir)
104
+ ckebug 0, "failed to cd to #{dir}"
105
+ return nil
106
+ end
107
+ end
108
+ ret = msg_exec "running #{cmd}" do
109
+ cmd.gsub!(/\\/,"\\\\\\\\\\\\\\\\")
110
+ cmd.gsub!(/\'/,"\\\\'")
111
+ cmd.gsub!(/\"/,"\\\\\\\\\\\"")
112
+ system("bash -c \"#{cmd}\"")
113
+ if $?.exitstatus != 0
114
+ print " failed exit code #{$?.exitstatus} "
115
+ end
116
+ end
117
+ $?
118
+ end
119
+
120
+ ################################################################################
121
+
122
+ def docmd_dir(h={})
123
+ ret = nil
124
+ if h[:dir]
125
+ unless Dir.chdir(h[:dir])
126
+ ckebug 0, "failed to cd to #{h[:dir]}"
127
+ return nil
128
+ end
129
+ end
130
+ if h[:commands]
131
+ h[:commands].each do |cmd|
132
+ ret = docmd(cmd)
133
+ if ret.exitstatus != 0
134
+ ckebug 0, "giving up"
135
+ return ret
136
+ end
137
+ end
138
+ end
139
+ ret
140
+ end
141
+
142
+ def emacs_trace
143
+ begin
144
+ yield
145
+ rescue Exception => e
146
+ puts e
147
+ puts "... exception thrown from ..."
148
+ e.backtrace.each do |trace|
149
+ a = trace.split(/:/)
150
+ if a[0].match(/^\//)
151
+ puts "#{a[0]}:#{a[1]}: #{a[2..a.length].join(':')}"
152
+ else
153
+ d = File.dirname(a[0])
154
+ f = File.basename(a[0])
155
+ dir = `cd #{d}; pwd`.chomp
156
+ goodpath = File.join(dir,f)
157
+
158
+ puts "#{goodpath}:#{a[1]}: #{a[2..a.length].join(':')}"
159
+ end
160
+ if $emacs_trace_debugger
161
+ require 'ruby-debug'
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+ def ckebug(level,msg)
168
+ CkuruTools::Debug.instance.debug(level,msg)
169
+ end
170
+
171
+ def current_method
172
+ caller[0].match(/`(.*?)'/)[1]
173
+ end
174
+
175
+ def calling_method
176
+ caller[1] ? caller[1].match(/`(.*?)'/)[1] : ""
177
+ end
178
+
179
+ def calling_method2
180
+ if caller[2]
181
+ if matchdata = caller[2].match(/`(.*?)'/)
182
+ matchdata[1]
183
+ else
184
+ ""
185
+ end
186
+ else
187
+ ""
188
+ end
189
+ end
190
+
191
+
192
+ def calling_method_sig
193
+ caller[1] ? caller[1] : ""
194
+ end
195
+
196
+
197
+
198
+ # def calling_method
199
+ # if caller[2]
200
+ # caller[2].match(/`(.*?)'/)[1]
201
+ # else
202
+ # ""
203
+ # end
204
+
205
+ # end
206
+
207
+ end
208
+ end
209
+
210
+ class Time
211
+ def ckuru_time_string
212
+ strftime("%m/%d/%Y %H:%M:%S")
213
+ end
214
+ end
215
+
216
+ def printmsg(msg,newline=true)
217
+ print "#{Time.new.ckuru_time_string}: #{msg}"
218
+ puts if newline
219
+ end
220
+
221
+
222
+ #module MlImportUtils
223
+ def msg_exec(msg)
224
+ printmsg("#{msg} ... ",false)
225
+ t1 = Time.new
226
+ ret = yield
227
+ puts "done (#{Time.new - t1})"
228
+ ret
229
+ end
230
+
231
+ def chatty_exec(level,name)
232
+ ret = yield
233
+ ckebug level, "#{name} is #{ret}"
234
+ ret
235
+ end
236
+
237
+
238
+ # EOF
data/lib/debug.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'singleton'
2
+
3
+ module CkuruTools
4
+ class Debug
5
+ include ::Singleton
6
+ attr_accessor :level
7
+
8
+ def debug(level,msg,newline=true)
9
+ raise "first argument to #{current_method} must be a number or support the #to_i method" unless
10
+ level.respond_to?(:to_i)
11
+ self.level = 0 if self.level.nil?
12
+ if level <= self.level
13
+ # print "#{Time.new}(#{calling_method}): #{msg}" + (newline ? "\n" : nil)
14
+ # debugger
15
+ print "#{Time.new.ckuru_time_string}(#{calling_method2}): #{msg}" + (newline ? "\n" : nil)
16
+ end
17
+ end
18
+
19
+ def self.set_level(level)
20
+ CkuruTools::Debug.instance.level = level
21
+ ckebug 0, "setting debug level to #{level}"
22
+ end
23
+ end
24
+ end
25
+
26
+ CkuruTools::Debug.instance.level = ENV['CKURU_DEBUG_LEVEL'] ? ENV['CKURU_DEBUG_LEVEL'].to_i : 0
@@ -0,0 +1,8 @@
1
+ # $Id$
2
+
3
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
4
+
5
+ describe CkuruTools do
6
+ end
7
+
8
+ # EOF
@@ -0,0 +1,17 @@
1
+ # $Id$
2
+
3
+ require File.expand_path(
4
+ File.join(File.dirname(__FILE__), %w[.. lib ckuru-tools]))
5
+
6
+ Spec::Runner.configure do |config|
7
+ # == Mock Framework
8
+ #
9
+ # RSpec uses it's own mocking framework by default. If you prefer to
10
+ # use mocha, flexmock or RR, uncomment the appropriate line:
11
+ #
12
+ # config.mock_with :mocha
13
+ # config.mock_with :flexmock
14
+ # config.mock_with :rr
15
+ end
16
+
17
+ # EOF
data/tasks/ann.rake ADDED
@@ -0,0 +1,81 @@
1
+ # $Id$
2
+
3
+ begin
4
+ require 'bones/smtp_tls'
5
+ rescue LoadError
6
+ require 'net/smtp'
7
+ end
8
+ require 'time'
9
+
10
+ namespace :ann do
11
+
12
+ # A prerequisites task that all other tasks depend upon
13
+ task :prereqs
14
+
15
+ file PROJ.ann.file do
16
+ ann = PROJ.ann
17
+ puts "Generating #{ann.file}"
18
+ File.open(ann.file,'w') do |fd|
19
+ fd.puts("#{PROJ.name} version #{PROJ.version}")
20
+ fd.puts(" by #{Array(PROJ.authors).first}") if PROJ.authors
21
+ fd.puts(" #{PROJ.url}") if PROJ.url.valid?
22
+ fd.puts(" (the \"#{PROJ.release_name}\" release)") if PROJ.release_name
23
+ fd.puts
24
+ fd.puts("== DESCRIPTION")
25
+ fd.puts
26
+ fd.puts(PROJ.description)
27
+ fd.puts
28
+ fd.puts(PROJ.changes.sub(%r/^.*$/, '== CHANGES'))
29
+ fd.puts
30
+ ann.paragraphs.each do |p|
31
+ fd.puts "== #{p.upcase}"
32
+ fd.puts
33
+ fd.puts paragraphs_of(PROJ.readme_file, p).join("\n\n")
34
+ fd.puts
35
+ end
36
+ fd.puts ann.text if ann.text
37
+ end
38
+ end
39
+
40
+ desc "Create an announcement file"
41
+ task :announcement => ['ann:prereqs', PROJ.ann.file]
42
+
43
+ desc "Send an email announcement"
44
+ task :email => ['ann:prereqs', PROJ.ann.file] do
45
+ ann = PROJ.ann
46
+ from = ann.email[:from] || PROJ.email
47
+ to = Array(ann.email[:to])
48
+
49
+ ### build a mail header for RFC 822
50
+ rfc822msg = "From: #{from}\n"
51
+ rfc822msg << "To: #{to.join(',')}\n"
52
+ rfc822msg << "Subject: [ANN] #{PROJ.name} #{PROJ.version}"
53
+ rfc822msg << " (#{PROJ.release_name})" if PROJ.release_name
54
+ rfc822msg << "\n"
55
+ rfc822msg << "Date: #{Time.new.rfc822}\n"
56
+ rfc822msg << "Message-Id: "
57
+ rfc822msg << "<#{"%.8f" % Time.now.to_f}@#{ann.email[:domain]}>\n\n"
58
+ rfc822msg << File.read(ann.file)
59
+
60
+ params = [:server, :port, :domain, :acct, :passwd, :authtype].map do |key|
61
+ ann.email[key]
62
+ end
63
+
64
+ params[3] = PROJ.email if params[3].nil?
65
+
66
+ if params[4].nil?
67
+ STDOUT.write "Please enter your e-mail password (#{params[3]}): "
68
+ params[4] = STDIN.gets.chomp
69
+ end
70
+
71
+ ### send email
72
+ Net::SMTP.start(*params) {|smtp| smtp.sendmail(rfc822msg, from, to)}
73
+ end
74
+ end # namespace :ann
75
+
76
+ desc 'Alias to ann:announcement'
77
+ task :ann => 'ann:announcement'
78
+
79
+ CLOBBER << PROJ.ann.file
80
+
81
+ # EOF
data/tasks/bones.rake ADDED
@@ -0,0 +1,21 @@
1
+ # $Id$
2
+
3
+ if HAVE_BONES
4
+
5
+ namespace :bones do
6
+
7
+ desc 'Show the PROJ open struct'
8
+ task :debug do |t|
9
+ atr = if t.application.top_level_tasks.length == 2
10
+ t.application.top_level_tasks.pop
11
+ end
12
+
13
+ if atr then Bones::Debug.show_attr(PROJ, atr)
14
+ else Bones::Debug.show PROJ end
15
+ end
16
+
17
+ end # namespace :bones
18
+
19
+ end # HAVE_BONES
20
+
21
+ # EOF
data/tasks/gem.rake ADDED
@@ -0,0 +1,126 @@
1
+ # $Id$
2
+
3
+ require 'rake/gempackagetask'
4
+
5
+ namespace :gem do
6
+
7
+ PROJ.gem._spec = Gem::Specification.new do |s|
8
+ s.name = PROJ.name
9
+ s.version = PROJ.version
10
+ s.summary = PROJ.summary
11
+ s.authors = Array(PROJ.authors)
12
+ s.email = PROJ.email
13
+ s.homepage = Array(PROJ.url).first
14
+ s.rubyforge_project = PROJ.rubyforge.name
15
+
16
+ s.description = PROJ.description
17
+
18
+ PROJ.gem.dependencies.each do |dep|
19
+ s.add_dependency(*dep)
20
+ end
21
+
22
+ s.files = PROJ.gem.files
23
+ s.executables = PROJ.gem.executables.map {|fn| File.basename(fn)}
24
+ s.extensions = PROJ.gem.files.grep %r/extconf\.rb$/
25
+
26
+ s.bindir = 'bin'
27
+ dirs = Dir["{#{PROJ.libs.join(',')}}"]
28
+ s.require_paths = dirs unless dirs.empty?
29
+
30
+ incl = Regexp.new(PROJ.rdoc.include.join('|'))
31
+ excl = PROJ.rdoc.exclude.dup.concat %w[\.rb$ ^(\.\/|\/)?ext]
32
+ excl = Regexp.new(excl.join('|'))
33
+ rdoc_files = PROJ.gem.files.find_all do |fn|
34
+ case fn
35
+ when excl; false
36
+ when incl; true
37
+ else false end
38
+ end
39
+ s.rdoc_options = PROJ.rdoc.opts + ['--main', PROJ.rdoc.main]
40
+ s.extra_rdoc_files = rdoc_files
41
+ s.has_rdoc = true
42
+
43
+ if test ?f, PROJ.test.file
44
+ s.test_file = PROJ.test.file
45
+ else
46
+ s.test_files = PROJ.test.files.to_a
47
+ end
48
+
49
+ # Do any extra stuff the user wants
50
+ PROJ.gem.extras.each do |msg, val|
51
+ case val
52
+ when Proc
53
+ val.call(s.send(msg))
54
+ else
55
+ s.send "#{msg}=", val
56
+ end
57
+ end
58
+ end # Gem::Specification.new
59
+
60
+ # A prerequisites task that all other tasks depend upon
61
+ task :prereqs
62
+
63
+ desc 'Show information about the gem'
64
+ task :debug => 'gem:prereqs' do
65
+ puts PROJ.gem._spec.to_ruby
66
+ end
67
+
68
+ pkg = Rake::PackageTask.new(PROJ.name, PROJ.version) do |pkg|
69
+ pkg.need_tar = PROJ.gem.need_tar
70
+ pkg.need_zip = PROJ.gem.need_zip
71
+ pkg.package_files += PROJ.gem._spec.files
72
+ end
73
+ Rake::Task['gem:package'].instance_variable_set(:@full_comment, nil)
74
+
75
+ gem_file = if PROJ.gem._spec.platform == Gem::Platform::RUBY
76
+ "#{pkg.package_name}.gem"
77
+ else
78
+ "#{pkg.package_name}-#{PROJ.gem._spec.platform}.gem"
79
+ end
80
+
81
+ desc "Build the gem file #{gem_file}"
82
+ task :package => ['gem:prereqs', "#{pkg.package_dir}/#{gem_file}"]
83
+
84
+ file "#{pkg.package_dir}/#{gem_file}" => [pkg.package_dir] + PROJ.gem._spec.files do
85
+ when_writing("Creating GEM") {
86
+ Gem::Builder.new(PROJ.gem._spec).build
87
+ verbose(true) {
88
+ mv gem_file, "#{pkg.package_dir}/#{gem_file}"
89
+ }
90
+ }
91
+ end
92
+
93
+ desc 'Install the gem'
94
+ task :install => [:clobber, 'gem:package'] do
95
+ sh "#{SUDO} #{GEM} install --local pkg/#{PROJ.gem._spec.full_name}"
96
+
97
+ # use this version of the command for rubygems > 1.0.0
98
+ #sh "#{SUDO} #{GEM} install --no-update-sources pkg/#{PROJ.gem._spec.full_name}"
99
+ end
100
+
101
+ desc 'Uninstall the gem'
102
+ task :uninstall do
103
+ installed_list = Gem.source_index.find_name(PROJ.name)
104
+ if installed_list and installed_list.collect { |s| s.version.to_s}.include?(PROJ.version) then
105
+ sh "#{SUDO} #{GEM} uninstall --version '#{PROJ.version}' --ignore-dependencies --executables #{PROJ.name}"
106
+ end
107
+ end
108
+
109
+ desc 'Reinstall the gem'
110
+ task :reinstall => [:uninstall, :install]
111
+
112
+ desc 'Cleanup the gem'
113
+ task :cleanup do
114
+ sh "#{SUDO} #{GEM} cleanup #{PROJ.gem._spec.name}"
115
+ end
116
+
117
+ end # namespace :gem
118
+
119
+ desc 'Alias to gem:package'
120
+ task :gem => 'gem:package'
121
+
122
+ task :clobber => 'gem:clobber_package'
123
+
124
+ remove_desc_for_task %w(gem:clobber_package)
125
+
126
+ # EOF