# Path: filetracker/utils.py
# def file_digest(source):
#     """Calculates SHA256 digest of a file.
# 
#     Args:
#         source: either a file-like object or a path to file
#     """
#     hash_sha256 = hashlib.sha256()
# 
#     should_close = False
# 
#     if isinstance(source, six.string_types):
#         should_close = True
#         source = open(source, 'rb')
# 
#     for chunk in iter(lambda: source.read(_BUFFER_SIZE), b''):
#         hash_sha256.update(chunk)
# 
#     if should_close:
#         source.close()
# 
#     return hash_sha256.hexdigest()


import contextlib
import email.utils
import errno
import fcntl
import gevent
import gzip
import logging
import os
import shutil
import subprocess
import sys
import tempfile
import bsddb3
import six
from filetracker.utils import file_digest
            # so waiting here without yielding would get the worker killed
            try:
                fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
                success = True
                break
            except IOError as e:
                if e.errno in [errno.EAGAIN, errno.EWOULDBLOCK]:
                    # This yields execution to other green threads.
                    gevent.sleep(_LOCK_SLEEP_TIME_S)
                    retries_left -= 1
                else:
                    raise

        if success:
            yield
        else:
            raise ConcurrentModificationError(path)
    finally:
        if success:
            fcntl.flock(fd, fcntl.LOCK_UN)
        os.close(fd)


@contextlib.contextmanager
def _no_lock():
    """Does nothing, just runs the code within the `with` statement.
    Used for conditional locking."""
    yield



# Path: filetracker/utils.py
# def file_digest(source):
#     """Calculates SHA256 digest of a file.
# 
#     Args:
#         source: either a file-like object or a path to file
#     """
#     hash_sha256 = hashlib.sha256()
# 
#     should_close = False
# 
#     if isinstance(source, six.string_types):
#         should_close = True
#         source = open(source, 'rb')
# 
#     for chunk in iter(lambda: source.read(_BUFFER_SIZE), b''):
#         hash_sha256.update(chunk)
# 
#     if should_close:
#         source.close()
# 
#     return hash_sha256.hexdigest()


# Path: filetracker/servers/storage.py
import contextlib
import email.utils
import errno
import fcntl
import gevent
import gzip
import logging
import os
import shutil
import subprocess
import sys
import tempfile
import bsddb3
import six
from filetracker.utils import file_digest

            # so waiting here without yielding would get the worker killed
            try:
                fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
                success = True
                break
            except IOError as e:
                if e.errno in [errno.EAGAIN, errno.EWOULDBLOCK]:
                    # This yields execution to other green threads.
                    gevent.sleep(_LOCK_SLEEP_TIME_S)
                    retries_left -= 1
                else:
                    raise

        if success:
            yield
        else:
            raise ConcurrentModificationError(path)
    finally:
        if success:
            fcntl.flock(fd, fcntl.LOCK_UN)
        os.close(fd)


@contextlib.contextmanager
def _no_lock():
    """Does nothing, just runs the code within the `with` statement.
    Used for conditional locking."""
    yield



def _makedirs(path):