pg-safesys 0.0.4
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +1 -0
- data/Rakefile +16 -0
- data/lib/pg-safesys/version.rb +5 -0
- data/lib/pg-safesys.rb +62 -0
- data/pg-safesys.gemspec +24 -0
- data/spec/pg-safesys_spec.rb +18 -0
- data/spec/spec_helper.rb +10 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SafeSys is for making shell calls in a nice way on Windows and *nix
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
desc "run all specs"
|
7
|
+
task :spec do
|
8
|
+
system "bundle exec rspec spec"
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :spec do
|
12
|
+
# more spec running tasks here
|
13
|
+
# whenever
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => [:features, :spec]
|
data/lib/pg-safesys.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'session'
|
2
|
+
|
3
|
+
require "pg-safesys/version"
|
4
|
+
|
5
|
+
# external dependencies
|
6
|
+
begin
|
7
|
+
require 'win32/open3'
|
8
|
+
rescue LoadError
|
9
|
+
puts "win32 LoadError: assume we're on *nix"
|
10
|
+
end
|
11
|
+
|
12
|
+
module PG
|
13
|
+
module SafeSys
|
14
|
+
# NOTE: win_sys executes in separate processes, nix_sys in one shell
|
15
|
+
# attempting to cd, for instance, will only work for nix_sys
|
16
|
+
def safe_system(*cmd)
|
17
|
+
is_windows = (!!RUBY_PLATFORM['mswin32'] || !!RUBY_PLATFORM['mingw'] || !!RUBY_PLATFORM['cygwin'])
|
18
|
+
is_windows ? win_sys(*cmd) : nix_sys(*cmd)
|
19
|
+
end
|
20
|
+
|
21
|
+
def win_sys(*commands)
|
22
|
+
output = []
|
23
|
+
commands.each do |cmd|
|
24
|
+
_in, _out, _err = Open3.popen3(cmd)
|
25
|
+
error = _err.read
|
26
|
+
|
27
|
+
if error != ""
|
28
|
+
close_all _in, _out, _err
|
29
|
+
raise IOError, "\"#{ cmd }\": #{ error }"
|
30
|
+
end
|
31
|
+
|
32
|
+
output << _out.read
|
33
|
+
close_all _in, _out, _err
|
34
|
+
end
|
35
|
+
output
|
36
|
+
end
|
37
|
+
|
38
|
+
def nix_sys(*commands)
|
39
|
+
sh = Session.new
|
40
|
+
output = []
|
41
|
+
commands.each do |cmd|
|
42
|
+
_out, _err = sh.execute cmd
|
43
|
+
if sh.exit_status > 0
|
44
|
+
sh.close
|
45
|
+
raise IOError, "\"#{ cmd }\": #{ _err }"
|
46
|
+
end
|
47
|
+
output << _out
|
48
|
+
end
|
49
|
+
sh.close
|
50
|
+
output
|
51
|
+
end
|
52
|
+
|
53
|
+
def close_all(*streams)
|
54
|
+
streams.each { |s| s.close }
|
55
|
+
end
|
56
|
+
|
57
|
+
def replace_in_file(filepath, regexp, *args, &block)
|
58
|
+
content = File.read(filepath).gsub(regexp, *args, &block)
|
59
|
+
File.open(filepath, 'wb') { |file| file.write(content) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/pg-safesys.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "pg-safesys/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "pg-safesys"
|
7
|
+
s.version = PG::SafeSys::VERSION
|
8
|
+
s.authors = ["Dave Johnson"]
|
9
|
+
s.email = ["dave@nitobi.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Safe, cross platform, shell calls}
|
12
|
+
s.description = %q{Safe, cross platform, shell calls}
|
13
|
+
|
14
|
+
s.rubyforge_project = "pg-safesys"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency('session', '>= 3.1.0')
|
22
|
+
|
23
|
+
s.add_development_dependency('rspec', '~> 2.1.0')
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PG::SafeSys do
|
4
|
+
include PG::SafeSys
|
5
|
+
|
6
|
+
describe "#safe_system" do
|
7
|
+
it "should return a list of shell outputs" do
|
8
|
+
commands = ["pwd", "ls", "echo hello"]
|
9
|
+
lambda { @outputs = safe_system *commands }.should_not raise_error
|
10
|
+
@outputs.length.should be 3
|
11
|
+
@outputs[2].should == "hello\n"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should raise error with invalid commands" do
|
15
|
+
lambda { safe_system "pwwddd" }.should raise_error IOError
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg-safesys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dave Johnson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-31 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: session
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
version: 3.1.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 11
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
version: 2.1.0
|
50
|
+
type: :development
|
51
|
+
version_requirements: *id002
|
52
|
+
description: Safe, cross platform, shell calls
|
53
|
+
email:
|
54
|
+
- dave@nitobi.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- lib/pg-safesys.rb
|
67
|
+
- lib/pg-safesys/version.rb
|
68
|
+
- pg-safesys.gemspec
|
69
|
+
- spec/pg-safesys_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: ""
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project: pg-safesys
|
100
|
+
rubygems_version: 1.7.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Safe, cross platform, shell calls
|
104
|
+
test_files:
|
105
|
+
- spec/pg-safesys_spec.rb
|
106
|
+
- spec/spec_helper.rb
|