[TIC-CORE] change the license from apache 2.0 to flora 1.1
[archive/20170607/tools/tic-core.git] / test / test_threadsafe.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 os
20 import threading
21 from tic.utils.file import make_dirs, decompress_gzip, copyfile_flock
22 from tic.utils.grabber import myurlgrab2
23 import contextlib
24
25 class MyThread(threading.Thread):
26     def __init__(self, name, url):
27         threading.Thread.__init__(self)
28         self.name = name
29         self.url = url
30         self.__suspend = False
31         self.__exit = False
32         self.__path = './test/thread/'
33         self.__cachepath = './test/thread/cache'
34     def run(self):
35         print('%s Thread start !!!' % self.name)
36         path=os.path.join(self.__path, self.name)
37         make_dirs(path)
38         filename=os.path.join(path, os.path.basename(self.url))
39         myurlgrab2(self.url, filename)
40         print('%s Thread download file !!!' % self.name)
41         # Check if file compressed or not
42         if filename.endswith(".gz"):
43             decompress_filename = os.path.splitext(filename)[0]
44             filename = decompress_gzip(filename, decompress_filename)
45         print('%s Thread decompress file !!!' % self.name)
46         
47         cachefile = os.path.join(self.__cachepath, os.path.basename(filename))
48         copyfile_flock(filename, cachefile)
49         print('%s Thread finish !!!' % self.name)
50         
51     def mySuspend(self):
52         self.__suspend = True
53     def myResume(self):
54         self.__suspend = False
55     def myExit(self):
56         self.__exit = True
57
58 if __name__ == '__main__':
59     url = ''
60     th1 = MyThread('th1', url)
61     th1.start()
62 #     th2 = MyThread('th2', url)
63 #     th2.start()