temppath 0.1.1 → 0.2.0
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 +1 -0
- data/.simplecov +1 -0
- data/README.md +20 -0
- data/Rakefile +1 -1
- data/lib/temppath.rb +101 -21
- data/lib/temppath/version.rb +1 -1
- data/test/spec_temppath.rb +73 -14
- metadata +8 -2
data/.gitignore
CHANGED
data/.simplecov
CHANGED
data/README.md
CHANGED
@@ -13,10 +13,30 @@ deleted when Ruby exits.
|
|
13
13
|
|
14
14
|
## Usage
|
15
15
|
|
16
|
+
#### Create a path
|
17
|
+
|
16
18
|
```ruby
|
17
19
|
path = Temppath.create
|
18
20
|
#=> #<Pathname:/tmp/ruby-temppath-20130407-5775-w5k77l/f41bd6c5-fc99-4b7a-8f68-95b7ae4a6b22>
|
19
21
|
path.exist? #=> false
|
22
|
+
path.open("w")
|
23
|
+
"%o" % path.stat.mode #=> "100600" (default permission 0600)
|
24
|
+
```
|
25
|
+
|
26
|
+
#### Create a directory
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
path = Temppath.mkdir
|
30
|
+
path.directory? #=> true
|
31
|
+
"%o" % path.stat.mode #=> "40700"
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Create an empty file
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
path = Temppath.touch
|
38
|
+
path.file? #=> true
|
39
|
+
"%o" % path.stat.mode #=> "100600"
|
20
40
|
```
|
21
41
|
|
22
42
|
## Documentation
|
data/Rakefile
CHANGED
data/lib/temppath.rb
CHANGED
@@ -8,15 +8,74 @@ require 'fileutils'
|
|
8
8
|
# that this library generates Pathname objects with no files and filenames are
|
9
9
|
# based on UUID. Files in paths generated by this are deleted when Ruby exits.
|
10
10
|
#
|
11
|
-
# @example
|
11
|
+
# @example Create a temporary path
|
12
12
|
# path = Temppath.create
|
13
13
|
# #=> #<Pathname:/tmp/ruby-temppath-20130407-5775-w5k77l/f41bd6c5-fc99-4b7a-8f68-95b7ae4a6b22>
|
14
14
|
# path.exist? #=> false
|
15
|
+
# path.open("w")
|
16
|
+
# "%o" % path.stat.mode #=> "100600" (default permission 0600)
|
17
|
+
# @example Touch a temporary file
|
18
|
+
# path = Temppath.touch
|
19
|
+
# path.file? #=> true
|
20
|
+
# "%o" % path.stat.mode #=> "100600"
|
21
|
+
# @example Create a temporary directory
|
22
|
+
# path = Temppath.mkdir
|
23
|
+
# path.directory? #=> true
|
24
|
+
# "%o" % path.stat.mode #=> "40700"
|
15
25
|
module Temppath
|
26
|
+
# OriginalMethodHolder keeps some original methods of Pathname.
|
27
|
+
module OriginalMethodHolder
|
28
|
+
# @api private
|
29
|
+
def self.extended(obj)
|
30
|
+
# keep original methods
|
31
|
+
obj.instance_exec do
|
32
|
+
class << self
|
33
|
+
alias :orig_open :open
|
34
|
+
alias :orig_mkdir :mkdir
|
35
|
+
alias :orig_mkpath :mkpath
|
36
|
+
alias :orig_sysopen :sysopen
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# SecurePermissionMethods provides some Pathname methods with secure default
|
43
|
+
# permission.
|
44
|
+
module SecurePermissionMethods
|
45
|
+
# Open file with permission 0600 by default. Otherwise, this is same as
|
46
|
+
# Pathname#open.
|
47
|
+
def open(*args, &b)
|
48
|
+
args[1] = 0600 unless args[1]
|
49
|
+
orig_open(*args, &b)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Make a directory with permission 0700 by defautl. Otherwise, this is same
|
53
|
+
# as Pathname#mkdir.
|
54
|
+
def mkdir(*args)
|
55
|
+
args[0] = 0700 unless args[0]
|
56
|
+
orig_mkdir(*args)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Make paths with permission 0700 by defautl. Otherwise, this is same as
|
60
|
+
# Pathname#mkpath.
|
61
|
+
def mkpath(*args)
|
62
|
+
args[0] = {} unless args[0]
|
63
|
+
args[0] = args[0].merge(mode: 0700) unless args[0][:mode]
|
64
|
+
FileUtils.mkpath(self.to_s, *args)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Open file with permission 0600 by default. Otherwise, this is same as
|
68
|
+
# Pathname#sysopen.
|
69
|
+
def sysopen(*args)
|
70
|
+
args[1] = 0600 unless args[1]
|
71
|
+
orig_sysopen(*args)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
16
75
|
class << self
|
17
76
|
# @return [Pathname]
|
18
|
-
# defalut
|
19
|
-
attr_reader :
|
77
|
+
# defalut base directory for paths created by Temppath
|
78
|
+
attr_reader :basedir
|
20
79
|
|
21
80
|
# @return [Boolean]
|
22
81
|
# true if unlink mode is enabled
|
@@ -24,42 +83,63 @@ module Temppath
|
|
24
83
|
|
25
84
|
# Create a temporary path. This method creates no files.
|
26
85
|
#
|
27
|
-
# @param
|
86
|
+
# @param option [Hash]
|
87
|
+
# @option option [String] :basename
|
28
88
|
# prefix of filename
|
29
|
-
# @
|
30
|
-
# pathname of
|
31
|
-
def create(
|
32
|
-
|
33
|
-
|
89
|
+
# @option option [Pathname] :basedir
|
90
|
+
# pathname of base directory
|
91
|
+
def create(option={})
|
92
|
+
basename = option[:basename] || ""
|
93
|
+
_basedir = option[:basedir] || basedir
|
94
|
+
path = Pathname.new(_basedir) + (basename.to_s + generate_uuid)
|
95
|
+
path.extend OriginalMethodHolder
|
96
|
+
path.extend SecurePermissionMethods
|
97
|
+
if _basedir != basedir
|
34
98
|
Kernel.at_exit {FileUtils.remove_entry_secure(path) rescue Errno::ENOENT}
|
35
99
|
end
|
36
100
|
return path
|
37
101
|
end
|
38
102
|
|
39
|
-
#
|
103
|
+
# Create a temporary directory.
|
104
|
+
def mkdir(option={})
|
105
|
+
mode = option[:mode] || 0700
|
106
|
+
path = create(option)
|
107
|
+
path.mkdir(mode)
|
108
|
+
return path
|
109
|
+
end
|
110
|
+
|
111
|
+
# Create a empty file.
|
112
|
+
def touch(option={})
|
113
|
+
mode = option[:mode] || 0600
|
114
|
+
path = create(option)
|
115
|
+
path.open("w", mode)
|
116
|
+
return path
|
117
|
+
end
|
118
|
+
|
119
|
+
# Remove current base directory, create a new base directory, and
|
40
120
|
# use it.
|
41
121
|
#
|
42
122
|
# @return [Pathname]
|
43
|
-
# new
|
44
|
-
def
|
45
|
-
|
46
|
-
@
|
123
|
+
# new base directory
|
124
|
+
def update_basedir
|
125
|
+
remove_basedir
|
126
|
+
@basedir = create_basedir
|
47
127
|
end
|
48
128
|
|
49
129
|
# Remove current temporary directory.
|
50
130
|
#
|
51
131
|
# @return [void]
|
52
|
-
def
|
53
|
-
FileUtils.remove_entry_secure(@
|
132
|
+
def remove_basedir
|
133
|
+
FileUtils.remove_entry_secure(@basedir) if @basedir.exist?
|
54
134
|
end
|
55
135
|
|
56
136
|
private
|
57
137
|
|
58
|
-
# Create a new
|
138
|
+
# Create a new base directory.
|
59
139
|
#
|
60
140
|
# @return [Pathname]
|
61
|
-
#
|
62
|
-
def
|
141
|
+
# base directory
|
142
|
+
def create_basedir
|
63
143
|
Pathname.new(Dir.mktmpdir("ruby-temppath-"))
|
64
144
|
end
|
65
145
|
|
@@ -72,13 +152,13 @@ module Temppath
|
|
72
152
|
end
|
73
153
|
end
|
74
154
|
|
75
|
-
@
|
155
|
+
@basedir = create_basedir
|
76
156
|
@unlink = true
|
77
157
|
end
|
78
158
|
|
79
159
|
# Remove Temppath's temporary directory.
|
80
160
|
Kernel.at_exit do
|
81
161
|
if Temppath.unlink
|
82
|
-
Temppath.
|
162
|
+
Temppath.remove_basedir rescue Errno::ENOENT
|
83
163
|
end
|
84
164
|
end
|
data/lib/temppath/version.rb
CHANGED
data/test/spec_temppath.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
require 'simplecov'
|
2
1
|
require 'temppath'
|
3
2
|
|
4
3
|
describe 'Temppath' do
|
5
4
|
before do
|
6
|
-
Temppath.
|
5
|
+
Temppath.update_basedir
|
7
6
|
end
|
8
7
|
|
9
8
|
it 'should get temporary path' do
|
@@ -12,28 +11,28 @@ describe 'Temppath' do
|
|
12
11
|
end
|
13
12
|
|
14
13
|
it 'should get temporary path with basename' do
|
15
|
-
Temppath.create("A_").basename.to_s[0,2].should == "A_"
|
14
|
+
Temppath.create(basename: "A_").basename.to_s[0,2].should == "A_"
|
16
15
|
end
|
17
16
|
|
18
|
-
it 'should get
|
19
|
-
dir = Dir.
|
20
|
-
Temppath.create(
|
17
|
+
it 'should get base directory path with basedir' do
|
18
|
+
dir = Dir.mktmpdir
|
19
|
+
Temppath.create(basedir: dir).dirname.should == Pathname.new(dir)
|
21
20
|
end
|
22
21
|
|
23
|
-
it 'should be in the
|
24
|
-
Temppath.create.dirname.should == Temppath.
|
22
|
+
it 'should be in the base directory' do
|
23
|
+
Temppath.create.dirname.should == Temppath.basedir
|
25
24
|
end
|
26
25
|
|
27
|
-
it 'should update current
|
28
|
-
old_dir = Temppath.
|
29
|
-
new_dir = Temppath.
|
26
|
+
it 'should update current base directory' do
|
27
|
+
old_dir = Temppath.basedir
|
28
|
+
new_dir = Temppath.update_basedir
|
30
29
|
old_dir.should != new_dir
|
31
30
|
old_dir.should.not.exist
|
32
31
|
end
|
33
32
|
|
34
|
-
it 'should remove current
|
35
|
-
dir = Temppath.
|
36
|
-
Temppath.
|
33
|
+
it 'should remove current base directory' do
|
34
|
+
dir = Temppath.basedir
|
35
|
+
Temppath.remove_basedir
|
37
36
|
dir.should.not.exist
|
38
37
|
end
|
39
38
|
|
@@ -46,4 +45,64 @@ describe 'Temppath' do
|
|
46
45
|
Temppath.unlink.should == false
|
47
46
|
Temppath.unlink = true
|
48
47
|
end
|
48
|
+
|
49
|
+
it 'should create a file with permission 0600 by default' do
|
50
|
+
path = Temppath.create
|
51
|
+
path.open("w")
|
52
|
+
("%o" % path.stat.mode).should == "100600"
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should create a file with permission 0644 explicitly' do
|
56
|
+
path = Temppath.create
|
57
|
+
path.open("w", 0644)
|
58
|
+
("%o" % path.stat.mode).should == "100644"
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should create a directory with permission 0700 by default' do
|
62
|
+
path = Temppath.create
|
63
|
+
path.mkdir
|
64
|
+
("%o" % path.stat.mode).should == "40700"
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should create a directory with permission 0755 explicitly' do
|
68
|
+
path = Temppath.create
|
69
|
+
path.mkdir(0755)
|
70
|
+
("%o" % path.stat.mode).should == "40755"
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should do mkpath with permission 0700 by default' do
|
74
|
+
path = Temppath.create
|
75
|
+
path.mkpath
|
76
|
+
("%o" % path.stat.mode).should == "40700"
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should do mkpath with permission 0755 explicitly' do
|
80
|
+
path = Temppath.create
|
81
|
+
path.mkpath(mode: 0755)
|
82
|
+
("%o" % path.stat.mode).should == "40755"
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should do sysopen with permission 0600 by default' do
|
86
|
+
path = Temppath.create
|
87
|
+
path.sysopen("w")
|
88
|
+
("%o" % path.stat.mode).should == "100600"
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should do sysopen with permission 0644 explicitly' do
|
92
|
+
path = Temppath.create
|
93
|
+
path.sysopen("w", 0644)
|
94
|
+
("%o" % path.stat.mode).should == "100644"
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should make a directory' do
|
98
|
+
path = Temppath.mkdir
|
99
|
+
path.should.directory
|
100
|
+
("%o" % path.stat.mode).should == "40700"
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should touch a file' do
|
104
|
+
path = Temppath.touch
|
105
|
+
path.should.file
|
106
|
+
("%o" % path.stat.mode).should == "100600"
|
107
|
+
end
|
49
108
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: temppath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: uuidtools
|
@@ -121,12 +121,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
121
121
|
- - ! '>='
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
hash: -3285866796712180964
|
124
127
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
128
|
none: false
|
126
129
|
requirements:
|
127
130
|
- - ! '>='
|
128
131
|
- !ruby/object:Gem::Version
|
129
132
|
version: '0'
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
hash: -3285866796712180964
|
130
136
|
requirements: []
|
131
137
|
rubyforge_project:
|
132
138
|
rubygems_version: 1.8.25
|