def maybe_download_and_extract(filename, working_directory, url_source, extract=False, expected_bytes=None):
 def _download(filename, working_directory, url_source):
  def _dlProgress(count, blockSize, totalSize):
   if (totalSize != 0):
    percent = ((float((count * blockSize)) / float(totalSize)) * 100.0)
    sys.stdout.write((('\rDownloading ' + filename) + ('...%d%%' % percent)))
    sys.stdout.flush()
  if (sys.version_info[0] == 2):
   from urllib import urlretrieve
  else:
   from urllib.request import urlretrieve
  filepath = os.path.join(working_directory, filename)
  urlretrieve((url_source + filename), filepath, reporthook=_dlProgress)
 exists_or_mkdir(working_directory, verbose=False)
 filepath = os.path.join(working_directory, filename)
 if (not os.path.exists(filepath)):
  _download(filename, working_directory, url_source)
  print ()
  statinfo = os.stat(filepath)
  print ('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
  if ((not (expected_bytes is None)) and (expected_bytes != statinfo.st_size)):
   raise Exception((('Failed to verify ' + filename) + '. Can you get to it with a browser?'))
  if extract:
   if tarfile.is_tarfile(filepath):
    print 'Trying to extract tar file'
    tarfile.open(filepath, 'r').extractall(working_directory)
    print '... Success!'
   elif zipfile.is_zipfile(filepath):
    print 'Trying to extract zip file'
    with zipfile.ZipFile(filepath) as zf:
     zf.extractall(working_directory)
    print '... Success!'
   else:
    print 'Unknown compression_format only .tar.gz/.tar.bz2/.tar and .zip supported'
 return filepath

def maybe_download_and_extract ( filename , working_directory , url_source , extract = False , expected_bytes = None ) : def _download ( filename , working_directory , url_source ) : def _dlProgress ( count , blockSize , totalSize ) : if ( totalSize != 0 ) : percent = ( ( float ( ( count * blockSize ) ) / float ( totalSize ) ) * 100.0 ) sys . stdout . write ( ( ( '\rDownloading ' + filename ) + ( '...%d%%' % percent ) ) ) sys . stdout . flush ( ) if ( sys . version_info [ 0 ] == 2 ) : from urllib import urlretrieve else : from urllib . request import urlretrieve filepath = os . path . join ( working_directory , filename ) urlretrieve ( ( url_source + filename ) , filepath , reporthook = _dlProgress ) exists_or_mkdir ( working_directory , verbose = False ) filepath = os . path . join ( working_directory , filename ) if ( not os . path . exists ( filepath ) ) : _download ( filename , working_directory , url_source ) print ( ) statinfo = os . stat ( filepath ) print ( 'Succesfully downloaded' , filename , statinfo . st_size , 'bytes.' ) if ( ( not ( expected_bytes is None ) ) and ( expected_bytes != statinfo . st_size ) ) : raise Exception ( ( ( 'Failed to verify ' + filename ) + '. Can you get to it with a browser?' ) ) if extract : if tarfile . is_tarfile ( filepath ) : print 'Trying to extract tar file' tarfile . open ( filepath , 'r' ) . extractall ( working_directory ) print '... Success!' elif zipfile . is_zipfile ( filepath ) : print 'Trying to extract zip file' with zipfile . ZipFile ( filepath ) as zf : zf . extractall ( working_directory ) print '... Success!' else : print 'Unknown compression_format only .tar.gz/.tar.bz2/.tar and .zip supported' return filepath
download and extract model tar file .