3 # Copyright (c) 2014 Intel, Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 from mic.utils import runner
26 from mic.utils.errors import CreatorError
27 from mic.utils.safeurl import SafeURL
29 from urlgrabber import grabber
30 from urlgrabber import __version__ as grabber_version
32 def myurlgrab(url, filename, proxies, progress_obj = None):
33 g = grabber.URLGrabber()
34 if progress_obj is None:
35 progress_obj = TextProgress()
37 if url.startswith("file:/"):
38 filepath = "/%s" % url.replace("file:", "").lstrip('/')
39 if not os.path.exists(filepath):
40 raise CreatorError("URLGrabber error: can't find file %s" % url)
41 if url.endswith('.rpm'):
44 # untouch repometadata in source path
45 runner.show(['cp', '-f', filepath, filename])
49 # cast url to str here, sometimes it can be unicode,
50 # but pycurl only accept str
51 filename = g.urlgrab(url=str(url),
53 ssl_verify_host=False,
54 ssl_verify_peer=False,
56 http_headers=(('Pragma', 'no-cache'),),
58 progress_obj=progress_obj)
59 except grabber.URLGrabError as err:
66 msg = msg.replace(url, tmp)
68 raise CreatorError(msg)
72 def terminal_width(fd=1):
73 """ Get the real terminal width """
76 buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, buf)
77 return struct.unpack('hhhh', buf)[1]
81 def truncate_url(url, width):
82 return os.path.basename(url)[0:width]
84 class TextProgress(object):
85 # make the class as singleton
87 def __new__(cls, *args, **kwargs):
89 cls._instance = super(TextProgress, cls).__new__(cls, *args, **kwargs)
93 def __init__(self, totalnum = None):
97 def start(self, filename, url, *args, **kwargs):
99 self.termwidth = terminal_width()
100 if self.total is None:
101 msger.info("Retrieving %s ..." % truncate_url(self.url, self.termwidth - 15))
103 msger.info("Retrieving %s [%d/%d] ..." % (truncate_url(self.url, self.termwidth - 25), self.counter, self.total))
105 def update(self, *args):
108 def end(self, *args):
109 if self.counter == self.total:
112 if self.total is not None: