Skip to content

Commit c596033

Browse files
authored
Support empy4 together with empy3 (#753)
Signed-off-by: Jose Luis Rivero <jrivero@honurobotics.com>
1 parent d5c7183 commit c596033

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

.github/workflows/ci.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ jobs:
1212
uses: ros-infrastructure/ci/.github/workflows/pytest.yaml@main
1313
with:
1414
matrix-filter: del(.matrix.os[] | select(contains("windows")))
15+
16+
# Dedicated job for testing with empy < 4
17+
pytest-empy-legacy:
18+
runs-on: ubuntu-latest
19+
name: Test empy < 4 compatibility
20+
steps:
21+
- uses: actions/checkout@v4
22+
- name: Set up Python
23+
uses: actions/setup-python@v4
24+
- name: Install dependencies with empy < 4
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install -e .[test] 'empy<4'
28+
- name: Run tests
29+
run: |
30+
python -m pytest test/ -v
1531
yamllint:
1632
runs-on: ubuntu-latest
1733
steps:

bloom/generators/debian/generator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
from bloom.logging import is_debug
7979
from bloom.logging import warning
8080

81+
from bloom.util import expand_template_em
82+
8183
from bloom.commands.git.patch.common import get_patch_config
8284
from bloom.commands.git.patch.common import set_patch_config
8385

@@ -534,7 +536,7 @@ def __process_template_folder(path, subs):
534536
info("Expanding '{0}' -> '{1}'".format(
535537
os.path.relpath(item),
536538
os.path.relpath(template_path)))
537-
result = em.expand(template, **subs)
539+
result = expand_template_em(template, subs)
538540
# Don't write an empty file
539541
if len(result) == 0 and \
540542
os.path.basename(template_path) in ['copyright']:

bloom/generators/rpm/generator.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181

8282
from bloom.util import code
8383
from bloom.util import execute_command
84+
from bloom.util import expand_template_em
8485
from bloom.util import maybe_continue
8586

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

92-
try:
93-
import em
94-
except ImportError:
95-
debug(traceback.format_exc())
96-
error("empy was not detected, please install it.", exit=True)
97-
9893
# Drop the first log prefix for this command
9994
enable_drop_first_log_prefix(True)
10095

@@ -379,7 +374,7 @@ def __process_template_folder(path, subs):
379374
info("Expanding '{0}' -> '{1}'".format(
380375
os.path.relpath(item),
381376
os.path.relpath(template_path)))
382-
result = em.expand(template, **subs)
377+
result = expand_template_em(template, subs)
383378
# Write the result
384379
with io.open(template_path, 'w', encoding='utf-8') as f:
385380
if sys.version_info.major == 2:

bloom/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@
8484
to_unicode = str
8585

8686

87+
def expand_template_em(template, subs):
88+
"""
89+
Compatibility function for EmPy 3 and 4.
90+
EmPy 3: em.expand(template, **kwargs)
91+
EmPy 4: em.expand(template, locals=dict)
92+
"""
93+
try:
94+
import em
95+
except ImportError:
96+
error("empy was not detected, please install it.", exit=True)
97+
98+
if em.__version__.startswith('3'):
99+
return em.expand(template, **subs)
100+
else:
101+
return em.expand(template, locals=subs)
102+
103+
87104
def flush_stdin():
88105
try:
89106
from termios import tcflush, TCIFLUSH

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22

3-
import sys
43
from setuptools import find_packages, setup
54

65

@@ -20,7 +19,7 @@
2019
install_requires=[
2120
'catkin_pkg >= 0.4.3',
2221
'setuptools < 82',
23-
'empy < 4',
22+
'empy',
2423
'packaging',
2524
'python-dateutil',
2625
'PyYAML',

test/unit_tests/test_generators/test_debian/test_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from ....utils.common import redirected_stdio
44

5-
from bloom.generators.debian.generator import em
65
from bloom.generators.debian.generator import get_changelogs
76
from bloom.generators.debian.generator import format_description
7+
from bloom.util import expand_template_em
88

99
from catkin_pkg.packages import find_packages
1010

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

2929

3030
def test_format_description():

0 commit comments

Comments
 (0)