Class: Smokegraphy::FileResourceController

Inherits:
ResourceController show all
Defined in:
lib/smokegraphy/resource_controller/file_resource_controller.rb

Instance Method Summary collapse

Methods inherited from ResourceController

build, #close

Constructor Details

#initialize(config) ⇒ FileResourceController

Returns a new instance of FileResourceController.



8
9
10
# File 'lib/smokegraphy/resource_controller/file_resource_controller.rb', line 8

def initialize(config)
  @config = config
end

Instance Method Details

#add_execute_permission(filename) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/smokegraphy/resource_controller/file_resource_controller.rb', line 39

def add_execute_permission(filename)
  begin
    File.chmod 0755, "#{@config["file"]["path"]}/#{filename}"
  rescue => evar
    print "Failed to chmod #{filename}: #{evar}"
  end
end

#cleanup(filename = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/smokegraphy/resource_controller/file_resource_controller.rb', line 47

def cleanup(filename = nil)
  if filename then
    filepath = "#{@config["file"]["path"]}/#{filename}"
    FileUtils.rm(filepath, :force => true)
    @uploaded.delete filepath
  else
    @uploaded.each do |f|
      FileUtils.rm(f, :force => true)
    end
  end
end

#initObject



12
13
14
# File 'lib/smokegraphy/resource_controller/file_resource_controller.rb', line 12

def init
  @uploaded = []
end

#transfer_source_file(filename, testsuite, destname = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/smokegraphy/resource_controller/file_resource_controller.rb', line 16

def transfer_source_file(filename, testsuite, destname = nil)

  begin
    source = "source/#{testsuite}/#{filename}"
    destname ||= filename
    destfile_path = "#{@config["file"]["path"]}/#{destname}"

    if File.exist?(source) then
      document_root = @config["http"]["document_root"]
      erb = ERB.new(File.new(source).read)

      File.open(destfile_path, "w") { |out| out << erb.result(binding) }
      @uploaded << destfile_path if !(@uploaded.include? destfile_path)

    else
      raise "no such file: #{source}"
    end

  rescue => evar
    raise "Failed to copy #{source}: #{evar}"
  end
end