Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add folders to system PATH environment variable when they contain dll's #31

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/pysweepme/ErrorMessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@

import os

def error(*args):

def error(*args: object) -> None:
"""Print arguments to the debug log including an exception stacktrace.

Args:
*args: The arguments to print to the debug log.
"""
year, month, day, hour, min, sec = localtime()[:6]
print("-"*60)
print('Time: %s.%s.%s %02d:%02d:%02d' % (day, month, year, hour, min, sec))
Expand All @@ -38,8 +43,13 @@ def error(*args):
print('-'*60)


def debug(*args, debugmode_only=False):

def debug(*args: object, debugmode_only: bool = False) -> None:
"""Print arguments to the debug log.

Args:
*args: The arguments to print to the debug log.
debugmode_only: True if the arguments shall be printed only when debug mode is on.
"""
if "SWEEPME_DEBUGMODE" in os.environ:
debug_mode = os.environ["SWEEPME_DEBUGMODE"] == "True"
else:
Expand All @@ -54,5 +64,10 @@ def debug(*args, debugmode_only=False):
print('Debug: %s.%s.%s %02d:%02d:%02d\t' % (day, month, year, hour, min, sec), *args)


def debug_only(*args):
def debug_only(*args: object) -> None:
"""Print arguments to the debug log if debug mode is on.

Args:
*args: The arguments to print to the debug log.
"""
debug(*args, debugmode_only=True)
24 changes: 15 additions & 9 deletions src/pysweepme/FolderManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,23 @@ def _prepend_to_sys_path(path_to_prepend: Path) -> None:


def _add_libs_dirs_to_path(libs_path: Path) -> None:
# Only the main folder is only added to sys.path, as adding subfolders
# leads to problems with the import of submodules that have the
# same name as the main package.
# Only folders that actually contain dll files are added to the
# environment variable PATH, because it has a limit of 32767 characters
# which would be exceeded quickly otherwise.
if not libs_path.is_dir():
return
_prepend_to_os_path(libs_path)
_prepend_to_sys_path(libs_path)
# add also library.zip in libs
if (libs_path / "library.zip").exists():
_prepend_to_sys_path(libs_path / "library.zip")

subfolders = [subfolder for subfolder in libs_path.rglob("*")
if subfolder.is_dir() and subfolder.name != "__pycache__"]
for folder in subfolders:
# we only update os.environ["PATH"] but not sys.path as this
# leads to problems with the import of submodules that have the
# same name as the main package
_prepend_to_os_path(folder)
# Find all dll files and then take the directory they are in
dll_folders = {dll_file.parent for dll_file in libs_path.rglob("*.dll") if dll_file.is_file()}
for dll_folder in dll_folders:
_prepend_to_os_path(dll_folder)


def addFolderToPATH(path_to_add: str = "") -> bool:
Expand All @@ -83,7 +85,11 @@ def addFolderToPATH(path_to_add: str = "") -> bool:
return False

_prepend_to_sys_path(main_path)
_prepend_to_os_path(main_path)
# Only add the main path to the OS PATH if it actually contains a dll.
# Although this should not happen, as dll's should be in the libs 32bit or 64bit directories.
# We use any(...) so that the glob Generator can stop when the first dll is found.
if any(main_path.glob("*.dll")):
_prepend_to_os_path(main_path)

libs_paths = [
main_path / "libraries" / f"libs_{version_info.python_suffix}", # architecture specific
Expand Down
2 changes: 1 addition & 1 deletion src/pysweepme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# SOFTWARE.


__version__ = "1.5.6.5"
__version__ = "1.5.6.6"

import sys

Expand Down