rubysl-tmpdir 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +0 -1
- data/.travis.yml +7 -0
- data/README.md +2 -2
- data/Rakefile +0 -1
- data/lib/rubysl/tmpdir.rb +2 -0
- data/lib/rubysl/tmpdir/tmpdir.rb +137 -0
- data/lib/{rubysl-tmpdir → rubysl/tmpdir}/version.rb +1 -1
- data/lib/tmpdir.rb +1 -0
- data/rubysl-tmpdir.gemspec +18 -18
- data/spec/dir/mktmpdir_spec.rb +129 -0
- data/spec/dir/tmpdir_spec.rb +9 -0
- metadata +71 -88
- data/lib/rubysl-tmpdir.rb +0 -7
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f465ce324da358775919276e049487ae381a03f
|
4
|
+
data.tar.gz: 99567400b80a6966d0e754e024badae4c2d90e31
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f916e8bd545ff3f33b616e0127e463a895923c4a860088c15aae2d22eca166a46c03267a6d895fcc0e8428902ab52272c706b7ee5d76ae514ad3bc54f43efde5
|
7
|
+
data.tar.gz: 1b16af989ad465436379c5f1ad414d5c9e93f334dc016981a2b663ee5a095b34016ff2f034f2451a8e6c22de3b080e31699399c23b397b72e6e25a785f562768
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Rubysl::Tmpdir
|
2
2
|
|
3
3
|
TODO: Write a gem description
|
4
4
|
|
@@ -24,6 +24,6 @@ TODO: Write usage instructions here
|
|
24
24
|
|
25
25
|
1. Fork it
|
26
26
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am '
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
28
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
29
|
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -0,0 +1,137 @@
|
|
1
|
+
#
|
2
|
+
# tmpdir - retrieve temporary directory path
|
3
|
+
#
|
4
|
+
# $Id: tmpdir.rb 21776 2009-01-26 02:12:10Z shyouhei $
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'fileutils'
|
8
|
+
|
9
|
+
class Dir
|
10
|
+
|
11
|
+
@@systmpdir = '/tmp'
|
12
|
+
|
13
|
+
begin
|
14
|
+
require 'Win32API'
|
15
|
+
CSIDL_LOCAL_APPDATA = 0x001c
|
16
|
+
max_pathlen = 260
|
17
|
+
windir = "\0"*(max_pathlen+1)
|
18
|
+
begin
|
19
|
+
getdir = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L')
|
20
|
+
raise RuntimeError if getdir.call(0, CSIDL_LOCAL_APPDATA, 0, 0, windir) != 0
|
21
|
+
windir = File.expand_path(windir.rstrip)
|
22
|
+
rescue RuntimeError
|
23
|
+
begin
|
24
|
+
getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 'PL', 'L')
|
25
|
+
rescue RuntimeError
|
26
|
+
getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 'L')
|
27
|
+
end
|
28
|
+
len = getdir.call(windir, windir.size)
|
29
|
+
windir = File.expand_path(windir[0, len])
|
30
|
+
end
|
31
|
+
temp = File.join(windir.untaint, 'temp')
|
32
|
+
@@systmpdir = temp if File.directory?(temp) and File.writable?(temp)
|
33
|
+
rescue LoadError
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Returns the operating system's temporary file path.
|
38
|
+
|
39
|
+
def Dir::tmpdir
|
40
|
+
tmp = '.'
|
41
|
+
if $SAFE > 0
|
42
|
+
tmp = @@systmpdir
|
43
|
+
else
|
44
|
+
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'],
|
45
|
+
ENV['USERPROFILE'], @@systmpdir, '/tmp']
|
46
|
+
if dir and File.directory?(dir) and File.writable?(dir)
|
47
|
+
tmp = dir
|
48
|
+
break
|
49
|
+
end
|
50
|
+
end
|
51
|
+
File.expand_path(tmp)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Dir.mktmpdir creates a temporary directory.
|
56
|
+
#
|
57
|
+
# The directory is created with 0700 permission.
|
58
|
+
#
|
59
|
+
# The prefix and suffix of the name of the directory is specified by
|
60
|
+
# the optional first argument, <i>prefix_suffix</i>.
|
61
|
+
# - If it is not specified or nil, "d" is used as the prefix and no suffix is used.
|
62
|
+
# - If it is a string, it is used as the prefix and no suffix is used.
|
63
|
+
# - If it is an array, first element is used as the prefix and second element is used as a suffix.
|
64
|
+
#
|
65
|
+
# Dir.mktmpdir {|dir| dir is ".../d..." }
|
66
|
+
# Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
|
67
|
+
# Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
|
68
|
+
#
|
69
|
+
# The directory is created under Dir.tmpdir or
|
70
|
+
# the optional second argument <i>tmpdir</i> if non-nil value is given.
|
71
|
+
#
|
72
|
+
# Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
|
73
|
+
# Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
|
74
|
+
#
|
75
|
+
# If a block is given,
|
76
|
+
# it is yielded with the path of the directory.
|
77
|
+
# The directory and its contents are removed
|
78
|
+
# using FileUtils.remove_entry_secure before Dir.mktmpdir returns.
|
79
|
+
# The value of the block is returned.
|
80
|
+
#
|
81
|
+
# Dir.mktmpdir {|dir|
|
82
|
+
# # use the directory...
|
83
|
+
# open("#{dir}/foo", "w") { ... }
|
84
|
+
# }
|
85
|
+
#
|
86
|
+
# If a block is not given,
|
87
|
+
# The path of the directory is returned.
|
88
|
+
# In this case, Dir.mktmpdir doesn't remove the directory.
|
89
|
+
#
|
90
|
+
# dir = Dir.mktmpdir
|
91
|
+
# begin
|
92
|
+
# # use the directory...
|
93
|
+
# open("#{dir}/foo", "w") { ... }
|
94
|
+
# ensure
|
95
|
+
# # remove the directory.
|
96
|
+
# FileUtils.remove_entry_secure dir
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
|
100
|
+
case prefix_suffix
|
101
|
+
when nil
|
102
|
+
prefix = "d"
|
103
|
+
suffix = ""
|
104
|
+
when String
|
105
|
+
prefix = prefix_suffix
|
106
|
+
suffix = ""
|
107
|
+
when Array
|
108
|
+
prefix = prefix_suffix[0]
|
109
|
+
suffix = prefix_suffix[1]
|
110
|
+
else
|
111
|
+
raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
|
112
|
+
end
|
113
|
+
tmpdir ||= Dir.tmpdir
|
114
|
+
t = Time.now.strftime("%Y%m%d")
|
115
|
+
n = nil
|
116
|
+
begin
|
117
|
+
path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
118
|
+
path << "-#{n}" if n
|
119
|
+
path << suffix
|
120
|
+
Dir.mkdir(path, 0700)
|
121
|
+
rescue Errno::EEXIST
|
122
|
+
n ||= 0
|
123
|
+
n += 1
|
124
|
+
retry
|
125
|
+
end
|
126
|
+
|
127
|
+
if block_given?
|
128
|
+
begin
|
129
|
+
yield path
|
130
|
+
ensure
|
131
|
+
FileUtils.remove_entry_secure path
|
132
|
+
end
|
133
|
+
else
|
134
|
+
path
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
data/lib/tmpdir.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rubysl/tmpdir"
|
data/rubysl-tmpdir.gemspec
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
#
|
2
|
-
require
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/tmpdir/version'
|
3
3
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-tmpdir"
|
6
|
+
spec.version = RubySL::Tmpdir::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library tmpdir.}
|
10
|
+
spec.summary = %q{Ruby standard library tmpdir.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-tmpdir"
|
12
|
+
spec.license = "BSD"
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
gem.require_paths = ["lib"]
|
16
|
-
gem.version = RubySL::Tmpdir::VERSION
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
gem.add_development_dependency "mspec", "~> 1.5"
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
+
spec.add_development_dependency "mspec", "~> 1.5"
|
22
22
|
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require "tmpdir"
|
2
|
+
|
3
|
+
ruby_version_is "1.8.7" do
|
4
|
+
describe "Dir.mktmpdir when passed no arguments" do
|
5
|
+
after :each do
|
6
|
+
Dir.rmdir @tmpdir if File.directory? @tmpdir
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns the path to the created tmp-dir" do
|
10
|
+
Dir.stub!(:mkdir)
|
11
|
+
Dir.should_receive(:tmpdir).and_return("/tmp")
|
12
|
+
@tmpdir = Dir.mktmpdir
|
13
|
+
@tmpdir.should =~ /^\/tmp\//
|
14
|
+
end
|
15
|
+
|
16
|
+
it "creates a new writable directory in the path provided by Dir.tmpdir" do
|
17
|
+
Dir.should_receive(:tmpdir).and_return(tmp(""))
|
18
|
+
@tmpdir = Dir.mktmpdir
|
19
|
+
File.directory?(@tmpdir).should be_true
|
20
|
+
File.writable?(@tmpdir).should be_true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "Dir.mkdir when passed a block" do
|
25
|
+
before(:each) do
|
26
|
+
Dir.stub!(:tmpdir).and_return("/tmp")
|
27
|
+
FileUtils.stub!(:remove_entry)
|
28
|
+
FileUtils.stub!(:remove_entry_secure)
|
29
|
+
end
|
30
|
+
|
31
|
+
after :each do
|
32
|
+
Dir.rmdir @tmpdir if File.directory? @tmpdir
|
33
|
+
end
|
34
|
+
|
35
|
+
it "yields the path to the passed block" do
|
36
|
+
Dir.stub!(:mkdir)
|
37
|
+
called = nil
|
38
|
+
Dir.mktmpdir do |path|
|
39
|
+
@tmpdir = path
|
40
|
+
called = true
|
41
|
+
path.should =~ /^\/tmp\//
|
42
|
+
end
|
43
|
+
called.should be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "creates the tmp-dir before yielding" do
|
47
|
+
Dir.should_receive(:tmpdir).and_return(tmp(""))
|
48
|
+
Dir.mktmpdir do |path|
|
49
|
+
@tmpdir = path
|
50
|
+
File.directory?(path).should be_true
|
51
|
+
File.writable?(path).should be_true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
ruby_version_is "1.8.7"..."2.0" do
|
56
|
+
it "removes the tmp-dir after executing the block" do
|
57
|
+
Dir.stub!(:mkdir)
|
58
|
+
Dir.mktmpdir do |path|
|
59
|
+
@tmpdir = path
|
60
|
+
FileUtils.should_receive(:remove_entry_secure).with(path)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
ruby_version_is "2.0" do
|
66
|
+
it "removes the tmp-dir after executing the block" do
|
67
|
+
Dir.stub!(:mkdir)
|
68
|
+
Dir.mktmpdir do |path|
|
69
|
+
@tmpdir = path
|
70
|
+
FileUtils.should_receive(:remove_entry).with(path)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it "returns the blocks return value" do
|
76
|
+
Dir.stub!(:mkdir)
|
77
|
+
result = Dir.mktmpdir do |path|
|
78
|
+
@tmpdir = path
|
79
|
+
:test
|
80
|
+
end
|
81
|
+
result.should equal(:test)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "Dir.mktmpdir when passed [String]" do
|
86
|
+
before :each do
|
87
|
+
Dir.stub!(:mkdir)
|
88
|
+
Dir.stub!(:tmpdir).and_return("/tmp")
|
89
|
+
end
|
90
|
+
|
91
|
+
after :each do
|
92
|
+
Dir.rmdir @tmpdir if File.directory? @tmpdir
|
93
|
+
end
|
94
|
+
|
95
|
+
it "uses the passed String as a prefix to the tmp-directory" do
|
96
|
+
prefix = "before"
|
97
|
+
@tmpdir = Dir.mktmpdir(prefix)
|
98
|
+
@tmpdir.should =~ /^\/tmp\/#{prefix}/
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "Dir.mktmpdir when passed [Array]" do
|
103
|
+
before :each do
|
104
|
+
Dir.stub!(:mkdir)
|
105
|
+
Dir.stub!(:tmpdir).and_return("/tmp")
|
106
|
+
FileUtils.stub!(:remove_entry_secure)
|
107
|
+
end
|
108
|
+
|
109
|
+
after :each do
|
110
|
+
Dir.rmdir @tmpdir if File.directory? @tmpdir
|
111
|
+
end
|
112
|
+
|
113
|
+
it "uses the first element of the passed Array as a prefix and the scond element as a suffix to the tmp-directory" do
|
114
|
+
prefix = "before"
|
115
|
+
suffix = "after"
|
116
|
+
|
117
|
+
@tmpdir = Dir.mktmpdir([prefix, suffix])
|
118
|
+
@tmpdir.should =~ /#{suffix}$/
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "Dir.mktmpdir when passed [Object]" do
|
123
|
+
it "raises an ArgumentError" do
|
124
|
+
lambda { Dir.mktmpdir(Object.new) }.should raise_error(ArgumentError)
|
125
|
+
lambda { Dir.mktmpdir(:symbol) }.should raise_error(ArgumentError)
|
126
|
+
lambda { Dir.mktmpdir(10) }.should raise_error(ArgumentError)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
metadata
CHANGED
@@ -1,118 +1,101 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-tmpdir
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Brian Shirai
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
11
|
+
date: 2013-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
22
21
|
prerelease: false
|
23
|
-
|
24
|
-
|
25
|
-
requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
26
24
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
|
30
|
-
- 1
|
31
|
-
- 0
|
32
|
-
version: "1.0"
|
33
|
-
type: :runtime
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
36
28
|
name: rake
|
37
|
-
|
38
|
-
|
39
|
-
none: false
|
40
|
-
requirements:
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
41
31
|
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
segments:
|
45
|
-
- 10
|
46
|
-
- 0
|
47
|
-
version: "10.0"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
48
34
|
type: :development
|
49
|
-
version_requirements: *id002
|
50
|
-
- !ruby/object:Gem::Dependency
|
51
|
-
name: mspec
|
52
35
|
prerelease: false
|
53
|
-
|
54
|
-
|
55
|
-
requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
56
38
|
- - ~>
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
63
48
|
type: :development
|
64
|
-
|
65
|
-
|
66
|
-
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
description: Ruby standard library tmpdir.
|
56
|
+
email:
|
67
57
|
- brixen@gmail.com
|
68
58
|
executables: []
|
69
|
-
|
70
59
|
extensions: []
|
71
|
-
|
72
60
|
extra_rdoc_files: []
|
73
|
-
|
74
|
-
files:
|
61
|
+
files:
|
75
62
|
- .gitignore
|
63
|
+
- .travis.yml
|
76
64
|
- Gemfile
|
77
65
|
- LICENSE
|
78
66
|
- README.md
|
79
67
|
- Rakefile
|
80
|
-
- lib/rubysl
|
81
|
-
- lib/rubysl
|
68
|
+
- lib/rubysl/tmpdir.rb
|
69
|
+
- lib/rubysl/tmpdir/tmpdir.rb
|
70
|
+
- lib/rubysl/tmpdir/version.rb
|
71
|
+
- lib/tmpdir.rb
|
82
72
|
- rubysl-tmpdir.gemspec
|
83
|
-
|
84
|
-
|
85
|
-
|
73
|
+
- spec/dir/mktmpdir_spec.rb
|
74
|
+
- spec/dir/tmpdir_spec.rb
|
75
|
+
homepage: https://github.com/rubysl/rubysl-tmpdir
|
76
|
+
licenses:
|
77
|
+
- BSD
|
78
|
+
metadata: {}
|
86
79
|
post_install_message:
|
87
80
|
rdoc_options: []
|
88
|
-
|
89
|
-
require_paths:
|
81
|
+
require_paths:
|
90
82
|
- lib
|
91
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
none: false
|
102
|
-
requirements:
|
103
|
-
- - ">="
|
104
|
-
- !ruby/object:Gem::Version
|
105
|
-
hash: 2002549777813010636
|
106
|
-
segments:
|
107
|
-
- 0
|
108
|
-
version: "0"
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
109
93
|
requirements: []
|
110
|
-
|
111
94
|
rubyforge_project:
|
112
|
-
rubygems_version:
|
95
|
+
rubygems_version: 2.0.7
|
113
96
|
signing_key:
|
114
|
-
specification_version:
|
115
|
-
summary: Ruby
|
116
|
-
test_files:
|
117
|
-
|
118
|
-
|
97
|
+
specification_version: 4
|
98
|
+
summary: Ruby standard library tmpdir.
|
99
|
+
test_files:
|
100
|
+
- spec/dir/mktmpdir_spec.rb
|
101
|
+
- spec/dir/tmpdir_spec.rb
|