Skip to content

Commit

Permalink
Adapt the package building scripts to use Python 3 (#231)
Browse files Browse the repository at this point in the history
Since upstream cloud-init has dropped python2 support,
adapt remaining package build scripts and tools to python3 only

Changes:

* Do not template debian/rules as python3 is the only supported version
* Drop six from requirements.txt
* Makefile: drop everything related to Python 2
* run-container: install the CI deps only on ubuntu|debian
* read-version: update the shebang to use Python 3
* brpm: read_dependencies(): drop unused argument
* read-dependencies: switch to Py3 and drop the --python-version option
* pkg-deps.json: drop the Python version field and update the redhat deps
* pkg-deps.json: drop the unittest2 and contextlib2 renames
* Update RPM the spec file to use Python 3 when building the RPM
* bddeb: drop support for Python 2
  • Loading branch information
paride authored May 2, 2020
1 parent 70dbccb commit 4d26848
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 137 deletions.
54 changes: 16 additions & 38 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
CWD=$(shell pwd)
PYVER ?= $(shell for p in python3 python2; do \
out=$$(command -v $$p 2>&1) && echo $$p && exit; done; exit 1)

YAML_FILES=$(shell find cloudinit tests tools -name "*.yaml" -type f )
YAML_FILES+=$(shell find doc/examples -name "cloud-config*.txt" -type f )

PIP_INSTALL := pip install

ifeq ($(PYVER),python3)
pyflakes = pyflakes3
unittests = unittest3
yaml = yaml
else
ifeq ($(PYVER),python2)
pyflakes = pyflakes
unittests = unittest
else
pyflakes = pyflakes pyflakes3
unittests = unittest unittest3
endif
endif
PYTHON = python3
PIP_INSTALL := pip3 install

ifeq ($(distro),)
distro = redhat
endif

READ_VERSION=$(shell $(PYVER) $(CWD)/tools/read-version || \
echo read-version-failed)
CODE_VERSION=$(shell $(PYVER) -c "from cloudinit import version; print(version.version_string())")
READ_VERSION=$(shell $(PYTHON) $(CWD)/tools/read-version || echo read-version-failed)
CODE_VERSION=$(shell $(PYTHON) -c "from cloudinit import version; print(version.version_string())")


all: check

check: check_version test $(yaml)
check: check_version test yaml

style-check: pep8 $(pyflakes)

Expand All @@ -42,20 +26,14 @@ pep8:
pyflakes:
@$(CWD)/tools/run-pyflakes

pyflakes3:
@$(CWD)/tools/run-pyflakes3

unittest: clean_pyc
python -m pytest -v tests/unittests cloudinit

unittest3: clean_pyc
python3 -m pytest -v tests/unittests cloudinit

ci-deps-ubuntu:
@$(PYVER) $(CWD)/tools/read-dependencies --distro ubuntu --test-distro
@$(PYTHON) $(CWD)/tools/read-dependencies --distro ubuntu --test-distro

ci-deps-centos:
@$(PYVER) $(CWD)/tools/read-dependencies --distro centos --test-distro
@$(PYTHON) $(CWD)/tools/read-dependencies --distro centos --test-distro

pip-requirements:
@echo "Installing cloud-init dependencies..."
Expand All @@ -65,7 +43,7 @@ pip-test-requirements:
@echo "Installing cloud-init test dependencies..."
$(PIP_INSTALL) -r "$@.txt" -q

test: $(unittests)
test: unittest

check_version:
@if [ "$(READ_VERSION)" != "$(CODE_VERSION)" ]; then \
Expand All @@ -74,7 +52,7 @@ check_version:
else true; fi

config/cloud.cfg:
$(PYVER) ./tools/render-cloudcfg config/cloud.cfg.tmpl config/cloud.cfg
$(PYTHON) ./tools/render-cloudcfg config/cloud.cfg.tmpl config/cloud.cfg

clean_pyc:
@find . -type f -name "*.pyc" -delete
Expand All @@ -84,30 +62,30 @@ clean: clean_pyc
rm -rf doc/rtd_html .tox .coverage

yaml:
@$(PYVER) $(CWD)/tools/validate-yaml.py $(YAML_FILES)
@$(PYTHON) $(CWD)/tools/validate-yaml.py $(YAML_FILES)

rpm:
$(PYVER) ./packages/brpm --distro=$(distro)
$(PYTHON) ./packages/brpm --distro=$(distro)

srpm:
$(PYVER) ./packages/brpm --srpm --distro=$(distro)
$(PYTHON) ./packages/brpm --srpm --distro=$(distro)

deb:
@which debuild || \
{ echo "Missing devscripts dependency. Install with:"; \
echo sudo apt-get install devscripts; exit 1; }

$(PYVER) ./packages/bddeb
$(PYTHON) ./packages/bddeb

deb-src:
@which debuild || \
{ echo "Missing devscripts dependency. Install with:"; \
echo sudo apt-get install devscripts; exit 1; }
$(PYVER) ./packages/bddeb -S -d
$(PYTHON) ./packages/bddeb -S -d

doc:
tox -e doc

.PHONY: test pyflakes pyflakes3 clean pep8 rpm srpm deb deb-src yaml
.PHONY: test pyflakes clean pep8 rpm srpm deb deb-src yaml
.PHONY: check_version pip-test-requirements pip-requirements clean_pyc
.PHONY: unittest unittest3 style-check doc
.PHONY: unittest style-check doc
26 changes: 6 additions & 20 deletions packages/bddeb
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,9 @@ def run_helper(helper, args=None, strip=True):
return stdout


def write_debian_folder(root, templ_data, is_python2, cloud_util_deps):
def write_debian_folder(root, templ_data, cloud_util_deps):
"""Create a debian package directory with all rendered template files."""
print("Creating a debian/ folder in %r" % (root))
if is_python2:
pyver = "2"
python = "python"
else:
pyver = "3"
python = "python3"

deb_dir = util.abs_join(root, 'debian')

Expand All @@ -83,30 +77,23 @@ def write_debian_folder(root, templ_data, is_python2, cloud_util_deps):

# Write out the control file template
reqs_output = run_helper(
'read-dependencies',
args=['--distro', 'debian', '--python-version', pyver])
'read-dependencies', args=['--distro', 'debian'])
reqs = reqs_output.splitlines()
test_reqs = run_helper(
'read-dependencies',
['--requirements-file', 'test-requirements.txt',
'--system-pkg-names', '--python-version', pyver]).splitlines()
'--system-pkg-names']).splitlines()

requires = ['cloud-utils | cloud-guest-utils'] if cloud_util_deps else []
# We consolidate all deps as Build-Depends as our package build runs all
# tests so we need all runtime dependencies anyway.
# NOTE: python package was moved to the front after debuild -S would fail with
# 'Please add apropriate interpreter' errors (as in debian bug 861132)
requires.extend([python] + reqs + test_reqs)
requires.extend(['python3'] + reqs + test_reqs)
templater.render_to_file(util.abs_join(find_root(),
'packages', 'debian', 'control.in'),
util.abs_join(deb_dir, 'control'),
params={'build_depends': ','.join(requires),
'python': python})

templater.render_to_file(util.abs_join(find_root(),
'packages', 'debian', 'rules.in'),
util.abs_join(deb_dir, 'rules'),
params={'python': python, 'pyver': pyver})
params={'build_depends': ','.join(requires)})


def read_version():
Expand Down Expand Up @@ -208,8 +195,7 @@ def main():
xdir = util.abs_join(tdir, "cloud-init-%s" % ver_data['version_long'])
templ_data.update(ver_data)

write_debian_folder(xdir, templ_data, is_python2=args.python2,
cloud_util_deps=args.cloud_utils)
write_debian_folder(xdir, templ_data, cloud_util_deps=args.cloud_utils)

print("Running 'debuild %s' in %r" % (' '.join(args.debuild_args),
xdir))
Expand Down
2 changes: 1 addition & 1 deletion packages/brpm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def run_helper(helper, args=None, strip=True):
return stdout


def read_dependencies(distro, requirements_file='requirements.txt'):
def read_dependencies(distro):
"""Returns the Python package depedencies from requirements.txt files.
@returns a tuple of (requirements, test_requirements)
Expand Down
3 changes: 1 addition & 2 deletions packages/debian/control.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ Standards-Version: 3.9.6
Package: cloud-init
Architecture: all
Depends: ${misc:Depends},
${${python}:Depends},
${python3:Depends},
iproute2,
isc-dhcp-client
Recommends: eatmydata, sudo, software-properties-common, gdisk
XB-Python-Version: ${python:Versions}
Description: Init scripts for cloud instances
Cloud instances need special scripts to run during initialisation
to retrieve and install ssh keys and to let the user run various scripts.
6 changes: 2 additions & 4 deletions packages/debian/rules.in → packages/debian/rules
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
## template:basic
#!/usr/bin/make -f
INIT_SYSTEM ?= systemd
export PYBUILD_INSTALL_ARGS=--init-system=$(INIT_SYSTEM)
PYVER ?= python${pyver}
DEB_VERSION := $(shell dpkg-parsechangelog --show-field=Version)

%:
dh $@ --with $(PYVER),systemd --buildsystem pybuild
dh $@ --with python3,systemd --buildsystem pybuild

override_dh_install:
dh_install
Expand All @@ -19,7 +17,7 @@ override_dh_install:

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
http_proxy= make PYVER=python${pyver} check
http_proxy= make check
else
@echo check disabled by DEB_BUILD_OPTIONS=$(DEB_BUILD_OPTIONS)
endif
Expand Down
55 changes: 17 additions & 38 deletions packages/pkg-deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,31 @@
"dh-systemd"
],
"renames" : {
"pyyaml" : {
"2" : "python-yaml",
"3" : "python3-yaml"
},
"pyserial" : {
"2" : "python-serial",
"3" : "python3-serial"
}
"pyyaml" : "python3-yaml",
"pyserial" : "python3-serial"
},
"requires" : [
"procps"
]
},
"centos" : {
"build-requires" : [
"python3-devel"
],
"requires" : [
"e2fsprogs",
"iproute",
"net-tools",
"procps",
"rsyslog",
"shadow-utils",
"sudo"
]
},
"redhat" : {
"build-requires" : [
"python-devel",
"python-setuptools"
"python3-devel"
],
"renames" : {
"jinja2" : {
"3" : "python36-jinja2"
},
"jsonschema" : {
"3" : "python36-jsonschema"
},
"pyflakes" : {
"2" : "pyflakes",
"3" : "python36-pyflakes"
},
"pyyaml" : {
"2" : "PyYAML",
"3" : "python36-PyYAML"
},
"pyserial" : {
"2" : "pyserial"
},
"pytest": {
"3": "python36-pytest"
},
"requests" : {
"3" : "python36-requests"
}
},
"requires" : [
"e2fsprogs",
"iproute",
Expand All @@ -61,9 +43,6 @@
},
"suse" : {
"renames" : {
"pyyaml" : {
"2" : "python-yaml"
}
},
"build-requires" : [
"fdupes",
Expand Down
10 changes: 4 additions & 6 deletions packages/redhat/cloud-init.spec.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
## template: jinja
%{!?python_sitelib: %global python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")}

%define use_systemd (0%{?fedora} && 0%{?fedora} >= 18) || (0%{?rhel} && 0%{?rhel} >= 7)

%if %{use_systemd}
Expand Down Expand Up @@ -94,11 +92,11 @@ ssh keys and to let the user run various scripts.
{% endfor %}

%build
%{__python} setup.py build
%{__python3} setup.py build

%install

%{__python} setup.py install -O1 \
%{__python3} setup.py install -O1 \
--skip-build --root $RPM_BUILD_ROOT \
--init-system=%{init_system}

Expand All @@ -109,7 +107,7 @@ cp -p tools/21-cloudinit.conf \
$RPM_BUILD_ROOT/%{_sysconfdir}/rsyslog.d/21-cloudinit.conf

# Remove the tests
rm -rf $RPM_BUILD_ROOT%{python_sitelib}/tests
rm -rf $RPM_BUILD_ROOT%{python3_sitelib}/tests

# Required dirs...
mkdir -p $RPM_BUILD_ROOT/%{_sharedstatedir}/cloud
Expand Down Expand Up @@ -213,4 +211,4 @@ fi
%dir %{_sharedstatedir}/cloud

# Python code is here...
%{python_sitelib}/*
%{python3_sitelib}/*
Loading

0 comments on commit 4d26848

Please sign in to comment.