2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
4 # Licensed under the Flora License, Version 1.1 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://floralicense.org/license/
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
25 class FileLockException(Exception):
27 class FileLock(object):
28 def __init__(self, file_name, timeout=10, delay=.05):
29 self.is_locked = False
30 self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
31 self.file_name = file_name
32 self.timeout = timeout
35 self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
40 os.unlink(self.lockfile)
41 self.is_locked = False
43 if not self.is_locked:
46 def __exit__(self, type, value, traceback):
55 except OSError as err:
56 if err.errno != errno.EEXIST:
59 def write(path, data):
61 make_dirs(os.path.dirname(path))
62 with(open(path, 'w')) as f:
65 def write_json_flock(path, data):
67 make_dirs(os.path.dirname(path))
69 with(open(path, 'w')) as f:
70 f.write(json.dumps(data))
72 if e.errno != errno.EEXIST:
80 except ValueError as ve:
82 except (OSError, IOError) as e:
86 def decompress_gzip(intput_path, output_path):
87 with(gzip.open(intput_path, 'rb')) as fobj:
88 f = open(output_path, 'wb')
93 def copyfile_flock(src, dest):
97 shutil.copy(src, dest)
99 if e.errno != errno.EEXIST:
104 def copyfile(src, dst, filename=None):
105 abs_dst=os.path.abspath(os.path.expanduser(dst))
108 abs_dst = os.path.join(abs_dst, filename)
109 shutil.copy(src, abs_dst)
112 return os.path.join(abs_dst, os.path.basename(src))