Module: Pkg::Util::File
- Defined in:
- lib/packaging/util/file.rb
Class Method Summary collapse
-
.directories(dir) ⇒ Object
Returns an array of all the directories at the top level of #dir.
- .directory?(file) ⇒ Boolean
- .empty_dir?(dir) ⇒ Boolean
- .erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding }) ⇒ Object
- .erb_string(erbfile, b = binding) ⇒ Object
- .exist?(file) ⇒ Boolean (also: exists?)
-
.fetch ⇒ Object
The fetch method pulls down two files from the build-data repo that contain additional data specific to Puppet Labs release infrastructure intended to augment/override any defaults specified in the source project repo, e.g.
- .file_exists?(file, args = { :required => false }) ⇒ Boolean
- .file_writable?(file, args = { :required => false }) ⇒ Boolean
-
.files_with_ext(dir, ext) ⇒ Object
Returns an array of all files with #ext inside #dir.
- .install_files_into_dir(file_patterns, workdir) ⇒ Object
-
.load_extras(temp_directory) ⇒ Object
The load_extras method is intended to load variables from the extra yaml file downloaded by the pl:fetch task.
- .mktemp ⇒ Object (also: get_temp)
- .untar_into(source, target = nil, options = "") ⇒ Object
Class Method Details
.directories(dir) ⇒ Object
Returns an array of all the directories at the top level of #dir
27 28 29 30 31 32 33 |
# File 'lib/packaging/util/file.rb', line 27 def directories(dir) if File.directory?(dir) Dir.chdir(dir) do Dir.glob("*").select { |entry| File.directory?(entry) } end end end |
.directory?(file) ⇒ Boolean
11 12 13 |
# File 'lib/packaging/util/file.rb', line 11 def directory?(file) ::File.directory?(file) end |
.empty_dir?(dir) ⇒ Boolean
21 22 23 |
# File 'lib/packaging/util/file.rb', line 21 def empty_dir?(dir) File.exist?(dir) and File.directory?(dir) and Dir["#{dir}/**/*"].empty? end |
.erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding }) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/packaging/util/file.rb', line 72 def erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding }) outfile ||= File.join(mktemp, File.basename(erbfile).sub(File.extname(erbfile), "")) output = erb_string(erbfile, opts[:binding]) File.open(outfile, 'w') { |f| f.write output } puts "Generated: #{outfile}" FileUtils.rm_rf erbfile if remove_orig outfile end |
.erb_string(erbfile, b = binding) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/packaging/util/file.rb', line 59 def erb_string(erbfile, b = binding) template = File.read(erbfile) # We should be using this but some images are pegged at Ruby <= 2.5; this interface # changed at Ruby 2.6 # message = ERB.new(template, safe_level: nil, trim_mode: '-') # rubocop:disable Lint/ErbNewArguments = ERB.new(template, nil, '-') # rubocop:enable Lint/ErbNewArguments .result(b) end |
.exist?(file) ⇒ Boolean Also known as: exists?
6 7 8 |
# File 'lib/packaging/util/file.rb', line 6 def exist?(file) ::File.exist?(file) end |
.fetch ⇒ Object
The fetch method pulls down two files from the build-data repo that contain additional data specific to Puppet Labs release infrastructure intended to augment/override any defaults specified in the source project repo, e.g. in ext/build_defaults.yaml
It uses curl to download the files, and places them in a temporary directory, e.g. /tmp/somedirectory/project,team/Pkg::Config.builder_data_file
Retrieve build-data configurations to override/extend local build_defaults
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/packaging/util/file.rb', line 138 def fetch # Each team has a build-defaults file that specifies local infrastructure targets # for things like builders, target locations for build artifacts, etc Since much # of these don't change, one file can be maintained for the team. Each project # also has a data file for information specific to it. If the project builds # both PE and not PE, it has two files, one for PE, and the other for FOSS # data_repo = Pkg::Config.build_data_repo if Pkg::Config.dev_build puts "NOTICE: This is a dev build!" project_data_branch = "#{Pkg::Config.project}-dev" else project_data_branch = Pkg::Config.project end team_data_branch = Pkg::Config.team if Pkg::Config.build_pe project_data_branch = "pe-#{project_data_branch}" unless project_data_branch =~ /^pe-/ team_data_branch = "pe-#{team_data_branch}" unless team_data_branch =~ /^pe-/ end # Remove .packaging directory from old-style extras loading FileUtils.rm_rf("#{ENV['HOME']}/.packaging") if File.directory?("#{ENV['HOME']}/.packaging") # Touch the .packaging file which is allows packaging to present remote tasks FileUtils.touch("#{ENV['HOME']}/.packaging") begin build_data_directory = Pkg::Util::File.mktemp %x(git clone #{data_repo} #{build_data_directory}) unless $?.success? fail 'Error: could not fetch the build-data repo. Maybe you do not have the correct permissions?' end Dir.chdir(build_data_directory) do [team_data_branch, project_data_branch].each do |branch| %x(git checkout #{branch}) unless $?.success? warn "Warning: no build_defaults found in branch '#{branch}' of '#{data_repo}'. Skipping." next end load_extras(build_data_directory) end end ensure FileUtils.rm_rf(build_data_directory) end end |
.file_exists?(file, args = { :required => false }) ⇒ Boolean
41 42 43 44 45 46 47 |
# File 'lib/packaging/util/file.rb', line 41 def file_exists?(file, args = { :required => false }) exists = File.exist? file if !exists and args[:required] fail "Required file #{file} could not be found" end exists end |
.file_writable?(file, args = { :required => false }) ⇒ Boolean
49 50 51 52 53 54 55 |
# File 'lib/packaging/util/file.rb', line 49 def file_writable?(file, args = { :required => false }) writable = File.writable? file if !writable and args[:required] fail "File #{file} is not writable" end writable end |
.files_with_ext(dir, ext) ⇒ Object
Returns an array of all files with #ext inside #dir
36 37 38 |
# File 'lib/packaging/util/file.rb', line 36 def files_with_ext(dir, ext) Dir.glob("#{dir}/**/*#{ext}") end |
.install_files_into_dir(file_patterns, workdir) ⇒ Object
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 |
# File 'lib/packaging/util/file.rb', line 93 def install_files_into_dir(file_patterns, workdir) install = [] # We need to add our list of file patterns from the configuration; this # used to be a list of "things to copy recursively", which would install # editor backup files and other nasty things. # # This handles that case correctly, with a deprecation warning, to augment # our FileList with the right things to put in place. # # Eventually, when all our projects are migrated to the new standard, we # can drop this in favour of just pushing the patterns directly into the # FileList and eliminate many lines of code and comment. Dir.chdir(Pkg::Config.project_root) do file_patterns.each do |pattern| if File.directory?(pattern) and !Pkg::Util::File.empty_dir?(pattern) install << Dir["#{pattern}/**/*"] else install << Dir[pattern] end end install.flatten! # Transfer all the files and symlinks into the working directory... install = install.select { |x| File.file?(x) or File.symlink?(x) or Pkg::Util::File.empty_dir?(x) } install.each do |file| if Pkg::Util::File.empty_dir?(file) FileUtils.mkpath(File.join(workdir, file), :verbose => false) else FileUtils.mkpath(File.dirname(File.join(workdir, file)), :verbose => false) FileUtils.cp(file, File.join(workdir, file), :verbose => false, :preserve => true) end end end Pkg::Util::Version.versionbump(workdir) if Pkg::Config.update_version_file end |
.load_extras(temp_directory) ⇒ Object
The load_extras method is intended to load variables from the extra yaml file downloaded by the pl:fetch task. The goal is to be able to augment/override settings in the source project’s build_data.yaml and project_data.yaml with Puppet Labs-specific data, rather than having to clutter the generic tasks with data not generally useful outside the PL Release team
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/packaging/util/file.rb', line 195 def load_extras(temp_directory) unless ENV['PARAMS_FILE'] && ENV['PARAMS_FILE'] != '' raise "load_extras requires a directory containing extras data" if temp_directory.nil? Pkg::Config.config_from_yaml("#{temp_directory}/#{Pkg::Config.builder_data_file}") # Environment variables take precedence over those loaded from configs, # so we make sure that any we clobbered are reset. Pkg::Config.load_envvars end end |
.mktemp ⇒ Object Also known as: get_temp
15 16 17 18 19 |
# File 'lib/packaging/util/file.rb', line 15 def mktemp mktemp = Pkg::Util::Tool.find_tool('mktemp', :required => true) stdout, = Pkg::Util::Execution.capture3("#{mktemp} -d -t pkgXXXXXX") stdout.strip end |
.untar_into(source, target = nil, options = "") ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/packaging/util/file.rb', line 81 def untar_into(source, target = nil, = "") tar = Pkg::Util::Tool.find_tool('tar', :required => true) # We only accept a writable directory as a target if target and !target.empty? and file_writable?(target) and File.directory?(target) target_opts = "-C #{target}" end if file_exists?(source, :required => true) stdout, = Pkg::Util::Execution.capture3(%(#{tar} #{} #{target_opts} -xf #{source})) stdout end end |