-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakedeb
executable file
·361 lines (354 loc) · 12.1 KB
/
makedeb
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
352
353
354
355
356
357
358
359
360
#!/bin/bash
#################################################################
# makedeb
#
# Make a Debian installer package from source files
#
# Author: DA
# Company: CyberRadio Solutions, Inc.
# Copyright: Copyright (c) 2015 CyberRadio Solutions, Inc. All
# rights reserved.
#################################################################
#################################################################
#
# usage: makedeb {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
# -s [SUBGROUP],--subgroup=[SUBGROUP] For source folders that support
# subgroups, build only subgroup
# SUBGROUP instead of everything
# -n,--no-cleanup Do not clean up intermediate files
# after making the package
#
# Source folder MUST contain a "debian" sub-folder with package building
# information.
#
# The makedeb 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 "-x [INFO],--extra=[INFO] Extra information to append to the "
echo " version tag"
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 "-s [SUBGROUP],--subgroup=[SUBGROUP] For source folders that support "
echo " subgroups, build only subgroup "
echo " SUBGROUP instead of everything"
echo "-n,--no-cleanup Do not clean up intermediate files "
echo " after making the package"
echo " "
echo "Source folder MUST contain a \"debian\" sub-folder with package building "
echo "information."
echo " "
echo "The makedeb 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
# DEB_PKG_NAME: Debian package name
# DEB_PKG_VERSION: Debian package version
# DEB_PKG_ARCH: Debian package architecture
# DEB_PKG_ARCH_GNU: Debian package GNU system type
# DEB_PKG_OS: Debian package OS name
# DEB_PKG_OS_VER: Debian package OS version number
# CLEANUP: Whether to do cleanup (1) or not (0)
# DEB_PKG_SUBGROUP: Subgroup of code to be included in package
DEB_PKG_VERSION=
DEB_PKG_EXTRA=
DEB_PKG_ARCH=$(dpkg-architecture -qDEB_BUILD_ARCH)
DEB_PKG_ARCH_GNU=$(dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_PKG_OS=$(lsb_release -si)
DEB_PKG_OS_VER=$(lsb_release -sr)
CLEANUP=1
DEB_PKG_SUBGROUP=ALL
# Parse command-line options
set -- `getopt -n $0 -o hv:x:p:s:n -l "help version: extra: package: subgroup: no-cleanup" -- "$@"`
while [ $# -gt 0 ]
do
case "$1" in
-v|--version)
DEB_PKG_VERSION=$(eval echo $2)
shift
;;
-x|--extra)
DEB_PKG_EXTRA=$(eval echo $2)
shift
;;
-p|--package)
DEB_PKG_NAME=$(eval echo $2)
shift
;;
-s|--subgroup)
DEB_PKG_SUBGROUP=$(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 ${DEB_PKG_NAME} ]
then
DEB_PKG_NAME=${ZZZ[0]}
fi
if [ -z ${DEB_PKG_VERSION} ]
then
DEB_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 ${DEB_PKG_NAME} ]
then
DEB_PKG_NAME=python-${ZZZ[0]}
fi
if [ -z ${DEB_PKG_VERSION} ]
then
DEB_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 ${DEB_PKG_NAME} ]
then
DEB_PKG_NAME=${ZZZ[0]}
fi
if [ -z ${DEB_PKG_VERSION} ]
then
DEB_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/makedeb
mkdir -p /tmp/makedeb
pushd /tmp/makedeb >/dev/null 2>&1
cmake ${CMAKE_SRC} -L -DPACKAGE_SUBGROUP=${DEB_PKG_SUBGROUP} >cmake_results.txt
if [ -z ${DEB_PKG_NAME} ]
then
ZZZ=$(grep 'PROJECT_NAME' cmake_results.txt)
#exit_if_error $?
DEB_PKG_NAME=${ZZZ##PROJECT_NAME:STRING=}
fi
if [ -z ${DEB_PKG_VERSION} ]
then
ZZZ=$(grep 'PROJECT_VERSION' cmake_results.txt)
#exit_if_error $?
DEB_PKG_VERSION=${ZZZ##PROJECT_VERSION:STRING=}
fi
popd >/dev/null 2>&1
rm -rf /tmp/makedeb
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 ${DEB_PKG_NAME} ]
then
DEB_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 ${DEB_PKG_VERSION} ]
then
DEB_PKG_VERSION=$(eval date +%y.%m.%d)
fi
# Try to make the provided package name conform to Debian naming
# conventions.
# NOTE -- doing this may still result in a failure if the package
# name cannot be made compliant!
DEB_PKG_NAME=$(echo "${DEB_PKG_NAME}" | tr "[:upper:]" "[:lower:]" | tr "_" "-")
# Append the extra info (if any) to the version number
if [ ! -z ${DEB_PKG_EXTRA} ]
then
DEB_PKG_VERSION=${DEB_PKG_VERSION}-${DEB_PKG_EXTRA}
fi
#########################################################
# Build the Debian 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 "Debian package parameters:"
echo "* Source folder name: ${SRC_FOLDER_NAME}"
echo "* Debian package name: ${DEB_PKG_NAME}"
echo "* Debian package version: ${DEB_PKG_VERSION}"
echo "* Debian package architecture: ${DEB_PKG_ARCH}"
echo "* Debian package GNU system type: ${DEB_PKG_ARCH_GNU}"
echo "* Debian package OS: ${DEB_PKG_OS}"
echo "* Debian package OS version: ${DEB_PKG_OS_VER}"
echo "* Debian package subgroup: ${DEB_PKG_SUBGROUP}"
echo " "
# Create temporary build folder
DEB_BUILD_FOLDER_NAME=${DEB_PKG_NAME}-${DEB_PKG_VERSION}
echo "Creating temporary build folder: ${DEB_BUILD_FOLDER_NAME}"
if [ -e ${DEB_BUILD_FOLDER_NAME} ]
then
rm -rf ${DEB_BUILD_FOLDER_NAME}
exit_if_error $?
fi
mkdir -p ${DEB_BUILD_FOLDER_NAME}
exit_if_error $?
# Copy files from source folder into build folder
echo "Copying files: ${SRC_FOLDER_NAME} ==> ${DEB_BUILD_FOLDER_NAME}"
cp -rf ${SRC_FOLDER_NAME}/* ${DEB_BUILD_FOLDER_NAME}
exit_if_error $? "In copying files"
cp -rf ${SRC_FOLDER_NAME}/.[1-9a-zA-Z]* ${DEB_BUILD_FOLDER_NAME}
# For files in debian sub-folder, replace DEB_PKG_* placeholders
# with our parameters
echo "Versioning Debian package: ${DEB_PKG_NAME}"
for FNAME in $(find ${DEB_BUILD_FOLDER_NAME}/debian -type f)
do
#echo "* ${FNAME}"
sed -i -e '/^#[^!]/ d' \
-e "s/DEB_PKG_NAME/${DEB_PKG_NAME}/g" \
-e "s/DEB_PKG_VERSION/${DEB_PKG_VERSION}/g" \
-e "s/DEB_PKG_ARCH_GNU/${DEB_PKG_ARCH_GNU}/g" \
-e "s/DEB_PKG_ARCH/${DEB_PKG_ARCH}/g" \
-e "s/DEB_PKG_OS_VER/${DEB_PKG_OS_VER}/g" \
-e "s/DEB_PKG_OS/${DEB_PKG_OS}/g" \
-e "s/DEB_PKG_SUBGROUP/${DEB_PKG_SUBGROUP}/g" \
${FNAME}
exit_if_error $? "In versioning file ${FNAME}"
done
# Build the Debian package
# -- Note that this Debian package will be an *unsigned* package.
# -- Also note that PYTHONPATH will be honored. This is primarily
# intended to support automated builds in Jenkins, but can easily
# break things, so be VERY careful with this one.
echo "Building Debian package: ${DEB_PKG_NAME}"
cd ${DEB_BUILD_FOLDER_NAME}
debuild --preserve-envvar=PYTHONPATH --no-tgz-check --no-lintian -uc -us
exit_if_error $? "In building package"
cd ..
# Clean up
if [ ${CLEANUP} -eq 1 ]
then
echo "Cleaning up: ${DEB_PKG_NAME}"
for FEXT in build changes dsc tar.gz
do
#echo "rm -f ${DEB_PKG_NAME}_${DEB_PKG_VERSION}*.${FEXT}"
rm -f ${DEB_PKG_NAME}_${DEB_PKG_VERSION}*.${FEXT}
done
rm -rf ${DEB_BUILD_FOLDER_NAME}
else
echo "Not cleaning up at user request"
fi
# Rename Debian packages to reflect OS and GNU system type
echo "Renaming built packages"
for DEBPKG in $(ls *.deb)
do
if [[ "${DEBPKG}" == *"_all.deb" ]]
then
DEBPKGTGT=${DEBPKG/_all.deb/_${DEB_PKG_OS}-${DEB_PKG_OS_VER}-all.deb}
echo "* Arch-independent: ${DEBPKG} ==> ${DEBPKGTGT}"
mv ${DEBPKG} ${DEBPKGTGT}
elif [[ "${DEBPKG}" == *"_${DEB_PKG_ARCH}.deb" ]]
then
DEBPKGTGT=${DEBPKG/_${DEB_PKG_ARCH}.deb/_${DEB_PKG_OS}-${DEB_PKG_OS_VER}-${DEB_PKG_ARCH_GNU}.deb}
echo "* Arch-dependent: ${DEBPKG} ==> ${DEBPKGTGT}"
mv ${DEBPKG} ${DEBPKGTGT}
fi
done
echo "PACKAGE BUILD COMPLETE"
fi