Skip to content

Commit 89ba629

Browse files
author
Jean-Michel Perraud
committed
Add env. var. update functions used by several packages that depend on refcount
1 parent 487025c commit 89ba629

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

refcount/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
66
Doing it this way provides for access in setup.py and via __version__
77
"""
8-
__version__ = "0.9.2"
8+
__version__ = "0.9.3"

refcount/putils.py

+37-20
Original file line numberDiff line numberDiff line change
@@ -174,29 +174,46 @@ def prepend_path_env(
174174
# return (x is None or x == '')
175175

176176

177-
# # The following is useful, but idiosyncratic. Consider and rethink..
178-
# class EnvironmenVariablesUpdates:
177+
# # The following is useful, but idiosyncratic. Consider and rethink.
178+
def build_new_path_env (from_env='LIBRARY_PATH', to_env='PATH', lib_short_fname='unknown.dll') -> str:
179+
"""Propose an update to an existing environment variable, based on the path(s) specified in another environment variable. This function is effectively meant to be useful on Windows only.
179180
180-
# def __init__(self) -> None:
181-
# pass
181+
Args:
182+
from_env (str, optional): name of the source environment variable specifying the location(s) of custom libraries to load. Defaults to 'LIBRARY_PATH'.
183+
to_env (str, optional): environment variable to update, most likely the Windows PATH env var. Defaults to 'PATH'.
184+
lib_short_fname (str, optional): short file name of the custom library to load. This information is optional and used only for possible warning/log output messages. Defaults to 'unknown.dll'.
182185
183-
# def build_new_path_env (self, from_env='LIBRARY_PATH', to_env='PATH'):
184-
# if(sys.platform == 'win32'):
185-
# path_sep = ';'
186-
# shared_lib_paths = os.environ.get(from_env)
187-
# if(shared_lib_paths is not None):
188-
# arch = os.environ["PROCESSOR_ARCHITECTURE"]
189-
# if arch == 'AMD64':
190-
# subfolder = '64'
191-
# else:
192-
# subfolder = '32'
193-
# shared_lib_paths_vec = shared_lib_paths.split(path_sep)
194-
# return prepend_path_env(shared_lib_paths_vec, subfolder, to_env=to_env)
195-
196-
# def update_path_windows (self, from_env='LIBRARY_PATH', to_env='PATH'):
197-
# if(sys.platform == 'win32'):
198-
# os.environ[to_env] = build_new_path_env(from_env, to_env)
186+
Returns:
187+
str: the proposed updated content for the 'to_env' environment variable.
188+
"""
189+
if(sys.platform == 'win32'):
190+
path_sep = ';'
191+
shared_lib_paths = os.environ.get(from_env)
192+
if(shared_lib_paths is not None):
193+
# startup_msg = appendstartup_msg(paste0('Found env var ', from_env, '=', shared_lib_paths), startup_msg)
194+
arch = os.environ["PROCESSOR_ARCHITECTURE"]
195+
if arch == 'AMD64':
196+
subfolder = '64'
197+
else:
198+
subfolder = '32'
199+
shared_lib_paths_vec = shared_lib_paths.split(path_sep)
200+
return prepend_path_env(shared_lib_paths_vec, subfolder, to_env=to_env)
201+
else:
202+
print("WARNING: a function was called to look for environment variable '{0}' to update the environment variable '{1}', but was not found. This may be fine, but if the package fails to load because '{2}' is not found, this is a likely cause.".format(from_env, to_env, lib_short_fname))
199203

204+
def update_path_windows (from_env='LIBRARY_PATH', to_env='PATH', lib_short_fname='unknown.dll') -> None:
205+
"""If called on Windows, append an environment variable, based on the path(s) specified in another environment variable. This function is effectively meant to be useful on Windows only.
206+
207+
Args:
208+
from_env (str, optional): name of the source environment variable specifying the location(s) of custom libraries to load. Defaults to 'LIBRARY_PATH'.
209+
to_env (str, optional): environment variable to update, most likely the Windows PATH env var. Defaults to 'PATH'.
210+
lib_short_fname (str, optional): short file name of the custom library to load. This information is optional and used only for possible warning/log output messages. Defaults to 'unknown.dll'.
211+
212+
Returns:
213+
None
214+
"""
215+
if(sys.platform == 'win32'):
216+
os.environ[to_env] = build_new_path_env(from_env, to_env, lib_short_fname)
200217

201218

202219

0 commit comments

Comments
 (0)