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