Completed
Push — master ( bc42c6...67fcfc )
by John
10:06
created

tclloader_main()   C

Complexity

Conditions 7

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
c 1
b 0
f 1
dl 0
loc 47
rs 5.5
1
#!/usr/bin/env python3
2
"""Generate autoloader from TCL autoloader template."""
3
4
import os  # path work
5
import shutil  # directory work
6
import sys  # load arguments
7
import requests  # session
0 ignored issues
show
Unused Code introduced by
The import requests seems to be unused.
Loading history...
8
from bbarchivist import archiveutils  # zip work
0 ignored issues
show
Bug introduced by
The name archiveutils does not seem to exist in module bbarchivist.
Loading history...
9
from bbarchivist import decorators  # enter to exit
10
from bbarchivist import loadergen  # packing loader
11
from bbarchivist import networkutils  # download android tools
12
from bbarchivist import scriptutils  # default parser
13
from bbarchivist import utilities  # argument filters
14
15
__author__ = "Thurask"
16
__license__ = "WTFPL v2"
17
__copyright__ = "2017 Thurask"
18
19
20
def grab_args():
21
    """
22
    Parse arguments from argparse/questionnaire.
23
24
    Invoke :func:`tclloader.tclloader_main` with those arguments.
25
    """
26
    parser = scriptutils.default_parser("bb-tclloader", "Create autoloaders from TCL templates")
27
    parser.add_argument("loaderfile",
28
        help="Loader zip file or directory",
29
        type=utilities.file_exists)
30
    parser.add_argument(
31
        "-n",
32
        "--name",
33
        dest="loadername",
34
        help="Manually specify loader name",
35
        metavar="NAME",
36
        default=None)
37
    parser.add_argument(
38
        "-d",
39
        "--directory",
40
        dest="directory",
41
        help="Use a directory instead of a zip file",
42
        action="store_true",
43
        default=False)
44
    parser.add_argument(
45
        "-l",
46
        "--localtools",
47
        dest="localtools",
48
        help="Use local fastboot tools instead of remote",
49
        action="store_true",
50
        default=False)
51
    parser.add_argument(
52
        "-c",
53
        "--compress",
54
        dest="compress",
55
        help="Compress final autoloader",
56
        action="store_true",
57
        default=False)
58
    args = parser.parse_args(sys.argv[1:])
59
    parser.set_defaults()
60
    tclloader_main(args.loaderfile, args.loadername, args.directory, args.localtools, args.compress)
61
62
63
def tclloader_main(loaderfile, loadername=None, directory=False, localtools=False, compress=False):
64
    """
65
    Scan every PRD and produce latest versions.
66
67
    :param loaderfile: Path to input file/folder.
68
    :type loaderfile: str
69
70
    :param loadername: Name of final autoloader. Default is auto-generated.
71
    :type loadername: str
72
73
    :param directory: If the input file is a folder. Default is False.
74
    :type directory: bool
75
76
    :param localtools: If fastboot is to be copied from the input itself. Default is False.
77
    :type localtools: bool
78
79
    :param compress: If the final loader is to be compressed. Default is False.
80
    :type compress: bool
81
    """
82
    scriptutils.slim_preamble("TCLLOADER")
83
    loaderdir = loaderfile if directory else loaderfile.replace(".zip", "")
84
    osver = loaderdir.split("-")[-1]
85
    if not directory:
86
        print("EXTRACTING FILE")
87
        archiveutils.extract_zip(loaderfile, loaderdir)
88
    if not localtools:
89
        print("DOWNLOADING FASTBOOT")
90
        networkutils.download_android_tools("plattools")
91
        print("VERIFYING FASTBOOT")
92
        andver = archiveutils.verify_android_tools("plattools")
93
        if andver:
94
            print("FASTBOOT OK, EXTRACTING")
95
            archiveutils.extract_android_tools("plattools")
96
        else:
97
            print("FASTBOOT DOWNLOAD FAILED, REVERTING TO LOCAL")
98
            localtools = True
99
    platform = os.listdir(os.path.join(loaderdir, "target", "product"))[0]
100
    if loadername is None:
101
        loadername = "{0}_autoloader_user-all-{1}".format(platform, osver)
102
    print("CREATING LOADER")
103
    loadergen.generate_tclloader(loaderdir, loadername, platform, localtools)
104
    shutil.rmtree("plattools")
105
    if compress:
106
        print("PACKING LOADER")
107
        archiveutils.pack_tclloader(loadername, loadername)
108
        print("COMPRESSION FINISHED")
109
    print("LOADER COMPLETE!")
110
111
if __name__ == "__main__":
112
    grab_args()
113
    decorators.enter_to_exit(False)
114