summarylogtreecommitdiffstats
path: root/download.py
blob: 3d2cd7a0232d5326d0665f6d93d620a5ec174698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import re
import sys
from pathlib import Path
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def humansize(nbytes):
    suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
    i = 0
    while nbytes >= 1024 and i < len(suffixes)-1:
        nbytes /= 1024.
        i += 1
    f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
    return '%s %s' % (f, suffixes[i])


def message(msg):
    _bar = "\u2588"
    sys.stdout.write(_bar+"\r"+msg)
    # sys.stdout.flush()


_ldk_filename = "Sentinel_LDK_Linux_Run-time_Installer_script.tar.gz"


def wait_downloading():
    timeout_start = 60
    timeout_download = 60*5
    target_path = Path(_ldk_filename)

    if not (target_path.exists() and target_path.stat().st_size > 0):
        while not list(Path('.').glob('*.part')) and timeout_start:
            message(f"waiting for download to start {timeout_start} sec.")
            sleep(1)
            timeout_start -= 1

        if not list(Path('.').glob('*.part')):
            raise Exception("Download failed")

        while (tmp_files := list(Path('.').glob('*.part'))) and timeout_download:
            filesize = os.path.getsize(tmp_files[0])
            message(f"Downloading file: {humansize(filesize)} wait {timeout_download} sec.")
            sleep(1)
            timeout_download -= 1

        if not (target_path.exists() and target_path.stat().st_size > 0):
            raise Exception("Download failed")

    message("Downloading complete\n")


options = webdriver.FirefoxOptions()
options.headless = True
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", os.getcwd())
options.set_preference(
    "browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

try:
    print("Selenium start..")
    browser = webdriver.Firefox(options=options)

    print("Searching url..")
    browser.get(
        'https://supportportal.gemalto.com/csm?id=kb_article_view&sysparm_article=KB0018315#')

    wait = WebDriverWait(browser, 10)
    element = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "DOW0003342")))
    element.click()
    element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn-success")))
    element.click()

    wait_downloading()
except Exception as error:
    print(error)
    sys.exit(1)
finally:
    browser.quit()

os.system(f"tar -xvzf {_ldk_filename}")
for root, dirs, files in os.walk(os.getcwd()):
    for file in files:
        if match := re.match(r"aksusbd-(\d+\.\d+)\.(\d+)", file):
            _vers, _rel = match.groups()
            os.system(f'echo "{_vers} {_rel}">version.txt')
            sys.exit(0)
sys.exit(1)