050118fd665e5c7f4d8670d6a2a150afeb22b717
[archive/20170607/tools/tic-core.git] / tic / utils / file.py
1 #!/usr/bin/python
2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3 #
4 # Contact: 
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
6
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19 # Contributors:
20 # - S-Core Co., Ltd
21
22 import errno
23 import gzip
24 import os
25 import shutil
26
27 class FileLockException(Exception):
28     pass
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
35         self.delay = delay
36     def acquire(self):
37         self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
38         self.is_locked = True
39     def release(self):
40         if self.is_locked:
41             os.close(self.fd)
42             os.unlink(self.lockfile)
43             self.is_locked = False
44     def __enter__(self):
45         if not self.is_locked:
46             self.acquire()
47         return self
48     def __exit__(self, type, value, traceback):
49         if self.is_locked:
50             self.release()
51     def __del__(self):
52         self.release()
53
54 def make_dirs(path):
55     try:
56         os.makedirs(path)
57     except OSError as err:
58         if err.errno != errno.EEXIST:
59             raise
60
61 def write(path, data):
62     # make directory
63     make_dirs(os.path.dirname(path))
64     with(open(path, 'w')) as f:
65         f.write(data)
66
67 def decompress_gzip(intput_path, output_path):
68     with(gzip.open(intput_path, 'rb')) as fobj:
69         f = open(output_path, 'wb')
70         f.write(fobj.read())
71         f.close()
72     return output_path
73
74 def copyfile_flock(src, dest):
75     ret = dest;
76     try:
77         with FileLock(dest):
78             shutil.copy(src, dest)
79     except OSError as e:
80         if e.errno != errno.EEXIST:
81             print(e)
82         ret = src
83     return ret
84
85 def copyfile(src, dst, filename=None):
86     abs_dst=os.path.abspath(os.path.expanduser(dst))
87     make_dirs(abs_dst)
88     if filename:
89         abs_dst = os.path.join(abs_dst, filename)
90     shutil.copy(src, abs_dst)
91     if filename:
92         return abs_dst
93     return os.path.join(abs_dst, os.path.basename(src))