From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id D0305138CCF for ; Wed, 13 May 2015 19:58:49 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id AEDDEE082D; Wed, 13 May 2015 19:58:47 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 4B8AEE0817 for ; Wed, 13 May 2015 19:58:47 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 79794340DCC for ; Wed, 13 May 2015 19:58:46 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 2AD3A9D5 for ; Wed, 13 May 2015 19:58:45 +0000 (UTC) From: "Devan Franchini" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Devan Franchini" Message-ID: <1431547153.f73e60a7df4dfe91bc43fd0bb9cbb481fe9dc294.twitch153@gentoo> Subject: [gentoo-commits] proj/layman:master commit in: layman/ X-VCS-Repository: proj/layman X-VCS-Files: layman/flocker.py X-VCS-Directories: layman/ X-VCS-Committer: twitch153 X-VCS-Committer-Name: Devan Franchini X-VCS-Revision: f73e60a7df4dfe91bc43fd0bb9cbb481fe9dc294 X-VCS-Branch: master Date: Wed, 13 May 2015 19:58:45 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: a71fb6fd-2062-4b15-8fb7-d47c6439d5db X-Archives-Hash: 0e63277f4bb3b376f0c74e1f7987e39e commit: f73e60a7df4dfe91bc43fd0bb9cbb481fe9dc294 Author: Devan Franchini gentoo org> AuthorDate: Wed May 13 19:59:13 2015 +0000 Commit: Devan Franchini gentoo org> CommitDate: Wed May 13 19:59:13 2015 +0000 URL: https://gitweb.gentoo.org/proj/layman.git/commit/?id=f73e60a7 flocker.py: Adds file locking utility class layman/flocker.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/layman/flocker.py b/layman/flocker.py new file mode 100644 index 0000000..d40925d --- /dev/null +++ b/layman/flocker.py @@ -0,0 +1,66 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# File: flocker.py +# +# Handles all file locking. +# +# Copyright: +# (c) 2015 Devan Franchini +# Distributed under the terms of the GNU General Public License v2 +# +# Author(s): +# Michał Górny +# Devan Franchini +# + +#=============================================================================== +# +# Dependencies +# +#------------------------------------------------------------------------------- +import fcntl + +from layman.compatibility import fileopen + + +class FileLocker(object): + + def __init__(self): + self.files = {} + self.locked = set() + + + def lock_file(self, path, exclusive=False): + '''Lock the file located at path.''' + file_mode = 'r' + lock_mode = fcntl.LOCK_SH + + if exclusive: + file_mode = 'w+' + lock_mode = fcntl.LOCK_EX + + assert path not in self.locked + + self.locked.add(path) + fcntl.flock(self.get_file(path, file_mode).fileno(), lock_mode) + + + def unlock_file(self, path): + '''Unlock the file located at path.''' + assert path in self.locked + + fcntl.flock(self.get_file(path).fileno(), fcntl.LOCK_UN) + self.locked.discard(path) + + + def get_file(self, path, mode='r'): + '''Obtains file object for given path''' + assert mode in ('r', 'w+') + + if path not in self.files: + self.files[path] = fileopen(path, mode) + + f = self.files[path] + f.seek(0) + + return f