Python lockfile.LockFile() Examples

The following are 14 code examples of lockfile.LockFile(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module lockfile , or try the search function .
Example #1
Source File: version.py    From renku-python with Apache License 2.0 6 votes vote down vote up
def dump(self, app_name):
        """Store information in a cache."""
        cache = self._cache(app_name)

        # Attempt to write out our version check file
        with lockfile.LockFile(str(cache)):
            if cache.exists():
                with cache.open() as fp:
                    state = json.load(fp)
            else:
                state = {}

            state[sys.prefix] = attr.asdict(self)

            with cache.open('w') as fp:
                json.dump(state, fp, sort_keys=True) 
Example #2
Source File: app.py    From web2board with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _lock_state_file(self):
        if not self.lock:
            return
        self._lockfile = LockFile(self.path)

        if (self._lockfile.is_locked() and
                (time() - getmtime(self._lockfile.lock_file)) > 10):
            self._lockfile.break_lock()

        self._lockfile.acquire() 
Example #3
Source File: pickle_flann.py    From ibeis with Apache License 2.0 5 votes vote down vote up
def _create_unique_file(temp):
        temp._check_open()
        with lockfile.LockFile(join(temp.dpath, 'tempfile.lock')):
            flag = True
            while flag or exists(temp.fpath):
                temp.fname = six.text_type(uuid.uuid4()) + '.temp'
                temp.fpath = join(temp.dpath, temp.fname)
                flag = False
            ut.touch(temp.fpath, verbose=temp.verbose) 
Example #4
Source File: lock.py    From cloudplow with GNU General Public License v3.0 5 votes vote down vote up
def upload():
    return lockfile.LockFile(os.path.join(lock_folder, 'upload')) 
Example #5
Source File: lock.py    From cloudplow with GNU General Public License v3.0 5 votes vote down vote up
def sync():
    return lockfile.LockFile(os.path.join(lock_folder, 'sync')) 
Example #6
Source File: lock.py    From cloudplow with GNU General Public License v3.0 5 votes vote down vote up
def hidden():
    return lockfile.LockFile(os.path.join(lock_folder, 'hidden')) 
Example #7
Source File: migrate.py    From datacats with GNU Affero General Public License v3.0 5 votes vote down vote up
def convert_environment(datadir, version, always_yes):
    """
    Converts an environment TO the version specified by `version`.
    :param datadir: The datadir to convert.
    :param version: The version to convert TO.
    :param always_yes: True if the user shouldn't be prompted about the migration.
    """
    # Since we don't call either load() or new() we have to call require_images ourselves.
    require_images()

    inp = None
    old_version = _get_current_format(datadir)
    migration_func = migrations[(old_version, version)]

    if version > CURRENT_FORMAT_VERSION:
        raise DatacatsError('Cannot migrate to a version higher than the '
                            'current one.')
    if version < 1:
        raise DatacatsError('Datadir versioning starts at 1.')

    if not always_yes:
        while inp != 'y' and inp != 'n':
            inp = raw_input(migration_func.__doc__.format(version))

        if inp == 'n':
            sys.exit(1)

    lockfile = LockFile(path_join(datadir, '.migration_lock'))
    lockfile.acquire()

    try:
        # FIXME: If we wanted to, we could find a set of conversions which
        # would bring us up to the one we want if there's no direct path.
        # This isn't necessary with just two formats, but it may be useful
        # at 3.
        # Call the appropriate conversion function
        migration_func(datadir)
    finally:
        lockfile.release() 
Example #8
Source File: migrate.py    From datacats with GNU Affero General Public License v3.0 5 votes vote down vote up
def is_locked(datadir):
    """
    Return True if this datadir is locked for migrations
    """
    lockfile = LockFile(datadir + '/.migration_lock')
    return lockfile.is_locked() 
Example #9
Source File: train.py    From pvse with MIT License 5 votes vote down vote up
def lock_and_write_to_file(filename, text):
  with LockFile(filename) as lock:
    with open(filename, 'a') as fid:
      fid.write('{}\n'.format(text)) 
Example #10
Source File: mbed_greentea_dlm.py    From mbed-os-tools with Apache License 2.0 5 votes vote down vote up
def greentea_get_app_sem():
    """ Obtain locking mechanism info
    """
    greentea_home_dir_init()
    gt_instance_uuid = str(uuid.uuid4())   # String version
    gt_file_sem_name = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, gt_instance_uuid)
    gt_file_sem = lockfile.LockFile(gt_file_sem_name)
    return gt_file_sem, gt_file_sem_name, gt_instance_uuid 
Example #11
Source File: mbed_greentea_dlm.py    From mbed-os-tools with Apache License 2.0 5 votes vote down vote up
def greentea_get_target_lock(target_id):
    greentea_home_dir_init()
    file_path = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, target_id)
    lock = lockfile.LockFile(file_path)
    return lock 
Example #12
Source File: mbed_greentea_dlm.py    From mbed-os-tools with Apache License 2.0 5 votes vote down vote up
def greentea_get_global_lock():
    greentea_home_dir_init()
    file_path = os.path.join(HOME_DIR, GREENTEA_HOME_DIR, GREENTEA_GLOBAL_LOCK)
    lock = lockfile.LockFile(file_path)
    return lock 
Example #13
Source File: utils.py    From flask-restplus-server-example with MIT License 4 votes vote down vote up
def download_file(
        url,
        local_filepath,
        chunk_size=1024*512,
        lock_timeout=10,
        http_timeout=None,
        session=None
):
    # pylint: disable=too-many-arguments
    """
    A helper function which can download a file from a specified ``url`` to a
    local file ``local_filepath`` in chunks and using a file lock to prevent
    a concurrent download of the same file.
    """
    # Avoid unnecessary dependencies when the function is not used.
    import lockfile
    import requests

    log.debug("Checking file existance in '%s'", local_filepath)
    lock = lockfile.LockFile(local_filepath)
    try:
        lock.acquire(timeout=lock_timeout)
    except lockfile.LockTimeout:
        log.info(
            "File '%s' is locked. Probably another instance is still downloading it.",
            local_filepath
        )
        raise
    try:
        if not os.path.exists(local_filepath):
            log.info("Downloading a file from '%s' to '%s'", url, local_filepath)
            if session is None:
                session = requests
            response = session.get(url, stream=True, timeout=http_timeout)
            if response.status_code != 200:
                log.error("Download '%s' is failed: %s", url, response)
                response.raise_for_status()
            with open(local_filepath, 'wb') as local_file:
                for chunk in response.iter_content(chunk_size=chunk_size):
                    # filter out keep-alive new chunks
                    if chunk:
                        local_file.write(chunk)
        log.debug("File '%s' has been downloaded", local_filepath)
        return local_filepath
    finally:
        lock.release() 
Example #14
Source File: plc_device.py    From OpenDCRE with GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, counter, **kwargs):
        super(PLCDevice, self).__init__(lock_path=kwargs['lockfile'])

        # these are required, so if they are missing from the config
        # dict passed in, we will want the exception to propagate up
        self.device_name = kwargs['device_name']
        self.hardware_type = self._device_hardware.get(kwargs['hardware_type'], DEVICEBUS_UNKNOWN_HARDWARE)

        # these are optional values, so they may not exist in the config.
        # if they do not exist, they will hold a default value
        self.bus_timeout = kwargs.get('timeout', 0.25)
        self.bus_baud = kwargs.get('bps', 115200)
        self.retry_limit = kwargs.get('retry_limit', 3)
        self.time_slice = kwargs.get('time_slice', 75)

        # hold the reference to the app's sequence number generator
        # FIXME - passing the reference around seems weird and could make things
        # uncomfortable later on. instead, one thing to investigate would be to
        # have the counter as part of the root class for devicebus interfaces, that
        # way, all implementations of devicebus interfaces should have access to it
        # and *should* be able to increment it safely, with all other interfaces
        # respecting the incrementation...
        #
        # another potential solution here would be to store the counter in the app
        # config and have the app context be passed to the devicebus interfaces on
        # init, but that isn't too different from this.
        self._count = counter

        self._lock = lockfile.LockFile(self.serial_lock)

        self._command_map = {
            cid.VERSION: self._version,
            cid.SCAN: self._scan,
            cid.SCAN_ALL: self._scan_all,
            cid.READ: self._read,
            cid.POWER: self._power,
            cid.ASSET: self._asset,
            cid.BOOT_TARGET: self._boot_target,
            cid.CHAMBER_LED: self._chamber_led,
            cid.LED: self._led,
            cid.FAN: self._fan,
            cid.HOST_INFO: self._host_info
        }

        # since PLC devices are "dumb", meaning one device talks to a range of board_ids
        # we expose the range here.  for single-board devices, an alternate 'board_id' property is exposed.
        self.board_id_range = kwargs['board_id_range']
        self.board_id_range_max = kwargs['board_id_range_max']