Completed
Push — master ( f5d9f6...12103c )
by John
03:10
created

call_specs()   A

Complexity

Conditions 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
"""Generate .exe files with PyInstaller."""
3
4
from os import remove, listdir, devnull, getcwd
5
from os.path import join, basename
6
from shutil import copy, rmtree
7
from subprocess import call, STDOUT
8
from requests import certs, get
9
from bbarchivist.bbconstants import VERSION, LONGVERSION, CAP, JSONFILE, COMMITDATE
10
from bbarchivist.utilities import prep_seven_zip, get_seven_zip
11
12
13
def write_versions():
14
    """
15
    Write temporary version files.
16
    """
17
    with open("version.txt", "w") as afile:
18
        afile.write(VERSION)
19
    with open("longversion.txt", "w") as afile:
20
        afile.write("{0}\n{1}".format(LONGVERSION, COMMITDATE))
21
22
23
def clean_versions():
24
    """
25
    Remove temporary version files.
26
    """
27
    remove("version.txt")
28
    remove("longversion.txt")
29
30
31
def generate_specs():
32
    """
33
    Generate pyinstaller spec files.
34
    """
35
    scripts = ["archivist", "autolookup", "barlinker", "carrierchecker", "certchecker", "devloader", "downloader", "droidlookup", "droidscraper", "escreens", "kernchecker", "lazyloader", "linkgen", "metachecker", "swlookup"]
36
    here = getcwd().replace("\\", "\\\\")
37
    for script in scripts:
38
        template = "# -*- mode: python -*-\n\nblock_cipher = None\n\n\na = Analysis(['bbarchivist\\\\scripts\\\\{0}.py'],\n             pathex=['{1}'],\n             binaries=None,\n             datas=None,\n             hiddenimports=[],\n             hookspath=[],\n             runtime_hooks=[],\n             excludes=[],\n             win_no_prefer_redirects=False,\n             win_private_assemblies=False,\n             cipher=block_cipher)\npyz = PYZ(a.pure, a.zipped_data,\n             cipher=block_cipher)\nexe = EXE(pyz,\n          a.scripts,\n          a.binaries,\n          a.zipfiles,\n          a.datas,\n          name='{0}',\n          debug=False,\n          strip=False,\n          upx=False,\n          console=True )\n".format(script, here)
39
        with open("{0}.spec".format(script), "w") as afile:
40
            afile.write(template)
41
42
43
def clean_specs():
44
    """
45
    Remove pyinstaller spec files.
46
    """
47
    specs = [x for x in listdir() if x.endswith(".spec")]
48
    for spec in specs:
49
        remove(spec)
50
51
52
def get_sevenzip():
53
    """
54
    Get 7-Zip.
55
    """
56
    szurl = "http://www.7-zip.org/a/7z1604-extra.7z"
57
    psz = prep_seven_zip()
58
    if psz:
59
        szexe = get_seven_zip()
60
        szfile = basename(szurl)
61
        with open(szfile, "wb") as afile:
62
            req = get(szurl, stream=True)
63
            for chunk in req.iter_content(chunk_size=1024):
64
                afile.write(chunk)
65
        cmd = "{0} x {1} -o7z".format(szexe, szfile)
66
        with open(devnull, "wb") as dnull:
67
            call(cmd, stdout=dnull, stderr=STDOUT, shell=True)
68
        remove(basename(szurl))
69
    else:
70
        print("GO TO {0} AND DO IT MANUALLY".format(szurl))
71
        raise SystemError
72
73
74
def call_specs():
75
    """
76
    Call pyinstaller to make specs.
77
    """
78
    specs = [x for x in listdir() if x.endswith(".spec")]
79
    for spec in specs:  # UPX 3.91 BSODs my computer, disable for now
80
        cmd = "pyinstaller --onefile --noupx --workpath pyinst-build --distpath pyinst-dist {0}".format(spec)
81
        call(cmd, shell=True)
82
83
84
def sz_wrapper(outdir):
85
    """
86
    Copy 7-Zip to outdir.
87
    """
88
    try:
89
        get_sevenzip()
90
    except SystemError:
91
        pass
92
    else:
93
        copy(join("7z", "7za.exe"), outdir)
94
        copy(join("7z", "x64", "7za.exe"), join(outdir, "7za64.exe"))
95
        rmtree("7z", ignore_errors=True)
96
97
98
def main():
99
    """
100
    Create .exes with dynamic spec files.
101
    """
102
    write_versions()
103
    generate_specs()
104
    call_specs()
105
    outdir = "pyinst-dist"
106
    copy("version.txt", outdir)
107
    copy("longversion.txt", outdir)
108
    copy(CAP.location, outdir)
109
    copy(JSONFILE, outdir)
110
    copy(certs.where(), join(outdir, "cacerts.pem"))
111
    sz_wrapper(outdir)
112
    clean_versions()
113
    clean_specs()
114
115
116
if __name__ == "__main__":
117
    main()
118