Browse Source

move locking code to own module, same for locking tests

fix imports, no other changes.
Thomas Waldmann 10 years ago
parent
commit
434dac0e48
7 changed files with 77 additions and 67 deletions
  1. 2 1
      borg/cache.py
  2. 2 42
      borg/helpers.py
  3. 43 0
      borg/locking.py
  4. 2 1
      borg/repository.py
  5. 2 22
      borg/testsuite/helpers.py
  6. 24 0
      borg/testsuite/locking.py
  7. 2 1
      borg/testsuite/repository.py

+ 2 - 1
borg/cache.py

@@ -10,8 +10,9 @@ import tarfile
 import tempfile
 
 from .key import PlaintextKey
-from .helpers import Error, get_cache_dir, decode_dict, st_mtime_ns, unhexlify, UpgradableLock, int_to_bigint, \
+from .helpers import Error, get_cache_dir, decode_dict, st_mtime_ns, unhexlify, int_to_bigint, \
     bigint_to_int
+from .locking import UpgradableLock
 from .hashindex import ChunkIndex
 
 

+ 2 - 42
borg/helpers.py

@@ -2,7 +2,6 @@ import argparse
 import binascii
 from collections import namedtuple
 import grp
-import msgpack
 import os
 import pwd
 import re
@@ -11,7 +10,8 @@ import time
 from datetime import datetime, timezone, timedelta
 from fnmatch import translate
 from operator import attrgetter
-import fcntl
+
+import msgpack
 
 from . import hashindex
 from . import chunker
@@ -31,46 +31,6 @@ class ExtensionModuleError(Error):
     """The Borg binary extension modules do not seem to be properly installed"""
 
 
-class UpgradableLock:
-
-    class ReadLockFailed(Error):
-        """Failed to acquire read lock on {}"""
-
-    class WriteLockFailed(Error):
-        """Failed to acquire write lock on {}"""
-
-    def __init__(self, path, exclusive=False):
-        self.path = path
-        try:
-            self.fd = open(path, 'r+')
-        except IOError:
-            self.fd = open(path, 'r')
-        try:
-            if exclusive:
-                fcntl.lockf(self.fd, fcntl.LOCK_EX)
-            else:
-                fcntl.lockf(self.fd, fcntl.LOCK_SH)
-        # Python 3.2 raises IOError, Python3.3+ raises OSError
-        except (IOError, OSError):
-            if exclusive:
-                raise self.WriteLockFailed(self.path)
-            else:
-                raise self.ReadLockFailed(self.path)
-        self.is_exclusive = exclusive
-
-    def upgrade(self):
-        try:
-            fcntl.lockf(self.fd, fcntl.LOCK_EX)
-        # Python 3.2 raises IOError, Python3.3+ raises OSError
-        except (IOError, OSError):
-            raise self.WriteLockFailed(self.path)
-        self.is_exclusive = True
-
-    def release(self):
-        fcntl.lockf(self.fd, fcntl.LOCK_UN)
-        self.fd.close()
-
-
 def check_extension_modules():
     from . import platform
     if hashindex.API_VERSION != 2:

+ 43 - 0
borg/locking.py

@@ -0,0 +1,43 @@
+import fcntl
+
+from borg.helpers import Error
+
+
+class UpgradableLock:
+
+    class ReadLockFailed(Error):
+        """Failed to acquire read lock on {}"""
+
+    class WriteLockFailed(Error):
+        """Failed to acquire write lock on {}"""
+
+    def __init__(self, path, exclusive=False):
+        self.path = path
+        try:
+            self.fd = open(path, 'r+')
+        except IOError:
+            self.fd = open(path, 'r')
+        try:
+            if exclusive:
+                fcntl.lockf(self.fd, fcntl.LOCK_EX)
+            else:
+                fcntl.lockf(self.fd, fcntl.LOCK_SH)
+        # Python 3.2 raises IOError, Python3.3+ raises OSError
+        except (IOError, OSError):
+            if exclusive:
+                raise self.WriteLockFailed(self.path)
+            else:
+                raise self.ReadLockFailed(self.path)
+        self.is_exclusive = exclusive
+
+    def upgrade(self):
+        try:
+            fcntl.lockf(self.fd, fcntl.LOCK_EX)
+        # Python 3.2 raises IOError, Python3.3+ raises OSError
+        except (IOError, OSError):
+            raise self.WriteLockFailed(self.path)
+        self.is_exclusive = True
+
+    def release(self):
+        fcntl.lockf(self.fd, fcntl.LOCK_UN)
+        self.fd.close()

+ 2 - 1
borg/repository.py

@@ -9,7 +9,8 @@ import sys
 from zlib import crc32
 
 from .hashindex import NSIndex
-from .helpers import Error, IntegrityError, read_msgpack, write_msgpack, unhexlify, UpgradableLock
+from .helpers import Error, IntegrityError, read_msgpack, write_msgpack, unhexlify
+from .locking import UpgradableLock
 from .lrucache import LRUCache
 
 MAX_OBJECT_SIZE = 20 * 1024 * 1024

+ 2 - 22
borg/testsuite/helpers.py

@@ -1,13 +1,11 @@
 import hashlib
 from time import mktime, strptime
 from datetime import datetime, timezone, timedelta
-import os
-import tempfile
-import unittest
 
 import msgpack
 
-from ..helpers import adjust_patterns, exclude_path, Location, format_timedelta, ExcludePattern, make_path_safe, UpgradableLock, prune_within, prune_split, \
+from ..helpers import adjust_patterns, exclude_path, Location, format_timedelta, ExcludePattern, make_path_safe, \
+    prune_within, prune_split, \
     StableDict, int_to_bigint, bigint_to_int, parse_timestamp
 from . import BaseTestCase
 
@@ -119,24 +117,6 @@ class MakePathSafeTestCase(BaseTestCase):
         self.assert_equal(make_path_safe('/'), '.')
 
 
-class UpgradableLockTestCase(BaseTestCase):
-
-    def test(self):
-        file = tempfile.NamedTemporaryFile()
-        lock = UpgradableLock(file.name)
-        lock.upgrade()
-        lock.upgrade()
-        lock.release()
-
-    @unittest.skipIf(os.getuid() == 0, 'Root can always open files for writing')
-    def test_read_only_lock_file(self):
-        file = tempfile.NamedTemporaryFile()
-        os.chmod(file.name, 0o444)
-        lock = UpgradableLock(file.name)
-        self.assert_raises(UpgradableLock.WriteLockFailed, lock.upgrade)
-        lock.release()
-
-
 class MockArchive:
 
     def __init__(self, ts):

+ 24 - 0
borg/testsuite/locking.py

@@ -0,0 +1,24 @@
+import os
+import tempfile
+import unittest
+
+from ..locking import UpgradableLock
+from . import BaseTestCase
+
+
+class UpgradableLockTestCase(BaseTestCase):
+
+    def test(self):
+        file = tempfile.NamedTemporaryFile()
+        lock = UpgradableLock(file.name)
+        lock.upgrade()
+        lock.upgrade()
+        lock.release()
+
+    @unittest.skipIf(os.getuid() == 0, 'Root can always open files for writing')
+    def test_read_only_lock_file(self):
+        file = tempfile.NamedTemporaryFile()
+        os.chmod(file.name, 0o444)
+        lock = UpgradableLock(file.name)
+        self.assert_raises(UpgradableLock.WriteLockFailed, lock.upgrade)
+        lock.release()

+ 2 - 1
borg/testsuite/repository.py

@@ -3,7 +3,8 @@ import shutil
 import tempfile
 
 from ..hashindex import NSIndex
-from ..helpers import Location, IntegrityError, UpgradableLock
+from ..helpers import Location, IntegrityError
+from ..locking import UpgradableLock
 from ..remote import RemoteRepository, InvalidRPCMethod
 from ..repository import Repository
 from . import BaseTestCase