File cleanup_abi_statement.rb of Package packaging-apparmor
#!/usr/bin/ruby
#
# Copyright (C) 2021 darix
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3. This program is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details. You should have received a
# copy of the GNU Affero General Public License along with this program. If
# not, see <https://www.gnu.org/licenses/>
#
require 'fileutils'
require 'optparse'
VERSION = "0.0.3"
begin
require 'diffy'
DEBUG = true
rescue LoadError
DEBUG = false
end
opts = OptionParser.new("Usage: #{$0}")
rest = opts.permute(ARGV)
aa_version = Gem::Version.new(0)
IO.popen("/sbin/apparmor_parser --version") do |io|
output = io.read
if mo=/^AppArmor parser version (?<version>\S+)/.match(output)
aa_version = Gem::Version.new(mo[:version])
else
STDERR.puts "Can not determine apparmor version. Exiting."
exit(1)
end
end
puts "Running version #{VERSION} in AppArmor #{aa_version} mode"
abi_regexp = /abi <abi\/4\.0>,\n\n/
run_regexp = /@{run}/
def diff_and_write(filename, old, new)
if DEBUG
puts Diffy::Diff.new(old, new, :context=>3)
end
File.write(filename, new)
end
FileUtils.mkdir_p('local')
ARGV.each do |filename|
next unless File.file?(filename)
puts "Patching #{filename}"
fc=File.read(filename)
if aa_version >= Gem::Version.new(4)
# nothing to be done here yet
elsif aa_version >= Gem::Version.new(3)
fc_patched = fc.gsub(abi_regexp, "abi <abi/3.0>,\n\n")
diff_and_write(filename, fc, fc_patched)
elsif aa_version >= Gem::Version.new(2)
fc_patched = fc.gsub(abi_regexp, '')
fc_patched = fc_patched.gsub(run_regexp, '/run')
diff_and_write(filename, fc, fc_patched)
end
fc.each_line do |line|
mo=/include (if exists )?<(?<include_path>local[^>]+)>/.match(line)
if mo
# includes << mo[:include_path]
puts "Creating include path #{mo[:include_path]}"
include_dir = File.dirname(mo[:include_path])
if ! File.directory?(include_dir)
puts "Include Directory missing #{include_dir}"
FileUtils.mkdir_p(include_dir)
end
FileUtils.touch(mo[:include_path])
end
end
end