Skip to content

Instantly share code, notes, and snippets.

@zinovyev
Created November 8, 2018 14:21
Show Gist options
  • Save zinovyev/0ad6bd54ac5a9f7b709607a8151a0334 to your computer and use it in GitHub Desktop.
Save zinovyev/0ad6bd54ac5a9f7b709607a8151a0334 to your computer and use it in GitHub Desktop.
Convert rpm package to Archlinux package (makepkg -s command required afterwards)
#!/usr/bin/env ruby
require "fileutils"
class Package
FILTERED_PARTS = %w[rpm x86_64]
INSTALL_SECTIONS = %w[pre_install post_install pre_upgrade
post_upgrade pre_remove post_remove].freeze
RPM_INSTALL_SECTIONS = %w[preinstall postinstall preupgrade
postupgrade preuninstall postuninstall].freeze
attr_accessor :path_to_rpm, :pwd
def initialize(path_to_rpm)
@path_to_rpm = path_to_rpm
@pwd = Dir.pwd
end
def run
create_dir
copy_rpm_file
create_pkgbuild
scripts = extract_scripts
create_install_script(scripts)
# Extract install scripts from rpm
# Create install script
end
def pkgbuild
<<-PKGBUILD.gsub(/\ {6}/, "")
pkgname=#{package_name}
pkgver=unknown
pkgrel=1
epoch=
pkgdesc=""
arch=("x86_64")
url=""
license=('Unknown')
groups=()
depends=()
makedepends=()
checkdepends=()
optdepends=()
provides=()
conflicts=()
replaces=()
backup=()
options=()
install="#{package_name}.install"
changelog=
source=()
noextract=()
md5sums=()
validpgpkeys=()
prepare() {
cp ../#{rpm_basename} $srcdir/
}
package() {
rpm2cpio #{rpm_basename} | cpio -idmv -D $pkgdir/
}
PKGBUILD
end
def extract_scripts
Dir.chdir(package_name)
rpm_scripts = %x(rpm -qp --scripts #{rpm_basename})
rpm_scripts = rpm_scripts.encode!("UTF-8", "UTF-8", invalid: :replace)
Dir.chdir(pwd)
parse_blocks(rpm_scripts)
end
def parse_blocks(rpm_scripts)
current_block = nil
rpm_scripts.split("\n").each_with_object({}) do |line, blocks|
if section = find_section_in_line(line)
blocks[section] = []
current_block = blocks[section]
elsif current_block
current_block << line
end
end
end
def find_section_in_line(line)
RPM_INSTALL_SECTIONS.each_with_index do |section, idx|
return INSTALL_SECTIONS[idx] if line =~ /#{section}/
end && nil
end
def create_install_script(scripts)
File.open(install_script_path, "w") do |f|
install_script = scripts.map do |function_name, code|
<<-CODE.gsub(/\ {8}/, "")
#{function_name}() {
#{code.join("\n")}
}
CODE
end.join("\n")
f.write(install_script)
end
end
def create_pkgbuild
File.open(pkgbuild_path, "w") do |f|
f.write(pkgbuild)
end
end
def copy_rpm_file
FileUtils.cp(rpm_basename, "#{package_name}/")
end
def create_dir
Dir.mkdir(package_name) unless Dir.exists?(package_name)
end
def rpm_basename
@_rpm_basename ||= File.basename(@path_to_rpm)
end
def install_script_path
"#{package_name}/#{package_name}.install"
end
def pkgbuild_path
"#{package_name}/PKGBUILD"
end
def package_name
@_package_name ||= begin
rpm_basename.split(".").reject do |p|
FILTERED_PARTS.include?(p.to_s)
end.join("_")
end
end
end
package = Package.new(ARGV[0])
puts "Basename: #{package.rpm_basename}"
puts "Package_name: #{package.package_name}"
puts "RUN: #{package.run}"
@ThalesMarcel
Copy link

Do I need to install any packages before running this script?

Can I save it to /usr/bin (for example) and create a symbolic link to execute it?

@clemencyworld1
Copy link

How do you run the script

@circonfl3x
Copy link

How do you run the script

After downloading the file, use an archiving application (if you have) to extract it, or go to terminal and type unrar filename.zip.Then make it executable... You can right click on the file, select properties, go to permissions then check "allow executing file as a program", or you can use chmod 775 filename.extension or chmod a+x filename.extension or chmod +x filename.extension.

Next, execute the script using ./scriptname.extension and for the argument point it to the file's location. So for example, I have my script stored in /home/user/ and the file I want to install is at /home/download, and my script is called rpmtoarch. I'd do ./rpmtoarch /home/user/Downloads/rpmfile.rpm (just an example). Although Im a year late hope this helped you or any other future user stuck around here :)

@bigbruno
Copy link

bigbruno commented Aug 3, 2021

Thanks for making your script available, I made some modifications, including some features and making the installation of .rpm packages very simple, is that ok for you? Is the license for this script GPL v2 or something similar?

Here is the derivation I made:
https://gitlab.com/biglinux/rpmtoarch

@Lightwel
Copy link

This comment @bigbruno
Happy Christmas!

Tried to download your repo and build but my way fails.

$ makepkg -sfi ==> Making package: rpmtoarch 1.0.0_1big-3 (Sun 26 Dec 2021 08:57:43 GMT) ==> Checking runtime dependencies... ==> Checking buildtime dependencies... ==> Retrieving sources... ==> ERROR: /home/[....]/Downloads/Git/rpmtoarch/rpmtoarch is not a clone of https://gitlab.com/biglinux/rpmtoarch.git Aborting...
Same error with just $ makepkg

I've looked at the file structure but don't understand why it includes a second rpmtoarch directory and a src directory. No help in README.MD

I would like to build and install - how do I do this?

Many thanks

R

@bigbruno
Copy link

I'm back to github: https://github.com/biglinux/rpmtoarch

All the makepkg really do is copy to same folder in system like /usr/bin/rpmtoarch and add dependencies

@attila123
Copy link

@bigbruno could you please kindly provide clear step-by-step instructions, please see biglinux/rpmtoarch#2

@asmuiahmad
Copy link

thank you very much!
who made this..
just save my life!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment