|
27 | 27 | from adb import common_cli |
28 | 28 |
|
29 | 29 | try: |
30 | | - from adb import sign_m2crypto |
31 | | - rsa_signer = sign_m2crypto.M2CryptoSigner |
| 30 | + from adb import sign_m2crypto |
| 31 | + |
| 32 | + rsa_signer = sign_m2crypto.M2CryptoSigner |
32 | 33 | except ImportError: |
33 | | - try: |
34 | | - from adb import sign_pythonrsa |
35 | | - rsa_signer = sign_pythonrsa.PythonRSASigner.FromRSAKeyPath |
36 | | - except ImportError: |
37 | 34 | try: |
38 | | - from adb import sign_pycryptodome |
| 35 | + from adb import sign_pythonrsa |
39 | 36 |
|
40 | | - rsa_signer = sign_pycryptodome.PycryptodomeAuthSigner |
| 37 | + rsa_signer = sign_pythonrsa.PythonRSASigner.FromRSAKeyPath |
41 | 38 | except ImportError: |
42 | | - rsa_signer = None |
| 39 | + try: |
| 40 | + from adb import sign_pycryptodome |
| 41 | + |
| 42 | + rsa_signer = sign_pycryptodome.PycryptodomeAuthSigner |
| 43 | + except ImportError: |
| 44 | + rsa_signer = None |
43 | 45 |
|
44 | 46 |
|
45 | 47 | def Devices(args): |
46 | | - """Lists the available devices. |
47 | | -
|
48 | | - Mimics 'adb devices' output: |
49 | | - List of devices attached |
50 | | - 015DB7591102001A device 1,2 |
51 | | - """ |
52 | | - for d in adb_commands.AdbCommands.Devices(): |
53 | | - if args.output_port_path: |
54 | | - print('%s\tdevice\t%s' % ( |
55 | | - d.serial_number, ','.join(str(p) for p in d.port_path))) |
56 | | - else: |
57 | | - print('%s\tdevice' % d.serial_number) |
58 | | - return 0 |
| 48 | + """Lists the available devices. |
| 49 | +
|
| 50 | + Mimics 'adb devices' output: |
| 51 | + List of devices attached |
| 52 | + 015DB7591102001A device 1,2 |
| 53 | + """ |
| 54 | + for d in adb_commands.AdbCommands.Devices(): |
| 55 | + if args.output_port_path: |
| 56 | + print('%s\tdevice\t%s' % ( |
| 57 | + d.serial_number, ','.join(str(p) for p in d.port_path))) |
| 58 | + else: |
| 59 | + print('%s\tdevice' % d.serial_number) |
| 60 | + return 0 |
59 | 61 |
|
60 | 62 |
|
61 | 63 | def List(device, device_path): |
62 | | - """Prints a directory listing. |
63 | | -
|
64 | | - Args: |
65 | | - device_path: Directory to list. |
66 | | - """ |
67 | | - files = device.List(device_path) |
68 | | - files.sort(key=lambda x: x.filename) |
69 | | - maxname = max(len(f.filename) for f in files) |
70 | | - maxsize = max(len(str(f.size)) for f in files) |
71 | | - for f in files: |
72 | | - mode = ( |
73 | | - ('d' if stat.S_ISDIR(f.mode) else '-') + |
74 | | - ('r' if f.mode & stat.S_IRUSR else '-') + |
75 | | - ('w' if f.mode & stat.S_IWUSR else '-') + |
76 | | - ('x' if f.mode & stat.S_IXUSR else '-') + |
77 | | - ('r' if f.mode & stat.S_IRGRP else '-') + |
78 | | - ('w' if f.mode & stat.S_IWGRP else '-') + |
79 | | - ('x' if f.mode & stat.S_IXGRP else '-') + |
80 | | - ('r' if f.mode & stat.S_IROTH else '-') + |
81 | | - ('w' if f.mode & stat.S_IWOTH else '-') + |
82 | | - ('x' if f.mode & stat.S_IXOTH else '-')) |
83 | | - t = time.gmtime(f.mtime) |
84 | | - yield '%s %*d %04d-%02d-%02d %02d:%02d:%02d %-*s\n' % ( |
85 | | - mode, maxsize, f.size, |
86 | | - t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, |
87 | | - maxname, f.filename) |
| 64 | + """Prints a directory listing. |
| 65 | +
|
| 66 | + Args: |
| 67 | + device_path: Directory to list. |
| 68 | + """ |
| 69 | + files = device.List(device_path) |
| 70 | + files.sort(key=lambda x: x.filename) |
| 71 | + maxname = max(len(f.filename) for f in files) |
| 72 | + maxsize = max(len(str(f.size)) for f in files) |
| 73 | + for f in files: |
| 74 | + mode = ( |
| 75 | + ('d' if stat.S_ISDIR(f.mode) else '-') + |
| 76 | + ('r' if f.mode & stat.S_IRUSR else '-') + |
| 77 | + ('w' if f.mode & stat.S_IWUSR else '-') + |
| 78 | + ('x' if f.mode & stat.S_IXUSR else '-') + |
| 79 | + ('r' if f.mode & stat.S_IRGRP else '-') + |
| 80 | + ('w' if f.mode & stat.S_IWGRP else '-') + |
| 81 | + ('x' if f.mode & stat.S_IXGRP else '-') + |
| 82 | + ('r' if f.mode & stat.S_IROTH else '-') + |
| 83 | + ('w' if f.mode & stat.S_IWOTH else '-') + |
| 84 | + ('x' if f.mode & stat.S_IXOTH else '-')) |
| 85 | + t = time.gmtime(f.mtime) |
| 86 | + yield '%s %*d %04d-%02d-%02d %02d:%02d:%02d %-*s\n' % ( |
| 87 | + mode, maxsize, f.size, |
| 88 | + t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, |
| 89 | + maxname, f.filename) |
88 | 90 |
|
89 | 91 |
|
90 | 92 | @functools.wraps(adb_commands.AdbCommands.Logcat) |
91 | 93 | def Logcat(device, *options): |
92 | | - return device.Logcat( |
93 | | - self, ' '.join(options), timeout_ms=0) |
| 94 | + return device.Logcat( |
| 95 | + device, ' '.join(options), timeout_ms=0) |
94 | 96 |
|
95 | 97 |
|
96 | 98 | def Shell(device, *command): |
97 | | - """Runs a command on the device and prints the stdout. |
98 | | -
|
99 | | - Args: |
100 | | - command: Command to run on the target. |
101 | | - """ |
102 | | - if command: |
103 | | - return device.StreamingShell(' '.join(command)) |
104 | | - else: |
105 | | - # Retrieve the initial terminal prompt to use as a delimiter for future reads |
106 | | - terminal_prompt = device.InteractiveShell() |
107 | | - print(terminal_prompt.decode('utf-8')) |
108 | | - |
109 | | - # Accept user input in a loop and write that into the interactive shells stdin, then print output |
110 | | - while True: |
111 | | - cmd = input('> ') |
112 | | - if not cmd: |
113 | | - continue |
114 | | - elif cmd == 'exit': |
115 | | - break |
116 | | - else: |
117 | | - stdout = device.InteractiveShell(cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True) |
118 | | - if stdout: |
119 | | - if isinstance(stdout, bytes): |
120 | | - stdout = stdout.decode('utf-8') |
121 | | - print(stdout) |
122 | | - |
123 | | - |
124 | | - device.Close() |
| 99 | + """Runs a command on the device and prints the stdout. |
125 | 100 |
|
| 101 | + Args: |
| 102 | + command: Command to run on the target. |
| 103 | + """ |
| 104 | + if command: |
| 105 | + return device.StreamingShell(' '.join(command)) |
| 106 | + else: |
| 107 | + # Retrieve the initial terminal prompt to use as a delimiter for future reads |
| 108 | + terminal_prompt = device.InteractiveShell() |
| 109 | + print(terminal_prompt.decode('utf-8')) |
| 110 | + |
| 111 | + # Accept user input in a loop and write that into the interactive shells stdin, then print output |
| 112 | + while True: |
| 113 | + cmd = input('> ') |
| 114 | + if not cmd: |
| 115 | + continue |
| 116 | + elif cmd == 'exit': |
| 117 | + break |
| 118 | + else: |
| 119 | + stdout = device.InteractiveShell(cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True) |
| 120 | + if stdout: |
| 121 | + if isinstance(stdout, bytes): |
| 122 | + stdout = stdout.decode('utf-8') |
| 123 | + print(stdout) |
| 124 | + |
| 125 | + device.Close() |
126 | 126 |
|
127 | | -def main(): |
128 | | - common = common_cli.GetCommonArguments() |
129 | | - common.add_argument( |
130 | | - '--rsa_key_path', action='append', default=[], |
131 | | - metavar='~/.android/adbkey', |
132 | | - help='RSA key(s) to use, use multiple times to load mulitple keys') |
133 | | - common.add_argument( |
134 | | - '--auth_timeout_s', default=60., metavar='60', type=int, |
135 | | - help='Seconds to wait for the dialog to be accepted when using ' |
136 | | - 'authenticated ADB.') |
137 | | - device = common_cli.GetDeviceArguments() |
138 | | - parents = [common, device] |
139 | | - |
140 | | - parser = argparse.ArgumentParser( |
141 | | - description=sys.modules[__name__].__doc__, parents=[common]) |
142 | | - subparsers = parser.add_subparsers(title='Commands', dest='command_name') |
143 | | - |
144 | | - subparser = subparsers.add_parser( |
145 | | - name='help', help='Prints the commands available') |
146 | | - subparser = subparsers.add_parser( |
147 | | - name='devices', help='Lists the available devices', parents=[common]) |
148 | | - subparser.add_argument( |
149 | | - '--output_port_path', action='store_true', |
150 | | - help='Outputs the port_path alongside the serial') |
151 | | - |
152 | | - common_cli.MakeSubparser( |
153 | | - subparsers, parents, adb_commands.AdbCommands.Install) |
154 | | - common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Uninstall) |
155 | | - common_cli.MakeSubparser(subparsers, parents, List) |
156 | | - common_cli.MakeSubparser(subparsers, parents, Logcat) |
157 | | - common_cli.MakeSubparser( |
158 | | - subparsers, parents, adb_commands.AdbCommands.Push, |
159 | | - {'source_file': 'Filename or directory to push to the device.'}) |
160 | | - common_cli.MakeSubparser( |
161 | | - subparsers, parents, adb_commands.AdbCommands.Pull, |
162 | | - { |
163 | | - 'dest_file': 'Filename to write to on the host, if not specified, ' |
164 | | - 'prints the content to stdout.', |
165 | | - }) |
166 | | - common_cli.MakeSubparser( |
167 | | - subparsers, parents, adb_commands.AdbCommands.Reboot) |
168 | | - common_cli.MakeSubparser( |
169 | | - subparsers, parents, adb_commands.AdbCommands.RebootBootloader) |
170 | | - common_cli.MakeSubparser( |
171 | | - subparsers, parents, adb_commands.AdbCommands.Remount) |
172 | | - common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Root) |
173 | | - common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.EnableVerity) |
174 | | - common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.DisableVerity) |
175 | | - common_cli.MakeSubparser(subparsers, parents, Shell) |
176 | | - |
177 | | - if len(sys.argv) == 1: |
178 | | - parser.print_help() |
179 | | - return 2 |
180 | | - |
181 | | - args = parser.parse_args() |
182 | | - if args.verbose: |
183 | | - logging.basicConfig(level=logging.DEBUG) |
184 | | - if not args.rsa_key_path: |
185 | | - default = os.path.expanduser('~/.android/adbkey') |
186 | | - if os.path.isfile(default): |
187 | | - args.rsa_key_path = [default] |
188 | | - if args.rsa_key_path and not rsa_signer: |
189 | | - parser.error('Please install either M2Crypto, python-rsa, or PycryptoDome') |
190 | | - |
191 | | - # Hacks so that the generated doc is nicer. |
192 | | - if args.command_name == 'devices': |
193 | | - return Devices(args) |
194 | | - if args.command_name == 'help': |
195 | | - parser.print_help() |
196 | | - return 0 |
197 | | - if args.command_name == 'logcat': |
198 | | - args.positional = args.options |
199 | | - elif args.command_name == 'shell': |
200 | | - args.positional = args.command |
201 | 127 |
|
202 | | - return common_cli.StartCli( |
203 | | - args, |
204 | | - adb_commands.AdbCommands, |
205 | | - auth_timeout_ms=int(args.auth_timeout_s * 1000), |
206 | | - rsa_keys=[rsa_signer(path) for path in args.rsa_key_path]) |
| 128 | +def main(): |
| 129 | + common = common_cli.GetCommonArguments() |
| 130 | + common.add_argument( |
| 131 | + '--rsa_key_path', action='append', default=[], |
| 132 | + metavar='~/.android/adbkey', |
| 133 | + help='RSA key(s) to use, use multiple times to load mulitple keys') |
| 134 | + common.add_argument( |
| 135 | + '--auth_timeout_s', default=60., metavar='60', type=int, |
| 136 | + help='Seconds to wait for the dialog to be accepted when using ' |
| 137 | + 'authenticated ADB.') |
| 138 | + device = common_cli.GetDeviceArguments() |
| 139 | + parents = [common, device] |
| 140 | + |
| 141 | + parser = argparse.ArgumentParser( |
| 142 | + description=sys.modules[__name__].__doc__, parents=[common]) |
| 143 | + subparsers = parser.add_subparsers(title='Commands', dest='command_name') |
| 144 | + |
| 145 | + subparser = subparsers.add_parser( |
| 146 | + name='help', help='Prints the commands available') |
| 147 | + subparser = subparsers.add_parser( |
| 148 | + name='devices', help='Lists the available devices', parents=[common]) |
| 149 | + subparser.add_argument( |
| 150 | + '--output_port_path', action='store_true', |
| 151 | + help='Outputs the port_path alongside the serial') |
| 152 | + |
| 153 | + common_cli.MakeSubparser( |
| 154 | + subparsers, parents, adb_commands.AdbCommands.Install) |
| 155 | + common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Uninstall) |
| 156 | + common_cli.MakeSubparser(subparsers, parents, List) |
| 157 | + common_cli.MakeSubparser(subparsers, parents, Logcat) |
| 158 | + common_cli.MakeSubparser( |
| 159 | + subparsers, parents, adb_commands.AdbCommands.Push, |
| 160 | + {'source_file': 'Filename or directory to push to the device.'}) |
| 161 | + common_cli.MakeSubparser( |
| 162 | + subparsers, parents, adb_commands.AdbCommands.Pull, |
| 163 | + { |
| 164 | + 'dest_file': 'Filename to write to on the host, if not specified, ' |
| 165 | + 'prints the content to stdout.', |
| 166 | + }) |
| 167 | + common_cli.MakeSubparser( |
| 168 | + subparsers, parents, adb_commands.AdbCommands.Reboot) |
| 169 | + common_cli.MakeSubparser( |
| 170 | + subparsers, parents, adb_commands.AdbCommands.RebootBootloader) |
| 171 | + common_cli.MakeSubparser( |
| 172 | + subparsers, parents, adb_commands.AdbCommands.Remount) |
| 173 | + common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Root) |
| 174 | + common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.EnableVerity) |
| 175 | + common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.DisableVerity) |
| 176 | + common_cli.MakeSubparser(subparsers, parents, Shell) |
| 177 | + |
| 178 | + if len(sys.argv) == 1: |
| 179 | + parser.print_help() |
| 180 | + return 2 |
| 181 | + |
| 182 | + args = parser.parse_args() |
| 183 | + if args.verbose: |
| 184 | + logging.basicConfig(level=logging.DEBUG) |
| 185 | + if not args.rsa_key_path: |
| 186 | + default = os.path.expanduser('~/.android/adbkey') |
| 187 | + if os.path.isfile(default): |
| 188 | + args.rsa_key_path = [default] |
| 189 | + if args.rsa_key_path and not rsa_signer: |
| 190 | + parser.error('Please install either M2Crypto, python-rsa, or PycryptoDome') |
| 191 | + |
| 192 | + # Hacks so that the generated doc is nicer. |
| 193 | + if args.command_name == 'devices': |
| 194 | + return Devices(args) |
| 195 | + if args.command_name == 'help': |
| 196 | + parser.print_help() |
| 197 | + return 0 |
| 198 | + if args.command_name == 'logcat': |
| 199 | + args.positional = args.options |
| 200 | + elif args.command_name == 'shell': |
| 201 | + args.positional = args.command |
| 202 | + |
| 203 | + return common_cli.StartCli( |
| 204 | + args, |
| 205 | + adb_commands.AdbCommands, |
| 206 | + auth_timeout_ms=int(args.auth_timeout_s * 1000), |
| 207 | + rsa_keys=[rsa_signer(path) for path in args.rsa_key_path]) |
207 | 208 |
|
208 | 209 |
|
209 | 210 | if __name__ == '__main__': |
210 | | - sys.exit(main()) |
| 211 | + sys.exit(main()) |
0 commit comments