Skip to content

Commit

Permalink
Add indirection for changes in empy 3 vs 4 (#261)
Browse files Browse the repository at this point in the history
* Update to use empy 4.0 API
Working towards #259
This is not backwards compatible at the moment. But works with empy==4.0

* Add indirection for changes in empy 3 vs 4
Fixes  #259

* remove now unnecessary imports
  • Loading branch information
tfoote authored Dec 6, 2023
1 parent d623245 commit 4de56c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/rocker/em.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2019 Open Source Robotics Foundation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import em

def empy_expand(template, substitution_variables):
"""Indirection for empy version compatibility."""
if em.__version__.startswith('3'):
return em.expand(template, substitution_variables)
else:
return em.expand(template, globals=substitution_variables)
8 changes: 4 additions & 4 deletions src/rocker/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import grp
import os
import docker
import em
import getpass
import pwd
import pkgutil
Expand All @@ -25,6 +24,7 @@
import sys

from .core import get_docker_client
from .em import empy_expand


def name_to_argument(name):
Expand Down Expand Up @@ -81,7 +81,7 @@ def get_preamble(self, cliargs):

def get_snippet(self, cliargs):
snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8')
return em.expand(snippet, self.get_environment_subs())
return empy_expand(snippet, self.get_environment_subs())

@staticmethod
def register_arguments(parser, defaults={}):
Expand Down Expand Up @@ -238,7 +238,7 @@ def get_preamble(self, cliargs):

def get_snippet(self, cliargs):
snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8')
return em.expand(snippet, self.get_environment_subs())
return empy_expand(snippet, self.get_environment_subs())

def get_docker_args(self, cliargs):
args = ' -v /run/user/%(user_id)s/pulse:/run/user/%(user_id)s/pulse --device /dev/snd '\
Expand Down Expand Up @@ -318,7 +318,7 @@ def get_snippet(self, cliargs):
substitutions['shell'] = None
else:
substitutions['shell'] = cliargs['user_override_shell']
return em.expand(snippet, substitutions)
return empy_expand(snippet, substitutions)

@staticmethod
def register_arguments(parser, defaults={}):
Expand Down
11 changes: 6 additions & 5 deletions src/rocker/nvidia_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import em
import getpass
import tempfile
from packaging.version import Version
Expand All @@ -27,6 +26,8 @@
from .extensions import name_to_argument
from .core import get_docker_client
from .core import RockerExtension
from .em import empy_expand


def get_docker_version():
docker_version_raw = get_docker_client().version()['Version']
Expand Down Expand Up @@ -116,11 +117,11 @@ def get_environment_subs(self, cliargs={}):

def get_preamble(self, cliargs):
preamble = pkgutil.get_data('rocker', 'templates/%s_preamble.Dockerfile.em' % self.name).decode('utf-8')
return em.expand(preamble, self.get_environment_subs(cliargs))
return empy_expand(preamble, self.get_environment_subs(cliargs))

def get_snippet(self, cliargs):
snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8')
return em.expand(snippet, self.get_environment_subs(cliargs))
return empy_expand(snippet, self.get_environment_subs(cliargs))

def get_docker_args(self, cliargs):
force_flag = cliargs.get('nvidia', None)
Expand Down Expand Up @@ -184,11 +185,11 @@ def get_environment_subs(self, cliargs={}):
def get_preamble(self, cliargs):
return ''
# preamble = pkgutil.get_data('rocker', 'templates/%s_preamble.Dockerfile.em' % self.name).decode('utf-8')
# return em.expand(preamble, self.get_environment_subs(cliargs))
# return empy_expand(preamble, self.get_environment_subs(cliargs))

def get_snippet(self, cliargs):
snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8')
return em.expand(snippet, self.get_environment_subs(cliargs))
return empy_expand(snippet, self.get_environment_subs(cliargs))

def get_docker_args(self, cliargs):
return ""
Expand Down

0 comments on commit 4de56c1

Please sign in to comment.