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