72911eb0c37b0825ae0960592927d8efa1e4ce7f
[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 import json
27
28 class FileLockException(Exception):
29     pass
30 class FileLock(object):
31     def __init__(self, file_name, timeout=10, delay=.05):
32         self.is_locked = False
33         self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
34         self.file_name = file_name
35         self.timeout = timeout
36         self.delay = delay
37     def acquire(self):
38         self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
39         self.is_locked = True
40     def release(self):
41         if self.is_locked:
42             os.close(self.fd)
43             os.unlink(self.lockfile)
44             self.is_locked = False
45     def __enter__(self):
46         if not self.is_locked:
47             self.acquire()
48         return self
49     def __exit__(self, type, value, traceback):
50         if self.is_locked:
51             self.release()
52     def __del__(self):
53         self.release()
54
55 def make_dirs(path):
56     try:
57         os.makedirs(path)
58     except OSError as err:
59         if err.errno != errno.EEXIST:
60             raise
61
62 def write(path, data):
63     # make directory
64     make_dirs(os.path.dirname(path))
65     with(open(path, 'w')) as f:
66         f.write(data)
67         
68 def write_json_flock(path, data):
69     try:
70         make_dirs(os.path.dirname(path))
71         with FileLock(path):
72             with(open(path, 'w')) as f:
73                 f.write(json.dumps(data))
74     except OSError as e:
75         if e.errno != errno.EEXIST:
76             print(e)
77
78 def read_json(path):
79     ret=None
80     try:
81         with open(path) as f:
82             ret=json.load(f)
83     except ValueError as ve:
84         print(ve)
85     except (OSError, IOError) as e:
86         print(e)
87     return ret
88
89 def decompress_gzip(intput_path, output_path):
90     with(gzip.open(intput_path, 'rb')) as fobj:
91         f = open(output_path, 'wb')
92         f.write(fobj.read())
93         f.close()
94     return output_path
95
96 def copyfile_flock(src, dest):
97     ret = dest;
98     try:
99         with FileLock(dest):
100             shutil.copy(src, dest)
101     except OSError as e:
102         if e.errno != errno.EEXIST:
103             print(e)
104         ret = src
105     return ret
106
107 def copyfile(src, dst, filename=None):
108     abs_dst=os.path.abspath(os.path.expanduser(dst))
109     make_dirs(abs_dst)
110     if filename:
111         abs_dst = os.path.join(abs_dst, filename)
112     shutil.copy(src, abs_dst)
113     if filename:
114         return abs_dst
115     return os.path.join(abs_dst, os.path.basename(src))