[TIC-CORE] change the license from apache 2.0 to flora 1.1
[archive/20170607/tools/tic-core.git] / tic / utils / file.py
1 #!/usr/bin/python
2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
3 #
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
7 #
8 #     http://floralicense.org/license/
9 #
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.
15 #
16 # Contributors:
17 # - S-Core Co., Ltd
18
19 import errno
20 import gzip
21 import os
22 import shutil
23 import json
24
25 class FileLockException(Exception):
26     pass
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
33         self.delay = delay
34     def acquire(self):
35         self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
36         self.is_locked = True
37     def release(self):
38         if self.is_locked:
39             os.close(self.fd)
40             os.unlink(self.lockfile)
41             self.is_locked = False
42     def __enter__(self):
43         if not self.is_locked:
44             self.acquire()
45         return self
46     def __exit__(self, type, value, traceback):
47         if self.is_locked:
48             self.release()
49     def __del__(self):
50         self.release()
51
52 def make_dirs(path):
53     try:
54         os.makedirs(path)
55     except OSError as err:
56         if err.errno != errno.EEXIST:
57             raise
58
59 def write(path, data):
60     # make directory
61     make_dirs(os.path.dirname(path))
62     with(open(path, 'w')) as f:
63         f.write(data)
64         
65 def write_json_flock(path, data):
66     try:
67         make_dirs(os.path.dirname(path))
68         with FileLock(path):
69             with(open(path, 'w')) as f:
70                 f.write(json.dumps(data))
71     except OSError as e:
72         if e.errno != errno.EEXIST:
73             print(e)
74
75 def read_json(path):
76     ret=None
77     try:
78         with open(path) as f:
79             ret=json.load(f)
80     except ValueError as ve:
81         print(ve)
82     except (OSError, IOError) as e:
83         print(e)
84     return ret
85
86 def decompress_gzip(intput_path, output_path):
87     with(gzip.open(intput_path, 'rb')) as fobj:
88         f = open(output_path, 'wb')
89         f.write(fobj.read())
90         f.close()
91     return output_path
92
93 def copyfile_flock(src, dest):
94     ret = dest;
95     try:
96         with FileLock(dest):
97             shutil.copy(src, dest)
98     except OSError as e:
99         if e.errno != errno.EEXIST:
100             print(e)
101         ret = src
102     return ret
103
104 def copyfile(src, dst, filename=None):
105     abs_dst=os.path.abspath(os.path.expanduser(dst))
106     make_dirs(abs_dst)
107     if filename:
108         abs_dst = os.path.join(abs_dst, filename)
109     shutil.copy(src, abs_dst)
110     if filename:
111         return abs_dst
112     return os.path.join(abs_dst, os.path.basename(src))