SHARE
TWEET

udo_bios_downloader

DeaD_EyE Jun 14th, 2019 13 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # udoo bios, not finished...
  2.  
  3. from argparse import ArgumentParser
  4. from urllib.request import (
  5.     Request,
  6.     urlopen,
  7.     urljoin,
  8.     )
  9. from urllib.error import HTTPError
  10. from shutil import unpack_archive
  11. from pathlib import Path
  12. import json
  13.  
  14.  
  15. BASE_URL = 'http://download.udoo.org/files/UDOO_X86/UEFI_update/'
  16. FILE_NAME = 'UDOOX86_B02-UEFI_Update_rel{}.zip'
  17. DB = Path('.udoo_bios_data.json')
  18.  
  19.  
  20. def get_bios_versions():
  21.     for i in range(100, 120):
  22.         url = urljoin(BASE_URL, FILE_NAME.format(i))
  23.         request = Request(url, method='HEAD')
  24.         try:
  25.             response = urlopen(request)
  26.         except HTTPError:
  27.             continue
  28.         if response.status == 200:
  29.             yield str(i / 100), url
  30.  
  31.  
  32. def download_bios(url):
  33.     filename = url.split('/')[-1]
  34.     with open(filename, 'wb') as fd:
  35.         response = urlopen(url)
  36.         while True:
  37.             chunk = response.read(64*1024**1)
  38.             if not chunk:
  39.                 break
  40.             fd.write(chunk)
  41.  
  42.  
  43. def unpack(url):
  44.     bios_dir = Path('BIOS')
  45.     filename = url.split('/')[-1]
  46.     if not bios_dir.exists():
  47.         bios_dir.mkdir()
  48.     unpack_archive(filename, bios_dir)
  49.  
  50.  
  51. def store_json(data):
  52.     with DB.open('w') as fd:
  53.         json.dump(data, fd)
  54.  
  55.  
  56. def load_json():
  57.     with DB.open() as fd:
  58.         return json.load(fd)
  59.  
  60.  
  61. def main(do_update, version):
  62.     if DB.exists() and not do_update:
  63.         data = load_json()
  64.     else:
  65.         data = dict(get_bios_versions())
  66.         store_json(data)
  67.     if version is not None:
  68.         url = data[version]
  69.         download_bios(url)
  70.         unpack(url)
  71.     else:
  72.         for version in data.keys():
  73.             print(f'[{version}]')
  74.  
  75.  
  76. if __name__ == '__main__':
  77.     parser = ArgumentParser(description='Show and install BIOS updates for Udoo Board X86')
  78.     parser.add_argument('-u', action='store_true', help='Check for new bios updates')
  79.     parser.add_argument('--install', metavar='version', type=str, required=False, default=None, help='Check for new bios updates')
  80.     args = parser.parse_args()
  81.     main(args.u, args.install)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top