Skip to content

Commit

Permalink
Ruff format the BCR tooling (#2320)
Browse files Browse the repository at this point in the history
Step 1 of
#2317.
This is `ruff format --line-length 120` plus some manual line reflowing
in the second commit.

This changes the indent to 4 spaces in all files and line length to 120
cols.

After this lands we can take the hash of the squashed and rebased commit
and add it to
[.git-blame-ignore-revs](https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view)
  • Loading branch information
lalten authored Jun 28, 2024
1 parent d40c3e6 commit b208c2b
Show file tree
Hide file tree
Showing 9 changed files with 1,489 additions and 1,400 deletions.
304 changes: 155 additions & 149 deletions tools/add_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,168 +53,174 @@


def yes_or_no(question, default):
if default:
question += " [Y/n]: "
else:
question += " [y/N]: "

var = None
while var is None:
user_input = ask_input(question).strip().lower()
if user_input == "y":
var = True
elif user_input == "n":
var = False
elif not user_input:
var = default
if default:
question += " [Y/n]: "
else:
print("Invalid selection: {}".format(user_input))
return var
question += " [y/N]: "

var = None
while var is None:
user_input = ask_input(question).strip().lower()
if user_input == "y":
var = True
elif user_input == "n":
var = False
elif not user_input:
var = default
else:
print("Invalid selection: {}".format(user_input))
return var


def ask_input(msg):
return input(f"{YELLOW}ACTION: {RESET}{msg}")
return input(f"{YELLOW}ACTION: {RESET}{msg}")


def from_user_input():
name = ask_input("Please enter the module name: ")
version = ask_input("Please enter the module version: ")
compatibility = ask_input(
"Please enter the compatibility level [default is 0]: ") or "0"
module = Module(name, version, compatibility)

url = ask_input("Please enter the URL of the source archive: ")
strip_prefix = ask_input(
"Please enter the strip_prefix value of the archive [default None]: ") or None
module.set_source(url, strip_prefix)

if yes_or_no("Do you want to add patch files?", False):
patches = ask_input("Please enter patch file paths, separated by `,`: ")
for patch in patches.strip().split(","):
module.add_patch(patch.strip())
patch_strip = ask_input("Please enter the patch strip number [Default is 1, compatible with git generated "
"patches]: ") or "1"
module.set_patch_strip(int(patch_strip.strip()))

if yes_or_no("Do you want to add a BUILD file?", False):
build_file = ask_input(
"Please enter the path of the BUILD file to be added: ")
module.set_build_file(build_file.strip())

if yes_or_no("Do you want to specify a MODULE.bazel file?", False):
path = ask_input("Please enter the MODULE.bazel file path: ").strip()
module.set_module_dot_bazel(path)
else:
if yes_or_no("Do you want to specify dependencies for this module?", False):
deps = ask_input(
"Please enter dependencies in the form of <name>@<version>, separated by `,`: ")
for dep in deps.strip().split(","):
name, version = dep.split("@")
module.add_dep(name, version)

presubmit_url = "https://github.com/bazelbuild/bazel-central-registry/tree/main/docs#presubmit"
if yes_or_no(f"Do you want to specify an existing presubmit.yml file? (See {presubmit_url})", False):
path = ask_input("Please enter the presubmit.yml file path: ").strip()
module.set_presubmit_yml(path)
else:
first = True
while not module.build_targets:
if not first:
print("Build targets cannot be empty, please re-enter!")
first = False
build_targets = ask_input(
"Please enter a list of build targets you want to expose to downstream users, separated by `,`: ")
for target in build_targets.strip().split(","):
if target:
module.add_build_target(target)

if yes_or_no("Do you have a test module in your source archive?", True):
module.test_module_path = ask_input(
"Please enter the test module path in your source archive: ")
first = True
while not (module.test_module_build_targets or module.test_module_test_targets):
if not first:
print("Build targets and test targets cannot both be empty, please re-enter!")
first = False
build_targets = ask_input(
"Please enter a list of build targets for the test module, separated by `,`: ")
for target in build_targets.strip().split(","):
if target:
module.add_test_module_build_target(target)
test_targets = ask_input(
"Please enter a list of test targets for the test module, separated by `,`: ")
for target in test_targets.strip().split(","):
if target:
module.add_test_module_test_target(target)
return module
name = ask_input("Please enter the module name: ")
version = ask_input("Please enter the module version: ")
compatibility = ask_input("Please enter the compatibility level [default is 0]: ") or "0"
module = Module(name, version, compatibility)

url = ask_input("Please enter the URL of the source archive: ")
strip_prefix = ask_input("Please enter the strip_prefix value of the archive [default None]: ") or None
module.set_source(url, strip_prefix)

if yes_or_no("Do you want to add patch files?", False):
patches = ask_input("Please enter patch file paths, separated by `,`: ")
for patch in patches.strip().split(","):
module.add_patch(patch.strip())
patch_strip = (
ask_input("Please enter the patch strip number [Default is 1, compatible with git generated " "patches]: ")
or "1"
)
module.set_patch_strip(int(patch_strip.strip()))

if yes_or_no("Do you want to add a BUILD file?", False):
build_file = ask_input("Please enter the path of the BUILD file to be added: ")
module.set_build_file(build_file.strip())

if yes_or_no("Do you want to specify a MODULE.bazel file?", False):
path = ask_input("Please enter the MODULE.bazel file path: ").strip()
module.set_module_dot_bazel(path)
else:
if yes_or_no("Do you want to specify dependencies for this module?", False):
deps = ask_input("Please enter dependencies in the form of <name>@<version>, separated by `,`: ")
for dep in deps.strip().split(","):
name, version = dep.split("@")
module.add_dep(name, version)

presubmit_url = "https://github.com/bazelbuild/bazel-central-registry/tree/main/docs#presubmit"
if yes_or_no(f"Do you want to specify an existing presubmit.yml file? (See {presubmit_url})", False):
path = ask_input("Please enter the presubmit.yml file path: ").strip()
module.set_presubmit_yml(path)
else:
first = True
while not module.build_targets:
if not first:
print("Build targets cannot be empty, please re-enter!")
first = False
build_targets = ask_input(
"Please enter a list of build targets you want to expose to downstream users, separated by `,`: "
)
for target in build_targets.strip().split(","):
if target:
module.add_build_target(target)

if yes_or_no("Do you have a test module in your source archive?", True):
module.test_module_path = ask_input("Please enter the test module path in your source archive: ")
first = True
while not (module.test_module_build_targets or module.test_module_test_targets):
if not first:
print("Build targets and test targets cannot both be empty, please re-enter!")
first = False
build_targets = ask_input(
"Please enter a list of build targets for the test module, separated by `,`: "
)
for target in build_targets.strip().split(","):
if target:
module.add_test_module_build_target(target)
test_targets = ask_input("Please enter a list of test targets for the test module, separated by `,`: ")
for target in test_targets.strip().split(","):
if target:
module.add_test_module_test_target(target)
return module


def get_maintainers_from_input():
maintainers = []
prefix = "a"
explain = " (See https://github.com/bazelbuild/bazel-central-registry/tree/main/docs/bcr-policies.md#become-a-module-maintainer)"
while yes_or_no(f"Do you want to add {prefix} maintainer for this module?{explain}", False):
maintainer = {}
name = ask_input("Please enter maintainer name: ")
maintainer["name"] = name
email = ask_input("Please enter the maintainer's email address: ")
maintainer["email"] = email
username = ask_input(
"(Optional) Please enter the maintainer's github username: ")
if username:
maintainer["github"] = username
maintainers.append(maintainer)
prefix = "another"
explain = ""
return maintainers
maintainers = []
prefix = "a"
explain = (
" (See https://github.com/bazelbuild/bazel-central-registry/tree/main"
"/docs/bcr-policies.md#become-a-module-maintainer)"
)
while yes_or_no(f"Do you want to add {prefix} maintainer for this module?{explain}", False):
maintainer = {}
name = ask_input("Please enter maintainer name: ")
maintainer["name"] = name
email = ask_input("Please enter the maintainer's email address: ")
maintainer["email"] = email
username = ask_input("(Optional) Please enter the maintainer's github username: ")
if username:
maintainer["github"] = username
maintainers.append(maintainer)
prefix = "another"
explain = ""
return maintainers


def main(argv=None):
if argv is None:
argv = sys.argv[1:]

parser = argparse.ArgumentParser()
parser.add_argument("--registry", type=str, default=".",
help="Specify the root path of the registry (default: the current working directory).")
parser.add_argument(
"--input", type=str, help="Take module information from a json file, which can be generated from previous input.")

args = parser.parse_args(argv)

if args.input:
log(f"Getting module information from {args.input}...")
module = Module()
module.from_json(args.input)
else:
log("Getting module information from user input...")
module = from_user_input()
timestamp = time.strftime("%Y%m%d-%H%M%S")
log(f"Saving module information to {module.name}.{timestamp}.json")
log(f"You can use it via --input={module.name}.{timestamp}.json")
module.dump(f"{module.name}.{timestamp}.json")

client = RegistryClient(args.registry)

if not client.contains(module.name):
log(f"{module.name} is a new Bazel module...")
homepage = ask_input(
"Please enter the homepage url for this module: ").strip()
maintainers = get_maintainers_from_input()
source_repository = ""
if module.url.startswith("https://github.com/"):
parts = module.url.split("/")
source_repository = "github:" + parts[3] + "/" + parts[4]
client.init_module(module.name, maintainers, homepage, source_repository)

client.add(module, override=True)
log(f"{module.name} {module.version} is added into the registry.")

log(f"Running ./tools/bcr_validation.py --check={module.name}@{module.version} --fix")
bcr_validation.main([f"--check={module.name}@{module.version}", "--fix"])
if argv is None:
argv = sys.argv[1:]

parser = argparse.ArgumentParser()
parser.add_argument(
"--registry",
type=str,
default=".",
help="Specify the root path of the registry (default: the current working directory).",
)
parser.add_argument(
"--input",
type=str,
help="Take module information from a json file, which can be generated from previous input.",
)

args = parser.parse_args(argv)

if args.input:
log(f"Getting module information from {args.input}...")
module = Module()
module.from_json(args.input)
else:
log("Getting module information from user input...")
module = from_user_input()
timestamp = time.strftime("%Y%m%d-%H%M%S")
log(f"Saving module information to {module.name}.{timestamp}.json")
log(f"You can use it via --input={module.name}.{timestamp}.json")
module.dump(f"{module.name}.{timestamp}.json")

client = RegistryClient(args.registry)

if not client.contains(module.name):
log(f"{module.name} is a new Bazel module...")
homepage = ask_input("Please enter the homepage url for this module: ").strip()
maintainers = get_maintainers_from_input()
source_repository = ""
if module.url.startswith("https://github.com/"):
parts = module.url.split("/")
source_repository = "github:" + parts[3] + "/" + parts[4]
client.init_module(module.name, maintainers, homepage, source_repository)

client.add(module, override=True)
log(f"{module.name} {module.version} is added into the registry.")

log(f"Running ./tools/bcr_validation.py --check={module.name}@{module.version} --fix")
bcr_validation.main([f"--check={module.name}@{module.version}", "--fix"])


if __name__ == "__main__":
# Under 'bazel run' we want to run within the source folder instead of the execroot.
if os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
sys.exit(main())
# Under 'bazel run' we want to run within the source folder instead of the execroot.
if os.getenv("BUILD_WORKSPACE_DIRECTORY"):
os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
sys.exit(main())
Loading

0 comments on commit b208c2b

Please sign in to comment.