Skip to content

Commit f1aca1b

Browse files
authored
Convert android-jar.py to Python 3 (#1175)
Also convert some string concat for paths to use `os.path.join`
1 parent 33588de commit f1aca1b

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

jar-infer/scripts/android-jar.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import sys, os, subprocess, ConfigParser
1+
import sys
2+
import os
3+
import subprocess
4+
import configparser
25
from optparse import OptionParser
36

47
class2jar = dict()
58
jars_to_process = set()
69

710
opt_parser = OptionParser()
811
opt_parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="set verbosity")
9-
opt_parser.add_option("-c", "--config", dest="config", default=os.path.dirname(sys.argv[0])+"/android-jar.conf", help="configuration file path")
12+
opt_parser.add_option("-c", "--config", dest="config", default=os.path.join(os.path.dirname(sys.argv[0]), "android-jar.conf"), help="configuration file path")
1013
(options, args) = opt_parser.parse_args()
1114
if not os.path.isfile(options.config):
12-
sys.exit('Error! No configuration file at: '+options.config)
13-
config_parser = ConfigParser.SafeConfigParser()
15+
sys.exit('Error! No configuration file at: ' + options.config)
16+
config_parser = configparser.ConfigParser()
1417
config_parser.read(options.config)
1518
ajars_dir = config_parser.get('android-paths', 'aosp-out-dir')
1619
stubs_jar = config_parser.get('android-paths', 'android-stubs-jar')
@@ -19,38 +22,38 @@
1922
astubx_file = config_parser.get('android-model', 'astubx-path')
2023

2124
### Finding android libraries ###
22-
print "> Finding android libraries..."
25+
print("> Finding android libraries...")
2326
# Since SDK 31, the directory structure mixes classes.jar and classes-header.jar files, we use
2427
# only the former, as classes-header.jar files are missing methods' bytecode.
2528
cmd = "find " + ajars_dir + " -name \"*.jar\" | grep -v classes-header.jar"
26-
ajars = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().splitlines()
29+
ajars = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().decode().splitlines()
2730
for ajar in ajars:
28-
cmd = "jar tvf " + ajar + " | grep \"\.class$\" | awk '{print $8}'"
29-
ajar_classes = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().splitlines()
31+
cmd = "jar tvf " + ajar + " | grep \"\\.class$\" | awk '{print $8}'"
32+
ajar_classes = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().decode().splitlines()
3033
for ajar_class in ajar_classes:
3134
if ajar_class in class2jar:
3235
if options.verbose:
33-
print "[Warn] Same class in multiple jars : " + ajar_class + " in " + class2jar[ajar_class] + " , " + ajar
36+
print("[Warn] Same class in multiple jars : " + ajar_class + " in " + class2jar[ajar_class] + " , " + ajar)
3437
else:
3538
class2jar[ajar_class] = ajar
36-
cmd = "jar tvf " + stubs_jar + " | grep \"\.class$\" | awk '{print $8}'"
37-
ajar_classes = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().splitlines()
39+
cmd = "jar tvf " + stubs_jar + " | grep \"\\.class$\" | awk '{print $8}'"
40+
ajar_classes = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().decode().splitlines()
3841
found = 0
3942
for ajar_class in ajar_classes:
4043
if ajar_class in class2jar:
4144
found += 1
4245
jars_to_process.add(class2jar[ajar_class])
4346
else:
4447
if options.verbose:
45-
print "[Warn] Class not found in any jar : " + ajar_class
46-
print ",".join(list(jars_to_process))
47-
print "Found " + str(found) + " / " + str(len(ajar_classes)) + " in " + str(len(jars_to_process)) + " jars"
48+
print("[Warn] Class not found in any jar : " + ajar_class)
49+
print(",".join(list(jars_to_process)))
50+
print("Found " + str(found) + " / " + str(len(ajar_classes)) + " in " + str(len(jars_to_process)) + " jars")
4851

4952
### Running jarinfer ###
50-
print "> Running jarinfer on android jars..."
51-
cmd = "java -jar " + jarinfer + " -i " + ",".join(list(jars_to_process)) + " -o " + wrk_dir + "/" + astubx_file
53+
print("> Running jarinfer on android jars...")
54+
cmd = "java -jar " + jarinfer + " -i " + ",".join(list(jars_to_process)) + " -o " + os.path.join(wrk_dir, astubx_file)
5255
if options.verbose:
5356
cmd += " -dv"
54-
print cmd
57+
print(cmd)
5558
returncode = subprocess.call(cmd, shell=True)
5659
sys.exit(returncode)

0 commit comments

Comments
 (0)