Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ jobs:
uses: ros-infrastructure/ci/.github/workflows/pytest.yaml@main
with:
matrix-filter: del(.matrix.os[] | select(contains("windows")))

# Dedicated job for testing with empy < 4
pytest-empy-legacy:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12', '3.13']
name: Test empy < 4 compatibility (Python ${{ matrix.python-version }})
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies with empy < 4
run: |
python -m pip install --upgrade pip
pip install 'empy<4'
pip install -e .[test]
- name: Run tests
run: |
python -m pytest test/ -v
yamllint:
runs-on: ubuntu-latest
steps:
Expand Down
44 changes: 9 additions & 35 deletions bloom/generators/debian/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
from bloom.logging import is_debug
from bloom.logging import warning

from bloom.util import expand_template_em

from bloom.commands.git.patch.common import get_patch_config
from bloom.commands.git.patch.common import set_patch_config

Expand All @@ -102,23 +104,6 @@
debug(traceback.format_exc())
error("rosdistro was not detected, please install it.", exit=True)

try:
import em
except ImportError:
debug(traceback.format_exc())
error("empy was not detected, please install it.", exit=True)

# Fix unicode bug in empy
# This should be removed once upstream empy is fixed
# See: https://github.com/ros-infrastructure/bloom/issues/196
try:
em.str = unicode
em.Stream.write_old = em.Stream.write
em.Stream.write = lambda self, data: em.Stream.write_old(self, data.encode('utf8'))
except NameError:
pass
# End fix

# Drop the first log prefix for this command
enable_drop_first_log_prefix(True)

Expand Down Expand Up @@ -151,10 +136,7 @@ def __place_template_folder(group, src, dst, gbp=False):
debug("Not overwriting existing file '{0}'".format(template_dst))
else:
with io.open(template_dst, 'w', encoding='utf-8') as f:
if not isinstance(template, str):
template = template.decode('utf-8')
# Python 2 API needs a `unicode` not a utf-8 string.
elif sys.version_info.major == 2:
if isinstance(template, bytes):
template = template.decode('utf-8')
f.write(template)
shutil.copystat(template_abs_path, template_dst)
Expand Down Expand Up @@ -483,17 +465,11 @@ def generate_substitutions_from_package(
data['Licenses'] = licenses

def convertToUnicode(obj):
if sys.version_info.major == 2:
if isinstance(obj, str):
return unicode(obj.decode('utf8'))
elif isinstance(obj, unicode):
return obj
else:
if isinstance(obj, bytes):
return str(obj.decode('utf8'))
elif isinstance(obj, str):
return obj
if isinstance(obj, list):
if isinstance(obj, bytes):
return str(obj.decode('utf8'))
elif isinstance(obj, str):
return obj
elif isinstance(obj, list):
for i, val in enumerate(obj):
obj[i] = convertToUnicode(val)
return obj
Expand Down Expand Up @@ -534,16 +510,14 @@ def __process_template_folder(path, subs):
info("Expanding '{0}' -> '{1}'".format(
os.path.relpath(item),
os.path.relpath(template_path)))
result = em.expand(template, **subs)
result = expand_template_em(template, subs)
# Don't write an empty file
if len(result) == 0 and \
os.path.basename(template_path) in ['copyright']:
processed_items.append(item)
continue
# Write the result
with io.open(template_path, 'w', encoding='utf-8') as f:
if sys.version_info.major == 2:
result = result.decode('utf-8')
f.write(result)
# Copy the permissions
shutil.copymode(item, template_path)
Expand Down
27 changes: 7 additions & 20 deletions bloom/generators/rpm/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

from bloom.util import code
from bloom.util import execute_command
from bloom.util import expand_template_em
from bloom.util import maybe_continue

try:
Expand All @@ -89,12 +90,6 @@
debug(traceback.format_exc())
error("rosdistro was not detected, please install it.", exit=True)

try:
import em
except ImportError:
debug(traceback.format_exc())
error("empy was not detected, please install it.", exit=True)

# Drop the first log prefix for this command
enable_drop_first_log_prefix(True)

Expand Down Expand Up @@ -325,17 +320,11 @@ def generate_substitutions_from_package(
summarize_dependency_mapping(data, depends, build_depends, resolved_deps)

def convertToUnicode(obj):
if sys.version_info.major == 2:
if isinstance(obj, str):
return unicode(obj.decode('utf8'))
elif isinstance(obj, unicode):
return obj
else:
if isinstance(obj, bytes):
return str(obj.decode('utf8'))
elif isinstance(obj, str):
return obj
if isinstance(obj, list):
if isinstance(obj, bytes):
return str(obj.decode('utf8'))
elif isinstance(obj, str):
return obj
elif isinstance(obj, list):
for i, val in enumerate(obj):
obj[i] = convertToUnicode(val)
return obj
Expand Down Expand Up @@ -378,11 +367,9 @@ def __process_template_folder(path, subs):
info("Expanding '{0}' -> '{1}'".format(
os.path.relpath(item),
os.path.relpath(template_path)))
result = em.expand(template, **subs)
result = expand_template_em(template, subs)
# Write the result
with io.open(template_path, 'w', encoding='utf-8') as f:
if sys.version_info.major == 2:
result = result.decode('utf-8')
f.write(result)
# Copy the permissions
shutil.copymode(item, template_path)
Expand Down
19 changes: 19 additions & 0 deletions bloom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@
to_unicode = str


def expand_template_em(template, subs):
"""
Compatibility function for EmPy 3 and 4.
EmPy 3: em.expand(template, **kwargs)
EmPy 4: em.expand(template, locals=dict)
"""
try:
import em
except ImportError:
error("empy was not detected, please install it.", exit=True)

try:
# Try EmPy 4 API first
return em.expand(template, locals=subs)
except (TypeError, NameError):
# Fall back to EmPy 3 API
return em.expand(template, **subs)


def flush_stdin():
try:
from termios import tcflush, TCIFLUSH
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python

import sys
from setuptools import find_packages, setup


Expand All @@ -20,7 +19,7 @@
install_requires=[
'catkin_pkg >= 0.4.3',
'setuptools',
'empy < 4',
'empy',
'packaging',
'python-dateutil',
'PyYAML',
Expand Down
4 changes: 2 additions & 2 deletions test/unit_tests/test_generators/test_debian/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from ....utils.common import redirected_stdio

from bloom.generators.debian.generator import em
from bloom.generators.debian.generator import get_changelogs
from bloom.generators.debian.generator import format_description
from bloom.util import expand_template_em

from catkin_pkg.packages import find_packages

Expand All @@ -24,7 +24,7 @@ def test_unicode_templating():
assert 'bad_changelog_pkg' in packages
chlogs = get_changelogs(packages['bad_changelog_pkg'])
template = "@(changelog)"
em.expand(template, {'changelog': chlogs[0][2]})
expand_template_em(template, {'changelog': chlogs[0][2]})


def test_format_description():
Expand Down
Loading