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