2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
27 class FileLockException(Exception):
29 class FileLock(object):
30 def __init__(self, file_name, timeout=10, delay=.05):
31 self.is_locked = False
32 self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
33 self.file_name = file_name
34 self.timeout = timeout
37 self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
42 os.unlink(self.lockfile)
43 self.is_locked = False
45 if not self.is_locked:
48 def __exit__(self, type, value, traceback):
57 except OSError as err:
58 if err.errno != errno.EEXIST:
61 def write(path, data):
63 make_dirs(os.path.dirname(path))
64 with(open(path, 'w')) as f:
67 def decompress_gzip(intput_path, output_path):
68 with(gzip.open(intput_path, 'rb')) as fobj:
69 f = open(output_path, 'wb')
74 def copyfile_flock(src, dest):
78 shutil.copy(src, dest)
80 if e.errno != errno.EEXIST:
85 def copyfile(src, dst, filename=None):
86 abs_dst=os.path.abspath(os.path.expanduser(dst))
89 abs_dst = os.path.join(abs_dst, filename)
90 shutil.copy(src, abs_dst)
93 return os.path.join(abs_dst, os.path.basename(src))