11# frozen_string_literal: true
22
3+ require "English"
34require "debug"
45
56module Pgpm
67 module Deb
78 class Builder
8-
99 def initialize ( spec )
1010 @spec = spec
11- @container_name = "pgpm-debian_build-#{ Time . now . to_i } _#{ rand ( 10000 ) } "
12- @pgpm_dir = Dir . mktmpdir
11+ @container_name = "pgpm-debian_build-#{ Time . now . to_i } _#{ rand ( 10_000 ) } "
12+ @pgpm_dir = Dir . mktmpdir
1313 end
1414
1515 def build
@@ -47,16 +47,16 @@ def prepare_versioned_source
4747
4848 fn = nil
4949 @spec . sources . map do |src |
50- srcfile = File . join ( " #{ @pgpm_dir } " , src . name )
50+ srcfile = File . join ( @pgpm_dir . to_s , src . name )
5151 File . write ( srcfile , src . read )
5252 fn = src . name
5353 end
5454
5555 system ( "tar -xf #{ @pgpm_dir } /#{ fn } -C #{ @pgpm_dir } /source-versioned/" )
5656 FileUtils . remove ( "#{ @pgpm_dir } /#{ fn } " )
5757
58- untar_dir_entries = Dir . entries ( "#{ @pgpm_dir } /source-versioned/" ) . select do |entry |
59- ! ( [ "." , ".." ] . include? ( entry ) )
58+ untar_dir_entries = Dir . entries ( "#{ @pgpm_dir } /source-versioned/" ) . reject do |entry |
59+ [ "." , ".." ] . include? ( entry )
6060 end
6161
6262 if untar_dir_entries . size == 1
@@ -68,11 +68,10 @@ def prepare_versioned_source
6868 end
6969 end
7070
71- [ "prepare_artifacts.sh" ] . each do |fn |
72- script_fn = File . expand_path ( "#{ __dir__ } /scripts/#{ fn } " )
71+ [ "prepare_artifacts.sh" ] . each do |f |
72+ script_fn = File . expand_path ( "#{ __dir__ } /scripts/#{ f } " )
7373 FileUtils . cp script_fn , "#{ @pgpm_dir } /source-versioned/"
7474 end
75-
7675 end
7776
7877 def prepare_default_source
@@ -107,22 +106,22 @@ def pull_image
107106 puts "Checking if podman image exists..."
108107 # Check if image exists
109108 system ( "podman image exists #{ image_name } " )
110- if $? . to_i > 0 # image doesn't exist -- pull image from a remote repository
109+ if $CHILD_STATUS . to_i . positive? # image doesn't exist -- pull image from a remote repository
111110 puts " No. Pulling image #{ image_name } ..."
112111 system ( "podman pull #{ image_name } " )
113112 else
114113 puts " Yes, image #{ image_name } already exists! OK"
115114 end
116115 end
117116
118- def generate_deb_src_files ( pkg_type = :versioned )
117+ def generate_deb_src_files ( pkg_type = :versioned )
119118 puts "Generating debian files..."
120119 Dir . mkdir "#{ @pgpm_dir } /source-#{ pkg_type } /debian"
121- [ : changelog, : control, : copyright, : files, : rules] . each do |f |
120+ %i[ changelog control copyright files rules ] . each do |f |
122121 puts " -> #{ @pgpm_dir } /source-#{ pkg_type } /debian/#{ f } "
123122 File . write "#{ @pgpm_dir } /source-#{ pkg_type } /debian/#{ f } " , @spec . generate ( f , pkg_type )
124123 end
125- File . chmod 0740 , "#{ @pgpm_dir } /source-#{ pkg_type } /debian/rules" # rules file must be executable
124+ File . chmod 0o740 , "#{ @pgpm_dir } /source-#{ pkg_type } /debian/rules" # rules file must be executable
126125 end
127126
128127 def start_container
@@ -134,9 +133,9 @@ def start_container
134133
135134 puts " Creating and starting container #{ @container_name } & running pbuilder"
136135 system ( "podman create -it #{ create_opts } " )
137- exit ( 1 ) if $? . to_i > 0
136+ exit ( 1 ) if $CHILD_STATUS . to_i . positive?
138137 system ( "podman start #{ @container_name } " )
139- exit ( 1 ) if $? . to_i > 0
138+ exit ( 1 ) if $CHILD_STATUS . to_i . positive?
140139 end
141140
142141 # Prevents clean-up after pbuilder finishes. There's no option
@@ -154,7 +153,7 @@ def patch_pbuilder
154153 system ( "podman exec #{ @container_name } /bin/bash -c '#{ cmd } '" )
155154 end
156155
157- def run_build ( pkg_type = :versioned )
156+ def run_build ( pkg_type = :versioned )
158157 dsc_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _0-1.dsc"
159158 deb_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _0-1_#{ @spec . arch } .deb"
160159
@@ -166,11 +165,11 @@ def run_build(pkg_type=:versioned)
166165 puts " Building package with pbuilder..."
167166 cmds . each do |cmd |
168167 system ( "podman exec -w /root/pgpm/source-#{ pkg_type } #{ @container_name } /bin/bash -c '#{ cmd } '" )
169- exit ( 1 ) if $? . to_i > 0
168+ exit ( 1 ) if $CHILD_STATUS . to_i . positive?
170169 end
171170 end
172171
173- def copy_build_from_container ( pkg_type = :versioned )
172+ def copy_build_from_container ( pkg_type = :versioned )
174173 puts "Copying .deb file from podman container into current directory..."
175174 deb_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _0-1_#{ @spec . arch } .deb"
176175 deb_copy_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _#{ @spec . arch } .deb"
@@ -203,7 +202,6 @@ def selinux_enabled?
203202 # This returns true or false by itself
204203 system ( "sestatus | grep 'SELinux status' | grep -o 'enabled'" )
205204 end
206-
207205 end
208206 end
209207end
0 commit comments