Initial Revision
[ohcount] / lib / ohcount / scratch_dir.rb
1 require 'fileutils'
2 require 'tmpdir'
3
4 # A utility class to manage the creation and automatic cleanup of temporary directories.
5 class ScratchDir
6         attr_reader :path
7
8         # Creates a uniquely named directory in the system tmp directory.
9         #
10         # If a block is passed to the constructor, the path to the created directory
11         # will be yielded to the block. The directory will then be deleted
12         # when this block returns.
13         #
14         # Sample usage:
15         #
16         #   ScratchDir.new do |path|
17         #     # Do some work in the new directory
18         #     File.new( path + '/foobaz', 'w' ) do
19         #       # ...
20         #     end
21         #   end # Scratch directory is deleted here
22         #
23         def initialize
24                 until @path
25                         @path = File.join(Dir.tmpdir, Time.now.utc.strftime("ohloh_%Y%H%m%S#{rand(900) + 100}"))
26                         begin
27                                 Dir.mkdir(@path)
28                         rescue Errno::EEXIST
29                                 @path = nil
30                         end
31                 end
32                 if block_given?
33                         yield @path
34                         FileUtils.rm_rf(@path)
35                 end
36         end
37 end
38
39 if $0 == __FILE__
40         path = nil
41         ScratchDir.new do |d|
42                 path = d
43                 STDOUT.puts "Created scratch direcory #{d}"
44                 File.open(File.join(d, "test"), "w") do |io|
45                         io.write "test"
46                 end
47         end
48         raise RuntimeError.new("Directory wasn't cleaned up") if FileTest.directory?(path)
49         STDOUT.puts "Test passed."
50 end