class Litbuild::SourceCodeManager
Find all of the tar files and patch files, possibly compressed, present in any of the directories passed to the constructor or any direct subdirectories of those directories. Given a package blueprint, provide suitable commands to unpack tar files and in-tree source tarfiles, apply patch files, or copy those files to a package user home directory.
Note, SourceCodeManager
does not recurse into additional levels of subdirectories – it turns out that makes the test suite really slow.
Constants
- DECOMPRESSORS
Public Class Methods
# File lib/litbuild/source_code_manager.rb, line 18 def initialize(*dirs) all_pkgfiles = dirs.map do |d| Dir.glob("#{d}/*.tar*") + Dir.glob("#{d}/*/*.tar*") + Dir.glob("#{d}/*.patch*") + Dir.glob("#{d}/*/*.patch*") end.flatten abs_paths = all_pkgfiles.map { |f| File.expand_path(f) } @available_files = abs_paths.sort.uniq end
Public Instance Methods
Package
Users expect to have tarfiles for the top-level package and any in-tree packages in their `src` directory, and patches in their `patches` directory. This produces commands that copy needed files from the TARFILE_DIR and PATCH_DIR to those directories, if not already present there.
# File lib/litbuild/source_code_manager.rb, line 63 def copy_source_files_commands(package) pkgusr = pkgusr_name(package) mkdir_commands = %w[src patches].map do |dir| "mkdir -p ~#{pkgusr}/#{dir}" end copy_commands = [] package.tar_files_needed.each do |filename| unless pkgusr_file_available?(package, filename) copy_commands << "cp #{find_tarfile(filename)} ~#{pkgusr}/src" end end package.patch_files.each do |filename| unless pkgusr_file_available?(package, filename) copy_commands << "cp #{find_file(filename)} ~#{pkgusr}/patches" end end mkdir_commands + copy_commands.sort end
# File lib/litbuild/source_code_manager.rb, line 33 def intree_untar_commands_for(package) commands = [] package.in_tree.each do |basename, version, path| intree = "#{basename}-#{version}" commands << unpack_tar(intree) if path commands << "mkdir -p #{File.dirname(path)}" commands << "mv #{intree} #{path}" else commands << "mv #{intree} #{basename}" end end commands end
# File lib/litbuild/source_code_manager.rb, line 48 def patch_commands_for(package) return [] unless package.patch_files package.patch_files.map do |patch_file| full_fn = find_file(patch_file) "#{decompress_command(full_fn)} < #{full_fn} | patch -p1" end end
# File lib/litbuild/source_code_manager.rb, line 29 def untar_command_for(package) unpack_tar(package.name_and_version) end
Private Instance Methods
# File lib/litbuild/source_code_manager.rb, line 119 def decompress_command(filename) decompressor = decompressor_for_file(filename) decompressor ? "#{decompressor} -d" : 'cat' end
# File lib/litbuild/source_code_manager.rb, line 114 def decompressor_for_file(file) ext = DECOMPRESSORS.keys.find { |e| file =~ /#{e}$/ } ext ? DECOMPRESSORS[ext] : nil end
# File lib/litbuild/source_code_manager.rb, line 94 def find_file(filename) @available_files.detect { |f| /#{filename}/ =~ f } || raise(Litbuild::MissingSource, "File #{filename} is needed but not available.") end
# File lib/litbuild/source_code_manager.rb, line 100 def find_tarfile(filename) filename = "#{filename}.tar" unless filename.end_with?('.tar') find_file(filename) end
# File lib/litbuild/source_code_manager.rb, line 137 def homedir(package) Dir.home(pkgusr_name(package)) rescue ArgumentError nil end
# File lib/litbuild/source_code_manager.rb, line 84 def pkgusr_file_available?(package, filename) pkghome = homedir(package) return false unless pkghome dir = filename.match?(/.tar$/) ? 'src' : 'patches' possible_names = DECOMPRESSORS.keys.map { |ext| "#{filename}.#{ext}" } possible_names << filename possible_names.any? { |f| File.exist?(File.join(pkghome, dir, f)) } end
# File lib/litbuild/source_code_manager.rb, line 129 def pkgusr_name(package) pkgusr = package['package-user'].first name = pkgusr['name'].first raise(InvalidDirective, 'package-user missing name') unless name name end
# File lib/litbuild/source_code_manager.rb, line 124 def tar_decompress_opt(tarfile) decompressor = decompressor_for_file(tarfile) decompressor ? "--use-compress-program=#{decompressor}" : '' end
# File lib/litbuild/source_code_manager.rb, line 105 def unpack_tar(basename) tarfile = find_tarfile(basename) decompress_opt = tar_decompress_opt(tarfile) "tar -x #{decompress_opt} -f #{tarfile}" end