Skip to content

Commit 6412fea

Browse files
[sonic-package-manager] remove make_python_identifier (#1801)
#### What I did It is not needed to make a CLI plugin name with valid python identifier. It is also possible that make_python_identifier will return same filename for two different packages. #### How I did it Removed make_python_identifier. #### How to verify it Install package with CLI plugin.
1 parent f738818 commit 6412fea

File tree

3 files changed

+32
-59
lines changed

3 files changed

+32
-59
lines changed

sonic_package_manager/manager.py

+32-14
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ def parse_reference_expression(expression):
140140
return PackageReference.parse(expression)
141141

142142

143+
def get_cli_plugin_directory(command: str) -> str:
144+
""" Returns a plugins package directory for command group.
145+
146+
Args:
147+
command: SONiC command: "show"/"config"/"clear".
148+
Returns:
149+
Path to plugins package directory.
150+
"""
151+
152+
pkg_loader = pkgutil.get_loader(f'{command}.plugins')
153+
if pkg_loader is None:
154+
raise PackageManagerError(f'Failed to get plugins path for {command} CLI')
155+
plugins_pkg_path = os.path.dirname(pkg_loader.path)
156+
return plugins_pkg_path
157+
158+
159+
def get_cli_plugin_path(package: Package, command: str) -> str:
160+
""" Returns a path where to put CLI plugin code.
161+
162+
Args:
163+
package: Package to generate this path for.
164+
command: SONiC command: "show"/"config"/"clear".
165+
Returns:
166+
Path generated for this package.
167+
"""
168+
169+
plugin_module_file = package.name + '.py'
170+
return os.path.join(get_cli_plugin_directory(command), plugin_module_file)
171+
172+
143173
def validate_package_base_os_constraints(package: Package, sonic_version_info: Dict[str, str]):
144174
""" Verify that all dependencies on base OS components are met.
145175
Args:
@@ -917,18 +947,6 @@ def _systemctl_action(self, package: Package, action: str):
917947
for npu in range(self.num_npus):
918948
run_command(f'systemctl {action} {name}@{npu}')
919949

920-
@staticmethod
921-
def _get_cli_plugin_name(package: Package):
922-
return utils.make_python_identifier(package.name) + '.py'
923-
924-
@classmethod
925-
def _get_cli_plugin_path(cls, package: Package, command):
926-
pkg_loader = pkgutil.get_loader(f'{command}.plugins')
927-
if pkg_loader is None:
928-
raise PackageManagerError(f'Failed to get plugins path for {command} CLI')
929-
plugins_pkg_path = os.path.dirname(pkg_loader.path)
930-
return os.path.join(plugins_pkg_path, cls._get_cli_plugin_name(package))
931-
932950
def _install_cli_plugins(self, package: Package):
933951
for command in ('show', 'config', 'clear'):
934952
self._install_cli_plugin(package, command)
@@ -941,14 +959,14 @@ def _install_cli_plugin(self, package: Package, command: str):
941959
image_plugin_path = package.manifest['cli'][command]
942960
if not image_plugin_path:
943961
return
944-
host_plugin_path = self._get_cli_plugin_path(package, command)
962+
host_plugin_path = get_cli_plugin_path(package, command)
945963
self.docker.extract(package.entry.image_id, image_plugin_path, host_plugin_path)
946964

947965
def _uninstall_cli_plugin(self, package: Package, command: str):
948966
image_plugin_path = package.manifest['cli'][command]
949967
if not image_plugin_path:
950968
return
951-
host_plugin_path = self._get_cli_plugin_path(package, command)
969+
host_plugin_path = get_cli_plugin_path(package, command)
952970
if os.path.exists(host_plugin_path):
953971
os.remove(host_plugin_path)
954972

sonic_package_manager/utils.py

-37
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,5 @@
11
#!/usr/bin/env python
22

3-
import keyword
4-
import re
5-
63
from docker_image.reference import Reference
74

85
DockerReference = Reference
9-
10-
11-
def make_python_identifier(string):
12-
"""
13-
Takes an arbitrary string and creates a valid Python identifier.
14-
15-
Identifiers must follow the convention outlined here:
16-
https://docs.python.org/2/reference/lexical_analysis.html#identifiers
17-
"""
18-
19-
# create a working copy (and make it lowercase, while we're at it)
20-
s = string.lower()
21-
22-
# remove leading and trailing whitespace
23-
s = s.strip()
24-
25-
# Make spaces into underscores
26-
s = re.sub('[\\s\\t\\n]+', '_', s)
27-
28-
# Remove invalid characters
29-
s = re.sub('[^0-9a-zA-Z_]', '', s)
30-
31-
# Remove leading characters until we find a letter or underscore
32-
s = re.sub('^[^a-zA-Z_]+', '', s)
33-
34-
# Check that the string is not a python identifier
35-
while s in keyword.kwlist:
36-
if re.match(".*?_\d+$", s):
37-
i = re.match(".*?_(\d+)$", s).groups()[0]
38-
s = s.strip('_'+i) + '_'+str(int(i)+1)
39-
else:
40-
s += '_1'
41-
42-
return s

tests/sonic_package_manager/test_utils.py

-8
This file was deleted.

0 commit comments

Comments
 (0)