# Path: filetracker/utils.py
# def split_name(name):
#     """Splits a (possibly versioned) name into unversioned name and version.
# 
#     Returns a tuple ``(unversioned_name, version)``, where ``version`` may
#     be ``None``.
#     """
#     s = name.rsplit('@', 1)
#     if len(s) == 1:
#         return s[0], None
#     else:
#         try:
#             return s[0], int(s[1])
#         except ValueError:
#             raise ValueError(
#                 "Invalid Filetracker filename: version must " "be int, not %r" % (s[1],)
#             )
# 
# def check_name(name, allow_version=True):
#     if not isinstance(name, six.string_types):
#         raise ValueError("Invalid Filetracker filename: not string: %r" % (name,))
#     parts = name.split('/')
#     if not parts:
#         raise ValueError("Invalid Filetracker filename: empty name")
#     if parts[0]:
#         raise ValueError("Invalid Filetracker filename: does not start with /")
#     if '..' in parts:
#         raise ValueError("Invalid Filetracker filename: .. in path")
#     if '@' in ''.join(parts[:-1]):
#         raise ValueError("Invalid Filetracker filename: @ in path")
#     if len(parts[-1].split('@')) > 2:
#         raise ValueError("Invalid Filetracker filename: multiple versions")
#     if '@' in parts[-1] and not allow_version:
#         raise ValueError(
#             "Invalid Filetracker filename: version not allowed " "in this API call"
#         )
# 
# def mkdir(name):
#     try:
#         os.makedirs(name, 0o700)
#     except OSError as e:
#         if e.errno != errno.EEXIST:
#             raise
# 
# def rmdirs(name, root):
#     """Removes empty directories from ``name`` upwards, stops at ``root``."""
#     while name != root:
#         try:
#             os.rmdir(name)
#             name = os.path.dirname(name)
#         except OSError as e:
#             if e.errno in (errno.ENOTEMPTY, errno.ENOENT):
#                 return
#             else:
#                 raise


import fcntl
import os
from filetracker.utils import split_name, check_name, mkdir, rmdirs
            fcntl.flock(self.fd, fcntl.LOCK_SH)

        def lock_exclusive(self):
            fcntl.flock(self.fd, fcntl.LOCK_EX)

        def unlock(self):
            fcntl.flock(self.fd, fcntl.LOCK_UN)

        def close(self, delete=False):
            if self.fd != -1:
                if delete:
                    fcntl.flock(self.manager.tree_lock_fd, fcntl.LOCK_EX)
                    try:
                        os.remove(self.filename)
                        dir_path = os.path.dirname(self.filename)
                        rmdirs(dir_path, self.manager.dir)
                    except OSError:
                        pass
                    finally:
                        fcntl.flock(self.manager.tree_lock_fd, fcntl.LOCK_UN)
                os.close(self.fd)
                self.fd = -1

        def __del__(self):
            # The file is unlocked when the a descriptor which was used to lock
            # it is closed.
            self.close()

    def __init__(self, dir):
        self.dir = dir

# Path: filetracker/utils.py
# def split_name(name):
#     """Splits a (possibly versioned) name into unversioned name and version.
# 
#     Returns a tuple ``(unversioned_name, version)``, where ``version`` may
#     be ``None``.
#     """
#     s = name.rsplit('@', 1)
#     if len(s) == 1:
#         return s[0], None
#     else:
#         try:
#             return s[0], int(s[1])
#         except ValueError:
#             raise ValueError(
#                 "Invalid Filetracker filename: version must " "be int, not %r" % (s[1],)
#             )
# 
# def check_name(name, allow_version=True):
#     if not isinstance(name, six.string_types):
#         raise ValueError("Invalid Filetracker filename: not string: %r" % (name,))
#     parts = name.split('/')
#     if not parts:
#         raise ValueError("Invalid Filetracker filename: empty name")
#     if parts[0]:
#         raise ValueError("Invalid Filetracker filename: does not start with /")
#     if '..' in parts:
#         raise ValueError("Invalid Filetracker filename: .. in path")
#     if '@' in ''.join(parts[:-1]):
#         raise ValueError("Invalid Filetracker filename: @ in path")
#     if len(parts[-1].split('@')) > 2:
#         raise ValueError("Invalid Filetracker filename: multiple versions")
#     if '@' in parts[-1] and not allow_version:
#         raise ValueError(
#             "Invalid Filetracker filename: version not allowed " "in this API call"
#         )
# 
# def mkdir(name):
#     try:
#         os.makedirs(name, 0o700)
#     except OSError as e:
#         if e.errno != errno.EEXIST:
#             raise
# 
# def rmdirs(name, root):
#     """Removes empty directories from ``name`` upwards, stops at ``root``."""
#     while name != root:
#         try:
#             os.rmdir(name)
#             name = os.path.dirname(name)
#         except OSError as e:
#             if e.errno in (errno.ENOTEMPTY, errno.ENOENT):
#                 return
#             else:
#                 raise


# Path: filetracker/client/lock_manager.py
import fcntl
import os
from filetracker.utils import split_name, check_name, mkdir, rmdirs

            fcntl.flock(self.fd, fcntl.LOCK_SH)

        def lock_exclusive(self):
            fcntl.flock(self.fd, fcntl.LOCK_EX)

        def unlock(self):
            fcntl.flock(self.fd, fcntl.LOCK_UN)

        def close(self, delete=False):
            if self.fd != -1:
                if delete:
                    fcntl.flock(self.manager.tree_lock_fd, fcntl.LOCK_EX)
                    try:
                        os.remove(self.filename)
                        dir_path = os.path.dirname(self.filename)
                        rmdirs(dir_path, self.manager.dir)
                    except OSError:
                        pass
                    finally:
                        fcntl.flock(self.manager.tree_lock_fd, fcntl.LOCK_UN)
                os.close(self.fd)
                self.fd = -1

        def __del__(self):
            # The file is unlocked when the a descriptor which was used to lock
            # it is closed.
            self.close()

    def __init__(self, dir):
        self.dir = dir

        mkdir(dir)