Skip to content
Snippets Groups Projects
Commit 3bfa6475 authored by Robert K's avatar Robert K
Browse files

[cleanup][Python] Remove portalocker dependency.

parent 91ea1984
No related branches found
No related tags found
No related merge requests found
Pipeline #41582 passed with warnings
Pipeline: Dune Nightly Test

#41585

    ......@@ -4,5 +4,5 @@ Author: The Dune Core developers
    Maintainer: dune-devel@lists.dune-project.org
    Description: Basis infrastructure for all Dune modules
    URL: https://gitlab.dune-project.org/core/dune-common
    Python-Requires: pip>=21 portalocker numpy wheel jinja2 mpi4py
    Python-Requires: pip>=21 numpy wheel jinja2 mpi4py
    Whitespace-Hook: Yes
    ......@@ -10,5 +10,5 @@
    # included in the package install from source.
    #
    [build-system]
    requires = ['cmake', 'jinja2', 'mpi4py', 'ninja', 'numpy', 'pip>=21.0.0', 'portalocker', 'requests', 'scikit-build', 'setuptools', 'wheel']
    requires = ['cmake', 'jinja2', 'mpi4py', 'ninja', 'numpy', 'pip>=21.0.0', 'requests', 'scikit-build', 'setuptools', 'wheel']
    build-backend = 'setuptools.build_meta'
    ## By default use portalocker, if available.
    ## However, portalocker is not available on all systems
    ## and therefore a fallback implementation is also provided
    try:
    from portalocker import Lock as _Lock
    from portalocker.constants import LOCK_EX, LOCK_SH
    class Lock(_Lock):
    def __init__(self, path, flags, *args, **kwargs):
    _Lock.__init__(self,path,*args,flags=flags,timeout=None,**kwargs)
    except ModuleNotFoundError:
    ## Fallback implementation if portalocker was not found
    ## This is still needed! Do not remove it.
    import os
    import fcntl
    from fcntl import LOCK_EX, LOCK_SH
    # file locking from fcntl
    def lock_file(f, cmd=fcntl.LOCK_EX):
    fcntl.flock(f, cmd)
    return f
    def unlock_file(f):
    fcntl.flock(f, fcntl.LOCK_UN)
    return f
    import os
    import fcntl
    from fcntl import LOCK_EX, LOCK_SH
    # file locking from fcntl
    def lock_file(f, cmd=fcntl.LOCK_EX):
    fcntl.flock(f, cmd)
    return f
    def unlock_file(f):
    fcntl.flock(f, fcntl.LOCK_UN)
    return f
    # This file opener *must* be used in a "with" block.
    class Lock:
    # Open the file with arguments provided by user. Then acquire
    # a lock on that file object (WARNING: Advisory locking).
    def __init__(self, path, flags, *args, **kwargs):
    # Open the file and acquire a lock on the file before operating
    self.file = open(path, mode='w+', *args, **kwargs)
    # Lock the opened file
    self.file = lock_file(self.file, flags) # flags are either LOCK_EX or LOCK_SH
    # This file opener *must* be used in a "with" block.
    class Lock:
    # Open the file with arguments provided by user. Then acquire
    # a lock on that file object (WARNING: Advisory locking).
    def __init__(self, path, flags, *args, **kwargs):
    # Open the file and acquire a lock on the file before operating
    self.file = open(path, mode='w+', *args, **kwargs)
    # Lock the opened file
    self.file = lock_file(self.file, flags) # flags are either LOCK_EX or LOCK_SH
    # Return the opened file object (knowing a lock has been obtained).
    def __enter__(self, *args, **kwargs): return self.file
    # Return the opened file object (knowing a lock has been obtained).
    def __enter__(self, *args, **kwargs): return self.file
    # Unlock the file and close the file object.
    def __exit__(self, exc_type=None, exc_value=None, traceback=None):
    # Flush to make sure all buffered contents are written to file.
    self.file.flush()
    os.fsync(self.file.fileno())
    # Release the lock on the file.
    self.file = unlock_file(self.file)
    self.file.close()
    # Handle exceptions that may have come up during execution, by
    # default any exceptions are raised to the user.
    return exc_type == None
    # Unlock the file and close the file object.
    def __exit__(self, exc_type=None, exc_value=None, traceback=None):
    # Flush to make sure all buffered contents are written to file.
    self.file.flush()
    os.fsync(self.file.fileno())
    # Release the lock on the file.
    self.file = unlock_file(self.file)
    self.file.close()
    # Handle exceptions that may have come up during execution, by
    # default any exceptions are raised to the user.
    return exc_type == None
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment