Skip to content

Commit

Permalink
Make gen_experiments.py runnable internally also
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 591902705
  • Loading branch information
ctiller authored and copybara-github committed Dec 18, 2023
1 parent 52719be commit c93798b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
11 changes: 8 additions & 3 deletions tools/codegen/core/experiments_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ def GenerateExperimentsHdr(self, output_file, mode):
if mode != "test":
include_guard = "GRPC_SRC_CORE_LIB_EXPERIMENTS_EXPERIMENTS_H"
else:
file_path_list = output_file.split("/")[0:-1]
file_name = output_file.split("/")[-1].split(".")[0]
real_output_file = output_file.replace(".github", "")
file_path_list = real_output_file.split("/")[0:-1]
file_name = real_output_file.split("/")[-1].split(".")[0]

include_guard = f"GRPC_{'_'.join(path.upper() for path in file_path_list)}_{file_name.upper()}_H"

Expand Down Expand Up @@ -562,9 +563,13 @@ def GenerateExperimentsSrc(self, output_file, header_file_path, mode):
break

print("#include <grpc/support/port_platform.h>", file=C)
print(file=C)
if any_requires:
print("#include <stdint.h>", file=C)
print(f'#include "{header_file_path}"', file=C)
print(file=C)
print(
f'#include "{header_file_path.replace(".github", "")}"', file=C
)
print(file=C)
print("#ifndef GRPC_EXPERIMENTS_ARE_FINAL", file=C)
idx = 0
Expand Down
27 changes: 23 additions & 4 deletions tools/codegen/core/gen_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@
from __future__ import print_function

import argparse
import os
import sys

import experiments_compiler as exp
import yaml

REPO_ROOT = os.path.normpath(
os.path.join(os.path.dirname(__file__), "../../..")
)
print(REPO_ROOT)
os.chdir(REPO_ROOT)

DEFAULTS = {
"broken": "false",
False: "false",
Expand Down Expand Up @@ -86,6 +93,11 @@ def ParseCommandLineArguments(args):
args = ParseCommandLineArguments(sys.argv[1:])


def _InjectGithubPath(path):
base, ext = os.path.splitext(path)
return base + ".github" + ext


def _GenerateExperimentFiles(args, mode):
if mode == "test":
_EXPERIMENTS_DEFS = (
Expand All @@ -96,11 +108,18 @@ def _GenerateExperimentFiles(args, mode):
)
_EXPERIMENTS_HDR_FILE = "test/core/experiments/fixtures/experiments.h"
_EXPERIMENTS_SRC_FILE = "test/core/experiments/fixtures/experiments.cc"
_EXPERIMENTS_BZL_FILE = "bazel/test_experiments.bzl"
else:
_EXPERIMENTS_DEFS = "src/core/lib/experiments/experiments.yaml"
_EXPERIMENTS_ROLLOUTS = "src/core/lib/experiments/rollouts.yaml"
_EXPERIMENTS_HDR_FILE = "src/core/lib/experiments/experiments.h"
_EXPERIMENTS_SRC_FILE = "src/core/lib/experiments/experiments.cc"
_EXPERIMENTS_BZL_FILE = "bazel/experiments.bzl"
if "/google3/" in REPO_ROOT:
_EXPERIMENTS_ROLLOUTS = _InjectGithubPath(_EXPERIMENTS_ROLLOUTS)
_EXPERIMENTS_HDR_FILE = _InjectGithubPath(_EXPERIMENTS_HDR_FILE)
_EXPERIMENTS_SRC_FILE = _InjectGithubPath(_EXPERIMENTS_SRC_FILE)
_EXPERIMENTS_BZL_FILE = _InjectGithubPath(_EXPERIMENTS_BZL_FILE)

with open(_EXPERIMENTS_DEFS) as f:
attrs = yaml.safe_load(f.read())
Expand Down Expand Up @@ -152,12 +171,12 @@ def _GenerateExperimentFiles(args, mode):
)

print("Generating experiments.bzl")
compiler.GenExperimentsBzl(mode, _EXPERIMENTS_BZL_FILE)
if mode == "test":
compiler.GenExperimentsBzl(mode, "bazel/test_experiments.bzl")
print("Generating experiments tests")
compiler.GenTest("test/core/experiments/experiments_test.cc")
else:
compiler.GenExperimentsBzl(mode, "bazel/experiments.bzl")
compiler.GenTest(
os.path.join(REPO_ROOT, "test/core/experiments/experiments_test.cc")
)


_GenerateExperimentFiles(args, "production")
Expand Down

0 comments on commit c93798b

Please sign in to comment.