Completed
Push — master ( c0a25d...3bf3d6 )
by John
03:47 queued 38s
created

tclloader_extract()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
"""Generate autoloader from TCL autoloader template."""
3
4
import shutil  # directory work
5
import sys  # load arguments
6
from bbarchivist import archiveutils  # zip work
7
from bbarchivist import decorators  # enter to exit
8
from bbarchivist import loadergen  # packing loader
9
from bbarchivist import networkutils  # download android tools
10
from bbarchivist import scriptutils  # default parser
11
from bbarchivist import utilities  # argument filters
12
13
__author__ = "Thurask"
14
__license__ = "WTFPL v2"
15
__copyright__ = "2017-2018 Thurask"
16
17
18 View Code Duplication
def grab_args():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19
    """
20
    Parse arguments from argparse/questionnaire.
21
22
    Invoke :func:`tclloader.tclloader_main` with those arguments.
23
    """
24
    parser = scriptutils.default_parser("bb-tclloader", "Create autoloaders from TCL templates")
25
    parser.add_argument("loaderfile",
26
        help="Loader zip file or directory",
27
        type=utilities.file_exists)
28
    parser.add_argument(
29
        "-n",
30
        "--name",
31
        dest="loadername",
32
        help="Manually specify loader name",
33
        metavar="NAME",
34
        default=None)
35
    parser.add_argument(
36
        "-d",
37
        "--directory",
38
        dest="directory",
39
        help="Use a directory instead of a zip file",
40
        action="store_true",
41
        default=False)
42
    parser.add_argument(
43
        "-l",
44
        "--localtools",
45
        dest="localtools",
46
        help="Use local fastboot tools instead of remote",
47
        action="store_true",
48
        default=False)
49
    parser.add_argument(
50
        "-c",
51
        "--compress",
52
        dest="compress",
53
        help="Compress final autoloader",
54
        action="store_true",
55
        default=False)
56
    parser.add_argument(
57
        "-nw",
58
        "--no-wipe",
59
        dest="wipe",
60
        help="Don't include lines to wipe userdata",
61
        action="store_false",
62
        default=True)
63
    if len(sys.argv) == 1 and getattr(sys, 'frozen', False):
64
        print("You're better off running this from command line")
65
        decorators.enter_to_exit(True)
66
    args = parser.parse_args(sys.argv[1:])
67
    parser.set_defaults()
68
    tclloader_main(args.loaderfile, args.loadername, args.directory, args.localtools, args.compress, args.wipe)
69
70
71
def tclloader_extract(loaderfile, loaderdir, directory=False):
72
    """
73
    Extract autoloader template zip if needed.
74
75
    :param loaderfile: Path to input file.
76
    :type loaderfile: str
77
78
    :param loaderdir: Path to output folder.
79
    :type loaderdir: str
80
81
    :param directory: If the input file is a folder. Default is False.
82
    :type directory: bool
83
    """
84
    if not directory:
85
        print("EXTRACTING FILE")
86
        archiveutils.extract_zip(loaderfile, loaderdir)
87
88
89
def tclloader_fastboot(localtools=False):
90
    """
91
    Download fastboot if needed.
92
93
    :param localtools: If fastboot is to be copied from the input itself. Default is False.
94
    :type localtools: bool
95
    """
96
    if not localtools:
97
        print("DOWNLOADING FASTBOOT")
98
        networkutils.download_android_tools("plattools")
99
        print("VERIFYING FASTBOOT")
100
        andver = archiveutils.verify_android_tools("plattools")
101
        if andver:
102
            print("FASTBOOT OK, EXTRACTING")
103
            archiveutils.extract_android_tools("plattools")
104
        else:
105
            print("FASTBOOT DOWNLOAD FAILED, REVERTING TO LOCAL")
106
            localtools = True
107
    return localtools
108
109
110
def tclloader_compress(compress=False, loadername=None):
111
    """
112
    Compress the final autoloader if needed.
113
114
    :param compress: If the final loader is to be compressed. Default is False.
115
    :type compress: bool
116
117
    :param loadername: Name of final autoloader.
118
    :type loadername: str
119
    """
120
    if compress:
121
        print("PACKING LOADER")
122
        archiveutils.pack_tclloader(loadername, loadername)
123
        print("COMPRESSION FINISHED")
124
125
126
def tclloader_main(loaderfile, loadername=None, directory=False, localtools=False, compress=False, wipe=True):
127
    """
128
    Scan every PRD and produce latest versions.
129
130
    :param loaderfile: Path to input file/folder.
131
    :type loaderfile: str
132
133
    :param loadername: Name of final autoloader. Default is auto-generated.
134
    :type loadername: str
135
136
    :param directory: If the input file is a folder. Default is False.
137
    :type directory: bool
138
139
    :param localtools: If fastboot is to be copied from the input itself. Default is False.
140
    :type localtools: bool
141
142
    :param compress: If the final loader is to be compressed. Default is False.
143
    :type compress: bool
144
145
    :param wipe: If the final loader wipes userdata. Default is True.
146
    :type wipe: bool
147
    """
148
    scriptutils.slim_preamble("TCLLOADER")
149
    loaderdir, osver = scriptutils.tclloader_prep(loaderfile, directory)
150
    tclloader_extract(loaderfile, loaderdir, directory)
151
    localtools = tclloader_fastboot(localtools)
152
    loadername, platform = scriptutils.tclloader_filename(loaderdir, osver, loadername)
153
    print("CREATING LOADER")
154
    loadergen.generate_tclloader(loaderdir, loadername, platform, localtools, wipe)
155
    shutil.rmtree("plattools")
156
    tclloader_compress(compress, loadername)
157
    print("LOADER COMPLETE!")
158
159
160
if __name__ == "__main__":
161
    grab_args()
162
    decorators.enter_to_exit(False)
163