X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=tic%2Futils%2Ffile.py;h=050118fd665e5c7f4d8670d6a2a150afeb22b717;hb=bf38c00cf6dd00c388415f576fb6fc62d5f2d23d;hp=b1cf3ac1fe45ff8cc8f74ed77edc1917886f2bc9;hpb=433db615f3102bf55ae73b66fcb38c1fa29f63be;p=archive%2F20170607%2Ftools%2Ftic-core.git diff --git a/tic/utils/file.py b/tic/utils/file.py index b1cf3ac..050118f 100644 --- a/tic/utils/file.py +++ b/tic/utils/file.py @@ -24,9 +24,36 @@ import gzip import os import shutil -def make_dirs(dirname): +class FileLockException(Exception): + pass +class FileLock(object): + def __init__(self, file_name, timeout=10, delay=.05): + self.is_locked = False + self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name) + self.file_name = file_name + self.timeout = timeout + self.delay = delay + def acquire(self): + self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR) + self.is_locked = True + def release(self): + if self.is_locked: + os.close(self.fd) + os.unlink(self.lockfile) + self.is_locked = False + def __enter__(self): + if not self.is_locked: + self.acquire() + return self + def __exit__(self, type, value, traceback): + if self.is_locked: + self.release() + def __del__(self): + self.release() + +def make_dirs(path): try: - os.makedirs(dirname) + os.makedirs(path) except OSError as err: if err.errno != errno.EEXIST: raise @@ -36,7 +63,7 @@ def write(path, data): make_dirs(os.path.dirname(path)) with(open(path, 'w')) as f: f.write(data) - + def decompress_gzip(intput_path, output_path): with(gzip.open(intput_path, 'rb')) as fobj: f = open(output_path, 'wb') @@ -44,6 +71,17 @@ def decompress_gzip(intput_path, output_path): f.close() return output_path +def copyfile_flock(src, dest): + ret = dest; + try: + with FileLock(dest): + shutil.copy(src, dest) + except OSError as e: + if e.errno != errno.EEXIST: + print(e) + ret = src + return ret + def copyfile(src, dst, filename=None): abs_dst=os.path.abspath(os.path.expanduser(dst)) make_dirs(abs_dst)