Skip to content

Commit

Permalink
Merge pull request #983 from iranzo/imp_vs_importlib
Browse files Browse the repository at this point in the history
Handle imp and importlib cases
  • Loading branch information
iranzo authored Dec 16, 2024
2 parents e220669 + 8ef6883 commit d862978
Show file tree
Hide file tree
Showing 25 changed files with 182 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401, E402, E722, C901, E231
max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9
3 changes: 0 additions & 3 deletions .github/workflows/refresh-authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ on:
- master
# Compare the last commit of master -> to the current commit of a PR branch.
# (Note: To compare changes between the last pushed commit to the remote PR branch set `since_last_remote_commit: true`)
pull_request:
branches:
- master

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
known_third_party = imp,pytest,setuptools
36 changes: 19 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
stages:
- commit-msg
repo: https://github.com/commitizen-tools/commitizen
rev: v4.1.0
rev: v3.29.0
- hooks:
- id: check-useless-excludes
repo: meta
Expand All @@ -15,13 +15,13 @@ repos:
id: prettier
repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8

- hooks:
- id: black
repo: https://github.com/python/black
rev: 24.10.0
rev: 24.8.0
- hooks:
- id: check-added-large-files
args: [--maxkb=10000]
- id: check-ast
- id: check-case-conflict
- id: check-executables-have-shebangs
Expand All @@ -48,13 +48,20 @@ repos:
- id: check-byte-order-marker
- id: check-docstring-first
repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
- hooks:
- id: flake8
repo: https://github.com/pycqa/flake8
rev: 7.1.1
rev: v4.6.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.9
hooks:
# Run the linter.
- id: ruff
args:
- --fix
# Run the formatter.
- id: ruff-format

- repo: https://github.com/scop/pre-commit-shfmt
rev: v3.10.0-2
rev: v3.9.0-1
hooks:
# Choose one of:
- id: shfmt # native (requires/installs Go to build)
Expand All @@ -63,13 +70,13 @@ repos:
- -i
- "4"
- -s

types:
- shell

- hooks:
- id: blacken-docs
repo: https://github.com/asottile/blacken-docs
rev: 1.19.1
rev: 1.18.0

# - repo: https://github.com/asottile/pyupgrade
# rev: v2.38.0
Expand All @@ -84,13 +91,8 @@ repos:
args: [--mapping, "2", --sequence, "4", --offset, "2", "--preserve-quotes"]

# - repo: https://github.com/hcodes/yaspeller.git
# rev: v8.0.1
# rev: v10.0.1
# hooks:
# - id: yaspeller
# types:
# - markdown

- repo: https://github.com/asottile/reorder-python-imports
rev: v3.14.0
hooks:
- id: reorder-python-imports
6 changes: 4 additions & 2 deletions maguiclient/magui.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ def autogroups(autodata):
# Precreate groups
for element in hostsdict:
for item in iter(hostsdict[element].items()):
if not item[0] in groups:
if item[0] not in groups:
groups["%s" % item[0]] = {}
if not item[1] in groups["%s" % item[0]]:
if item[1] not in groups["%s" % item[0]]:
groups["%s" % item[0]]["%s" % item[1]] = [element]
else:
groups["%s" % item[0]]["%s" % item[1]].append(element)
Expand Down Expand Up @@ -726,6 +726,8 @@ def runmaguiandplugs(
# Now we've Magui saved for the whole execution provided in 'results' var

# Start working on autogroups
autodata = ""

for result in results:
if result["plugin"] == "metadata-outputs":
autodata = result["result"]["err"]
Expand Down
2 changes: 1 addition & 1 deletion maguiclient/plugins/openstack/mysql/seqno.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run(data, quiet=False): # do not edit this line
# being UUID: seqno
err = [
data[ourdata]["sosreport"][sosreport]["err"]
for sosreport in data[ourdata["sosreport"]]
for sosreport in data[ourdata]["sosreport"]
]

if len(sorted(set(err))) != 1:
Expand Down
3 changes: 1 addition & 2 deletions risuclient/plugins/core/system/reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import os
import re
import sys
from datetime import datetime
from datetime import timedelta
from datetime import datetime, timedelta

# Getting environment
global root_path
Expand Down
3 changes: 1 addition & 2 deletions risuclient/plugins/metadata/events/reboots.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import os
import re
import sys
from datetime import datetime
from datetime import timedelta
from datetime import datetime, timedelta

# Getting environment
global root_path
Expand Down
70 changes: 66 additions & 4 deletions risuclient/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,66 @@
import os
import re
import tempfile
import sys


if sys.version_info >= (3, 4):
# Handle imp deprecation towards importlib

import importlib.util

def dynamic_import(name, info):
file_path = info[1]
spec = importlib.util.spec_from_file_location(name, file_path)
if spec is None:
raise ImportError(f"Cannot find module spec for {name} at {file_path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module

def find_module(name, program_paths=None):
"""
Locate a module using importlib.util.find_spec and additional program-specific paths.
:param name: Name of the module
:param program_paths: List of additional paths to search for the module
:return: Tuple (file, pathname, description) for compatibility
"""
search_paths = program_paths if program_paths else []
for path in search_paths:
module_path = os.path.join(path, name + ".py")
if os.path.isfile(module_path):
return None, module_path, ("", "", importlib.machinery.SOURCE_SUFFIXES)

# Fallback to standard find_spec
spec = importlib.util.find_spec(name)
if spec and spec.origin:
return None, spec.origin, ("", "", importlib.machinery.SOURCE_SUFFIXES)

raise ImportError(f"Cannot find module {name}")

else:
import imp

def dynamic_import(name, info):
return imp.load_module(name, *info)

def find_module(name, program_paths=None):
"""
Locate a module using imp.find_module and additional program-specific paths.
:param name: Name of the module
:param program_paths: List of additional paths to search for the module
:return: Tuple (file, pathname, description)
"""
search_paths = program_paths if program_paths else []
for path in search_paths:
try:
return imp.find_module(name, [path])
except ImportError:
continue

# Fallback to standard find_module
return imp.find_module(name)

import imp

# Do not require everyone to use requests
try:
Expand Down Expand Up @@ -142,7 +200,7 @@ def getExtensions(folder=ExtensionFolder):
if i != "__init__.py" and os.path.splitext(i)[1] == ".py":
i = os.path.splitext(i)[0]
try:
info = imp.find_module(i, [folder])
info = find_module(i, program_paths=[folder])
except:
info = False
if i and info:
Expand All @@ -157,7 +215,8 @@ def loadPymodules(Extension):
:param Extension: Extension to load
:return: loader for Extension
"""
return imp.load_module(Extension["name"], *Extension["info"])

return dynamic_import(Extension["name"], Extension["info"])


def initPymodules(extensions=getExtensions()):
Expand All @@ -167,11 +226,14 @@ def initPymodules(extensions=getExtensions()):
"""

exts = []

exttriggers = {}

for i in extensions:
newplug = loadPymodules(i)
exts.append(newplug)
triggers = []

for each in newplug.init():
triggers.append(each)
exttriggers[i["name"]] = triggers
Expand Down Expand Up @@ -208,7 +270,7 @@ def getPymodules(options=None, folders=[HooksFolder]):
module = os.path.splitext(os.path.basename(i))[0]
modpath = os.path.dirname(i)
try:
info = imp.find_module(module, [modpath])
info = find_module(module, program_paths=[modpath])
except:
info = False
if i and info:
Expand Down
2 changes: 2 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[lint]
ignore = ["E722"]
8 changes: 6 additions & 2 deletions tests/plugins-unit-tests/test_all_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
from unittest import TestCase

import maguiclient.magui as magui
import risuclient.shell as risu

sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../"))
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu


testplugins = os.path.join(risu.risudir, "plugins", "test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import sys
from unittest import TestCase

sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../"))
import risuclient.shell as risu
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu

testplugins = os.path.join(risu.risudir, "plugins", "test")
pluginsdir = os.path.join(risu.risudir, "plugins", "core")
Expand Down
8 changes: 6 additions & 2 deletions tests/plugins-unit-tests/test_brackets-parenthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import sys
from unittest import TestCase

sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../"))
import risuclient.shell as risu
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu

testplugins = os.path.join(risu.risudir, "plugins", "test")
risudir = risu.risudir
Expand Down
8 changes: 7 additions & 1 deletion tests/plugins-unit-tests/test_bugzilla_httpd_bug_1406417.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
import os
import shutil
import subprocess
import sys
import tempfile
from unittest import TestCase

import risuclient.shell as risu
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu

# To create your own test, update NAME with plugin name and copy this file to test_$NAME.py
NAME = "1406417"
Expand Down
9 changes: 7 additions & 2 deletions tests/plugins-unit-tests/test_executable_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import sys
from unittest import TestCase

sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../"))
import risuclient.shell as risu
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu


testplugins = os.path.join(risu.risudir, "plugins", "test")
risudir = risu.risudir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
import os
import shutil
import subprocess
import sys
import tempfile
from unittest import TestCase

import risuclient.shell as risu
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu

# To create your own test, update NAME with plugin name and copy this file to test_$NAME.py
NAME = "freeradius_incompatible_regex"
Expand Down
7 changes: 6 additions & 1 deletion tests/plugins-unit-tests/test_have_description_longname.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
# encoding: utf-8
# Copyright (C) 2019, 2022, 2023 Pablo Iranzo Gómez <[email protected]>
import os
import sys
from unittest import TestCase

import pytest

import risuclient.shell as risu
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/" + "../" + "../"))

try:
import risuclient.shell as risu
except:
import shell as risu
testplugins = os.path.join(risu.risudir, "plugins", "test")
risudir = risu.risudir

Expand Down
Loading

0 comments on commit d862978

Please sign in to comment.