diff options
author | George Burgess IV <gbiv@google.com> | 2022-09-02 16:59:27 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-07 21:15:07 +0000 |
commit | 74bd380a27f4f0e8e90ff2dc1cef0b502d74961b (patch) | |
tree | be028f89ec1e2eca735bb4aa1610530147a53625 /go | |
parent | 8448c60a6a2337ec993923837e1d55b41f49dabc (diff) | |
download | toolchain-utils-74bd380a27f4f0e8e90ff2dc1cef0b502d74961b.tar.gz |
Autoformat all Python code
This autoformats all Python code with our new Python formatter, `black`.
BUG=b:244644217
TEST=None
Change-Id: I15ee49233d98fb6295c0c53c129bbf8e78e0d9ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3877337
Tested-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com>
Commit-Queue: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'go')
-rwxr-xr-x | go/chromeos/setup_chromeos_testing.py | 363 |
1 files changed, 197 insertions, 166 deletions
diff --git a/go/chromeos/setup_chromeos_testing.py b/go/chromeos/setup_chromeos_testing.py index cbb8bc29..53254a99 100755 --- a/go/chromeos/setup_chromeos_testing.py +++ b/go/chromeos/setup_chromeos_testing.py @@ -18,15 +18,15 @@ from cros_utils import command_executer SUCCESS = 0 DEBUG = False -ARCH_DATA = {'x86_64': 'amd64', 'arm32': 'arm', 'arm64': 'arm64'} +ARCH_DATA = {"x86_64": "amd64", "arm32": "arm", "arm64": "arm64"} CROS_TOOLCHAIN_DATA = { - 'x86_64': 'x86_64-cros-linux-gnu', - 'arm32': 'armv7a-cros-linux-gnueabihf', - 'arm64': 'aarch64-cros-linux-gnu' + "x86_64": "x86_64-cros-linux-gnu", + "arm32": "armv7a-cros-linux-gnueabihf", + "arm64": "aarch64-cros-linux-gnu", } -GLIBC_DATA = {'x86_64': 'glibc', 'arm32': 'glibc32', 'arm64': 'glibc'} +GLIBC_DATA = {"x86_64": "glibc", "arm32": "glibc32", "arm64": "glibc"} CONFIG_TEMPLATE = """ Host %s @@ -70,183 +70,214 @@ GOOS="linux" GOARCH="%s" \\ def log(msg): - if DEBUG: - print(msg) + if DEBUG: + print(msg) def WriteFile(file_content, file_name): - with open(file_name, 'w', encoding='utf-8') as out_file: - out_file.write(file_content) + with open(file_name, "w", encoding="utf-8") as out_file: + out_file.write(file_content) def GenerateGoHelperScripts(ce, arm_board, x86_board, chromeos_root): - keys = ['x86_64', 'arm32', 'arm64'] - names = { - 'x86_64': x86_board, - 'arm64': arm_board, - 'arm32': ('%s32' % arm_board) - } - - toolchain_dir = os.path.join(chromeos_root, 'src', 'third_party', - 'toolchain-utils', 'go', 'chromeos') - for k in keys: - name = names[k] - arch = ARCH_DATA[k] - toolchain = CROS_TOOLCHAIN_DATA[k] - glibc = GLIBC_DATA[k] - - base_file = os.path.join(toolchain_dir, ('go_%s' % name)) - base_file_content = BASE_TEMPLATE % (name, arch, arch, toolchain, toolchain, - toolchain) - WriteFile(base_file_content, base_file) - cmd = 'chmod 755 %s' % base_file - ce.RunCommand(cmd) - - exec_file = os.path.join(toolchain_dir, ('go_%s_exec' % name)) - exec_file_content = EXEC_TEMPLATE % (name, arch, glibc, name) - WriteFile(exec_file_content, exec_file) - cmd = 'chmod 755 %s' % exec_file - ce.RunCommand(cmd) - - return 0 - - -def UpdateChrootSshConfig(ce, arm_board, arm_dut, x86_board, x86_dut, - chromeos_root): - log('Entering UpdateChrootSshConfig') - # Copy testing_rsa to .ssh and set file protections properly. - user = getpass.getuser() - ssh_dir = os.path.join(chromeos_root, 'chroot', 'home', user, '.ssh') - dest_file = os.path.join(ssh_dir, 'testing_rsa') - src_file = os.path.join(chromeos_root, 'src', 'scripts', - 'mod_for_test_scripts', 'testing_rsa') - if not os.path.exists(dest_file): - if os.path.exists(src_file): - cmd = 'cp %s %s' % (src_file, dest_file) - ret = ce.RunCommand(cmd) - if ret != SUCCESS: - print('Error executing "%s". Exiting now...' % cmd) - sys.exit(1) - cmd = 'chmod 600 %s' % dest_file - ret = ce.RunCommand(cmd) - if ret != SUCCESS: - print('Error executing %s; may need to re-run this manually.' % cmd) + keys = ["x86_64", "arm32", "arm64"] + names = { + "x86_64": x86_board, + "arm64": arm_board, + "arm32": ("%s32" % arm_board), + } + + toolchain_dir = os.path.join( + chromeos_root, "src", "third_party", "toolchain-utils", "go", "chromeos" + ) + for k in keys: + name = names[k] + arch = ARCH_DATA[k] + toolchain = CROS_TOOLCHAIN_DATA[k] + glibc = GLIBC_DATA[k] + + base_file = os.path.join(toolchain_dir, ("go_%s" % name)) + base_file_content = BASE_TEMPLATE % ( + name, + arch, + arch, + toolchain, + toolchain, + toolchain, + ) + WriteFile(base_file_content, base_file) + cmd = "chmod 755 %s" % base_file + ce.RunCommand(cmd) + + exec_file = os.path.join(toolchain_dir, ("go_%s_exec" % name)) + exec_file_content = EXEC_TEMPLATE % (name, arch, glibc, name) + WriteFile(exec_file_content, exec_file) + cmd = "chmod 755 %s" % exec_file + ce.RunCommand(cmd) + + return 0 + + +def UpdateChrootSshConfig( + ce, arm_board, arm_dut, x86_board, x86_dut, chromeos_root +): + log("Entering UpdateChrootSshConfig") + # Copy testing_rsa to .ssh and set file protections properly. + user = getpass.getuser() + ssh_dir = os.path.join(chromeos_root, "chroot", "home", user, ".ssh") + dest_file = os.path.join(ssh_dir, "testing_rsa") + src_file = os.path.join( + chromeos_root, "src", "scripts", "mod_for_test_scripts", "testing_rsa" + ) + if not os.path.exists(dest_file): + if os.path.exists(src_file): + cmd = "cp %s %s" % (src_file, dest_file) + ret = ce.RunCommand(cmd) + if ret != SUCCESS: + print('Error executing "%s". Exiting now...' % cmd) + sys.exit(1) + cmd = "chmod 600 %s" % dest_file + ret = ce.RunCommand(cmd) + if ret != SUCCESS: + print( + "Error executing %s; may need to re-run this manually." + % cmd + ) + else: + print( + "Cannot find %s; you will need to update testing_rsa by hand." + % src_file + ) else: - print('Cannot find %s; you will need to update testing_rsa by hand.' % - src_file) - else: - log('testing_rsa exists already.') + log("testing_rsa exists already.") - # Save ~/.ssh/config file, if not already done. - config_file = os.path.expanduser('~/.ssh/config') - saved_config_file = os.path.join( - os.path.expanduser('~/.ssh'), 'config.save.go-scripts') - if not os.path.exists(saved_config_file): - cmd = 'cp %s %s' % (config_file, saved_config_file) - ret = ce.RunCommand(cmd) - if ret != SUCCESS: - print('Error making save copy of ~/.ssh/config. Exiting...') - sys.exit(1) + # Save ~/.ssh/config file, if not already done. + config_file = os.path.expanduser("~/.ssh/config") + saved_config_file = os.path.join( + os.path.expanduser("~/.ssh"), "config.save.go-scripts" + ) + if not os.path.exists(saved_config_file): + cmd = "cp %s %s" % (config_file, saved_config_file) + ret = ce.RunCommand(cmd) + if ret != SUCCESS: + print("Error making save copy of ~/.ssh/config. Exiting...") + sys.exit(1) - # Update ~/.ssh/config file - log('Reading ssh config file') - with open(config_file, 'r') as input_file: - config_lines = input_file.read() + # Update ~/.ssh/config file + log("Reading ssh config file") + with open(config_file, "r") as input_file: + config_lines = input_file.read() - x86_host_config = CONFIG_TEMPLATE % (x86_board, x86_dut) - arm_names = '%s %s32' % (arm_board, arm_board) - arm_host_config = CONFIG_TEMPLATE % (arm_names, arm_dut) + x86_host_config = CONFIG_TEMPLATE % (x86_board, x86_dut) + arm_names = "%s %s32" % (arm_board, arm_board) + arm_host_config = CONFIG_TEMPLATE % (arm_names, arm_dut) - config_lines += x86_host_config - config_lines += arm_host_config + config_lines += x86_host_config + config_lines += arm_host_config - log('Writing ~/.ssh/config') - WriteFile(config_lines, config_file) + log("Writing ~/.ssh/config") + WriteFile(config_lines, config_file) - return 0 + return 0 def CleanUp(ce, x86_board, arm_board, chromeos_root): - # Find and remove go helper scripts - keys = ['x86_64', 'arm32', 'arm64'] - names = { - 'x86_64': x86_board, - 'arm64': arm_board, - 'arm32': ('%s32' % arm_board) - } - - toolchain_dir = os.path.join(chromeos_root, 'src', 'third_party', - 'toolchain-utils', 'go', 'chromeos') - for k in keys: - name = names[k] - base_file = os.path.join(toolchain_dir, ('go_%s' % name)) - exec_file = os.path.join(toolchain_dir, ('go_%s_exec' % name)) - cmd = ('rm -f %s; rm -f %s' % (base_file, exec_file)) - ce.RunCommand(cmd) - - # Restore saved config_file - config_file = os.path.expanduser('~/.ssh/config') - saved_config_file = os.path.join( - os.path.expanduser('~/.ssh'), 'config.save.go-scripts') - if not os.path.exists(saved_config_file): - print('Could not find file: %s; unable to restore ~/.ssh/config .' % - saved_config_file) - else: - cmd = 'mv %s %s' % (saved_config_file, config_file) - ce.RunCommand(cmd) - - return 0 + # Find and remove go helper scripts + keys = ["x86_64", "arm32", "arm64"] + names = { + "x86_64": x86_board, + "arm64": arm_board, + "arm32": ("%s32" % arm_board), + } + + toolchain_dir = os.path.join( + chromeos_root, "src", "third_party", "toolchain-utils", "go", "chromeos" + ) + for k in keys: + name = names[k] + base_file = os.path.join(toolchain_dir, ("go_%s" % name)) + exec_file = os.path.join(toolchain_dir, ("go_%s_exec" % name)) + cmd = "rm -f %s; rm -f %s" % (base_file, exec_file) + ce.RunCommand(cmd) + + # Restore saved config_file + config_file = os.path.expanduser("~/.ssh/config") + saved_config_file = os.path.join( + os.path.expanduser("~/.ssh"), "config.save.go-scripts" + ) + if not os.path.exists(saved_config_file): + print( + "Could not find file: %s; unable to restore ~/.ssh/config ." + % saved_config_file + ) + else: + cmd = "mv %s %s" % (saved_config_file, config_file) + ce.RunCommand(cmd) + + return 0 def Main(argv): - # pylint: disable=global-statement - global DEBUG - - parser = argparse.ArgumentParser() - parser.add_argument('-a', '--arm64_board', dest='arm_board', required=True) - parser.add_argument( - '-b', '--x86_64_board', dest='x86_64_board', required=True) - parser.add_argument( - '-c', '--chromeos_root', dest='chromeos_root', required=True) - parser.add_argument('-x', '--x86_64_dut', dest='x86_64_dut', required=True) - parser.add_argument('-y', '--arm64_dut', dest='arm_dut', required=True) - parser.add_argument( - '-z', '--cleanup', dest='cleanup', default=False, action='store_true') - parser.add_argument( - '-v', '--verbose', dest='verbose', default=False, action='store_true') - - options = parser.parse_args(argv[1:]) - - if options.verbose: - DEBUG = True - - if not os.path.exists(options.chromeos_root): - print('Invalid ChromeOS Root: %s' % options.chromeos_root) - - ce = command_executer.GetCommandExecuter() - all_good = True - for m in (options.x86_64_dut, options.arm_dut): - cmd = 'ping -c 3 %s > /dev/null' % m - ret = ce.RunCommand(cmd) - if ret != SUCCESS: - print('Machine %s is currently not responding to ping.' % m) - all_good = False - - if not all_good: - return 1 - - if not options.cleanup: - UpdateChrootSshConfig(ce, options.arm_board, options.arm_dut, - options.x86_64_board, options.x86_64_dut, - options.chromeos_root) - GenerateGoHelperScripts(ce, options.arm_board, options.x86_64_board, - options.chromeos_root) - else: - CleanUp(ce, options.x86_64_board, options.arm_board, options.chromeos_root) - - return 0 - - -if __name__ == '__main__': - val = Main(sys.argv) - sys.exit(val) + # pylint: disable=global-statement + global DEBUG + + parser = argparse.ArgumentParser() + parser.add_argument("-a", "--arm64_board", dest="arm_board", required=True) + parser.add_argument( + "-b", "--x86_64_board", dest="x86_64_board", required=True + ) + parser.add_argument( + "-c", "--chromeos_root", dest="chromeos_root", required=True + ) + parser.add_argument("-x", "--x86_64_dut", dest="x86_64_dut", required=True) + parser.add_argument("-y", "--arm64_dut", dest="arm_dut", required=True) + parser.add_argument( + "-z", "--cleanup", dest="cleanup", default=False, action="store_true" + ) + parser.add_argument( + "-v", "--verbose", dest="verbose", default=False, action="store_true" + ) + + options = parser.parse_args(argv[1:]) + + if options.verbose: + DEBUG = True + + if not os.path.exists(options.chromeos_root): + print("Invalid ChromeOS Root: %s" % options.chromeos_root) + + ce = command_executer.GetCommandExecuter() + all_good = True + for m in (options.x86_64_dut, options.arm_dut): + cmd = "ping -c 3 %s > /dev/null" % m + ret = ce.RunCommand(cmd) + if ret != SUCCESS: + print("Machine %s is currently not responding to ping." % m) + all_good = False + + if not all_good: + return 1 + + if not options.cleanup: + UpdateChrootSshConfig( + ce, + options.arm_board, + options.arm_dut, + options.x86_64_board, + options.x86_64_dut, + options.chromeos_root, + ) + GenerateGoHelperScripts( + ce, options.arm_board, options.x86_64_board, options.chromeos_root + ) + else: + CleanUp( + ce, options.x86_64_board, options.arm_board, options.chromeos_root + ) + + return 0 + + +if __name__ == "__main__": + val = Main(sys.argv) + sys.exit(val) |