-
Notifications
You must be signed in to change notification settings - Fork 0
/
renamer.py
1227 lines (1022 loc) · 47.4 KB
/
renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Rename APK and APKS files to new names based on a supplied pattern.
"""
from __future__ import annotations
import argparse
import copy
import json
import os
import platform
import re
import shutil
import subprocess
import sys
import tempfile
import textwrap
from datetime import datetime
from typing import List, Optional
from colorama import Fore, init
APK_EXTENSION = ".apk"
APKS_EXTENSION = ".apks"
PACKAGE_NAME = "Package Name"
PACKAGE_VERSION_CODE = "Version Code"
PACKAGE_VERSION_NAME = "Version Name"
PACKAGE_MIN_SDK = "Minimum SDK"
PACKAGE_MAX_SDK = "Maximum SDK"
PACKAGE_TARGET_SDK = "Target SDK"
PACKAGE_COMPILE_SDK = "Compile SDK"
PACKAGE_SUPPORTED_SCREENS = "Supported Screens"
PACKAGE_SUPPORTED_ABIS = "Supported ABIs"
PACKAGE_SUPPORTED_DEVICES = "Supported Devices"
PACKAGE_DENSITIES = "Densities"
PACKAGE_LOCALES = "Locales"
PACKAGE_LABEL = "Label"
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(
"""
Rename APK and APKS files based on the supplied pattern.
If any placeholder contains multiple values they are all merged with a comma as the separator.
If any placeholder contains illegal characters or spaces they are all replaced with the supplied separator or '_' by default.
Placeholders supported:
======================================
- Original filename: %orig_name%
- Original app name: %label% | %name%
- Version name: %version% | %version_name%
- Version code: %build% | %version_code%
- Package ID/Name: %package% | %package_name% | %package_id%
- SDK: %min_sdk%, %max_sdk%, %target_sdk%, %compile_sdk%
- Android version number: %min_android_num%, %max_android_num%, %target_android_num%, %compile_android_num%
- Android version name: %min_android_name%, %max_android_name%, %target_android_name%, %compile_android_name%
- Supported screens: %supported_screens% | %screens%
- Supported DPIs: %supported_dpis% | %dpis%
- Supported ABIs: %supported_abis% | %abis%
- Supported devices: %supported_devices% | %devices%
- Supported locales: %supported_locales% | %locales%
======================================
"""))
parser.add_argument("--path",
help="Path to a directory or a single APK file.",
required=True,
type=str,
nargs=1)
parser.add_argument("--pattern",
help="Pattern used to rename files.",
required=True,
type=str,
nargs=1)
parser.add_argument("--separator",
help="Replace any white space and invalid characters in the filename with this character. "
"It doesn't apply to ABIs, densities, screens, locales and supported devices."
" Optional, default is one underscore (_).",
default=["_"],
type=str,
nargs=1)
parser.add_argument("--skip-if-exists",
help="Skip renaming if the file already exists. "
"By default a numeric suffix is appended to the name.",
action="store_true")
parser.add_argument("--build-tools-path",
help="Path to aapt and aapt2 executables. By default uses the ones located in PATH.",
type=str,
nargs=1)
parser.add_argument("--log-path",
help="Path to the directory where to store the log files. Default: Program's directory.",
type=str,
nargs=1)
parser.add_argument("--no-log",
help="Don't create log files.",
action="store_true")
parser.add_argument("--convert-apks",
help="Convert .apks files to .apk.",
action="store_true")
parser.add_argument("--apk-editor-path",
help="Path to the ApkEditor.jar file.",
type=str,
nargs=1)
parser.add_argument("--sign-apk",
help="Sign converted APK files.",
action="store_true")
parser.add_argument("--key-file",
help="Key file used to sign the APK, required if --convert-apks is used.",
type=str,
nargs=1)
parser.add_argument("--cert-file",
help="Certificate file used to sign the APK, required if --convert-apks is used.",
type=str,
nargs=1)
parser.add_argument("--certificate-password",
help="Certificate's password used to sign the converted APK.",
type=str,
nargs=1)
args = parser.parse_args()
init(autoreset=True)
if args.build_tools_path is None:
build_tools_path = args.build_tools_path
else:
build_tools_path = os.path.abspath(args.build_tools_path[0]) # type: Optional[str]
if args.key_file is None:
key_file = args.key_file
else:
key_file = os.path.abspath(args.key_file[0]) # type: Optional[str]
if args.cert_file is None:
cert_file = args.cert_file
else:
cert_file = os.path.abspath(args.cert_file[0]) # type: Optional[str]
if args.certificate_password is None:
certificate_password = args.certificate_password
else:
certificate_password = args.certificate_password[0] # type: Optional[str]
if args.apk_editor_path is None:
apk_editor_path = args.apk_editor_path
else:
apk_editor_path = os.path.abspath(args.apk_editor_path[0]) # type: Optional[str]
if args.log_path is None:
log_path = get_program_dir()
else:
log_path = os.path.abspath(args.log_path[0])
item_path = os.path.abspath(args.path[0])
pattern = args.pattern[0] # type: str
separator = args.separator[0] # type: str
no_log = args.no_log # type: bool
skip_if_exists = args.skip_if_exists # type: bool
convert_apks = args.convert_apks # type: bool
sign_apk = args.sign_apk # type: bool
if build_tools_path is None:
if shutil.which("aapt") is None:
print(Fore.RED + "ERROR: Could not find aapt executable in PATH. Please install it.")
exit(1)
if shutil.which("aapt2") is None:
print("Could not find aapt2 executable in PATH. Please install it.")
exit(1)
else:
if not os.path.exists(build_tools_path):
print(Fore.RED + "ERROR: Provided aapt path doesn't exist.")
exit(1)
if not os.path.isdir(build_tools_path):
print(Fore.RED + "ERROR: Provided aapt path is not a directory.")
exit(1)
if not os.path.exists(log_path):
os.makedirs(log_path)
elif not os.path.isdir(log_path):
print(Fore.RED + "ERROR: Provided log path is not a directory.")
exit(1)
if convert_apks:
if shutil.which("java") is None:
print(Fore.RED + "ERROR: Java not found in PATH, can't convert .apks files to .apk.")
exit(1)
if apk_editor_path is None:
print(Fore.RED + "ERROR: Please specify the full path to the ApkEditor JAR file.")
exit(1)
elif not os.path.isfile(apk_editor_path):
print(Fore.RED + "ERROR: Invalid ApkEditor JAR path.")
exit(1)
if sign_apk:
if build_tools_path is None and shutil.which("apksigner") is None:
print(Fore.RED + "ERROR:Please install the build-tools package of the Android SDK if you want to convert "
"APKS files.")
print(Fore.RED + "Apksigner not found.")
exit(1)
if key_file is None or cert_file is None:
print(Fore.RED + "ERROR: Please provide the key and certificate files for APK signing.\n")
exit(1)
else:
if not os.path.isfile(key_file):
print(Fore.RED + "ERROR: Invalid key file path.")
exit(1)
if not os.path.isfile(cert_file):
print(Fore.RED + "ERROR: Invalid cert file path.")
exit(1)
if (os.path.isfile(item_path) and os.path.splitext(item_path)[1].lower() != APK_EXTENSION
and os.path.splitext(item_path)[1].lower() != APKS_EXTENSION):
print(Fore.RED + "ERROR: Supplied path is not an APK or APKS file.")
exit(1)
errored_apps_list = []
process_path(item_path=item_path,
pattern=pattern,
separator=separator,
errored_apps_list=errored_apps_list,
build_tools_path=build_tools_path,
convert_apks=convert_apks,
apk_editor_path=apk_editor_path,
sign_apk=sign_apk,
key_file=key_file,
cert_file=cert_file,
certificate_password=certificate_password,
skip_if_exists=skip_if_exists)
if len(errored_apps_list) > 0 and not no_log:
write_log(items=errored_apps_list,
file_name="ErroredApps",
log_path=log_path)
print(Fore.GREEN + "All done!")
def process_path(item_path: str,
pattern: str,
separator: str = "_",
errored_apps_list: Optional[list] = None,
build_tools_path: Optional[str] = None,
convert_apks: bool = False,
apk_editor_path: Optional[str] = None,
sign_apk: bool = False,
key_file: Optional[str] = None,
cert_file: Optional[str] = None,
certificate_password: Optional[str] = None,
skip_if_exists: bool = False) -> None:
"""
| Rename APK and APKS files based on the supplied pattern.
|
| If any placeholder contains multiple values, they are all merged with a comma as the separator.
|
| If any placeholder contains illegal characters or spaces, they are all replaced with the supplied separator or
one '_' (underscore) by default.
|
| Placeholders supported:
* **Original filename**: %orig_name%
* **Original app name**: %label% **|** %name%
* **Version name**: %version% **|** %version_name%
* **Version code**: %build% **|** %version_code%
* **Package ID/Name**: %package% **|** %package_name% **|** %package_id%
* **SDK**: %min_sdk%, %max_sdk%, %target_sdk%, %compile_sdk%
* **Android version number**: %min_android_num%, %max_android_num%, %target_android_num%, %compile_android_num%
* **Android version name**: %min_android_name%, %max_android_name%, %target_android_name%,
%compile_android_name%
* **Supported screens**: %supported_screens% **|** %screens%
* **Supported DPIs**: %supported_dpis% **|** %dpis%
* **Supported ABIs**: %supported_abis% **|** %abis%
* **Supported devices**: %supported_devices% **|** %devices%
* **Supported locales**: %supported_locales% **|** %locales%
:param item_path: Path to a file or directory.
:param pattern: Pattern for file renaming.
:param separator: Character to replace with any spaces and illegal characters found in the placeholder values.
:param errored_apps_list: List to store errored files.
:param build_tools_path: Path to build-tools directory of the Android SDK. Only *aapt* and *aapt2* are used from
this directory unless `sign_apk` is `True` in which case *apksigner* will be used too. If not supplied, they will
be searched for in **PATH**.
:param convert_apks: Whether to convert APKS files to APK. Defaults to `False`.
:param apk_editor_path: Path to the ApkEditor JAR file used to convert APKS files.
:param sign_apk: Whether to sign the resulting APK files. Defaults to `False`.
:param key_file: Path to the key file used for APK signing.
:param cert_file: Path to the certificate file used for APK signing.
:param certificate_password: Certificate's password.
:param skip_if_exists: Skip the rename if the output filename already exists. Defaults to `False`.
"""
if os.path.isfile(item_path):
process_file(item_path=item_path,
pattern=pattern,
separator=separator,
errored_apps_list=errored_apps_list,
build_tools_path=build_tools_path,
convert_apks=convert_apks,
apk_editor_path=apk_editor_path,
sign_apk=sign_apk,
key_file=key_file,
cert_file=cert_file,
certificate_password=certificate_password,
skip_if_exists=skip_if_exists)
elif os.path.isdir(item_path):
# for root, dirs, files in os.walk(item_path):
# filenames = dirs + files
# for file in filenames:
for file in os.listdir(item_path):
file_path = os.path.join(item_path, file)
process_file(item_path=file_path,
pattern=pattern,
separator=separator,
errored_apps_list=errored_apps_list,
build_tools_path=build_tools_path,
convert_apks=convert_apks,
apk_editor_path=apk_editor_path,
sign_apk=sign_apk,
key_file=key_file,
cert_file=cert_file,
certificate_password=certificate_password,
skip_if_exists=skip_if_exists)
def process_file(item_path: str,
pattern: str,
separator: str,
errored_apps_list: Optional[list] = None,
build_tools_path: Optional[str] = None,
convert_apks: bool = False,
apk_editor_path: Optional[str] = None,
sign_apk: bool = False,
key_file: Optional[str] = None,
cert_file: Optional[str] = None,
certificate_password: Optional[str] = None,
skip_if_exists: bool = False) -> None:
apk_info = {}
if os.path.isdir(item_path):
return
if (os.path.splitext(item_path)[1].lower() != APK_EXTENSION and
os.path.splitext(item_path)[1].lower() != APKS_EXTENSION):
return
apk_info = get_info(app_file_path=item_path,
build_tools_path=build_tools_path,
errored_apps_list=errored_apps_list,
apk_info=apk_info)
if len(apk_info) == 0:
return
new_file_name = rename_file(pattern=pattern,
apk_info=apk_info,
separator=separator,
file_path=item_path,
errored_apps_list=errored_apps_list,
skip_if_exists=skip_if_exists)
if new_file_name is None:
return
if item_path.lower().endswith(APKS_EXTENSION) and convert_apks:
convert_to_apk(key_file=key_file,
cert_file=cert_file,
certificate_password=certificate_password,
apks_file=new_file_name,
build_tools_path=build_tools_path,
sign_apk=sign_apk,
apk_editor_path=apk_editor_path)
def write_log(items: list,
file_name: str,
log_path: str) -> None:
today_date = datetime.today().strftime("%Y%m%d_%H%M%S")
file_name = os.path.join(log_path, file_name + "_" + today_date + ".log")
try:
log_stream = open(file_name, "w")
except IOError as e:
print(e, end="\n\n")
return
for item in items:
try:
log_stream.write(item + "\n")
except IOError as e:
print(e, end="\n\n")
return
def rename_file(file_path: str,
pattern: str,
apk_info: dict,
separator: str = "_",
errored_apps_list: list = None,
skip_if_exists: bool = False) -> Optional[str]:
"""
Rename file and return new filename.
:param file_path: Path to the file to rename.
:param pattern: The pattern to rename to.
:param apk_info: Dict containing the information of the APK/APKS file.
:param separator: The separator to use for the replacing of spaces and invalid characters.
:param errored_apps_list: List to store errored files.
:param skip_if_exists: Whether to skip the renaming if output file exists.
:return: The new name of the file, or the same name if `skip_if_exists` is `True` and the new filename already
exists.
"""
new_name = pattern
if "%original_name%" in new_name:
new_name = new_name.replace("%original_name%", apk_info["Original Name"].replace(" ", separator))
if "%label%" in new_name:
new_name = new_name.replace("%label%", apk_info[PACKAGE_LABEL].replace(" ", separator))
if "%name%" in new_name:
new_name = new_name.replace("%name%", apk_info[PACKAGE_LABEL].replace(" ", separator))
if "%version%" in new_name:
new_name = new_name.replace("%version%", apk_info[PACKAGE_VERSION_NAME].replace(" ", separator))
if "%version_name%" in new_name:
new_name = new_name.replace("%version_name%", apk_info[PACKAGE_VERSION_NAME].replace(" ", separator))
if "%build%" in new_name:
new_name = new_name.replace("%build%", apk_info[PACKAGE_VERSION_CODE].replace(" ", separator))
if "%version_code%" in new_name:
new_name = new_name.replace("%version_code%", apk_info[PACKAGE_VERSION_CODE].replace(" ", separator))
if "%package%" in new_name:
new_name = new_name.replace("%package%", apk_info[PACKAGE_NAME].replace(" ", separator))
if "%package_name%" in new_name:
new_name = new_name.replace("%package_name%", apk_info[PACKAGE_NAME].replace(" ", separator))
if "%package_id%" in new_name:
new_name = new_name.replace("%package_id%", apk_info[PACKAGE_NAME].replace(" ", separator))
if "%min_sdk%" in new_name:
new_name = new_name.replace("%min_sdk%", apk_info[PACKAGE_MIN_SDK].replace(" ", separator))
if "%max_sdk%" in new_name:
new_name = new_name.replace("%max_sdk%", apk_info[PACKAGE_MAX_SDK].replace(" ", separator))
if "%target_sdk%" in new_name:
new_name = new_name.replace("%target_sdk%", apk_info[PACKAGE_TARGET_SDK].replace(" ", separator))
if "%compile_sdk%" in new_name:
new_name = new_name.replace("%compile_sdk%", apk_info[PACKAGE_COMPILE_SDK].replace(" ", separator))
if "%min_android_num%" in new_name:
new_name = new_name.replace("%min_android_num%",
translate_sdk(apk_info[PACKAGE_MIN_SDK], True).replace(" ", separator))
if "%max_android_num%" in new_name:
new_name = new_name.replace("%max_android_num%",
translate_sdk(apk_info[PACKAGE_MAX_SDK], True).replace(" ", separator))
if "%target_android_num%" in new_name:
new_name = new_name.replace("%target_android_num%",
translate_sdk(apk_info[PACKAGE_TARGET_SDK], True).replace(" ", separator))
if "%compile_android_num%" in new_name:
new_name = new_name.replace("%compile_android_num%",
translate_sdk(apk_info[PACKAGE_COMPILE_SDK], True).replace(" ", separator))
if "%min_android_name%" in new_name:
new_name = new_name.replace("%min_android_name%",
translate_sdk(apk_info[PACKAGE_MIN_SDK], False).replace(" ", separator))
if "%max_android_name%" in new_name:
new_name = new_name.replace("%max_android_name%",
translate_sdk(apk_info[PACKAGE_MAX_SDK], False).replace(" ", separator))
if "%target_android_name%" in new_name:
new_name = new_name.replace("%target_android_name%",
translate_sdk(apk_info[PACKAGE_TARGET_SDK], False).replace(" ", separator))
if "%compile_android_name%" in new_name:
new_name = new_name.replace("%compile_android_name%",
translate_sdk(apk_info[PACKAGE_COMPILE_SDK], False).replace(" ", separator))
if "%supported_screens%" in new_name:
new_name = new_name.replace("%screens%",
join_values(apk_info[PACKAGE_SUPPORTED_SCREENS]).replace(" ", separator))
if "%screens%" in new_name:
new_name = new_name.replace("%screens%",
join_values(apk_info[PACKAGE_SUPPORTED_SCREENS]).replace(" ", separator))
if "%supported_dpis%" in new_name:
new_name = new_name.replace("%supported_dpis%",
join_values(apk_info[PACKAGE_DENSITIES]).replace(" ", separator))
if "%dpis%" in new_name:
new_name = new_name.replace("%dpis%",
join_values(apk_info[PACKAGE_DENSITIES]).replace(" ", separator))
if "%supported_abis%" in new_name:
new_name = new_name.replace("%supported_abis%",
join_values(apk_info[PACKAGE_SUPPORTED_ABIS]).replace(" ", separator))
if "%abis%" in new_name:
new_name = new_name.replace("%abis%",
join_values(apk_info[PACKAGE_SUPPORTED_ABIS]).replace(" ", separator))
if "%supported_devices%" in new_name:
new_name = new_name.replace("%supported_devices%",
join_values(apk_info[PACKAGE_SUPPORTED_DEVICES]).replace(" ", separator))
if "%devices%" in new_name:
new_name = new_name.replace("%devices%",
join_values(apk_info[PACKAGE_SUPPORTED_DEVICES]).replace(" ", separator))
if "%supported_locales%" in new_name:
new_name = new_name.replace("%supported_locales%",
join_values(apk_info[PACKAGE_LOCALES]).replace(" ", separator))
if "%locales%" in new_name:
new_name = new_name.replace("%locales%",
join_values(apk_info[PACKAGE_LOCALES]).replace(" ", separator))
new_name = sanitize_name(name=new_name, separator=separator)
new_name_ext = os.path.splitext(file_path)[1]
old_name = os.path.splitext(os.path.basename(file_path))[0]
if new_name == old_name:
return file_path
new_name = os.path.join(os.path.split(file_path)[0], new_name)
new_new_name = new_name
i = 1
# str(i).zfill(pad_amount)
if skip_if_exists and os.path.exists(new_name + new_name_ext):
return file_path
while os.path.exists(new_new_name + new_name_ext):
new_new_name = new_name + separator + str(i).zfill(2)
i += 1
try:
os.rename(file_path, new_new_name + new_name_ext)
except PermissionError as e:
print(Fore.RED + "ERROR: Couldn't rename {}. Permission denied.".format(file_path), end="\n\n")
print(e, end="\n\n")
if errored_apps_list is not None:
errored_apps_list.append(file_path)
return None
sig_ext = ".idsig"
if os.path.exists(file_path + sig_ext):
try:
os.rename(file_path + sig_ext, new_new_name + new_name_ext + sig_ext)
except PermissionError as e:
print(Fore.RED + "ERROR: Couldn't rename {}. Permission denied.".format(file_path + sig_ext), end="\n\n")
print(e, end="\n\n")
if errored_apps_list is not None:
errored_apps_list.append(file_path + sig_ext)
return None
return new_new_name + new_name_ext
def sanitize_name(name: str,
separator: str) -> str:
illegal_characters = ("\\",
"/",
":",
"*",
"?",
"\"",
"<",
">",
"|",
"\t",
"\n")
for character in illegal_characters:
name = name.replace(character, separator)
regex_pattern = r"(\(\)|{}|\[\])"
name = re.sub(separator + regex_pattern, "", name)
name = re.sub(regex_pattern + separator, "", name)
name = re.sub(regex_pattern, "", name)
return name
def join_values(list_values: list) -> str:
new_name = ""
if len(list_values) != 0:
for value in list_values:
new_name += value + ","
if new_name.endswith(","):
new_name = new_name[:-1]
return new_name
def get_program_dir() -> str:
if getattr(sys, "frozen", False):
return os.path.dirname(sys.executable)
elif __file__:
return os.path.abspath(os.path.dirname(__file__))
def translate_sdk(sdk: str,
number: bool) -> str:
if sdk == "":
return ""
stream = open(os.path.join(get_program_dir(), "sdk_names.json"), "r", encoding="utf_8")
sdk_names = json.load(stream)
sdk = "SDK-" + sdk
if number:
new_sdk_name = sdk_names[sdk][0]
else:
new_sdk_name = sdk_names[sdk][1]
return new_sdk_name
def get_info(app_file_path: str,
build_tools_path: str = None,
errored_apps_list: list = None,
apk_info: dict = None) -> dict:
"""
| Get information from an APK or APKS file.
|
| Available keys are:
* Supported Devices: `list`
* Supported Screens: `list`
* Densities: `list`
* Supported ABIs: `list`
* Locales: `list`
* Label: `str`
* Package Name: `str`
* Version Code: `str`
* Version Name: `str`
* Compile SDK: `str`
* Minimum SDK: `str`
* Maximum SDK: `str`
* Target SDK: `str`
:param app_file_path: Path to an APK file
:param build_tools_path: Path to build-tools directory of the Android SDK. Only *aapt* and *aapt2* are used from
this directory unless `sign_apk` is `True` in which case *apksigner* will be used too. If not supplied, they will
be searched for in **PATH**.
:param errored_apps_list: List to store errored files. If not supplied, no errored files are stored.
:param apk_info: Dictionary to store the APK information. If not supplied, a new dict is created.
:return: Dictionary containing the information about the APK/APKS file.
"""
if apk_info is None:
apk_info = {}
is_apks = False
apks_content = []
temp_path = os.path.join(tempfile.gettempdir(), str(os.getpid()))
if os.path.splitext(app_file_path)[1].lower() == APKS_EXTENSION:
temp_path = pre_process_apks(apks_file=app_file_path, temp_path=temp_path)
if temp_path is None:
return {}
is_apks = True
app_directory = os.path.dirname(app_file_path)
app_filename = os.path.basename(app_file_path)
apk_info["Original Name"] = os.path.splitext(app_filename)[0]
if is_apks:
apks_content = sorted(os.listdir(temp_path))
badging_content = badging(app_file_path=app_file_path,
is_apks=is_apks,
apks_content=apks_content,
build_tools_path=build_tools_path,
errored_apps_list=errored_apps_list,
temp_path=temp_path)
try:
shutil.rmtree(temp_path)
except FileNotFoundError:
pass
except PermissionError as e:
print(Fore.RED + "\tERROR: Couldn't remove the temp directory for {}. Permission denied.".format(app_filename),
end="\n\n")
print(e, end="\n\n")
exit(1)
if badging_content is None:
return {}
apk_info = parse_badging(badging_content=badging_content,
apk_info=apk_info)
# Essential information, if we couldn't get this info then something is either wrong with this program or the
# APK file.
if len(apk_info[PACKAGE_NAME]) == 0 or len(apk_info[PACKAGE_VERSION_NAME]) == 0 or len(
apk_info[PACKAGE_VERSION_CODE]) == 0:
basename = os.path.basename(app_filename)
print(Fore.RED + "\tERROR: We couldn't extract information from {}, please report this.".format(basename),
end="\n\n")
if errored_apps_list is not None:
errored_apps_list.append(app_file_path)
return {}
return apk_info
def badging(app_file_path: str,
is_apks: bool,
apks_content: List[str],
temp_path: str,
build_tools_path: str = None,
errored_apps_list: list = None) -> Optional[str]:
output = ""
# TODO: Add option to look in the ANDROID_HOME ENV VAR
aapt_bin_path = shutil.which("aapt")
aapt2_bin_path = shutil.which("aapt2")
if build_tools_path is not None:
aapt_bin_path = os.path.join(build_tools_path[0], "aapt")
aapt2_bin_path = os.path.join(build_tools_path[0], "aapt2")
orig_badging_command = [aapt_bin_path,
"d",
"--include-meta-data",
"badging"]
orig_badging_command_alt = [aapt2_bin_path,
"d",
"badging",
"--include-meta-data"]
error_text = "Illegal byte sequence"
if is_apks:
for apk_file in apks_content:
apk_file = os.path.join(temp_path, apk_file)
if os.path.splitext(apk_file)[1].lower() == APK_EXTENSION:
try:
badging_command = copy.deepcopy(orig_badging_command)
badging_command.append(apk_file)
output += subprocess.check_output(args=badging_command,
encoding="utf_8",
errors="replace").strip() + "\n"
except subprocess.CalledProcessError:
pass
if output == "" or error_text in output:
output = "" # reset value
for apk_file in apks_content:
apk_file = os.path.join(temp_path, apk_file)
if os.path.splitext(apk_file)[1].lower() == APK_EXTENSION:
try:
badging_command_alt = copy.deepcopy(orig_badging_command_alt)
badging_command_alt.append(apk_file)
output += subprocess.check_output(args=badging_command_alt,
encoding="utf_8",
errors="replace").strip() + "\n"
except subprocess.CalledProcessError:
pass
else:
try:
badging_command = copy.deepcopy(orig_badging_command)
badging_command.append(app_file_path)
output = subprocess.check_output(args=badging_command,
encoding="utf_8",
errors="replace").strip()
except subprocess.CalledProcessError:
pass
if output == "" or error_text in output:
try:
badging_command_alt = copy.deepcopy(orig_badging_command_alt)
badging_command_alt.append(app_file_path)
output = subprocess.check_output(args=badging_command_alt,
encoding="utf_8",
errors="replace").strip()
except subprocess.CalledProcessError:
pass
if output == "" or error_text in output:
if errored_apps_list is not None:
errored_apps_list.append(app_file_path)
print(Fore.RED + "\tERROR: There was an error getting the badge information for {}".format(app_file_path),
end="\n\n")
return None
else:
return output
def parse_badging(badging_content: str,
apk_info: dict) -> dict:
badging_lines = badging_content.splitlines()
any_density = False
apk_info[PACKAGE_SUPPORTED_DEVICES] = ["Android"]
apk_info[PACKAGE_SUPPORTED_SCREENS] = []
apk_info[PACKAGE_DENSITIES] = []
apk_info[PACKAGE_SUPPORTED_ABIS] = []
apk_info[PACKAGE_LOCALES] = []
apk_info[PACKAGE_LABEL] = ""
apk_info[PACKAGE_NAME] = ""
apk_info[PACKAGE_VERSION_CODE] = ""
apk_info[PACKAGE_VERSION_NAME] = ""
apk_info[PACKAGE_COMPILE_SDK] = ""
apk_info[PACKAGE_MIN_SDK] = ""
apk_info[PACKAGE_MAX_SDK] = ""
apk_info[PACKAGE_TARGET_SDK] = ""
if "leanback-launchable-activity" in badging_content:
apk_info[PACKAGE_SUPPORTED_DEVICES].append("Android TV")
if "com.google.android.gms.car.application" in badging_content:
apk_info[PACKAGE_SUPPORTED_DEVICES].append("Android Auto")
if "android.hardware.type.watch" in badging_content:
apk_info[PACKAGE_SUPPORTED_DEVICES].append("Wear OS")
for line in badging_lines:
line = line.strip()
split_values = line.split(":", maxsplit=1)
key = split_values[0].strip()
if len(split_values) == 1:
value = ""
else:
value = split_values[1].strip()
if key == "application-label":
get_label(apk_info, value)
elif key == "package":
get_package_info(apk_info, value)
elif key == "sdkVersion" and apk_info.get(PACKAGE_MIN_SDK, "") == "":
get_sdk_version(apk_info, value)
elif key == "maxSdkVersion" and apk_info.get(PACKAGE_MAX_SDK, "") == "":
get_max_sdk_version(apk_info, value)
elif key == "targetSdkVersion" and apk_info.get(PACKAGE_TARGET_SDK, "") == "":
get_target_sdk_version(apk_info, value)
elif key == "supports-screens":
get_supported_screens(apk_info, value)
elif key == "supports-any-density":
if value.replace("'", "") == "true":
any_density = True
elif key == "densities":
get_densities(apk_info, value)
elif key == "native-code" or key == "alt-native-code":
get_supported_abis(apk_info, value)
elif key == "locales":
get_supported_locales(apk_info, value)
rename_densities(apk_info, any_density)
return apk_info
def pre_process_apks(apks_file: str,
temp_path: str) -> Optional[str]:
"""
Pre-process APKS files, extracting their content and saving them in a temporary directory returning the path to
that temporary directory.
:param apks_file: APKS file to pre-process.
:param temp_path: Main temporary path where to store the temporary directory and content of this APKS file.
:return: Path to the new temporary directory or None if an error occurred.
"""
new_temp_path = os.path.join(temp_path, os.path.splitext(os.path.basename(apks_file))[0].strip())
try:
os.makedirs(new_temp_path)
except FileExistsError:
pass
import zipfile
try:
zipfile.ZipFile(apks_file).extractall(new_temp_path)
except (zipfile.BadZipFile, PermissionError) as e:
print(e, end="\n\n")
return None
return new_temp_path
def convert_to_apk(apks_file: str,
apk_editor_path: str,
sign_apk: bool = False,
key_file: str = None,
cert_file: str = None,
certificate_password: str = None,
build_tools_path: str = None) -> bool:
"""
Convert an APKS file to APK, optionally signing it.
:param apks_file: Path to the APKS file.
:param apk_editor_path: Path to APKEditor JAR file.
:param sign_apk: Whether to sign the resulting APK file.
:param key_file: Path to the key file used to sign the resulting APK file.
:param cert_file: Path to the certificate file used to sign the resulting APK file.
:param certificate_password: Certificate's password.
:param build_tools_path: Path to build-tools directory of the Android SDK. Only *apksigner* is used from this path,
if not specified **PATH** is used.
:return: *True* if the conversion was successful and *False* otherwise.
"""
print(Fore.GREEN + "\tConverting {} to .APK...".format(os.path.basename(apks_file)), end="\n\n")
convert_command_orig = ["java",
"-jar",
apk_editor_path,
"m",
"-i",
"",
"-o",
"",
"-f"]
if sign_apk:
apk_path_unsigned = os.path.splitext(apks_file)[0] + "_unsigned.apk"
else:
apk_path_unsigned = os.path.splitext(apks_file)[0] + APK_EXTENSION
try:
old_app_stats = os.lstat(apks_file)
except PermissionError:
print(Fore.YELLOW + "\tWARNING: Couldn't get stats of the APKS file, check permissions."
" Old timestamps wont be restored.", end="\n\n")
old_app_stats = None
try:
convert_command = copy.deepcopy(convert_command_orig)
convert_command[5] = apks_file
convert_command[7] = apk_path_unsigned
subprocess.run(convert_command, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(Fore.RED + "\tERROR: There was an error converting {} to .apk".format(os.path.basename(apks_file)),
end="\n\n")
print(e, end="\n\n")
if os.path.exists(apk_path_unsigned):
try:
os.remove(apk_path_unsigned)
except PermissionError as e:
print(Fore.RED + "\t\tERROR: Couldn't remove unfinished APK file. Permission denied.", end="\n\n")
print(e, end="\n\n")
return False
if sign_apk:
apk_path_signed = os.path.splitext(apks_file)[0] + APK_EXTENSION
if not sign_apk_file(key_file=key_file,
cert_file=cert_file,
apk_path_unsigned=apk_path_unsigned,
apk_path_signed=apk_path_signed,
certificate_password=certificate_password,
build_tools_path=build_tools_path):
return False
if old_app_stats is not None:
restore_dates(old_app_stats=old_app_stats,
apk_file=apk_path_signed)
elif old_app_stats is not None:
restore_dates(old_app_stats=old_app_stats,
apk_file=apk_path_unsigned)
try:
os.remove(apks_file)
except PermissionError as e:
print(Fore.RED + "\tERROR: Error deleting file {}. Permission denied".format(os.path.basename(apks_file)),
end="\n\n")
print(e, end="\n\n")
if sign_apk:
try:
os.remove(apk_path_unsigned)
except PermissionError as e:
print(Fore.RED + "\tERROR: Error deleting file {}. Permission denied".format(os.path.basename(
apk_path_unsigned)), end="\n\n")
print(e, end="\n\n")
print(Fore.GREEN + "\tDone converting {}.".format(os.path.basename(apks_file)), end="\n\n")
return True
def sign_apk_file(key_file: str,