Completed
Push — master ( 95641c...983a39 )
by John
03:37
created

generate_specs()   A

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
1
#!/usr/bin/env python3
2
3
from os import remove, listdir, devnull, getcwd
4
from os.path import join, basename
5
from shutil import copy, rmtree
6
from subprocess import call, STDOUT
7
from requests import certs, get
8
from bbarchivist.bbconstants import VERSION, LONGVERSION, CAP, JSONFILE, COMMITDATE
9
from bbarchivist.utilities import prep_seven_zip, get_seven_zip
10
11
12
def write_versions():
13
    """
14
    Write temporary version files.
15
    """
16
    with open("version.txt", "w") as afile:
17
        afile.write(VERSION)
18
    with open("longversion.txt", "w") as afile:
19
        afile.write("{0}\n{1}".format(LONGVERSION, COMMITDATE))
20
21
22
def clean_versions():
23
    """
24
    Remove temporary version files.
25
    """
26
    remove("version.txt")
27
    remove("longversion.txt")
28
29
30
def generate_specs():
31
    """
32
    Generate pyinstaller spec files.
33
    """
34
    scripts = ["archivist", "autolookup", "barlinker", "carrierchecker", "certchecker", "devloader", "downloader", "droidlookup", "droidscraper", "escreens", "kernchecker", "lazyloader", "linkgen", "metachecker", "swlookup"]
35
    here = getcwd().replace("\\", "\\\\")
36
    for script in scripts:
37
        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)
38
        with open("{0}.spec".format(script), "w") as afile:
39
            afile.write(template)
40
41
42
def clean_specs():
43
    """
44
    Remove pyinstaller spec files.
45
    """
46
    specs = [x for x in listdir() if x.endswith(".spec")]
47
    for spec in specs:
48
        remove(spec)
49
50
51
def get_sevenzip():
52
    """
53
    Get 7-Zip.
54
    """
55
    szurl = "http://www.7-zip.org/a/7z1604-extra.7z"
56
    psz = prep_seven_zip()
57
    if psz:
58
        szexe = get_seven_zip()
59
        szfile = basename(szurl)
60
        with open(szfile, "wb") as afile:
61
            req = get(szurl, stream=True)
62
            for chunk in req.iter_content(chunk_size=1024):
63
                afile.write(chunk)
64
        cmd = "{0} x {1} -o7z".format(szexe, szfile)
65
        with open(devnull, "wb") as dnull:
66
            call(cmd, stdout=dnull, stderr=STDOUT, shell=True)
67
        remove(basename(szurl))
68
    else:
69
        print("GO TO {0} AND DO IT MANUALLY".format(szurl))
70
        raise SystemError
71
72
73
if __name__ == "__main__":
74
    write_versions()
75
    generate_specs()
76
    specs = [x for x in listdir() if x.endswith(".spec")]
77
    for spec in specs:  # UPX 3.91 BSODs my computer, disable for now
78
        cmd = "pyinstaller --onefile --noupx --workpath pyinst-build --distpath pyinst-dist {0}".format(spec)
79
        call(cmd, shell=True)
80
    outdir = "pyinst-dist"
81
    copy("version.txt", outdir)
82
    copy("longversion.txt", outdir)
83
    copy(CAP.location, outdir)
84
    copy(JSONFILE, outdir)
85
    copy(certs.where(), join(outdir, "cacerts.pem"))
86
    try:
87
        get_sevenzip()
88
    except SystemError:
89
        pass
90
    else:
91
        copy(join("7z", "7za.exe"), outdir)
92
        copy(join("7z", "x64", "7za.exe"), join(outdir, "7za64.exe"))
93
        rmtree("7z", ignore_errors=True)
94
    clean_versions()
95
    clean_specs()
96