Skip to content

Commit

Permalink
Remove argv usage from Python scripts (#7311)
Browse files Browse the repository at this point in the history
argparse will automatically read sys.argv, so we don't need to pass it
in manually. Furthermore, none of our scripts customize argv.
  • Loading branch information
calcmogul authored Nov 3, 2024
1 parent 7c91b81 commit a48f3c3
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pregen_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path


def main(argv):
def main():
script_path = Path(__file__).resolve()
REPO_ROOT = script_path.parent.parent.parent
parser = argparse.ArgumentParser()
Expand All @@ -15,7 +15,7 @@ def main(argv):
help="Path to the quickbuf protoc plugin",
required=True,
)
args = parser.parse_args(argv)
args = parser.parse_args()
subprocess.run(
[sys.executable, f"{REPO_ROOT}/hal/generate_usage_reporting.py"], check=True
)
Expand Down Expand Up @@ -61,4 +61,4 @@ def main(argv):


if __name__ == "__main__":
main(sys.argv[1:])
main()
9 changes: 4 additions & 5 deletions hal/generate_usage_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import sys
from pathlib import Path


Expand Down Expand Up @@ -68,8 +68,7 @@ def generate_usage_reporting(output_directory: Path, template_directory: Path):
usage_reporting_hdr.write_text(contents, encoding="utf-8", newline="\n")


def main(argv):

def main():
dirname = Path(__file__).parent

parser = argparse.ArgumentParser()
Expand All @@ -85,10 +84,10 @@ def main(argv):
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_usage_reporting(args.output_directory, args.template_root)


if __name__ == "__main__":
main(sys.argv[1:])
main()
8 changes: 4 additions & 4 deletions ntcore/generate_topics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

import argparse
import json
import sys
from pathlib import Path

from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -122,7 +122,7 @@ def generate_topics(
Output(output_directory / jni_subdirectory, "types_jni.cpp", output)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -145,10 +145,10 @@ def main(argv):
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_topics(args.output_directory, args.template_root, args.types_schema_file)


if __name__ == "__main__":
main(sys.argv[1:])
main()
12 changes: 6 additions & 6 deletions thirdparty/imgui_suite/generate_gl3w.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3

import os
import sys
import argparse
import os
from pathlib import Path
import sys

def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -16,8 +16,8 @@ def main(argv):
default=dirname / "generated/gl3w",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

sys.path.append(str(dirname / "gl3w"))

args.output_directory.mkdir(parents=True, exist_ok=True)
Expand All @@ -27,4 +27,4 @@ def main(argv):


if __name__ == "__main__":
main(sys.argv[1:])
main()
11 changes: 3 additions & 8 deletions upstream_utils/upstream_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,8 @@ def copy_upstream_to_thirdparty(self):

self.copy_upstream_src(self.wpilib_root)

def main(self, argv=sys.argv[1:]):
"""Processes the given arguments.
Keyword argument:
argv -- The arguments to process. Defaults to the arguments passed to
the program.
"""
def main(self):
"""Process arguments of upstream_utils script"""
parser = argparse.ArgumentParser(
description=f"CLI manager of the {self.name} upstream library"
)
Expand Down Expand Up @@ -515,7 +510,7 @@ def main(self, argv=sys.argv[1:]):
help="Copies files from the upstream repository into the thirdparty directory in allwpilib",
)

args = parser.parse_args(argv)
args = parser.parse_args()

self.wpilib_root = get_repo_root()
if args.subcommand == "info":
Expand Down
8 changes: 4 additions & 4 deletions wpilibNewCommands/generate_hids.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import json
import sys
from pathlib import Path

from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -63,7 +63,7 @@ def generate_hids(output_directory: Path, template_directory: Path, schema_file:
write_controller_file(root_path, controllerName, output)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -86,10 +86,10 @@ def main(argv):
default="wpilibj/src/generate/hids.json",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_hids(args.output_directory, args.template_root, args.schema_file)


if __name__ == "__main__":
main(sys.argv[1:])
main()
8 changes: 4 additions & 4 deletions wpilibc/generate_hids.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import json
import sys
from pathlib import Path

from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -76,7 +76,7 @@ def generate_hids(output_directory: Path, template_directory: Path, schema_file:
write_controller_file(root_path, controllerName, output)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -99,10 +99,10 @@ def main(argv):
default="wpilibj/src/generate/hids.json",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_hids(args.output_directory, args.template_root, args.schema_file)


if __name__ == "__main__":
main(sys.argv[1:])
main()
8 changes: 4 additions & 4 deletions wpilibc/generate_pwm_motor_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import json
import os
import sys
from pathlib import Path
from typing import Any, Dict

Expand Down Expand Up @@ -67,7 +67,7 @@ def generate_pwm_motor_controllers(
generate_cpp_sources(output_root, template_root, controllers)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -90,12 +90,12 @@ def main(argv):
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_pwm_motor_controllers(
args.output_directory, args.template_root, args.schema_root
)


if __name__ == "__main__":
main(sys.argv[1:])
main()
8 changes: 4 additions & 4 deletions wpilibj/generate_hids.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import json
import sys
from pathlib import Path

from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -43,7 +43,7 @@ def generate_hids(output_directory: Path, template_directory: Path):
write_controller_file(rootPath, controllerName, output)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -60,10 +60,10 @@ def main(argv):
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_hids(args.output_directory, args.template_root)


if __name__ == "__main__":
main(sys.argv[1:])
main()
8 changes: 4 additions & 4 deletions wpilibj/generate_pwm_motor_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import json
import sys
from pathlib import Path
from typing import Any, Dict

Expand Down Expand Up @@ -40,7 +40,7 @@ def generate_pwm_motor_controllers(output_root, template_root):
render_template(template, root_path, controller_name, controller)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -57,10 +57,10 @@ def main(argv):
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_pwm_motor_controllers(args.output_directory, args.template_root)


if __name__ == "__main__":
main(sys.argv[1:])
main()
7 changes: 3 additions & 4 deletions wpimath/generate_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# the WPILib BSD license file in the root directory of this project.

import argparse
import sys
from pathlib import Path

from jinja2 import Environment, FileSystemLoader
Expand Down Expand Up @@ -39,7 +38,7 @@ def generate_numbers(output_directory: Path, template_root: Path):
output(rootPath, "Nat.java", contents)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -56,10 +55,10 @@ def main(argv):
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_numbers(args.output_directory, args.template_root)


if __name__ == "__main__":
main(sys.argv[1:])
main()
8 changes: 4 additions & 4 deletions wpimath/generate_quickbuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

import argparse
import subprocess
import sys
from pathlib import Path


Expand Down Expand Up @@ -37,7 +37,7 @@ def generate_quickbuf(
)


def main(argv):
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent

Expand All @@ -64,12 +64,12 @@ def main(argv):
default=dirname / "src/main/proto",
type=Path,
)
args = parser.parse_args(argv)
args = parser.parse_args()

generate_quickbuf(
args.protoc, args.quickbuf_plugin, args.output_directory, args.proto_directory
)


if __name__ == "__main__":
main(sys.argv[1:])
main()
5 changes: 3 additions & 2 deletions wpiunits/generate_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.

# This script generates unit-specific interfaces and mutable and immutable implementations of
# those interfaces.
# This script generates unit-specific interfaces and mutable and immutable
# implementations of those interfaces.
#
# Generated files will be located in wpiunits/src/generated/main/

import argparse
Expand Down

0 comments on commit a48f3c3

Please sign in to comment.