File gem2rpm of Package obs-service-gem2rpm
#!/usr/bin/ruby
require 'optparse'
require 'ostruct'
require 'pathname'
require 'fileutils'
require 'open3'
# Defines the script's behavior based on parsed arguments and then executes a command.
def parse_args(args)
options = OpenStruct.new(noop: 'disable')
OptionParser.new do |opts|
opts.banner = "Usage: #{$PROGRAM_NAME} [options]\nConvert ruby Gems to source RPMs and specfiles. Uses a template to generate the RPM specfile from the Gem spec"
opts.on("-g", "--gem [FILE_OR_NAME]", "The path to locally stored gem package file or the name of the gem when using --fetch.") do |g|
options.gem = g
end
opts.on("--config [FILE]", "Path to gem2rpm.yaml") do |c|
options.config = c
end
opts.on("--fetch [ENABLE]", "Fetch the gem from rubygems.org") do |f|
options.fetch = f
end
opts.on("--no-doc [ENABLE]", "Disable document subpackage") do |nd|
options.no_doc = nd
end
opts.on("-n", "--nongem [ENABLE]", "Generate a subpackage for non-gem use") do |ng|
options.nongem = ng
end
opts.on("--noop [MODE]", ['enable', 'disable'], "Do nothing (enable or disable). Default: disable") do |mode|
options.noop = mode
end
opts.on("-d", "--dependencies [ENABLE]", "Print the dependencies of the gem") do |d|
options.dependencies = d
end
opts.on("-t", "--template [TEMPLATE]", "Use TEMPLATE for the specfile") do |t|
options.template = t
end
opts.on("-l", "--local [ENABLE]", "Do not retrieve Gem information over the network. Use on disconnected machines") do |l|
options.local = l
end
opts.on("-s", "--srpm [ENABLE]", "Create a source RPM") do |s|
options.srpm = s
end
opts.on("-o", "--output [FILE]", "Send the specfile to FILE") do |o|
options.output = o
end
opts.on("--outdir [DIRECTORY]", "Output directory") do |od|
options.outdir = od
end
opts.on("-T", "--current-template [ENABLE]", "Print the current template") do |ct|
options.current_template = ct
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!(args)
options
end
# --- Main logic ---
# Parse the command line arguments provided to the script (ARGV)
dc = parse_args(ARGV)
# If noop is enabled, exit immediately
if dc.noop == 'enable'
exit(0)
end
# Build up the command for the external process (gem2rpm)
args = ['gem2rpm']
# Handle the --gem argument and globs
if dc.gem
if dc.fetch
args << dc.gem
args << '--fetch'
else
glob_paths = Dir.glob(dc.gem)
if glob_paths.any?
args << File.absolute_path(glob_paths.first)
end
end
end
if dc.config
glob_paths = Dir.glob(dc.config)
if glob_paths.any?
args << '--config'
args << File.absolute_path(glob_paths.first)
end
end
# Append simple flag arguments if they are "enabled"
args << '--no-doc' if dc.no_doc == 'enable'
args << '--nongem' if dc.nongem == 'enable'
args << '--dependencies' if dc.dependencies == 'enable'
args << '--local' if dc.local != 'disable'
args << '--srpm' if dc.srpm == 'enable'
args << '--current-template' if dc.current_template == 'enable'
# Append key-value arguments if the value is present
if dc.template
args << '--template'
args << dc.template
end
if dc.output
args << '--output'
args << dc.output
end
# Change directory if outdir is specified
if dc.outdir
# Ensure the directory exists before changing into it
FileUtils.mkdir_p(dc.outdir) unless Dir.exist?(dc.outdir)
Dir.chdir(dc.outdir)
end
# Execute the external command (equivalent to subprocess.run(args))
puts "Executing command: #{args.join(' ')}"
# Open3.popen2e is useful for capturing stdout and stderr, or simply use system() for direct execution.
# Using system() for simplicity here to mimic the Python script's direct execution.
system(*args)