93bc8eaff4c1b2eaec62f01d390050743c5d6ac6
[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 from urlgrabber import grabber
25 from tic.utils.error import TICError, RepoError
26 from tic.utils import process
27
28
29 def myurlgrab(url, filename, proxies, progress_obj = None):
30     logger = logging.getLogger(__name__)
31     g = grabber.URLGrabber()
32     
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             # untouch repometadata in source path
41             process.run(['cp', '-f', filepath, filename])
42             logger.info('copy file ' + filepath)
43     else:
44         try:
45             # cast url to str here, sometimes it can be unicode,
46             # but pycurl only accept str
47             filename = g.urlgrab(url=str(url),
48                                  filename=filename,
49                                  ssl_verify_host=False,
50                                  ssl_verify_peer=False,
51                                  proxies=proxies,
52                                  http_headers=(('Pragma', 'no-cache'),),
53                                  quote=0,
54                                  progress_obj=progress_obj)
55             logger.info('download file from ' + str(url))
56         except grabber.URLGrabError as err:
57             if err.code == 22:
58                 msg = 'The requested url was not found (%s)' % url
59             else:
60                 msg = str(err)
61                 if msg.find(url) < 0:
62                     msg += ' on %s' % url
63             raise TICError(msg)
64
65     return filename
66
67 if __name__ == '__main__':
68     # file url
69     full_url = 'file://home/shinchulwoo/project/tic_view.json'
70     filename = '/var/tmp/tic_view.json'
71     myurlgrab(full_url, filename, None)
72     # http url
73     full_url = 'https://download.tizen.org/snapshots/tizen/mobile/latest/repos/arm64-wayland/packages/repodata/repomd.xml'
74     filename = '/var/tmp/repomd.xml'
75     myurlgrab(full_url, filename, None)
76     
77