Skip to content

Commit

Permalink
[sonic-package-manager] remove make_python_identifier (sonic-net#1801)
Browse files Browse the repository at this point in the history
#### 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.
  • Loading branch information
stepanblyschak authored Oct 18, 2021
1 parent f738818 commit 6412fea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
46 changes: 32 additions & 14 deletions sonic_package_manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ def parse_reference_expression(expression):
return PackageReference.parse(expression)


def get_cli_plugin_directory(command: str) -> str:
""" Returns a plugins package directory for command group.
Args:
command: SONiC command: "show"/"config"/"clear".
Returns:
Path to plugins package directory.
"""

pkg_loader = pkgutil.get_loader(f'{command}.plugins')
if pkg_loader is None:
raise PackageManagerError(f'Failed to get plugins path for {command} CLI')
plugins_pkg_path = os.path.dirname(pkg_loader.path)
return plugins_pkg_path


def get_cli_plugin_path(package: Package, command: str) -> str:
""" Returns a path where to put CLI plugin code.
Args:
package: Package to generate this path for.
command: SONiC command: "show"/"config"/"clear".
Returns:
Path generated for this package.
"""

plugin_module_file = package.name + '.py'
return os.path.join(get_cli_plugin_directory(command), plugin_module_file)


def validate_package_base_os_constraints(package: Package, sonic_version_info: Dict[str, str]):
""" Verify that all dependencies on base OS components are met.
Args:
Expand Down Expand Up @@ -917,18 +947,6 @@ def _systemctl_action(self, package: Package, action: str):
for npu in range(self.num_npus):
run_command(f'systemctl {action} {name}@{npu}')

@staticmethod
def _get_cli_plugin_name(package: Package):
return utils.make_python_identifier(package.name) + '.py'

@classmethod
def _get_cli_plugin_path(cls, package: Package, command):
pkg_loader = pkgutil.get_loader(f'{command}.plugins')
if pkg_loader is None:
raise PackageManagerError(f'Failed to get plugins path for {command} CLI')
plugins_pkg_path = os.path.dirname(pkg_loader.path)
return os.path.join(plugins_pkg_path, cls._get_cli_plugin_name(package))

def _install_cli_plugins(self, package: Package):
for command in ('show', 'config', 'clear'):
self._install_cli_plugin(package, command)
Expand All @@ -941,14 +959,14 @@ def _install_cli_plugin(self, package: Package, command: str):
image_plugin_path = package.manifest['cli'][command]
if not image_plugin_path:
return
host_plugin_path = self._get_cli_plugin_path(package, command)
host_plugin_path = get_cli_plugin_path(package, command)
self.docker.extract(package.entry.image_id, image_plugin_path, host_plugin_path)

def _uninstall_cli_plugin(self, package: Package, command: str):
image_plugin_path = package.manifest['cli'][command]
if not image_plugin_path:
return
host_plugin_path = self._get_cli_plugin_path(package, command)
host_plugin_path = get_cli_plugin_path(package, command)
if os.path.exists(host_plugin_path):
os.remove(host_plugin_path)

Expand Down
37 changes: 0 additions & 37 deletions sonic_package_manager/utils.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
#!/usr/bin/env python

import keyword
import re

from docker_image.reference import Reference

DockerReference = Reference


def make_python_identifier(string):
"""
Takes an arbitrary string and creates a valid Python identifier.
Identifiers must follow the convention outlined here:
https://docs.python.org/2/reference/lexical_analysis.html#identifiers
"""

# create a working copy (and make it lowercase, while we're at it)
s = string.lower()

# remove leading and trailing whitespace
s = s.strip()

# Make spaces into underscores
s = re.sub('[\\s\\t\\n]+', '_', s)

# Remove invalid characters
s = re.sub('[^0-9a-zA-Z_]', '', s)

# Remove leading characters until we find a letter or underscore
s = re.sub('^[^a-zA-Z_]+', '', s)

# Check that the string is not a python identifier
while s in keyword.kwlist:
if re.match(".*?_\d+$", s):
i = re.match(".*?_(\d+)$", s).groups()[0]
s = s.strip('_'+i) + '_'+str(int(i)+1)
else:
s += '_1'

return s
8 changes: 0 additions & 8 deletions tests/sonic_package_manager/test_utils.py

This file was deleted.

0 comments on commit 6412fea

Please sign in to comment.