Debian Codegraph @

misc/pkgos-debpypi

raw
1 #!/bin/sh
2
3 set -e
4 #set -x
5
6 if [ "${1}" = "-u" ] && [ -n "${2}" ] ; then
7 ORIG_URL="${2}"
8 shift
9 shift
10 fi
11
12 if [ -z "${1}" ] ; then
13 echo "This tool creates a template Debian package out of a PyPi package name."
14 echo "Once this script has run, make sure it did what you expected, then use"
15 echo "the newly created source package as a template. Make sure you correct"
16 echo "the debian/copyright file before publishing any package."
17 echo "Usage: ${0} package-name"
18 exit 1
19 fi
20
21 PKG_NAME=${1}
22 # Calculate the package name based on the command line argument
23 LOWER_PKG_NAME=`echo ${PKG_NAME} | awk '{print tolower($0)}'`
24 LOWER_PKG_NAME=`echo ${LOWER_PKG_NAME} | sed 's/_/-/g'`
25 if echo ${LOWER_PKG_NAME} | grep -q '^python-' ; then
26 DEB_PKG_NAME=${LOWER_PKG_NAME}
27 else
28 DEB_PKG_NAME=python-${LOWER_PKG_NAME}
29 fi
30 echo "===> Package name will be ${DEB_PKG_NAME}"
31
32 if ! [ -e ${PKG_NAME}.json ] ; then
33 wget https://pypi.org/pypi/${PKG_NAME}/json -O ${PKG_NAME}.json
34 fi
35 PY3_PKG_NAME=python3-$(echo ${DEB_PKG_NAME} | sed 's/python-//g')
36
37 # Get the info from .json using jq
38 VERSION_STRING=$(cat ${PKG_NAME}.json | jq --raw-output '. | .info.version')
39 SHORT_DESC=$(cat ${PKG_NAME}.json | jq --raw-output '. | .info.summary')
40 UP_MAINT_NAME=$(cat ${PKG_NAME}.json | jq --raw-output '. | .info.author')
41 AUTHOR_EMAIL=$(cat ${PKG_NAME}.json | jq --raw-output '. | .info.author_email')
42 HOMEPAGE=$(cat ${PKG_NAME}.json | jq --raw-output '. | .info.home_page')
43 FIRST_LETTER=$(echo ${PKG_NAME} | awk '{print substr($0,1,1)}')
44
45 if [ -e ${DEB_PKG_NAME}_${VERSION_STRING}.orig.tar.xz ] ; then
46 ORIG=${DEB_PKG_NAME}_${VERSION_STRING}.orig.tar.xz
47 else
48 ORIG=${DEB_PKG_NAME}_${VERSION_STRING}.orig.tar.gz
49 fi
50
51 if [ ! -e ${ORIG} ] ; then
52 echo "===> Downloading ${ORIG} file"
53 if [ -z "${ORIG_URL}" ] ; then
54 if [ $(cat ${PKG_NAME}.json | jq --raw-output '. | .urls[0].packagetype') = "sdist" ] ; then
55 ORIG_URL=$(cat ${PKG_NAME}.json | jq --raw-output '. | .urls[0].url')
56 else
57 if [ $(cat ${PKG_NAME}.json | jq --raw-output '. | .urls[1].packagetype') = "sdist" ] ; then
58 ORIG_URL=$(cat ${PKG_NAME}.json | jq --raw-output '. | .urls[1].url')
59 else
60 echo "First and 2nd URL aren't sdist, please manually download..."
61 exit 1
62 fi
63 fi
64 fi
65 wget -nv "${ORIG_URL}" -O ${ORIG}
66 fi
67
68 echo "===> Extracting ${ORIG}"
69 if ! [ -e ${DEB_PKG_NAME}-${VERSION_STRING} ] ; then
70 tar -xf ${ORIG}
71 if ! [ ${PKG_NAME}-${VERSION_STRING} = ${DEB_PKG_NAME}-${VERSION_STRING} ] ; then
72 # The sed is in case there's a package called package_name with underscore, in which
73 # case we want to replace that by dash
74 mv $(echo ${PKG_NAME} | sed 's#_#*#g' | sed 's#-#*#g')-${VERSION_STRING} ${DEB_PKG_NAME}-${VERSION_STRING}
75 fi
76 fi
77
78 if [ -z "${HOMEPAGE}" ] || [ "${HOMEPAGE}" = "null" ] ; then
79 echo -n "Could not find homepage URL. Please enter it: "
80 read HOMEPAGE
81 fi
82
83 echo "===> Package info:
84 Upstream version: ${VERSION_STRING}
85 Author: ${UP_MAINT_NAME}
86 Homepage: ${HOMEPAGE}"
87
88 echo "===> Cloning package:"
89 if ! [ -d ${DEB_PKG_NAME} ] ; then
90 git clone ${HOMEPAGE} ${DEB_PKG_NAME}
91 fi
92
93 echo "===> Getting info"
94 echo "For which team should this be packaged?"
95 echo "1 - OpenStack team"
96 echo "2 - Python team"
97 echo "3 - Home Assistant team"
98 echo -n "Enter 1, 2 or 3: "
99 read TEAM_NUM
100 case ${TEAM_NUM} in
101 1)
102 TEAM=openstack
103 MAINTAINER_FIELD="Debian OpenStack <[email protected]>"
104 VCS_URL_START=https://salsa.debian.org/openstack-team/libs
105 ;;
106 2)
107 TEAM=python
108 MAINTAINER_FIELD="Debian Python Team <[email protected]>"
109 VCS_URL_START=https://salsa.debian.org/python-team/packages
110 ;;
111 3)
112 TEAM=homeassistant
113 MAINTAINER_FIELD="Home Assistant Team <[email protected]>"
114 VCS_URL_START=https://salsa.debian.org/homeassistant-team/deps
115 ;;
116 *)
117 echo "Try again, bye bye..."
118 exit 1
119 ;;
120 esac
121
122 echo "===> Checking for license:"
123 if [ -e ${DEB_PKG_NAME}/LICENSE ] ; then
124 LICENSE_FILE=${DEB_PKG_NAME}/LICENSE
125 fi
126 if [ -e ${DEB_PKG_NAME}/LICENSE.md ] ; then
127 LICENSE_FILE=${DEB_PKG_NAME}/LICENSE.md
128 fi
129
130 if [ -e "${LICENSE_FILE}" ] ; then
131 if grep -q "Apache Software License 2.0" ${LICENSE_FILE} ; then
132 LICENSE_TYPE=1
133 fi
134 if grep -q "Permission is hereby granted, free of charge" ${LICENSE_FILE} ; then
135 LICENSE_TYPE=3
136 fi
137 fi
138
139 if [ -z "${LICENSE_TYPE}" ] ; then
140 echo "What is the license of this package?"
141 echo "1 - Apache-2.0"
142 echo "2 - BSD-2-clause"
143 echo "3 - Expat"
144 echo "4 - GPL-2+"
145 echo "5 - LGPL-2.1"
146 echo "6 - Other"
147 echo -n "Enter 1, 2, 3, 4, 5 or 6: "
148 read LICENSE_TYPE
149 fi
150
151 case ${LICENSE_TYPE} in
152 1)
153 LICENSE_TITLE="Apache-2.0"
154 LICENSE_TEXT=" Licensed under the Apache License, Version 2.0 (the "License");
155 you may not use this file except in compliance with the License.
156 You may obtain a copy of the License at
157 .
158 http://www.apache.org/licenses/LICENSE-2.0
159 .
160 Unless required by applicable law or agreed to in writing, software
161 distributed under the License is distributed on an \"AS IS\" BASIS,
162 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163 See the License for the specific language governing permissions and
164 limitations under the License.
165 .
166 On Debian-based systems the full text of the Apache version 2.0 license
167 can be found in /usr/share/common-licenses/Apache-2.0.
168 "
169 ;;
170 2)
171 LICENSE_TITLE="BSD-2-clause"
172 LICENSE_TEXT=""
173 ;;
174 3)
175 LICENSE_TITLE="Expat"
176 LICENSE_TEXT=" Permission is hereby granted, free of charge, to any person obtaining a copy
177 of this software and associated documentation files (the \"Software\"), to deal
178 in the Software without restriction, including without limitation the rights
179 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
180 copies of the Software, and to permit persons to whom the Software is
181 furnished to do so, subject to the following conditions:
182 .
183 The above copyright notice and this permission notice shall be included in all
184 copies or substantial portions of the Software.
185 .
186 THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
189 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
190 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
191 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
192 SOFTWARE.
193 "
194 ;;
195 4)
196 LICENSE_TITLE="GPL-2+"
197 LICENSE_TEXT=" This program is free software; you can redistribute it and/or modify it under
198 the terms of the GNU General Public License as published by the Free Software
199 Foundation; either version 2 of the License, or (at your option) any later
200 version.
201 .
202 This program is distributed in the hope that it will be useful, but WITHOUT
203 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
204 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
205 .
206 You should have received a copy of the GNU General Public License along with
207 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
208 St, Fifth Floor, Boston, MA 02110-1301 USA
209 .
210 On Debian systems, the complete text of the GNU General Public License v2
211 (GPL) can be found in /usr/share/common-licenses/GPL-2.
212 "
213 ;;
214 5)
215 LICENSE_TITLE="LGPL-2.1"
216 LICENSE_TEXT=" This library is free software; you can redistribute it and/or modify it under
217 the terms of the GNU Lesser General Public License as published by the Free
218 Software Foundation; either version 2 of the License, or (at your option) any
219 later version.
220 .
221 This library is distributed in the hope that it will be useful, but WITHOUT
222 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
223 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
224 details.
225 .
226 You should have received a copy of the GNU Lesser General Public License along
227 with this library; if not, write to the Free Software Foundation, Inc., 51
228 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
229 .
230 On Debian systems, the complete text of the GNU Lesser General Public License
231 (LGPL) may be found in /usr/share/common-licenses/LGPL-2.1.
232 "
233 ;;
234 6)
235 LICENSE_TITLE="TODO"
236 LICENSE_TEXT=" TODO"
237 ;;
238 *)
239 echo "Unknown entry: bye bye..."
240 exit 1
241 ;;
242 esac
243
244 echo "===> Creating debian folder for ${DEB_PKG_NAME}"
245 if [ ! -d ${DEB_PKG_NAME}-${VERSION_STRING}/debian/source ] ; then
246 mkdir -p ${DEB_PKG_NAME}-${VERSION_STRING}/debian/source
247 fi
248 cd ${DEB_PKG_NAME}-${VERSION_STRING}
249
250 echo "===> Searching for sphinx doc folder"
251 if [ -e doc ] ; then
252 DOC_FOLDER=doc
253 elif [ -e docs ] ; then
254 DOC_FOLDER=docs
255 fi
256 if [ -n "${DOC_FOLDER}" ] ; then
257 CONFPY_FILE=`find ${DOC_FOLDER} -name 'conf.py'`
258 if [ -r "${CONFPY_FILE}" ] ; then
259 DOC_FOLDER=`dirname ${CONFPY_FILE}`
260 SPHINX_BUILD_DEP=", python3-sphinx"
261 RULES_WITH=",sphinxdoc"
262 SUGGEST_DOC="
263 Suggests: ${DEB_PKG_NAME}-doc"
264 else
265 DOC_FOLDER=
266 SPHINX_BUILD_DEP=""
267 RULES_WITH=""
268 SUGGEST_DOC=""
269 fi
270 fi
271
272 echo "===> Checking for PBR and fixing accordingly"
273 # If the package uses PBR, then we need openstack-pkg-tools
274 if grep -q "setup_requires=\['pbr'\]" ${DEB_PKG_NAME}-${VERSION_STRING}/setup.py ; then
275 OSTACK_PKG_T_CTRL=", openstack-pkg-tools"
276 # We need a mandatory include...
277 # ... and the export OSLO_PACKAGE_VERSION=$(VERSION)
278 OSTACK_PKG_T_RULES="include /usr/share/openstack-pkg-tools/pkgos.make
279 export OSLO_PACKAGE_VERSION=\$(VERSION)"
280 else
281 # Otherwise, we just include it non-mandatorily, so that
282 # we can use ./debian/rules gen-orig-xz
283 OSTACK_PKG_T_CTRL=""
284 OSTACK_PKG_T_RULES="include /usr/share/openstack-pkg-tools/pkgos.make"
285 fi
286
287 echo "===> Creating debian/control"
288 echo "Source: ${DEB_PKG_NAME}
289 Section: python
290 Priority: optional
291 Maintainer: ${MAINTAINER_FIELD}
292 Uploaders:
293 Thomas Goirand <[email protected]>,
294 Build-Depends:
295 debhelper-compat (= 11),
296 dh-python,
297 pybuild-plugin-pyproject,
298 openstack-pkg-tools,
299 python3-setuptools,
300 python3-all${SPHINX_BUILD_DEP}${OSTACK_PKG_T_CTRL}
301 Build-Depends-Indep:
302 python3-pytest,
303 Standards-Version: 4.6.1
304 Vcs-Browser: ${VCS_URL_START}/${DEB_PKG_NAME}
305 Vcs-Git: ${VCS_URL_START}/${DEB_PKG_NAME}.git
306 Homepage: ${HOMEPAGE}
307
308 Package: ${PY3_PKG_NAME}
309 Architecture: all
310 Depends: \${python3:Depends}, \${misc:Depends}${SUGGEST_DOC}
311 Description: ${SHORT_DESC}
312 This package provides
313 .
314 This package
315 " >debian/control
316
317 if [ -n "${DOC_FOLDER}" ] ; then
318 echo "Package: python-"$(echo ${LOWER_PKG_NAME} | sed s/python-//)"-doc
319 Section: doc
320 Architecture: all
321 Depends: \${misc:Depends}, \${sphinxdoc:Depends}
322 Description: ${SHORT_DESC} - doc
323 - REPLACE ME -
324 .
325 This package contains the documentation.
326 " >>debian/control
327
328 echo "Document: ${DEB_PKG_NAME}-doc
329 Title: ${PKG_NAME} Documentation
330 Author: N/A
331 Abstract: Sphinx documentation for ${PKG_NAME}
332 Section: Programming/Python
333
334 Format: HTML
335 Index: /usr/share/doc/python-${LOWER_PKG_NAME}-doc/html/index.html
336 Files: /usr/share/doc/python-${LOWER_PKG_NAME}-doc/html/*" >debian/python-${LOWER_PKG_NAME}-doc.doc-base
337 fi
338
339 EDITOR=touch dch --create --package ${DEB_PKG_NAME} --distribution unstable --urgency medium -v ${VERSION_STRING}-1
340 rm +1
341
342 echo "===> Creating debian/copyright"
343 if [ -n "${AUTHOR_EMAIL}" ] ; then
344 UP_MAINT_AND_EMAIL="${UP_MAINT_NAME} <${AUTHOR_EMAIL}>"
345 else
346 UP_MAINT_AND_EMAIL=${UP_MAINT_NAME}
347 fi
348 echo "Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
349 Upstream-Name: ${PKG_NAME}
350 Source: ${HOMEPAGE}
351
352 Files: *
353 Copyright: (c) 2024, ${UP_MAINT_AND_EMAIL}
354 License: ${LICENSE_TITLE}
355
356 Files: debian/*
357 Copyright: (c) 2024, Thomas Goirand <[email protected]>
358 License: ${LICENSE_TITLE}
359
360 License: ${LICENSE_TITLE}
361 ${LICENSE_TEXT}
362 " >debian/copyright
363
364 echo "===> Creating debian/rules"
365 if [ -n "${DOC_FOLDER}" ] ; then
366 SPHINX_BUILD_RULES="override_dh_sphinxdoc:
367 ifeq (,\$(findstring nodoc, \$(DEB_BUILD_OPTIONS)))
368 PYTHONPATH=. python3 -m sphinx -b html ${DOC_FOLDER} debian/python-"$(echo ${LOWER_PKG_NAME} | sed s/python-//)"-doc/usr/share/doc/python-"$(echo ${LOWER_PKG_NAME} | sed s/python-//)"-doc/html
369 dh_sphinxdoc
370 endif
371 "
372 else
373 SPHINX_BUILD_RULES=""
374 fi
375
376 if [ -e .testr.conf ] || [ -e .stestr.conf ] ; then
377 UNIT_TEST_RULES="override_dh_auto_test:
378 ifeq (,\$(findstring nocheck, \$(DEB_BUILD_OPTIONS)))
379 pkgos-dh_auto_test --no-py2
380 endif
381 "
382 else
383 UNIT_TEST_RULES="
384 ifeq (,\$(findstring nocheck, \$(DEB_BUILD_OPTIONS)))
385 set -e ; for pyvers in \$(PYTHON3S); do \\
386 python\$\$pyvers setup.py test ; \\
387 done
388 endif
389 "
390 fi
391
392 echo "#!/usr/bin/make -f
393
394 UPSTREAM_GIT := ${HOMEPAGE}.git
395 ${OSTACK_PKG_T_RULES}
396
397 %:
398 dh \$@ --buildsystem=pybuild --with python3${RULES_WITH}
399
400 ${SPHINX_BUILD_RULES}
401 " >debian/rules
402 chmod +x debian/rules
403
404 #echo "/usr" >debian/python3-${LOWER_PKG_NAME}.install
405 echo "===> Creating debian/watch and debian/source"
406 if echo ${HOMEPAGE} | grep -q github.com ; then
407 echo "version=4
408 opts=\"dversionmangle=auto,mode=git,uversionmangle=s/\.0rc/~rc/;s/\.0b1/~b1/;s/\.0b2/~b2/;s/\.0b3/~b3/\" \
409 ${HOMEPAGE} refs/tags/(\d[brc\d\.]+)
410 " >debian/watch
411 else
412 echo "version=3
413 opts=uversionmangle=s/(rc|a|b|c)/~\$1/ \\
414 https://pypi.debian.net/${PKG_NAME}/${PKG_NAME}-(.+)\.(?:zip|tgz|tbz|txz|(?:tar\.(?:gz|bz2|xz)))
415 " >debian/watch
416 fi
417
418 echo "3.0 (quilt)" >debian/source/format
419 echo 'extend-diff-ignore = "^[^/]*[.]egg-info/"' >debian/source/options
420
421 echo "===> Adding pybuild-autopkgtest in debian/tests"
422 mkdir -p debian/tests
423 echo "Test-Command: pybuild-autopkgtest
424 Depends: @, @builddeps@, pybuild-plugin-autopkgtest
425 Restrictions: allow-stderr, skippable,
426 Features: test-name=pybuild-autopkgtest
427 " >debian/tests/control
428
429 echo "===> Attempting to configure the Debian repo"
430 cd ../${DEB_PKG_NAME}
431 if git show v${VERSION_STRING} >/dev/null 2>/dev/null; then
432 git tag ${VERSION_STRING} v${VERSION_STRING}
433 fi
434 if ! git show ${VERSION_STRING} >/dev/null 2>/dev/null ; then
435 echo "Cannot find upstream tag: exiting."
436 exit 1
437 fi
438
439 git reset --hard ${VERSION_STRING}
440 GIT_CUR_BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
441 git checkout -b debian/unstable
442 git branch -D ${GIT_CUR_BRANCH}
443 cp -axuf ../${DEB_PKG_NAME}-${VERSION_STRING}/debian .
444 git add debian
445 wrap-and-sort -bastk
446
447 echo "===> Regenerating orig tarball"
448 rm -f ../*.orig.tar.*
449 ./debian/rules gen-orig-xz
450
451 echo "===> Outputing reportbug template"
452 echo "* Package name : ${DEB_PKG_NAME}
453 Version : ${VERSION_STRING}
454 Upstream Author : ${UP_MAINT_AND_EMAIL}
455 * URL : ${HOMEPAGE}
456 * License : ${LICENSE_TITLE}
457 Programming Lang: Python
458 Description : ${SHORT_DESC}
459 "