pa 1.0.3 → 1.1.3
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/Gemfile +4 -6
- data/README.md +29 -14
- data/lib/pa.rb +34 -36
- data/lib/pa/cmd.rb +506 -414
- data/lib/pa/dir.rb +215 -175
- data/lib/pa/path.rb +282 -306
- data/lib/pa/state.rb +91 -52
- data/lib/pa/util.rb +32 -0
- data/lib/pa/version.rb +3 -0
- data/pa.gemspec +3 -5
- data/spec/pa/cmd_spec.rb +1 -1
- data/spec/pa/dir_spec.rb +19 -21
- data/spec/pa/path_spec.rb +22 -47
- metadata +22 -41
- data/LICENSE +0 -20
- data/version.rb +0 -9
data/lib/pa/state.rb
CHANGED
@@ -1,64 +1,103 @@
|
|
1
1
|
class Pa
|
2
|
-
module
|
3
|
-
|
4
|
-
|
2
|
+
module State
|
3
|
+
extend Util::Concern
|
4
|
+
module ClassMethods
|
5
|
+
# goes to File
|
6
|
+
[ :exists?, :atime, :ctime, :mtime, :stat, :lstat, :size, :zero?, :executable?, :executable_real?, :world_executable?, :readable?, :readable_real?, :world_readalbe?, :writeable?, :writeable_real?, :world_writeable?, :directory?, :file?, :blockdev?, :chardev?, :piple?, :socket?, :symlink?, :dangling?, :owned?, :grpowned?, :setgid?, :setuid?, :stricky?, :identical? ].each do |name|
|
7
|
+
define_method(name) { |*args|
|
8
|
+
File.__send__ name, *args
|
9
|
+
}
|
10
|
+
end
|
5
11
|
|
6
|
-
|
7
|
-
|
12
|
+
# @see File.chmod
|
13
|
+
def chmod(mode, *paths)
|
14
|
+
paths.map!{|v|get(v)}
|
15
|
+
File.chmod(mode, *paths)
|
16
|
+
end
|
8
17
|
|
9
|
-
|
10
|
-
|
18
|
+
# @see File.lchmod
|
19
|
+
def lchmod(mode, *paths)
|
20
|
+
paths.map!{|v|get(v)}
|
21
|
+
File.lchmod(mode, *paths)
|
22
|
+
end
|
11
23
|
|
12
|
-
|
13
|
-
|
24
|
+
# @see File.chown
|
25
|
+
def chown(user, group=nil, *paths)
|
26
|
+
paths.map!{|v|get(v)}
|
27
|
+
File.chown(user, group, *paths)
|
28
|
+
end
|
14
29
|
|
15
|
-
|
16
|
-
|
30
|
+
# @see File.lchown
|
31
|
+
def lchown(user, group=nil, *paths)
|
32
|
+
paths.map!{|v|get(v)}
|
33
|
+
File.lchown(user, group, *paths)
|
34
|
+
end
|
17
35
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# @param [String] path
|
24
|
-
# @return [String]
|
25
|
-
def type(path)
|
26
|
-
case (t=File.ftype(get(path)))
|
27
|
-
when "characterSpecial"
|
28
|
-
"chardev"
|
29
|
-
when "blockSpecial"
|
30
|
-
"blockdev"
|
31
|
-
when "link"
|
32
|
-
"symlink"
|
33
|
-
else
|
34
|
-
t
|
35
|
-
end
|
36
|
-
end # def type
|
36
|
+
# @see File.utime
|
37
|
+
def utime(atime, mtime, *paths)
|
38
|
+
paths.map!{|v|get(v)}
|
39
|
+
File.utime(atime, mtime, *paths)
|
40
|
+
end
|
37
41
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
42
|
+
# get file type
|
43
|
+
#
|
44
|
+
# file types:
|
45
|
+
# "chardev" "blockdev" "symlink" ..
|
46
|
+
#
|
47
|
+
# @param [String] path
|
48
|
+
# @return [String]
|
49
|
+
def type(path)
|
50
|
+
case (t=File.ftype(get(path)))
|
51
|
+
when "characterSpecial"
|
52
|
+
"chardev"
|
53
|
+
when "blockSpecial"
|
54
|
+
"blockdev"
|
55
|
+
when "link"
|
56
|
+
"symlink"
|
57
|
+
else
|
58
|
+
t
|
59
|
+
end
|
60
|
+
end # def type
|
53
61
|
|
54
|
-
module State
|
55
|
-
def chmod(mode); File.chmod(mode, path) end
|
56
|
-
def lchmod(mode); File.lchmod(mode, path) end
|
57
|
-
def chown(uid, gid=nil); File.chown(uid, gid, path) end
|
58
|
-
def lchown(uid, gid=nil); File.lchown(uid, gid, path) end
|
59
|
-
def utime(atime, mtime); File.utime(atime, mtime, path) end
|
60
|
-
end
|
61
62
|
|
63
|
+
# is path a mountpoint?
|
64
|
+
#
|
65
|
+
# @param[String] path
|
66
|
+
# @return [Boolean]
|
67
|
+
def mountpoint?(path)
|
68
|
+
path=get(path)
|
69
|
+
begin
|
70
|
+
stat1 = File.lstat(path)
|
71
|
+
stat2 = File.lstat(File.join(path, '..'))
|
72
|
+
stat1.dev == stat2.dev && stat1.ino == stat2.ino || stat1.dev != stat2.dev
|
73
|
+
rescue Errno::ENOENT
|
74
|
+
false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
module InstanceMethods
|
80
|
+
def chmod(mode)
|
81
|
+
File.chmod(mode, path)
|
82
|
+
end
|
83
|
+
|
84
|
+
def lchmod(mode)
|
85
|
+
File.lchmod(mode, path)
|
86
|
+
end
|
87
|
+
|
88
|
+
def chown(uid, gid=nil)
|
89
|
+
File.chown(uid, gid, path)
|
90
|
+
end
|
91
|
+
|
92
|
+
def lchown(uid, gid=nil)
|
93
|
+
File.lchown(uid, gid, path)
|
94
|
+
end
|
95
|
+
|
96
|
+
def utime(atime, mtime)
|
97
|
+
File.utime(atime, mtime, path)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
62
101
|
end
|
63
102
|
|
64
103
|
|
data/lib/pa/util.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Pa
|
2
|
+
class Util
|
3
|
+
module Concern
|
4
|
+
def included(base)
|
5
|
+
base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
|
6
|
+
base.send :include, const_get(:InstanceMethods) if const_defined?(:InstanceMethods)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# extract options
|
12
|
+
# @see extract_options!
|
13
|
+
# @example
|
14
|
+
# def mkdir(*args)
|
15
|
+
# paths, o = args.extract_options
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# @return [Array<Array,Hash>]
|
19
|
+
def extract_options(ary, default={})
|
20
|
+
if ary.last.is_a?(Hash) && ary.last.instance_of?(Hash)
|
21
|
+
[ary[0...-1], ary[-1].merge(default)]
|
22
|
+
else
|
23
|
+
[ary, default]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def wrap_array(value)
|
28
|
+
Array === value ? value : [value]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/pa/version.rb
ADDED
data/pa.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
$: <<
|
2
|
-
require "version"
|
1
|
+
$: << File.expand_path("../lib", __FILE__)
|
2
|
+
require "pa/version"
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "pa"
|
6
|
-
s.version = Pa::VERSION
|
6
|
+
s.version = Pa::VERSION
|
7
7
|
s.summary = "a path library for Ruby"
|
8
8
|
s.description = <<-EOF
|
9
9
|
a path library for Ruby
|
@@ -15,6 +15,4 @@ a path library for Ruby
|
|
15
15
|
s.rubyforge_project = "xx"
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
|
-
|
19
|
-
s.add_dependency "tagen", "~>1.0.0"
|
20
18
|
end
|
data/spec/pa/cmd_spec.rb
CHANGED
data/spec/pa/dir_spec.rb
CHANGED
@@ -20,7 +20,7 @@ describe Pa do
|
|
20
20
|
FileUtils.rm_r @tmpdir
|
21
21
|
end
|
22
22
|
|
23
|
-
describe "#
|
23
|
+
describe "#glob2" do
|
24
24
|
before(:each) do
|
25
25
|
@files = %w(fa .fa)
|
26
26
|
FileUtils.touch(@files)
|
@@ -31,18 +31,18 @@ describe Pa do
|
|
31
31
|
|
32
32
|
context "call without any option" do
|
33
33
|
it "returns 1 items" do
|
34
|
-
Pa.
|
34
|
+
Pa.glob2("*").should have(1).items
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
context "call with :dotmatch option" do
|
39
39
|
it "returns 2 items" do
|
40
|
-
Pa.
|
40
|
+
Pa.glob2("*", dotmatch: true).should have(2).items
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
describe "#
|
45
|
+
describe "#each2" do
|
46
46
|
# fa .fa fa~
|
47
47
|
# dira/
|
48
48
|
# dirb/
|
@@ -60,33 +60,33 @@ describe Pa do
|
|
60
60
|
|
61
61
|
it "runs on" do
|
62
62
|
ret = []
|
63
|
-
Pa.
|
63
|
+
Pa.each2{|pa| ret << pa}
|
64
64
|
ret.sort.should == %w(.fa dira fa fa~)
|
65
65
|
end
|
66
66
|
|
67
67
|
it "return a Enumerator when call without block" do
|
68
|
-
Pa.
|
68
|
+
Pa.each2.should be_an_instance_of Enumerator
|
69
69
|
end
|
70
70
|
|
71
71
|
it "raise Errno::ENOENT if path doesn't exists" do
|
72
|
-
lambda { Pa.
|
72
|
+
lambda { Pa.each2("path_doesn't_exits"){} }.should raise_error(Errno::ENOENT)
|
73
73
|
end
|
74
74
|
|
75
75
|
it "raise Errno::ENOTDIDR if path isn't a directory" do
|
76
|
-
lambda { Pa.
|
76
|
+
lambda { Pa.each2("fa"){} }.should raise_error(Errno::ENOTDIR)
|
77
77
|
end
|
78
78
|
|
79
|
-
it "
|
80
|
-
Pa.
|
79
|
+
it "each2(.) return 'foo' not '.foo'" do
|
80
|
+
Pa.each2.with_object([]){|pa,m| m<<pa}.sort.should == %w(.fa dira fa fa~)
|
81
81
|
end
|
82
82
|
|
83
|
-
it "
|
84
|
-
Pa.
|
83
|
+
it "each2(nodot: true) -> list all files except dot file" do
|
84
|
+
Pa.each2(nodot: true).with_object([]){|pa,m|m<<pa}.sort.should == %w(dira fa fa~)
|
85
85
|
end
|
86
86
|
|
87
87
|
end
|
88
88
|
|
89
|
-
describe "#
|
89
|
+
describe "#each2_r" do
|
90
90
|
# fa .fa fa~
|
91
91
|
# dira/
|
92
92
|
# dirb/
|
@@ -102,14 +102,14 @@ describe Pa do
|
|
102
102
|
FileUtils.rm_r @dirs
|
103
103
|
end
|
104
104
|
|
105
|
-
it "
|
106
|
-
Pa.
|
107
|
-
Pa.
|
105
|
+
it "each2_r -> Enumerator" do
|
106
|
+
Pa.each2_r.should be_an_instance_of Enumerator
|
107
|
+
Pa.each2_r.with_object([]){|(pa,r),m|m<<r}.sort.should == %w(.fa dira dira/dirb dira/dirb/b fa fa~)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
111
|
|
112
|
-
describe "#
|
112
|
+
describe "#ls2" do
|
113
113
|
# filea
|
114
114
|
# dira/
|
115
115
|
# fileb
|
@@ -125,13 +125,11 @@ describe Pa do
|
|
125
125
|
end
|
126
126
|
|
127
127
|
it "runs ok -> Array" do
|
128
|
-
Pa.
|
128
|
+
Pa.ls2.should == ["filea", "dira"]
|
129
129
|
end
|
130
130
|
|
131
131
|
it "call a block" do
|
132
|
-
Pa.
|
132
|
+
Pa.ls2 { |pa, fname| File.directory?(pa) }.should == ["dira"]
|
133
133
|
end
|
134
134
|
end
|
135
|
-
|
136
|
-
|
137
135
|
end
|
data/spec/pa/path_spec.rb
CHANGED
@@ -1,39 +1,26 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Pa do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "#dir2" do
|
11
|
-
it "runs ok" do
|
12
|
-
Pa('/home/foo').dir2.should be_an_instance_of Pa
|
13
|
-
end
|
14
|
-
end
|
4
|
+
context "dir and dir2" do
|
5
|
+
it "#dir => Pa" do
|
6
|
+
Pa.dir("/home/guten").should be_an_instance_of Pa
|
7
|
+
end
|
15
8
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
9
|
+
it "#dir2 => Pa" do
|
10
|
+
Pa.dir2("/home/guten").should be_an_instance_of String
|
11
|
+
end
|
21
12
|
|
22
|
-
|
23
|
-
it "runs ok" do
|
24
|
-
Pa.pwd2.should be_an_instance_of Pa
|
25
|
-
end
|
26
|
-
end
|
13
|
+
end
|
27
14
|
|
28
|
-
describe ".
|
15
|
+
describe ".shorten2" do
|
29
16
|
it "short /home/usr/file into ~/file" do
|
30
17
|
ENV["HOME"] = "/home/foo"
|
31
|
-
Pa.
|
18
|
+
Pa.shorten2("/home/foo/file").should == "~/file"
|
32
19
|
end
|
33
20
|
|
34
21
|
it "not short /home/other-user/file" do
|
35
22
|
ENV["HOME"] = "/home/foo"
|
36
|
-
Pa.
|
23
|
+
Pa.shorten2("/home/bar/file").should == "/home/bar/file"
|
37
24
|
end
|
38
25
|
end
|
39
26
|
|
@@ -48,31 +35,26 @@ describe Pa do
|
|
48
35
|
|
49
36
|
end
|
50
37
|
|
51
|
-
describe "
|
52
|
-
it "get a basename of a path" do
|
53
|
-
Pa.basename("/home/foo").should == "foo"
|
54
|
-
end
|
55
|
-
|
38
|
+
describe ".base2" do
|
56
39
|
it "get name, ext with :ext => true" do
|
57
|
-
Pa.
|
40
|
+
Pa.base2("/home/foo.bar", ext: true).should == ["foo", "bar"]
|
58
41
|
end
|
59
|
-
|
60
42
|
end
|
61
|
-
|
62
|
-
describe '.
|
43
|
+
|
44
|
+
describe '.parent2' do
|
63
45
|
before :each do
|
64
46
|
@path = "/home/foo/a.txt"
|
65
47
|
end
|
66
48
|
|
67
49
|
it "return parent path" do
|
68
|
-
Pa.
|
50
|
+
Pa.parent2(@path).should == "/home/foo"
|
69
51
|
end
|
70
52
|
|
71
53
|
it "return parent upto 2 level path" do
|
72
|
-
Pa.
|
54
|
+
Pa.parent2(@path, 2).should == "/home"
|
73
55
|
end
|
74
56
|
end
|
75
|
-
|
57
|
+
|
76
58
|
describe "#==" do
|
77
59
|
it "runs ok" do
|
78
60
|
(Pa('/home') == Pa('/home')).should be_true
|
@@ -85,9 +67,9 @@ describe Pa do
|
|
85
67
|
end
|
86
68
|
end
|
87
69
|
|
88
|
-
describe "#
|
70
|
+
describe "#sub2" do
|
89
71
|
it "runs ok" do
|
90
|
-
Pa('/home/foo').
|
72
|
+
Pa('/home/foo').sub2(/o/,'').should == '/hme/foo'
|
91
73
|
end
|
92
74
|
end
|
93
75
|
|
@@ -99,9 +81,9 @@ describe Pa do
|
|
99
81
|
end
|
100
82
|
end
|
101
83
|
|
102
|
-
describe "#
|
84
|
+
describe "#gsub2" do
|
103
85
|
it "runs ok" do
|
104
|
-
Pa('/home/foo').
|
86
|
+
Pa('/home/foo').gsub2(/o/,'').should == '/hme/f'
|
105
87
|
end
|
106
88
|
end
|
107
89
|
|
@@ -136,11 +118,4 @@ describe Pa do
|
|
136
118
|
(Pa('/home/foo') =~ /foo/).should be_true
|
137
119
|
end
|
138
120
|
end
|
139
|
-
|
140
|
-
describe "#dir_pa" do
|
141
|
-
it "return Pa instance" do
|
142
|
-
Pa('/home/guten').dir_pa.should be_an_instance_of(Pa)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
121
|
end
|