[TIC-CORE] change the license from apache 2.0 to flora 1.1
[archive/20170607/tools/tic-core.git] / tic / utils / grabber.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 logging
21 import urllib2
22 import contextlib
23 from urlgrabber import grabber
24 from tic.utils.error import TICError
25 from tic.utils import process
26 from tic.utils.file import copyfile
27 from tic.config import configmgr
28
29 def myurlgrab2(url, filename):
30     logger = logging.getLogger(__name__)
31     if url.startswith("file:/"):
32         filepath = "/%s" % url.replace("file:", "").lstrip('/')
33         if not os.path.exists(filepath):
34             raise TICError(configmgr.message['repo_not_found'] % url)
35         if url.endswith('.rpm'):
36             return filepath
37         else:
38             copyfile(filepath, os.path.dirname(filename))
39             logger.info('copy file ' + filepath)
40     else:
41         try:
42             with contextlib.closing(urllib2.urlopen(url)) as op:
43                 with open(filename, 'w') as f:
44                     f.write(op.read())
45             logger.info('download file from %s' % str(url))
46         except urllib2.HTTPError as err:
47             if err.code == 404:
48                 msg = configmgr.message['repo_not_found'] % url
49             else:
50                 msg = str(err)
51             logger.info(err)
52             raise TICError(msg)
53         except urllib2.URLError as err:
54             logger.info(err)
55             raise TICError(configmgr.message['server_error'])
56     return filename
57
58 def myurlgrab(url, filename, proxies, progress_obj = None):
59     logger = logging.getLogger(__name__)
60     g = grabber.URLGrabber()
61     if url.startswith("file:/"):
62         filepath = "/%s" % url.replace("file:", "").lstrip('/')
63         if not os.path.exists(filepath):
64             raise TICError("URLGrabber error: can't find file %s" % url)
65         if url.endswith('.rpm'):
66             return filepath
67         else:
68             # untouch repometadata in source path
69             process.run(['cp', '-f', filepath, filename])
70             logger.info('copy file ' + filepath)
71     else:
72         try:
73             # cast url to str here, sometimes it can be unicode,
74             # but pycurl only accept str
75             filename = g.urlgrab(url=str(url),
76                                  filename=filename,
77                                  ssl_verify_host=False,
78                                  ssl_verify_peer=False,
79                                  proxies=proxies,
80                                  http_headers=(('Pragma', 'no-cache'),),
81                                  quote=0,
82                                  progress_obj=progress_obj)
83             logger.info('download file from ' + str(url))
84         except grabber.URLGrabError as err:
85             if err.code == 22:
86                 msg = 'The requested url was not found (%s)' % url
87             else:
88                 msg = str(err)
89                 if msg.find(url) < 0:
90                     msg += ' on %s' % url
91             raise TICError(msg)
92
93     return filename
94
95 if __name__ == '__main__':
96     pass