-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakerpm
executable file
·352 lines (345 loc) · 12.1 KB
/
makerpm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
#################################################################
# makerpm
#
# Make a RedHat-class installer package from source files
#
# Author: DA
# Company: CyberRadio Solutions, Inc.
# Copyright: Copyright (c) 2017 CyberRadio Solutions, Inc. All
# rights reserved.
#################################################################
#################################################################
#
# usage: makerpm {options} [Source folder name]
#
# -v [TAG],--version=[TAG] Use version tag [TAG] rather than
# tagging with the default (either
# today's date or the version number
# from a setup.py/configure.ac file)
# -p [PACKAGE],--package=[PACKAGE] Use PACKAGE as the package name
# instead of basing the package name
# off of the source folder name
# -n,--no-cleanup Do not clean up intermediate files
# after making the package
#
# Source folder MUST contain an RPM spec file with package building
# information. It is assumed that each source file folder will contain its
# own spec file.
#
# The makerpm script can automatically determine package name and version
# number from a variety of sources. Sources are checked in the following
# order:
# (1) Version information file (version.txt)
# (2) Python distutils setup file (setup.py)
# (3) GNU Autotools configuration file (configure.ac)
# (4) CMake lists file (CMakeLists.txt)
#
#################################################################
print_usage()
{
echo "usage: $0 {options} [Source folder name]"
echo " "
echo "-v [TAG],--version=[TAG] Use version tag [TAG] rather than "
echo " tagging with the default (either "
echo " today's date or the version number "
echo " from a setup.py/configure.ac file)"
echo "-p [PACKAGE],--package=[PACKAGE] Use PACKAGE as the package name "
echo " instead of basing the package name "
echo " off of the source folder name"
echo "-n,--no-cleanup Do not clean up intermediate files "
echo " after making the package"
echo " "
echo "Source folder MUST contain an RPM spec file with package building "
echo "information. It is assumed that each source file folder will contain "
echo "its own spec file."
echo " "
echo "The makerpm script can automatically determine package name and version"
echo "number from a variety of sources. Sources are checked in the following"
echo "order:"
echo "(1) Version information file (version.txt)"
echo "(2) Python distutils setup file (setup.py)"
echo "(3) GNU Autotools configuration file (configure.ac)"
echo "(4) CMake lists file (CMakeLists.txt)"
echo " "
}
exit_with_usage()
{
# $1: Exit code
# $2: Error message to display
echo "ERROR: $2"
echo " "
print_usage
exit $1
}
exit_if_error()
{
# $1: Exit code from prior command
if [ $1 -ne 0 ]
then
echo "ERROR: Command returned error code: $1"
echo "ERROR condition detected. Exiting script."
exit $1
fi
}
#########################################################
# Set options and variables
#########################################################
# SRC_FOLDER_NAME: Source folder name
# RPM_PKG_NAME: RPM package name
# RPM_PKG_VERSION: RPM package version
# RPM_PKG_ARCH: RPM package architecture
# RPM_PKG_ARCH_GNU: RPM package GNU system type
# RPM_PKG_OS: RPM package OS name
# RPM_PKG_OS_VER: RPM package OS (major) version number
# CLEANUP: Whether to do cleanup (1) or not (0)
RPM_PKG_VERSION=
RPM_PKG_ARCH=$(arch)
RPM_PKG_ARCH_GNU=$(arch)
# How we determine Package OS depends on the OS
if [ -e /etc/centos-release ]
then
RPM_PKG_TEMP=( $(cut /etc/centos-release -d' ' -f1-) )
RPM_PKG_OS=$(echo ${RPM_PKG_TEMP[0]} | tr '[:upper:]' '[:lower:]')
RPM_PKG_OS_VER=$(echo ${RPM_PKG_TEMP[3]} | cut -d'.' -f1)
elif [ -e /etc/redhat-release ]
then
RPM_PKG_TEMP=( $(cut /etc/redhat-release -d' ' -f1-) )
RPM_PKG_OS=$(echo ${RPM_PKG_TEMP[0]}${RPM_PKG_TEMP[1]} | tr '[:upper:]' '[:lower:]')
RPM_PKG_OS_VER=$(echo ${RPM_PKG_TEMP[6]} | cut -d'.' -f1)
else
RPM_PKG_OS=$(lsb_release -si | tr '[:upper:]' '[:lower:]')
RPM_PKG_OS_VER=$(lsb_release -sr | cut -d'.' -f1)
fi
CLEANUP=1
# Parse command-line options
set -- `getopt -n $0 -o hv:p:n -l "help version: package: no-cleanup" -- "$@"`
while [ $# -gt 0 ]
do
case "$1" in
-v|--version)
RPM_PKG_VERSION=$(eval echo $2)
shift
;;
-p|--package)
RPM_PKG_NAME=$(eval echo $2)
shift
;;
-n|--no-cleanup)
CLEANUP=0
;;
-h|--help)
print_usage
exit 2
;;
-*)
break
;;
*)
SRC_FOLDER_NAME=$1
break
;;
esac
shift
done
# Get the source folder name from the command line, and make sure
# that there is no trailing slash (from bash command line completion
# or some other source)
if [ $# -gt 1 ]
then
SRC_FOLDER_NAME=$(eval echo $2)
SRC_FOLDER_NAME=${SRC_FOLDER_NAME%/}
# Sanity check: Make sure source folder provided is actually a folder
if [ ! -d "${SRC_FOLDER_NAME}" ]
then
echo "ERROR: Source folder name is not a directory or does not exist."
exit_if_error 1
fi
fi
# If the source folder contains a version.txt file, extract the package
# name and package version number from the file, if the user didn't
# override them on the command line
if [ -e ${SRC_FOLDER_NAME}/version.txt ]
then
QQQ=$(< ${SRC_FOLDER_NAME}/version.txt )
exit_if_error $?
eval ZZZ=(${QQQ})
if [ -z ${RPM_PKG_NAME} ]
then
RPM_PKG_NAME=${ZZZ[0]}
fi
if [ -z ${RPM_PKG_VERSION} ]
then
RPM_PKG_VERSION=${ZZZ[1]}
fi
# Or, if the source folder contains a setup.py file, use the
# package name (prepending "python-") and package version
# number provided by the setup.py file, if the user didn't
# override them on the command line
elif [ -e ${SRC_FOLDER_NAME}/setup.py ]
then
QQQ=$(python ${SRC_FOLDER_NAME}/setup.py --name --version)
exit_if_error $?
eval ZZZ=(${QQQ})
if [ -z ${RPM_PKG_NAME} ]
then
RPM_PKG_NAME=python-${ZZZ[0]}
fi
if [ -z ${RPM_PKG_VERSION} ]
then
RPM_PKG_VERSION=${ZZZ[1]}
fi
# Or, if the source folder contains a configure.ac file, use the
# package name and package version number provided by the AC_INIT line
# in the setup.py file, if the user didn't override them on the command
# line
elif [ -e ${SRC_FOLDER_NAME}/configure.ac ]
then
QQQ=$($(grep '^AC_INIT' ${SRC_FOLDER_NAME}/configure.ac | sed -e 's/AC_INIT(//g' -e 's/)//g' -e 's/,//g'))
exit_if_error $?
eval ZZZ=(${QQQ})
if [ -z ${RPM_PKG_NAME} ]
then
RPM_PKG_NAME=${ZZZ[0]}
fi
if [ -z ${RPM_PKG_VERSION} ]
then
RPM_PKG_VERSION=${ZZZ[1]}
fi
# Or, if the source folder contains a CMakeLists.txt file, determine
# the project name and project version from CMake cache variables, if
# the user didn't override them on the command line.
# NOTE: The CMakeLists.txt file needs to be set up so that PROJECT_NAME
# and PROJECT_VERSION are defined as cache variables.
elif [ -e ${SRC_FOLDER_NAME}/CMakeLists.txt ]
then
CMAKE_SRC=$(readlink -f ${SRC_FOLDER_NAME})
rm -rf /tmp/makerpm
mkdir -p /tmp/makerpm
pushd /tmp/makerpm >/dev/null 2>&1
cmake ${CMAKE_SRC} -L >cmake_results.txt
if [ -z ${RPM_PKG_NAME} ]
then
ZZZ=$(grep 'PROJECT_NAME' cmake_results.txt)
#exit_if_error $?
RPM_PKG_NAME=${ZZZ##PROJECT_NAME:STRING=}
fi
if [ -z ${RPM_PKG_VERSION} ]
then
ZZZ=$(grep 'PROJECT_VERSION' cmake_results.txt)
#exit_if_error $?
RPM_PKG_VERSION=${ZZZ##PROJECT_VERSION:STRING=}
fi
popd >/dev/null 2>&1
rm -rf /tmp/makerpm
fi
# If the user did not specify a package name on the command line,
# and if the package name cannot be obtained from some other source,
# form the package name from the source folder name.
if [ -z ${RPM_PKG_NAME} ]
then
RPM_PKG_NAME=$(basename "${SRC_FOLDER_NAME}")
fi
# If the user did not specify a version number, and if
# the version number cannot be obtained by some other
# source, then use today's date for the version number
if [ -z ${RPM_PKG_VERSION} ]
then
RPM_PKG_VERSION=$(eval date +%y.%m.%d)
fi
# Try to make the provided package name and version conform to RPM
# naming conventions.
# NOTE -- doing this may still result in a failure if the package
# name cannot be made compliant!
RPM_PKG_NAME=$(echo "${RPM_PKG_NAME}" | tr "[:upper:]" "[:lower:]" | tr "_" "-")
RPM_PKG_VERSION=$(echo "${RPM_PKG_VERSION}" | tr "[:upper:]" "[:lower:]" | tr -d "_-")
#########################################################
# Build the RPM package
#########################################################
# Make sure the source folder has been specified
if [ -z ${SRC_FOLDER_NAME} ]
then
exit_with_usage 2 "Source folder was not provided"
else
echo "RPM package parameters:"
echo "* Source folder name: ${SRC_FOLDER_NAME}"
echo "* RPM package name: ${RPM_PKG_NAME}"
echo "* RPM package version: ${RPM_PKG_VERSION}"
echo "* RPM package architecture: ${RPM_PKG_ARCH}"
echo "* RPM package GNU system type: ${RPM_PKG_ARCH_GNU}"
echo "* RPM package OS: ${RPM_PKG_OS}"
echo "* RPM package OS version: ${RPM_PKG_OS_VER}"
echo " "
# Create temporary build folder
RPM_BUILD_FOLDER_NAME=${RPM_PKG_NAME}-${RPM_PKG_VERSION}
echo "Creating temporary build folder: ${RPM_BUILD_FOLDER_NAME}"
if [ -e ${RPM_BUILD_FOLDER_NAME} ]
then
rm -rf ${RPM_BUILD_FOLDER_NAME}
exit_if_error $?
fi
mkdir -p ${RPM_BUILD_FOLDER_NAME}
exit_if_error $?
# Copy files from source folder into build folder
echo "Copying files: ${SRC_FOLDER_NAME} ==> ${RPM_BUILD_FOLDER_NAME}"
cp -rf ${SRC_FOLDER_NAME}/* ${RPM_BUILD_FOLDER_NAME}
exit_if_error $? "In copying files"
cp -rf ${SRC_FOLDER_NAME}/.[1-9a-zA-Z]* ${RPM_BUILD_FOLDER_NAME}
# Identify the spec file
RPM_SPEC_FILE_LIST=( $(find ${RPM_BUILD_FOLDER_NAME} -name "*.spec") )
RPM_SPEC_FILE=${RPM_SPEC_FILE_LIST[0]}
# Replace RPM_PKG_* placeholders in the spec file with our parameters,
# and store the result in a new spec file in the current directory
RPM_TARGET_SPEC_FILE=$(basename ${RPM_SPEC_FILE})
echo "Versioning RPM package: ${RPM_PKG_NAME}"
sed -e '/^\s*#\([^!]\|$\)/ d' \
-e "s/RPM_PKG_NAME/${RPM_PKG_NAME}/g" \
-e "s/RPM_PKG_VERSION/${RPM_PKG_VERSION}/g" \
-e "s/RPM_PKG_ARCH_GNU/${RPM_PKG_ARCH_GNU}/g" \
-e "s/RPM_PKG_ARCH/${RPM_PKG_ARCH}/g" \
-e "s/RPM_PKG_OS_VER/${RPM_PKG_OS_VER}/g" \
-e "s/RPM_PKG_OS/${RPM_PKG_OS}/g" \
${RPM_SPEC_FILE} > ${RPM_TARGET_SPEC_FILE}
exit_if_error $? "In versioning file ${RPM_SPEC_FILE}"
# Read the spec file to get the source code archive name
RPM_SOURCE_ARCHIVE=$(grep 'Source: ' ${RPM_TARGET_SPEC_FILE})
exit_if_error $? "In getting source code archive name"
RPM_SOURCE_ARCHIVE=${RPM_SOURCE_ARCHIVE/Source: /}
# Build the source code archive
echo "Building source code archive: ${RPM_SOURCE_ARCHIVE}"
tar zcf ${RPM_SOURCE_ARCHIVE} ${RPM_BUILD_FOLDER_NAME}
exit_if_error $? "In building source code archive ${RPM_SOURCE_ARCHIVE}"
echo "Removing build folder: ${RPM_BUILD_FOLDER_NAME}"
rm -rf ${RPM_BUILD_FOLDER_NAME}
exit_if_error $?
# Execute rpmbuild in the current folder, using the target spec file
# and the source code archive
echo "Building RPM package"
rpmbuild --define "_topdir $(pwd)" \
--define "_builddir $(pwd)" \
--define "_rpmdir $(pwd)" \
--define "_sourcedir $(pwd)" \
--define "_specdir $(pwd)" \
--define "_srcrpmdir $(pwd)" \
--define "_buildrootdir $(pwd)" \
-bb ${RPM_TARGET_SPEC_FILE}
exit_if_error $?
# Move the generated RPM files from <architecture> folder to
# the current folder, then remove <architecture> folder
mv ${RPM_PKG_ARCH}/*.rpm .
rm -rf ${RPM_PKG_ARCH}
# Clean up
if [ ${CLEANUP} -eq 1 ]
then
echo "Cleaning up: ${RPM_PKG_NAME}"
# Source code archive
rm -rf ${RPM_SOURCE_ARCHIVE}
# Target spec file
rm -rf ${RPM_TARGET_SPEC_FILE}
# Build folder
rm -rf ${RPM_BUILD_FOLDER_NAME}
else
echo "Not cleaning up at user request"
fi
echo "PACKAGE BUILD COMPLETE"
fi