From ee675ddc6ddee6405ac2013873dde25dba308775 Mon Sep 17 00:00:00 2001 From: Tully Foote Date: Sun, 4 Feb 2024 21:50:34 -0800 Subject: [PATCH] homogenize to use get_name() in all extensions --- src/rocker/extensions.py | 41 +++------------------------------- src/rocker/git_extension.py | 8 +++---- src/rocker/nvidia_extension.py | 15 +++++-------- src/rocker/ssh_extension.py | 8 +++---- src/rocker/volume_extension.py | 9 ++++---- test/test_core.py | 28 +++++++++++------------ test/test_file_writing.py | 9 +++----- 7 files changed, 36 insertions(+), 82 deletions(-) diff --git a/src/rocker/extensions.py b/src/rocker/extensions.py index bf5e7734..f679acde 100644 --- a/src/rocker/extensions.py +++ b/src/rocker/extensions.py @@ -37,9 +37,6 @@ class Devices(RockerExtension): def get_name(): return 'devices' - def __init__(self): - self.name = Devices.get_name() - def get_preamble(self, cliargs): return '' @@ -68,8 +65,6 @@ def get_name(): def __init__(self): self._env_subs = None - self.name = DevHelpers.get_name() - def get_environment_subs(self): if not self._env_subs: @@ -80,7 +75,7 @@ def get_preamble(self, cliargs): return '' def get_snippet(self, cliargs): - snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8') + snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.get_name()).decode('utf-8') return empy_expand(snippet, self.get_environment_subs()) @staticmethod @@ -96,9 +91,6 @@ class Hostname(RockerExtension): def get_name(): return 'hostname' - def __init__(self): - self.name = Hostname.get_name() - def get_preamble(self, cliargs): return '' @@ -119,9 +111,6 @@ class Name(RockerExtension): def get_name(): return 'name' - def __init__(self): - self.name = Name.get_name() - def get_preamble(self, cliargs): return '' @@ -143,9 +132,6 @@ class Network(RockerExtension): def get_name(): return 'network' - def __init__(self): - self.name = Network.get_name() - def get_preamble(self, cliargs): return '' @@ -168,9 +154,6 @@ class Expose(RockerExtension): def get_name(): return 'expose' - def __init__(self): - self.name = Expose.get_name() - def get_preamble(self, cliargs): return '' @@ -194,9 +177,6 @@ class Port(RockerExtension): def get_name(): return 'port' - def __init__(self): - self.name = Port.get_name() - def get_preamble(self, cliargs): return '' @@ -222,8 +202,6 @@ def get_name(): def __init__(self): self._env_subs = None - self.name = PulseAudio.get_name() - def get_environment_subs(self): if not self._env_subs: @@ -237,7 +215,7 @@ def get_preamble(self, cliargs): return '' def get_snippet(self, cliargs): - snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8') + snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.get_name()).decode('utf-8') return empy_expand(snippet, self.get_environment_subs()) def get_docker_args(self, cliargs): @@ -258,9 +236,6 @@ class HomeDir(RockerExtension): def get_name(): return 'home' - def __init__(self): - self.name = HomeDir.get_name() - def get_docker_args(self, cliargs): return ' -v %s:%s ' % (Path.home(), Path.home()) @@ -288,10 +263,9 @@ def get_environment_subs(self): def __init__(self): self._env_subs = None - self.name = User.get_name() def get_snippet(self, cliargs): - snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.name).decode('utf-8') + snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.get_name()).decode('utf-8') substitutions = self.get_environment_subs() if 'user_override_name' in cliargs and cliargs['user_override_name']: substitutions['name'] = cliargs['user_override_name'] @@ -355,9 +329,6 @@ class Environment(RockerExtension): def get_name(): return 'env' - def __init__(self): - self.name = Environment.get_name() - def get_snippet(self, cli_args): return '' @@ -403,9 +374,6 @@ class Privileged(RockerExtension): def get_name(): return 'privileged' - def __init__(self): - self.name = Privileged.get_name() - def get_snippet(self, cli_args): return '' @@ -426,9 +394,6 @@ class GroupAdd(RockerExtension): def get_name(): return 'group_add' - def __init__(self): - self.name = GroupAdd.get_name() - def get_preamble(self, cliargs): return '' diff --git a/src/rocker/git_extension.py b/src/rocker/git_extension.py index b35c3979..92230e85 100644 --- a/src/rocker/git_extension.py +++ b/src/rocker/git_extension.py @@ -20,11 +20,9 @@ class Git(RockerExtension): - name = 'git' - - @classmethod - def get_name(cls): - return cls.name + @staticmethod + def get_name(): + return 'git' def get_docker_args(self, cli_args): diff --git a/src/rocker/nvidia_extension.py b/src/rocker/nvidia_extension.py index 37837ab5..de605b8b 100644 --- a/src/rocker/nvidia_extension.py +++ b/src/rocker/nvidia_extension.py @@ -40,7 +40,6 @@ def get_name(): return 'x11' def __init__(self): - self.name = X11.get_name() self._env_subs = None self._xauth = None @@ -85,7 +84,6 @@ def get_name(): def __init__(self): self._env_subs = None - self.name = Nvidia.get_name() self.supported_distros = ['Ubuntu', 'Debian GNU/Linux'] self.supported_versions = ['16.04', '18.04', '20.04', '10', '22.04'] @@ -116,11 +114,11 @@ def get_environment_subs(self, cliargs={}): return self._env_subs def get_preamble(self, cliargs): - preamble = pkgutil.get_data('rocker', 'templates/%s_preamble.Dockerfile.em' % self.name).decode('utf-8') - return empy_expand(preamble, self.get_environment_subs(cliargs)) + preamble = pkgutil.get_data('rocker', 'templates/%s_preamble.Dockerfile.em' % self.get_name()).decode('utf-8') + return em.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') + snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.get_name()).decode('utf-8') return empy_expand(snippet, self.get_environment_subs(cliargs)) def get_docker_args(self, cliargs): @@ -149,7 +147,6 @@ def get_name(): def __init__(self): self._env_subs = None - self.name = Cuda.get_name() self.supported_distros = ['Ubuntu', 'Debian GNU/Linux'] self.supported_versions = ['20.04', '22.04', '18.04', '11'] # Debian 11 @@ -184,11 +181,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 empy_expand(preamble, self.get_environment_subs(cliargs)) + # preamble = pkgutil.get_data('rocker', 'templates/%s_preamble.Dockerfile.em' % self.get_name()).decode('utf-8') + # return em.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') + snippet = pkgutil.get_data('rocker', 'templates/%s_snippet.Dockerfile.em' % self.get_name()).decode('utf-8') return empy_expand(snippet, self.get_environment_subs(cliargs)) def get_docker_args(self, cliargs): diff --git a/src/rocker/ssh_extension.py b/src/rocker/ssh_extension.py index 37de6c68..bc358c6d 100644 --- a/src/rocker/ssh_extension.py +++ b/src/rocker/ssh_extension.py @@ -21,11 +21,9 @@ class Ssh(RockerExtension): - name = 'ssh' - - @classmethod - def get_name(cls): - return cls.name + @staticmethod + def get_name(): + return 'ssh' def precondition_environment(self, cli_args): pass diff --git a/src/rocker/volume_extension.py b/src/rocker/volume_extension.py index 6f2626fb..24b90c78 100644 --- a/src/rocker/volume_extension.py +++ b/src/rocker/volume_extension.py @@ -21,11 +21,10 @@ class Volume(RockerExtension): ARG_DOCKER_VOLUME = "-v" ARG_ROCKER_VOLUME = "--volume" - name = 'volume' - @classmethod - def get_name(cls): - return cls.name + @staticmethod + def get_name(): + return 'volume' def get_docker_args(self, cli_args): """ @@ -40,7 +39,7 @@ def get_docker_args(self, cli_args): args = [''] # flatten cli_args['volume'] - volumes = [ x for sublist in cli_args[self.name] for x in sublist] + volumes = [ x for sublist in cli_args[self.get_name()] for x in sublist] for volume in volumes: elems = volume.split(':') diff --git a/test/test_core.py b/test/test_core.py index a258a5f8..ca4eab43 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -139,13 +139,13 @@ def test_extension_manager(self): def test_strict_required_extensions(self): class Foo(RockerExtension): - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'foo' class Bar(RockerExtension): - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'bar' def required(self, cli_args): @@ -163,13 +163,13 @@ def required(self, cli_args): def test_implicit_required_extensions(self): class Foo(RockerExtension): - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'foo' class Bar(RockerExtension): - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'bar' def required(self, cli_args): @@ -190,13 +190,13 @@ def required(self, cli_args): def test_extension_sorting(self): class Foo(RockerExtension): - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'foo' class Bar(RockerExtension): - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'bar' def invoke_after(self, cli_args): @@ -254,8 +254,8 @@ class UserSnippet(RockerExtension): def __init__(self): self.name = 'usersnippet' - @classmethod - def get_name(cls): + @staticmethod + def get_name(): return 'usersnippet' def get_snippet(self, cli_args): diff --git a/test/test_file_writing.py b/test/test_file_writing.py index 899b7660..b90988c9 100644 --- a/test/test_file_writing.py +++ b/test/test_file_writing.py @@ -39,12 +39,9 @@ def test_name_to_argument(self): self.assertEqual(name_to_argument('as-df'), '--as-df') class TestFileInjection(RockerExtension): - - name = 'test_file_injection' - - @classmethod - def get_name(cls): - return cls.name + @staticmethod + def get_name(): + return 'test_file_injection' def get_files(self, cliargs): all_files = {}