def download_file_from_internet(path_from_internet, path_to_disk): if not os.path.exists(path_to_disk) or os.path.getsize(path_to_disk) < 314: try: print_log('downloading ' + str(path_from_internet) + ' -> ' + str(path_to_disk)) with open(path_to_disk, 'wb') as handle: t = time.time() response = requests.get(path_from_internet, stream=True) length = int(response.headers['Content-Length']) print('Downloading ... ') print('destination: {}'.format(path_to_disk)) print(' source: {}'.format(path_from_internet)) print(' size : {} ({:.3f} Mb)'.format(length, length / (1024.0 * 1024.0))) print(' data : {}'.format(response.headers['Last-Modified'])) block_size = min(max(length >> 10, 1024 * 16), 1024 * 1024 * 16) # block_size = [16kb ... 16mb] i = 0 for block in response.iter_content(block_size): print("progress: {:7.2%} (left: {:10.3f} Mb)".format(i * block_size / length, (length - i * block_size) / (1024.0 * 1024.0)), end='\r') i += 1 handle.write(block) print("progress: 100.00%" + " " * 50 + '\n') sec = time.time() - t print("done. [time: {:.3f} sec speed: {:.3f} Mb/sec]".format(sec, float(length / (1024.0 * 1024.0)) / sec)) except: if os.path.isfile(path_to_disk): os.remove(path_to_disk)
def unpack_stage3(path_to_install): path_to_stage3 = '/tmp/stage3-amd64-latest.tar.bz2' cmd = 'tar xvjpf ' + path_to_stage3 print_log('chdir ' + path_to_install) os.chdir(path_to_install) print_log('cleaning ... ') print_log('[rm -rf *]') os.system('rm -rf *') print_log('done') print_log('installing ... ') status, msg = subprocess.getstatusoutput(cmd) print_log(msg) print_log('status ' + str(status)) path = Dir(path_to_install) print_log('cp /etc/resolv.conf ' + path.full('etc/resolv.conf')) shutil.copy2('/etc/resolv.conf', path.full('etc/resolv.conf'))