github.com/platonnetwork/platon-go@v0.7.6/cases/common/download.py (about)

     1  import os
     2  import shutil
     3  import tarfile
     4  import requests
     5  from conf.settings import PLATON_BIN_FILE
     6  
     7  
     8  def download_platon(download_url: 'str', path=PLATON_BIN_FILE):
     9      """
    10      :param download_url: new package download address
    11      :param path: platon relative path
    12      :return:
    13      """
    14  
    15      packge_name = download_url.split('/')[-1][:-7]
    16      platon_path = os.path.abspath(path)
    17      platon_path = os.path.join(platon_path, "../")
    18      platon_path = os.path.abspath(platon_path)
    19      platon_tar_file = os.path.join(platon_path, 'platon.tar.gz')
    20      extractall_path = os.path.join(platon_path, packge_name)
    21  
    22      if not os.path.exists(platon_path):
    23          os.makedirs(platon_path)
    24  
    25      # download
    26      resp = requests.get(url=download_url, timeout=30)
    27      with open(platon_tar_file, 'wb') as f:
    28          f.write(resp.content)
    29          f.close()
    30  
    31      # Extract files
    32      tar = tarfile.open(platon_tar_file)
    33      tar.extractall(path=platon_path)
    34      tar.close()
    35  
    36      # copy file
    37      shutil.copy(os.path.join(extractall_path, 'platon'), platon_path)
    38  
    39      # remove directory and file
    40      for root, dirs, files in os.walk(extractall_path, topdown=False):
    41          for name in files:
    42              os.remove(os.path.join(root, name))
    43          for name in dirs:
    44              os.rmdir(os.path.join(root, name))
    45      os.rmdir(extractall_path)
    46      os.remove(platon_tar_file)