ion1-mischacks 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/COPYING ADDED
@@ -0,0 +1,13 @@
1
+ Copyright © 2009 Johan Kiviniemi
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 / 2009-06-28
2
+
3
+ * Initial release
4
+
data/Manifest.txt ADDED
@@ -0,0 +1,9 @@
1
+ COPYING
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/mischacks.rb
7
+ mischacks.gemspec
8
+ spec/mischacks_spec.rb
9
+ spec/spec_helper.rb
data/README.txt ADDED
@@ -0,0 +1,39 @@
1
+ = MiscHacks
2
+
3
+ * http://johan.kiviniemi.name/software/mischacks/
4
+
5
+ == DESCRIPTION:
6
+
7
+ Safely pass parameters to sh scripts.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ == SYNOPSIS:
12
+
13
+ MiscHacks.sh %q{
14
+ diff -u "$1" "$2" | tr a-z A-Z >"$output"
15
+ }, '/dev/null', '/etc/motd', :output => 'foo'
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * None
20
+
21
+ == INSTALL:
22
+
23
+ * sudo gem install ion1-mischacks --source http://gems.github.com/
24
+
25
+ == LICENSE:
26
+
27
+ Copyright © 2009 Johan Kiviniemi
28
+
29
+ Permission to use, copy, modify, and/or distribute this software for any
30
+ purpose with or without fee is hereby granted, provided that the above
31
+ copyright notice and this permission notice appear in all copies.
32
+
33
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
34
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
35
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
36
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
37
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
38
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
39
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # mischacks – Miscellaneous methods that may or may not be useful
2
+ # Copyright © 2009 Johan Kiviniemi
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for any
5
+ # purpose with or without fee is hereby granted, provided that the above
6
+ # copyright notice and this permission notice appear in all copies.
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+
16
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)+'/lib')
17
+
18
+ require 'rubygems'
19
+
20
+ require 'hoe'
21
+ require 'rake'
22
+ require 'rake/clean'
23
+
24
+ task :default => :spec
25
+
26
+ Hoe.spec 'mischacks' do
27
+ developer 'Johan Kiviniemi', 'devel@johan.kiviniemi.name'
28
+ end
29
+
30
+ # http://blog.behindlogic.com/2008/10/auto-generate-your-manifest-and-gemspec.html
31
+ desc "Generate manifest, gemspec"
32
+ task :cultivate do
33
+ system 'touch Manifest.txt; rake -s check_manifest | patch'
34
+ system 'rake -s debug_gem >"$(basename "$(pwd)")".gemspec'
35
+ end
36
+
37
+ # vim:set et sw=2 sts=2:
data/lib/mischacks.rb ADDED
@@ -0,0 +1,91 @@
1
+ # mischacks – Miscellaneous methods that may or may not be useful
2
+ # Copyright © 2009 Johan Kiviniemi
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for any
5
+ # purpose with or without fee is hereby granted, provided that the above
6
+ # copyright notice and this permission notice appear in all copies.
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+
16
+ module MiscHacks
17
+ VERSION = '0.0.1'
18
+
19
+ class ChildError < RuntimeError
20
+ attr_reader :status
21
+
22
+ def initialize status
23
+ @status = Integer status
24
+ super "Child failed with status #{status}"
25
+ end
26
+ end
27
+
28
+ def self.checking_exit_status
29
+ fork do
30
+ yield
31
+ end.tap do |pid|
32
+ _, status = Process.wait2 pid
33
+ raise ChildError, status.exitstatus if status.exitstatus != 0
34
+ end
35
+
36
+ nil
37
+ end
38
+
39
+ def self.do_and_exit bang=false
40
+ my_exit = if bang then method :exit! else method :exit end
41
+
42
+ status = 1
43
+ begin
44
+ yield
45
+ rescue SystemExit => e
46
+ status = e.status
47
+ ensure
48
+ my_exit.call status
49
+ end
50
+ end
51
+
52
+ def self.do_and_exit! &block
53
+ do_and_exit true, &block
54
+ end
55
+
56
+ def self.sh cmd, *args
57
+ env = if args.last.is_a? Hash then args.pop else {} end
58
+
59
+ checking_exit_status do
60
+ do_and_exit! do
61
+ begin
62
+ env.each_pair do |k, v| ENV[k.to_s] = v.to_s end
63
+ exec *(%W{sh -e -c #{cmd} sh} + args.map {|a| a.to_s })
64
+ rescue Exception => e
65
+ warn e.to_formatted_string
66
+ end
67
+ end
68
+ end
69
+
70
+ nil
71
+ end
72
+ end
73
+
74
+ module MiscHacks
75
+ module ExceptionMixin
76
+ def to_formatted_string
77
+ bt = backtrace.dup
78
+ head = bt.shift
79
+ (
80
+ ["#{head}: #{self} (#{self.class})"] +
81
+ bt.map do |l| "\tfrom #{l}" end
82
+ ).map do |l| "#{l}\n" end.join
83
+ end
84
+ end
85
+ end
86
+
87
+ Exception.class_eval do
88
+ include MiscHacks::ExceptionMixin
89
+ end
90
+
91
+ # vim:set et sw=2 sts=2:
data/mischacks.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{mischacks}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Johan Kiviniemi"]
9
+ s.date = %q{2009-06-28}
10
+ s.description = %q{Safely pass parameters to sh scripts.}
11
+ s.email = ["devel@johan.kiviniemi.name"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
13
+ s.files = ["COPYING", "History.txt", "Manifest.txt", "README.txt", "Rakefile", "lib/mischacks.rb", "mischacks.gemspec", "spec/mischacks_spec.rb", "spec/spec_helper.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://johan.kiviniemi.name/software/mischacks/}
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{mischacks}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Safely pass parameters to sh scripts.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<hoe>, [">= 2.3.1"])
28
+ else
29
+ s.add_dependency(%q<hoe>, [">= 2.3.1"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<hoe>, [">= 2.3.1"])
33
+ end
34
+ end
@@ -0,0 +1,170 @@
1
+ # mischacks – Miscellaneous methods that may or may not be useful
2
+ # Copyright © 2009 Johan Kiviniemi
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for any
5
+ # purpose with or without fee is hereby granted, provided that the above
6
+ # copyright notice and this permission notice appear in all copies.
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+
16
+ require File.expand_path(File.dirname(__FILE__)+'/spec_helper.rb')
17
+ require 'mischacks'
18
+
19
+ require 'digest/sha1'
20
+
21
+ mh = MiscHacks
22
+ ce = MiscHacks::ChildError
23
+
24
+ describe mh do
25
+ describe 'checking_exit_status' do
26
+ it 'should raise an error when child fails' do
27
+ lambda do mh.checking_exit_status do exit 1 end end.should raise_error ce
28
+ lambda do mh.checking_exit_status do exit! 1 end end.should raise_error ce
29
+
30
+ lambda do mh.checking_exit_status do exit 0 end end.should_not raise_error
31
+ lambda do mh.checking_exit_status do exit! 0 end end.should_not raise_error
32
+ end
33
+
34
+ it 'should handle exec' do
35
+ lambda do mh.checking_exit_status do exec 'false' end end.should raise_error ce
36
+ lambda do mh.checking_exit_status do exec 'true' end end.should_not raise_error
37
+ end
38
+ end
39
+
40
+ describe 'do_and_exit' do
41
+ it 'should have an exit status of 1 when the block does not exit' do
42
+ lambda do
43
+ mh.do_and_exit do end
44
+ exit 2 # Should never reach this.
45
+ end.should exit_with 1
46
+ end
47
+
48
+ it 'should have the proper exit status when the block exits' do
49
+ [0, 1, 42, 255].each do |i|
50
+ lambda do
51
+ mh.do_and_exit do exit i end
52
+ exit 2 # Should never reach this.
53
+ end.should exit_with i
54
+ end
55
+ end
56
+
57
+ it 'should handle exec' do
58
+ [0, 1, 42, 255].each do |i|
59
+ lambda do
60
+ mh.do_and_exit do exec *%W{sh -c #{'exit "$1"'} sh #{i}} end
61
+ exit 2 # Should never reach this.
62
+ end.should exit_with i
63
+ end
64
+ end
65
+
66
+ it 'should handle exit! and plain exit' do
67
+ lambda do
68
+ begin
69
+ mh.do_and_exit do end
70
+ rescue SystemExit => e
71
+ exit 2
72
+ end
73
+ end.should exit_with 2
74
+
75
+ lambda do
76
+ begin
77
+ mh.do_and_exit true do end
78
+ rescue SystemExit => e
79
+ exit 2 # Should never reach this.
80
+ end
81
+ end.should exit_with 1
82
+
83
+ lambda do
84
+ begin
85
+ mh.do_and_exit! do end
86
+ rescue SystemExit => e
87
+ exit 2 # Should never reach this.
88
+ end
89
+ end.should exit_with 1
90
+ end
91
+ end
92
+
93
+ describe 'sh' do
94
+ unsafe_str = %q{" 'foo' $(bar) `baz` "}
95
+ checksum = Digest::SHA1.hexdigest unsafe_str
96
+
97
+ it "should raise #{ce} on error" do
98
+ good = ['true', '', 'printf ""']
99
+ bad = ['false', 'exit 2', 'return 2']
100
+
101
+ bad.each do |c|
102
+ lambda do mh.sh c end.should raise_error ce
103
+ end
104
+
105
+ good.each do |c|
106
+ lambda do mh.sh c end.should_not raise_error
107
+ end
108
+ end
109
+
110
+ it 'should call sh with -e' do
111
+ lambda do mh.sh 'false; true' end.should raise_error ce
112
+ end
113
+
114
+ it 'should pass normal parameters safely' do
115
+ test = lambda do |str, sha|
116
+ mh.sh %q{
117
+ temp="$(mktemp -t mischacks.XXXXXXXXXX)"
118
+ trap 'rm -f "$temp"' 0 1 2 13 15
119
+ printf "%s" "$1" >"$temp"
120
+ printf "%s *%s\n" "$2" "$temp" | sha1sum -c >/dev/null 2>&1
121
+ }, str, sha
122
+ end
123
+
124
+ lambda do test.call unsafe_str, checksum end.should_not raise_error
125
+ lambda do test.call 'foo', checksum end.should raise_error ce
126
+ end
127
+
128
+ it 'should pass environment variables safely' do
129
+ test = lambda do |str, sha|
130
+ mh.sh %q{
131
+ temp="$(mktemp -t mischacks.XXXXXXXXXX)"
132
+ trap 'rm -f "$temp"' 0 1 2 13 15
133
+ printf "%s" "$string" >"$temp"
134
+ printf "%s *%s\n" "$checksum" "$temp" | sha1sum -c >/dev/null 2>&1
135
+ }, :string => str, :checksum => sha
136
+ end
137
+
138
+ lambda do test.call unsafe_str, checksum end.should_not raise_error
139
+ lambda do test.call 'foo', checksum end.should raise_error ce
140
+ end
141
+ end
142
+
143
+ describe 'ExceptionMixin to_formatted_string' do
144
+ it 'should return the proper string for an exception' do
145
+ klass = Class.new RuntimeError
146
+
147
+ begin
148
+ raise klass, 'foo'
149
+
150
+ rescue klass => e
151
+ lines = e.to_formatted_string.split /\n/
152
+ head = lines.shift
153
+
154
+ e_class_re = Regexp.quote e.class.to_s
155
+ e_msg_re = Regexp.quote e.to_s
156
+ filename_re = Regexp.quote File.basename(__FILE__)
157
+
158
+ head_re = %r{\A.+/#{filename_re}:\d+: #{e_msg_re} \(#{e_class_re}\)\z}
159
+ head.should match head_re
160
+
161
+ lines.length.should == caller.length
162
+ lines.zip caller do |a, b|
163
+ a.should == "\tfrom #{b}"
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ # vim:set et sw=2 sts=2:
@@ -0,0 +1,44 @@
1
+ # mischacks – Miscellaneous methods that may or may not be useful
2
+ # Copyright © 2009 Johan Kiviniemi
3
+ #
4
+ # Permission to use, copy, modify, and/or distribute this software for any
5
+ # purpose with or without fee is hereby granted, provided that the above
6
+ # copyright notice and this permission notice appear in all copies.
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+
16
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)+'/../lib')
17
+
18
+ require 'mischacks'
19
+
20
+ Spec::Matchers.define :exit_with do |expected|
21
+ match do |block|
22
+ @status = 0
23
+
24
+ begin
25
+ MiscHacks.checking_exit_status do
26
+ block.call
27
+ end
28
+ rescue MiscHacks::ChildError => e
29
+ @status = e.status
30
+ end
31
+
32
+ @status.eql? expected
33
+ end
34
+
35
+ failure_message_for_should do |block|
36
+ "expected exit value #{expected}, got #{@status}"
37
+ end
38
+
39
+ failure_message_for_should do |block|
40
+ "did not expect exit value #{@status}"
41
+ end
42
+ end
43
+
44
+ # vim:set et sw=2 sts=2:
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ion1-mischacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Johan Kiviniemi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.1
24
+ version:
25
+ description: Safely pass parameters to sh scripts.
26
+ email:
27
+ - devel@johan.kiviniemi.name
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - COPYING
38
+ - History.txt
39
+ - Manifest.txt
40
+ - README.txt
41
+ - Rakefile
42
+ - lib/mischacks.rb
43
+ - mischacks.gemspec
44
+ - spec/mischacks_spec.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://johan.kiviniemi.name/software/mischacks/
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.txt
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: mischacks
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Safely pass parameters to sh scripts.
73
+ test_files: []
74
+