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