From bbf1815848ce943575c41624f55a56da1f047224 Mon Sep 17 00:00:00 2001 From: ahaw021 Date: Fri, 21 Jul 2017 12:40:57 +0800 Subject: [PATCH] Windows Challenges Part 1 - Locks Added import logic that will only use FCNTL on Linux hosts. Added a Class for Windows Lock Files Added methods to preform same functions as linux locking (makes code easier to port) Added if clause at the beginning so windows methods are called for locking. --- certbot/lock.py | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/certbot/lock.py b/certbot/lock.py index 5f59cc090..6aae985e1 100644 --- a/certbot/lock.py +++ b/certbot/lock.py @@ -1,9 +1,14 @@ """Implements file locks for locking files and directories in UNIX.""" import errno -import fcntl import logging import os +try: + import fcntl +except ImportError: + print("No File Locking") +#TO-DO add a windows file locking library currently thinking: http://portalocker.readthedocs.io/en/latest/ + from certbot import errors logger = logging.getLogger(__name__) @@ -14,6 +19,8 @@ def lock_dir(dir_path): The lock file is placed in the root of dir_path with the name .certbot.lock. + + Different locking libraries are used depending on the OS. :param str dir_path: path to directory @@ -23,8 +30,10 @@ def lock_dir(dir_path): :raises errors.LockError: if unable to acquire the lock """ - return LockFile(os.path.join(dir_path, '.certbot.lock')) - + if os.name == 'posix': + return LockFile(os.path.join(dir_path, '.certbot.lock')) + if os.name == 'nt': + return LockFileWindows(os.path.join(dir_path, '.certbot.lock')) class LockFile(object): """A UNIX lock file. @@ -137,3 +146,24 @@ class LockFile(object): os.close(self._fd) finally: self._fd = None + +class LockFileWindows(object): + print("Locking File Windows Library") + """A Windows lock file. + + This lock file is released when the locked file is closed or the + process exits. + + """ + def __init__(self, path): + print("Windows Lock Library -- __inti__ Called") + def acquire(self): + print("Windows Lock Library -- acquire Called") + def _try_lock(self, fd): + print("Windows Lock Library -- _try_locks Called") + def _lock_success(self, fd): + print("Windows Lock Library -- _lock_success Called") + def __repr__(self): + print("Windows Lock Library -- __repr__ Called") + def release(self): + print("Windows Lock Library - release Called")