-
-
-
-def download_url(url, dest_path):
- """
- Download a file to a destination path given a URL
- """
- name = url.rsplit('/')[-1]
- dest = dest_path + name
- try:
- response = urllib2.urlopen(url)
- except (urllib2.HTTPError, urllib2.URLError):
- logger.error("Error in fetching %s" %url)
- return False
-
- with open(dest, 'wb') as f:
- f.write(response.read())
- return True
-
-
-def download_url_with_progress(url, dest_path):
- """
- Download a file to a destination path given a URL showing the progress
- """
- name = url.rsplit('/')[-1]
- dest = dest_path + name
- try:
- response = urllib2.urlopen(url)
- except (urllib2.HTTPError, urllib2.URLError):
- logger.error("Error in fetching %s" %url)
- return False
-
- f = open(dest, 'wb')
- meta = response.info()
- file_size = int(meta.getheaders("Content-Length")[0])
- logger.info("Downloading: %s Bytes: %s" %(dest, file_size))
-
- file_size_dl = 0
- block_sz = 8192
- while True:
- buffer = response.read(block_sz)
- if not buffer:
- break
-
- file_size_dl += len(buffer)
- f.write(buffer)
- status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
- status = status + chr(8)*(len(status)+1)
- print status,
-
- f.close()
- print("\n")
- return True
-
-
-def check_internet_connectivity(url='http://www.google.com/'):
- """
- Check if there is access to the internet
- """
- try:
- urllib2.urlopen(url, timeout=5)
- return True
- except urllib.request.URLError:
- return False
-
-def execute_command(cmd):
- """
- Execute Linux command
- """
- logger.debug('Executing command : {}'.format(cmd))
- #p = os.popen(cmd,"r")
- #logger.debug(p.read())
- output_file = "/tmp/output.txt"
- f = open(output_file, 'w+')
- p = subprocess.call(cmd,shell=True, stdout=f, stderr=subprocess.STDOUT)
- f.close()
- f = open(output_file, 'r')
- logger.debug(f.read())
- #p = subprocess.call(cmd,shell=True);
- if p == 0 :
- return True
- else:
- logger.error("Error when executing command %s" %cmd)
- exit(-1)
-
-
-
-