Completed
Push — master ( 49762e...e3cf28 )
by John
10:08
created

get_sevenzip()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 20
rs 8.5454
1
#!/usr/bin/env python3
2
3
from os import remove, listdir, devnull
4
from os.path import join, abspath, dirname, 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 get_sevenzip():
31
    """
32
    Get 7-Zip.
33
    """
34
    szurl = "http://www.7-zip.org/a/7z1604-extra.7z"
35
    psz = prep_seven_zip()
36
    if psz:
37
        szexe = get_seven_zip()
38
        szfile = basename(szurl)
39
        with open(szfile, "wb") as afile:
40
            req = get(szurl, stream=True)
41
            for chunk in req.iter_content(chunk_size=1024):
42
                afile.write(chunk)
43
        cmd = "{0} x {1} -o7z".format(szexe, szfile)
44
        with open(devnull, "wb") as dnull:
45
            call(cmd, stdout=dnull, stderr=STDOUT, shell=True)
46
        remove(basename(szurl))
47
    else:
48
        print("GO TO {0} AND DO IT MANUALLY".format(szurl))
49
        raise SystemError
50
51
52
if __name__ == "__main__":
53
    write_versions()
54
    specs = [x for x in listdir() if x.endswith(".spec")]
55
    for spec in specs:
56
        cmd = "pyinstaller --onefile --workpath pyinst-build --distpath pyinst-dist {0}".format(spec)
57
        call(cmd, shell=True)
58
    outdir = "pyinst-dist"
59
    copy("version.txt", outdir)
60
    copy("longversion.txt", outdir)
61
    copy(CAP.location, outdir)
62
    copy(join("bbarchivist", "bbconstants.json"), outdir)
63
    copy(certs.where(), join(outdir, "cacerts.pem"))
64
    try:
65
        get_sevenzip()
66
    except SystemError:
67
        pass
68
    else:
69
        copy(join("7z", "7za.exe"), outdir)
70
        copy(join("7z", "x64", "7za.exe"), join(outdir, "7za64.exe"))
71
        rmtree("7z", ignore_errors=True)
72
    clean_versions()
73