From c37ecee4560049a13620fae3f9498b6ac89743f2 Mon Sep 17 00:00:00 2001 From: Igor Chudov Date: Wed, 15 Jan 2020 16:24:10 +0400 Subject: [PATCH] Use util.paths in order to eliminate dependency on variable definitions --- gpoa/backend/samba_backend.py | 1 - gpoa/gpt/gpt.py | 25 +++++++++++++------------ gpoa/storage/sqlite_cache.py | 3 ++- gpoa/storage/sqlite_registry.py | 3 ++- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/gpoa/backend/samba_backend.py b/gpoa/backend/samba_backend.py index 0c689f9..1d90fdd 100644 --- a/gpoa/backend/samba_backend.py +++ b/gpoa/backend/samba_backend.py @@ -32,7 +32,6 @@ import util.preg from util.logging import slogm class samba_backend(applier_backend): - __default_policy_path = '/usr/share/local-policy/default' def __init__(self, sambacreds, username, domain): self.storage = registry_factory('registry') diff --git a/gpoa/gpt/gpt.py b/gpoa/gpt/gpt.py index a2a5b87..edfbc8e 100644 --- a/gpoa/gpt/gpt.py +++ b/gpoa/gpt/gpt.py @@ -31,15 +31,14 @@ from .envvars import read_envvars from .drives import read_drives import util import util.preg +from util.paths import ( + default_policy_path, + cache_dir, + local_policy_cache +) from util.logging import slogm -global __default_policy_path - -__default_policy_path = '/usr/share/local-policy/default' -__cache_dir = '/var/cache/gpupdate' - class gpt: - __default_policy_path = '/usr/share/local-policy/default' __user_policy_mode_key = 'Software\\Policies\\Microsoft\\Windows\\System\\UserPolicyMode' def __init__(self, gpt_path, sid): @@ -127,7 +126,10 @@ class gpt: ''' search_path = os.path.join(self._machine_path, 'Preferences', 'Shortcuts') if 'user' == part: - search_path = os.path.join(self._user_path, 'Preferences', 'Shortcuts') + try: + search_path = os.path.join(self._user_path, 'Preferences', 'Shortcuts') + except Exception as exc: + return None if not search_path: return None @@ -257,8 +259,7 @@ def lp2gpt(): ''' Convert local-policy to full-featured GPT. ''' - lppath = os.path.join(__default_policy_path, 'local.xml') - machinesettings = os.path.join(__default_policy_path, 'Machine') + lppath = os.path.join(default_policy_path(), 'local.xml') # Load settings from XML PolFile polparser = GPPolParser() @@ -266,7 +267,7 @@ def lp2gpt(): polparser.pol_file = polfile # Create target default policy directory if missing - destdir = os.path.join(__cache_dir, 'local-policy', 'Machine') + destdir = os.path.join(local_policy_cache(), 'Machine') os.makedirs(destdir, exist_ok=True) # Write PReg @@ -276,9 +277,9 @@ def get_local_gpt(sid): ''' Convert default policy to GPT and create object out of it. ''' + logging.debug(slogm('Re-caching Local Policy')) lp2gpt() - gpt_dir = os.path.join(__cache_dir, 'local-policy') - local_policy = gpt(gpt_dir, sid) + local_policy = gpt(str(local_policy_cache()), sid) local_policy.set_name('Local Policy') return local_policy diff --git a/gpoa/storage/sqlite_cache.py b/gpoa/storage/sqlite_cache.py index 01edd44..41acf63 100644 --- a/gpoa/storage/sqlite_cache.py +++ b/gpoa/storage/sqlite_cache.py @@ -34,6 +34,7 @@ from sqlalchemy.orm import ( ) from util.logging import slogm +from util.paths import cache_dir def mapping_factory(mapper_suffix): exec( @@ -47,7 +48,7 @@ class mapped_id_{}(object): return eval('mapped_id_{}'.format(mapper_suffix)) class sqlite_cache(cache): - __cache_dir = 'sqlite:////var/cache/samba' + __cache_dir = 'sqlite:///{}'.format(cache_dir()) def __init__(self, cache_name): self.cache_name = cache_name diff --git a/gpoa/storage/sqlite_registry.py b/gpoa/storage/sqlite_registry.py index cc1709c..a1c494a 100644 --- a/gpoa/storage/sqlite_registry.py +++ b/gpoa/storage/sqlite_registry.py @@ -33,6 +33,7 @@ from sqlalchemy.orm import ( ) from util.logging import slogm +from util.paths import cache_dir from .registry import registry class samba_preg(object): @@ -78,7 +79,7 @@ class printer_entry(object): self.printer = pobj.to_json() class sqlite_registry(registry): - __registry_path = 'sqlite:////var/cache/samba' + __registry_path = 'sqlite:///{}'.format(cache_dir()) def __init__(self, db_name): self.db_name = db_name