forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiqCollectFiles.rb
executable file
·138 lines (120 loc) · 3.72 KB
/
MiqCollectFiles.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'find'
require 'fileutils'
require 'yaml'
require 'ostruct'
class MiqCollectFiles
attr_writer :verbose
def initialize(collectionSpec)
@csa = collectionSpec
@csa = YAML.load_file(collectionSpec) if @csa.kind_of? String
@csa = [@csa] if @csa.kind_of? Hash
raise "Invalid collection spec" if [email protected]_of? Array
@verbose = false
end
def collect
@csa.each { |cs| doCollect(OpenStruct.new(cs)) }
end
def dumpSpec(specFile)
YAML.dump(@csa, File.open(specFile, "w"))
end
private
def doCollect(cs)
pwd = Dir.pwd
#
# The directory where the collection will be created.
#
destDir = File.join(pwd, cs.todir)
makePath(destDir) unless File.exists? destDir
#
# The directory relative to which the files will be collected.
#
Dir.chdir(cs.basedir)
puts "BASEDIR: #{Dir.pwd}" if @verbose
cs.include.concat(cs.include_always).uniq! if cs.include && cs.include_always
#
# Loop through the files and directories that are to be included in the collection.
#
cs.include.each do |i|
# collect_files.yaml, generated by svn diffs, can contain file paths the have been URL encoded, so decode them if the file doesn't exist
# - .../vendor/gems/railties-3.2.8/.../%25file_name%25_generator.rb.tt
unless File.exists? i
require 'uri'
i = URI.decode(i)
end
raise "File: #{i} does not exist" unless File.exists? i
#
# If this is a plain file, then include it in the collection.
#
if !File.directory? i
#
# Skip files that match an exclude RE.
#
next if cs.exclude && cs.exclude.detect { |e| i =~ e }
puts "FILE: #{i}" if @verbose
toFile = File.join(destDir, i)
makePath(File.dirname(toFile))
#
# If the file path matches an encrypt RE and doesn't
# match a noencrypt RE, then encrypt the contents of
# the file before copying it to the collection.
#
if cs.encrypt && cs.encrypt.detect { |e| i =~ e }
if !cs.noencrypt || !cs.noencrypt.detect { |ne| i =~ ne }
encryptFile(i, toFile)
next
end
end
copyFile(i, toFile)
next
end
#
# If this is a directory, then recursively copy its contents
# to the collection directory.
#
puts "DIR: #{i}" if @verbose
Find.find(i) do |path|
#
# Prune directories that match an exclude RE.
#
if File.directory? path
if cs.exclude && cs.exclude.detect { |e| path =~ e }
Find.prune
next
end
makePath(path)
next
end
#
# Skip files that match an exclude RE.
#
next if cs.exclude && cs.exclude.detect { |e| path =~ e }
toFile = File.join(destDir, path)
makePath(File.dirname(toFile))
#
# If the file path matches an encritp RE and doesn't
# match a noencrypt RE, then encrypt the contents of
# the file before copying it to the collection.
#
if cs.encrypt && cs.encrypt.detect { |e| path =~ e }
if !cs.noencrypt || !cs.noencrypt.detect { |ne| path =~ ne }
encryptFile(path, toFile)
next
end
end
copyFile(path, toFile)
end
end if cs.include
Dir.chdir(pwd)
end
def makePath(path)
return if File.exists? path
parentDir = File.dirname(path)
makePath(parentDir) if !File.exists? parentDir
Dir.mkdir(path)
end
def copyFile(src, dest)
puts "\t COPY: #{src}\n\t TO: #{dest}\n\n" if @verbose
FileUtils.copy(src, dest)
end
alias_method :encryptFile, :copyFile
end # class MiqCollectFiles