tempdir 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGELOG +3 -0
  2. data/LICENSE +31 -0
  3. data/README +1 -0
  4. data/lib/temp_dir.rb +91 -0
  5. data/test/test_tempdir.rb +142 -0
  6. metadata +53 -0
@@ -0,0 +1,3 @@
1
+ -----
2
+ 0.0.1
3
+ * First release
data/LICENSE ADDED
@@ -0,0 +1,31 @@
1
+ TempDir LICENSE
2
+
3
+ Copyright (c) 2005, Jim Freeze
4
+ All rights reserved.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files
8
+ (the "Software"), to deal in the Software without restriction,
9
+ including without limitation the rights to use, copy, modify,
10
+ merge, publish, distribute, sublicense, and/or sell copies of the
11
+ Software, and to permit persons to whom the Software is furnished
12
+ to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ http://www.opensource.org/licenses/mit-license.php
30
+ http://www.opensource.org/licenses/bsd-license.php
31
+
data/README ADDED
@@ -0,0 +1 @@
1
+ TempDir is a library that makes it easy to manage the creation and removing of temporary directories.
@@ -0,0 +1,91 @@
1
+ class TempDir
2
+ require 'fileutils'
3
+
4
+ DICT = ('a'..'z').to_a
5
+ DFLT_CREATE_ATTRS = {:basename => "temp_dir", :rootpath => "/tmp", :name => nil}
6
+
7
+ #
8
+ # ==Usage
9
+ # +TempDir+ can be called with or without a block.
10
+ # ===With Block
11
+ # TempDir.create( [:basename => value] [, :rootpath => value] [, :name => value]) do |tmpdir|
12
+ # # Directory is automatically changed to tmpdir
13
+ # code here
14
+ # end
15
+ # ===Examples
16
+ # TempDir.create { |dir| # do work here } # Directory is auto deleted
17
+ # TempDir.create(:rootpath => "/mytmp" ) { |dir| ... }
18
+ # TempDir.create(:rootpath => "./", :name => "mytempfolder") { |dir| ... }
19
+ # TempDir.create(:basename => "picfiles") { |dir| ... }
20
+ # ===Without Block
21
+ # tempdir = TempDir.create
22
+ # # User is responsible for removing tempdir
23
+ def self.create(attrs = {}, &block)
24
+ new(DFLT_CREATE_ATTRS.merge(attrs)).send(:create, &block)
25
+ end
26
+
27
+ def initialize(attrs, &block)
28
+ attrs.each { |k,v|
29
+ case k
30
+ when :basename then @base_name = v
31
+ when :rootpath then @root_path = v
32
+ when :name then @name = v
33
+ else
34
+ raise "Unknown attribute #{k.inspect} => #{v.inspect}."
35
+ end
36
+ }
37
+ end
38
+ private_class_method :new
39
+
40
+ #######
41
+ private
42
+ #######
43
+
44
+ def create
45
+ tries = []
46
+ 25.times { # surely we can find a non existant directory in 25 random chances
47
+ @directory = pathname
48
+ tries << @directory
49
+ if File.exists?(@directory) && @name
50
+ @named_directory_exists = true
51
+ raise "Directory '#{@name}' already exists."
52
+ elsif File.exists?(@directory)
53
+ next
54
+ end
55
+
56
+ @remove_root_path = true unless File.exists?(@root_path)
57
+ $stderr.puts "Creating #{@directory}" if $VERBOSE
58
+ FileUtils.mkdir_p(@directory)
59
+ return @directory unless block_given?
60
+ Dir.chdir(@directory) { yield @directory }
61
+ return
62
+ }
63
+ msg =
64
+ if @name
65
+ "Could not create a temporary directory with name: '#{@name}'."
66
+ else
67
+ "Could not find a non existant directory name in 25 tries: #{tries.inspect}."
68
+ end
69
+ raise msg
70
+ ensure
71
+ if block_given?
72
+ $stderr.puts "==> Deleting #{@directory}" if $VERBOSE
73
+ FileUtils.rm_rf(@directory) unless @named_directory_exists
74
+ $stderr.puts "==> Deleted:: #{@directory}" if $VERBOSE
75
+
76
+ $stderr.puts "==> Deleting #{@root_path}" if @remove_root_path && $VERBOSE
77
+ FileUtils.rm_rf(@root_path) if @remove_root_path
78
+ $stderr.puts "==> Deleted: #{@root_path}" if @remove_root_path && $VERBOSE
79
+ end
80
+ end
81
+
82
+ def pathname
83
+ base_name = @name || "#{@base_name}_#{Process.ppid}_#{Process.pid}_#{random}"
84
+ File.expand_path(File.join(@root_path, base_name))
85
+ end
86
+
87
+ def random
88
+ Array.new(5) { DICT[rand(9999) % 26] }.join
89
+ end
90
+
91
+ end#class TempDir
@@ -0,0 +1,142 @@
1
+ require 'test/unit'
2
+
3
+ require 'temp_dir'
4
+
5
+ class TestTempDir < Test::Unit::TestCase
6
+
7
+ def setup
8
+ data_dir = File.join(File.dirname(__FILE__), "data")
9
+ FileUtils.mkdir(data_dir) unless File.exists?(data_dir)
10
+ end
11
+
12
+ def test_new
13
+ assert_raises(NoMethodError) { TempDir.new }
14
+ end
15
+
16
+ def test_create_with_block
17
+ dir = nil
18
+ TempDir.create { |dir|
19
+ assert_match /\/tmp\/temp_dir_\d+_\d+_\w+/, dir
20
+ assert File.exists?(dir)
21
+ }
22
+ assert !File.exists?(dir)
23
+ end
24
+
25
+ def test_create_without_block
26
+ dir = TempDir.create
27
+ assert_match /\/tmp\/temp_dir_\d+_\d+_\w+/, dir
28
+ assert File.exists?(dir)
29
+ ensure
30
+ FileUtils.rm_rf(dir)
31
+ assert !File.exists?(dir)
32
+ end
33
+
34
+ def test_create_with_block_and_basename
35
+ dir = nil
36
+ TempDir.create(:basename => "work_dir") { |dir|
37
+ assert_match /\/tmp\/work_dir_\d+_\d+_\w+/, dir, "Bad dir name: #{dir}."
38
+ assert File.exists?(dir), "Dir #{dir} should exist."
39
+ }
40
+ assert !File.exists?(dir)
41
+ end
42
+
43
+ def test_create_without_block_and_with_basename
44
+ dir = TempDir.create(:basename => "work_dir")
45
+ assert_match /\/tmp\/work_dir_\d+_\d+_\w+/, dir
46
+ assert File.exists?(dir)
47
+ ensure
48
+ FileUtils.rm_rf(dir)
49
+ assert !File.exists?(dir)
50
+ end
51
+
52
+ def test_create_with_block_and_rootpath
53
+ dir = nil
54
+ TempDir.create(:rootpath => "./data") { |dir|
55
+ assert_match /\/data\/temp_dir_\d+_\d+_\w+/, dir, "Bad dir name: #{dir}."
56
+ assert File.exists?(dir), "Dir #{dir} should exist."
57
+ }
58
+ assert !File.exists?(dir)
59
+ end
60
+
61
+ def test_create_without_block_and_with_existing_rootpath
62
+ rootpath = File.join(File.dirname(__FILE__), "./data")
63
+ assert File.exists?(rootpath), "Path #{rootpath} should exist before test."
64
+
65
+ dir = TempDir.create(:rootpath => rootpath)
66
+ assert_match /\/data\/temp_dir_\d+_\d+_\w+/, dir
67
+ assert File.exists?(dir)
68
+ ensure
69
+ if dir
70
+ FileUtils.rm_rf(dir)
71
+ assert !File.exists?(dir)
72
+ end
73
+ end
74
+
75
+ def test_create_with_non_existing_dir
76
+ dir = nil
77
+ TempDir.create(:rootpath => "./data3") { |dir|
78
+ assert_match /\/data3\/temp_dir_\d+_\d+_\w+/, dir, "Bad dir name: #{dir}."
79
+ assert File.exists?(dir), "Dir #{dir} should exist."
80
+ }
81
+ ensure
82
+ assert !File.exists?(dir), "Dir #{dir} should NOT exist."
83
+ assert !File.exists?("./data3"), "Dir './data3' should NOT exist."
84
+ end
85
+
86
+ def test_nested_temp_dirs_with_basename
87
+ TempDir.create(:basename => "outer") { |outer|
88
+ TempDir.create(:rootpath => outer, :basename => "wa") { |wa|
89
+ assert File.directory?(wa)
90
+ assert_match /\/tmp\/outer.*\/wa_/, wa
91
+ }
92
+ }
93
+ end
94
+
95
+ def test_nested_temp_dirs_with_rootpath_and_basename
96
+ TempDir.create(:rootpath => "./", :basename => "outer") { |outer|
97
+ TempDir.create(:rootpath => outer, :basename => "wa") { |wa|
98
+ assert File.directory?(wa)
99
+ assert_match /\/outer.*\/wa_/, wa
100
+ #p wa
101
+ }
102
+ }
103
+ end
104
+
105
+ def test_nested_temp_dirs_with_rootpath_and_name
106
+ assert !File.exists?("./parent"), "./parent should not exist before test."
107
+ path = File.expand_path(".")
108
+ TempDir.create(:rootpath => "./", :name => "parent") { |parent|
109
+ TempDir.create(:rootpath => parent, :name => "wa") { |wa|
110
+ assert File.directory?(wa)
111
+ assert_equal File.join(path, "parent/wa"), wa
112
+ }
113
+ }
114
+ assert !File.exists?("./parent"), "./parent should not exist after test."
115
+ end
116
+
117
+ def test_name_exists_without_block
118
+ rootpath = File.dirname(__FILE__)
119
+ assert File.exists?(File.join(rootpath, "./data")),
120
+ "./data should exist before this test."
121
+ assert_raises(RuntimeError,
122
+ "Should not be able to create a tempdir with name of existing directory.") {
123
+ TempDir.create(:rootpath => rootpath, :name => "data")
124
+ }
125
+ assert File.exists?(File.join(rootpath, "./data")),
126
+ "./data should exist before this test without a block."
127
+ end
128
+
129
+ def test_name_exists_with_block
130
+ rootpath = File.dirname(__FILE__)
131
+ assert File.exists?(File.join(rootpath, "./data")),
132
+ "./data should exist before this test with block."
133
+ assert_raises(RuntimeError,
134
+ "Should not be able to create a tempdir with name of existing directory.") {
135
+ TempDir.create(:rootpath => rootpath, :name => "data") {}
136
+ }
137
+ assert File.exists?(File.join(rootpath, "./data")),
138
+ "./data should exist before this test with block."
139
+ end
140
+
141
+ end#class TestTempDir
142
+
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: tempdir
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-01-03 00:00:00 -06:00
8
+ summary: Library to facilitate creation and removal of temporary directories.
9
+ require_paths:
10
+ - lib
11
+ email: tempdir@freeze.org
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: temp_dir
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Jim Freeze
31
+ files:
32
+ - test/data
33
+ - test/test_tempdir.rb
34
+ - lib/temp_dir.rb
35
+ - CHANGELOG
36
+ - README
37
+ - LICENSE
38
+ test_files:
39
+ - test/test_tempdir.rb
40
+ rdoc_options: []
41
+
42
+ extra_rdoc_files:
43
+ - README
44
+ - LICENSE
45
+ - CHANGELOG
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+