def main(conf_file): with open(conf_file) as j: config = json.load(j) r = runfiles.Create() isWindows = platform.system() == 'Windows' bazelBinary = r.Rlocation( os.path.join(config['bazelBinaryWorkspace'], 'bazel.exe' if isWindows else 'bazel')) workspacePath = config['workspaceRoot'] # Canonicalize bazel external/some_repo/foo if workspacePath.startswith('external/'): workspacePath = '..' + workspacePath[len('external'):] for command in config['bazelCommands']: bazel_args = command.split(' ') try: doubleHyphenPos = bazel_args.index('--') print("patch that in ", doubleHyphenPos) except ValueError: pass # Bazel's wrapper script needs this or you get # 2020/07/13 21:58:11 could not get the user's cache directory: $HOME is not defined os.environ['HOME'] = str(Path.home()) bazel_args.insert(0, bazelBinary) bazel_process = Popen(bazel_args, cwd=workspacePath) bazel_process.wait() if bazel_process.returncode != 0: # Test failure in Bazel is exit 3 # https://github.com/bazelbuild/bazel/blob/486206012a664ecb20bdb196a681efc9a9825049/src/main/java/com/google/devtools/build/lib/util/ExitCode.java#L44 sys.exit(3)
def setUp(self): self.runfiles = runfiles.Create() self.maxDiff = None self.rpm_bin_version = rpm_util.get_rpm_version_as_tuple() # These tests will fail on unsupported versions of rpm(8), so skip them # in that case. if not self.rpmBinSupportsSourceDateEpoch(): self.skipTest("RPM version too old to support SOURCE_DATE_EPOCH." " Must be {} or newer (is {})".format( version_to_string(self.SOURCE_DATE_EPOCH_MIN_VERSION), version_to_string(self.rpm_bin_version), )) if "TEST_RPM" not in os.environ: self.fail("TEST_RPM must be set in the environment, containing " "the name of the RPM to test") # Allow for parameterization of this test based on the desired RPM to # test. self.rpm_file_path = self.runfiles.Rlocation("/".join([ os.environ["TEST_WORKSPACE"], "tests", "rpm", "source_date_epoch", # The object behind os.environ is not a dict, and thus doesn't have # the "getdefault()" we'd otherwise use here. os.environ["TEST_RPM"], ]))
def runfiles_path(relpath: str) -> Path: """Resolve the path to a runfiles data path. Use environment variable COMPILER_GYM_RUNFILES=/path/to/runfiles if running outside of bazel. """ # There are three ways of determining a runfiles path: # 1. Set the COMPILER_GYM_RUNFILES environment variable. # 2. Using pkg_resources to find package data. # 3. Using bazel's runfiles library to find data. # # The last two options depend on the calling context - whether the code # was built by bazel or installed using setuptools. runfiles_path = os.environ.get("COMPILER_GYM_RUNFILES") if runfiles_path: return Path(runfiles_path) / relpath else: try: from rules_python.python.runfiles import runfiles return Path(runfiles.Create().Rlocation(relpath)) except ModuleNotFoundError: # Try to find the files relative to the current file, assuming that # they are all given as paths "CompilerGym/compiler_gym/foo/bar.txt" # and such. return _PACKAGE_ROOT / Path(*Path(relpath).parts[1:])
def runfiles_path(relpath: str) -> Path: """Resolve the path to a runfiles data path. No checks are to made to ensure that the path, or the containing directory, exist. Use environment variable COMPILER_GYM_RUNFILES=/path/to/runfiles if running outside of bazel. :param relpath: The relative path within the runfiles tree. :return: An absolute path. """ # There are three ways of determining a runfiles path: # 1. Set the COMPILER_GYM_RUNFILES environment variable. # 2. Using the rules_python library that is provided by bazel. This will # fail if not being executed within a bazel sandbox. # 3. Computing the path relative to the location of this file. This is the # fallback approach that is used for when the code has been installed # by setuptools. runfiles_path = os.environ.get("COMPILER_GYM_RUNFILES") if runfiles_path: return Path(runfiles_path) / relpath else: try: from rules_python.python.runfiles import runfiles return Path(runfiles.Create().Rlocation( "CompilerGym" if relpath == "." else f"CompilerGym/{relpath}")) except (ModuleNotFoundError, TypeError): return _PACKAGE_ROOT / relpath
def _prepareForExecution(self): runfiles_instance = runfiles.Create() with open(runfiles_instance.Rlocation( self._config_template_path)) as f: data = yaml.load(f, Loader=yaml.FullLoader) data = _substitute_yaml_values(runfiles_instance, data, self._parameters) Path(self.tmpdir).mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".config.yaml", dir=self.tmpdir) as tmp: self._parameterized_config_path = tmp.name yaml.safe_dump(data, tmp, default_flow_style=False, explicit_start=True, allow_unicode=True, encoding='utf-8') with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".adminport", dir=self.tmpdir) as tmp: self._admin_address_path = tmp.name
def test_smoke_test_01(self): r = runfiles.Create() with open( r.Rlocation( "com_github_hcoona_one/one/yadm_package_installer/test/smoke_test_01.sky" ), "r") as f: contents = f.read() s = ast.parse(contents, "smoke_test_01.sky") logging.info(ast.dump(s)) f = compile(s, "smoke_test_01.sky", "exec") apt_install_called = False global_called_list = [] def _apt_install(name, packages, deps=[]): logging.info("name=%s, packages=%s", name, packages) nonlocal apt_install_called nonlocal global_called_list apt_install_called = True global_called_list += [{ "type": "apt_install", "name": name, "packages": packages, "deps": deps, }] eval(f, None, { "global_called_list": global_called_list, "apt_install": _apt_install }) logging.info("%s", global_called_list) self.assertTrue(apt_install_called)
def test_activity(self): m = Module() r = runfiles.Create() m.submodules.cpu = cpu = core.Minerva(with_debug=True) m.submodules.periph = periph = peripheral.Peripherals( r.Rlocation( 'nmigen_nexys/board/nexysa7100t/riscv_demo/deadbeef.bin')) m.d.comb += cpu.ibus.connect(periph.ibus) m.d.comb += cpu.dbus.connect(periph.dbus) # TODO: enable timeout m.submodules.timer = timer = test_util.Timer(self, timeout_s=1e-6) sim = Simulator(m) sim.add_clock(1.0 / util.SIMULATION_CLOCK_FREQUENCY) events = event.EventSeries(timer.cycle_counter) wmon = WishboneMonitor(periph.dbus) wmon.attach(sim, events) test_dir = test_util.BazelTestOutput(self.id()) os.makedirs(test_dir, exist_ok=True) traces = sum([ self._wishbone_traces(periph.ibus), self._wishbone_traces(periph.dbus), ], []) with sim.write_vcd(os.path.join(test_dir, "test.vcd"), os.path.join(test_dir, "test.gtkw"), traces=traces): sim.run_until(1e-6, run_passive=True) events.ShowEvents() events.ValidateConstraints(self, [ Write(adr=0x2000 >> 2, sel=0b1111, dat_w=0xdeadbeef), WriteAck(), ])
def elaborate(self, platform: Platform) -> Module: m = Module() m.submodules.cpu = cpu = core.Minerva(with_debug=True) # Linux tools don't like it when I try to use 12 Mbaud :( m.submodules.rbb = rbb = remote_bitbang.RemoteBitbang(3_000_000) uart = platform.request('uart', 0) m.d.comb += [ rbb.uart.rx.eq(uart.rx), uart.tx.eq(rbb.uart.tx), # TODO: RTS and CTS should probably be swapped in RemoteBitbang uart.cts.eq(rbb.uart.rts_n), rbb.uart.cts_n.eq(uart.rts), ] m.d.comb += [ cpu.jtag.tck.eq(rbb.jtag.tck), cpu.jtag.tdi.eq(rbb.jtag.tdi), rbb.jtag.tdo.eq(cpu.jtag.tdo), cpu.jtag.tms.eq(rbb.jtag.tms), cpu.jtag.trst.eq(rbb.jtag.trst), ] # Connect peripherals to the CPU and external world r = runfiles.Create() m.submodules.periph = periph = peripheral.Peripherals(r.Rlocation( 'nmigen_nexys/board/nexysa7100t/riscv_demo/main.bin')) m.d.comb += platform.request('display_7seg').eq(periph.segments) m.d.comb += platform.request('display_7seg_an').eq(periph.anodes) m.d.comb += cpu.ibus.connect(periph.ibus) m.d.comb += cpu.dbus.connect(periph.dbus) return m
def get_path(relpath: str) -> str: path = os.path.join(_BASE_PATH, relpath) r = runfiles.Create() runfile_path = r.Rlocation(path) if not os.path.exists(runfile_path): raise FileNotFoundError(f'Cannot find runfile {relpath}') return runfile_path
def get_path_to_datafile(path): """Get the path to the specified file in the data dependencies. The path is relative to tensorflow/ Args: path: a string resource path relative to tensorflow/ Returns: The path to the specified file present in the data attribute of py_test or py_binary. Raises: IOError: If the path is not found, or the resource can't be opened. """ # First, try finding in the new path. if runfiles: r = runfiles.Create() new_fpath = r.Rlocation( _os.path.abspath(_os.path.join('tensorflow', path))) if new_fpath is not None and _os.path.exists(new_fpath): return new_fpath # Then, the old style path, as people became dependent on this buggy call. old_filepath = _os.path.join( _os.path.dirname(_inspect.getfile(_sys._getframe(1))), path) return old_filepath
def setUp(self): self.runfiles = runfiles.Create() self.test_rpm_path = self.runfiles.Rlocation( "rules_pkg/experimental/tests/rpm/test_rpm.rpm") self.test_rpm_bzip2_path = self.runfiles.Rlocation( "rules_pkg/experimental/tests/rpm/test_rpm-bzip2.rpm") self.maxDiff = None
def preview_main(gen_script, default_port): """Main entrypoint for previewing documentation. Args: gen_script: Generation script, required to generate docs. default_port: Default port for local HTTP server. """ sphinx_build = runfiles.Create().Rlocation("sphinx/sphinx-build") assert isfile(sphinx_build), "Please execute via 'bazel run'" parser = argparse.ArgumentParser() parser.register('type', 'bool', _str2bool) parser.add_argument( "--browser", type='bool', default=True, metavar='BOOL', help="Open browser. Disable this if you are frequently recompiling.") parser.add_argument("--port", type=int, default=default_port, metavar='PORT', help="Port for serving doc pages with a HTTP server.") args = parser.parse_args() # Choose an arbitrary location for generating documentation. out_dir = abspath("sphinx-tmp") if isdir(out_dir): rmtree(out_dir) # Generate. check_call([sys.executable, gen_script, "--out_dir", out_dir]) print("Sphinx preview docs are available at:") file_url = "file://{}".format(join(out_dir, "index.html")) browser_url = file_url print() print(" {}".format(file_url)) # Serve the current directory for local browsing. Required for MacOS. # N.B. We serve the preview via a HTTP server because it is necessary for # certain browsers (Safari on MacOS, possibly Chrome) due to local file # restrictions. os.chdir(out_dir) sockaddr = ("127.0.0.1", args.port) TCPServer.allow_reuse_address = True httpd = TCPServer(sockaddr, _Handler) http_url = "http://{}:{}/index.html".format(*sockaddr) print() print(" {}".format(http_url)) # Default to using HTTP serving only on MacOS; on Ubuntu, it can spit # out errors and exceptions about broken pipes, 404 files, etc. if sys.platform == "darwin": browser_url = http_url # Try the default browser. if args.browser: webbrowser.open(browser_url) # Wait for server. print() print("Serving and waiting ... use Ctrl-C to exit.") httpd.serve_forever()
def run_detector(self) -> None: # buf requires protobuf files to be in a subdirectory of the yaml file with tempfile.TemporaryDirectory(prefix=str(Path(".").absolute()) + os.sep) as temp_dir: buf_path = runfiles.Create().Rlocation("com_github_bufbuild_buf/bin/buf") buf_config_loc = Path(".", "tools", "api_proto_breaking_change_detector") yaml_file_loc = Path(".", "buf.yaml") copyfile(Path(buf_config_loc, "buf.yaml"), yaml_file_loc) target = Path(temp_dir, f"{Path(self._path_to_before).stem}.proto") buf_args = [ "--path", # buf requires relative pathing for roots str(target.relative_to(Path(".").absolute())), "--config", str(yaml_file_loc), ] buf_args.extend(self._additional_args or []) copyfile(self._path_to_before, target) lock_location = Path(temp_dir, BUF_STATE_FILE) initial_code, initial_out, initial_err = run_command( ' '.join([buf_path, f"build -o {lock_location}", *buf_args])) initial_out, initial_err = ''.join(initial_out), ''.join(initial_err) if initial_code != 0 or len(initial_out) > 0 or len(initial_err) > 0: raise ChangeDetectorInitializeError( f"Unexpected error during init:\n\tExit Status Code: {initial_code}\n\tstdout: {initial_out}\n\t stderr: {initial_err}\n" ) with open(lock_location, "r") as f: self._initial_lock = f.readlines() copyfile(self._path_to_after, target) final_code, final_out, final_err = run_command( ' '.join([buf_path, f"breaking --against {lock_location}", *buf_args])) final_out, final_err = ''.join(final_out), ''.join(final_err) if len(final_out) == len(final_err) == final_code == 0: _, _, _ = run_command(' '.join([buf_path, f"build -o {lock_location}", *buf_args])) with open(lock_location, "r") as f: self._final_lock = f.readlines() self._initial_result = initial_code, initial_out, initial_err self._final_result = final_code, final_out, final_err
def runfiles_path(relpath: str) -> Path: """Resolve the path to a runfiles data path. Use environment variable PROGRAML_RUNFILES=/path/to/runfiles if running outside of bazel. """ runfiles_path = os.environ.get("PROGRAML_RUNFILES") if runfiles_path: return Path(runfiles_path) / relpath else: # Defer importing this module so that if we have set # $COMPILER_GYM_RUNFILES we do not need any bazel dependencies. from rules_python.python.runfiles import runfiles return Path(runfiles.Create().Rlocation(relpath))
def setUp(self): self.runfiles = runfiles.Create() self.maxDiff = None # Allow for parameterization of this test based on the desired RPM to # test. self.rpm_path = self.runfiles.Rlocation( os.path.join( os.environ["TEST_WORKSPACE"], "tests", "rpm", "tree_artifacts", # The object behind os.environ is not a dict, and thus doesn't have # the "getdefault()" we'd otherwise use here. os.environ["TEST_RPM"] if "TEST_RPM" in os.environ else "treeartifact_ops_rpm.rpm", ))
def setUp(self): self.runfiles = runfiles.Create() self.maxDiff = None # Allow for parameterization of this test based on the desired RPM to # test. # runfiles prefers Bazely (POSIX-style) relative paths, so we can't # really use os.path.join() here. self.rpm_path = self.runfiles.Rlocation('/'.join([ os.environ["TEST_WORKSPACE"], "tests", "rpm", "tree_artifacts", # The object behind os.environ is not a dict, and thus doesn't have # the "getdefault()" we'd otherwise use here. os.environ["TEST_RPM"] if "TEST_RPM" in os.environ else "test_dirs_rpm.rpm", ]))
def verify(biffield_path, peripheral): handle, cc_path = tempfile.mkstemp(suffix='.cc') r = runfiles.Create() runfiles_dir = r.EnvVars()['RUNFILES_DIR'] biffield_generate = r.Rlocation('etl/biffield/generate.h') temp_dir = tempfile.mkdtemp() include_path = os.path.join(temp_dir, 'third_party') os.symlink(runfiles_dir, include_path) with open(handle, 'w') as f: f.write('// clang-format off\n') f.write('#ifndef __VERIFY__\n') f.write('#define __VERIFY__\n') f.write('#include <cstdint>\n') f.write('#include <cstddef>\n') f.write('\n') f.write(f'struct {peripheral.name} {{\n') f.write(f'#define ETL_BFF_DEFINITION_FILE \\\n') f.write(f' "{biffield_path}"\n') f.write(f'#include "{biffield_generate}"\n') f.write(f'#undef ETL_BFF_DEFINITION_FILE\n') f.write('\n') f.write(' void Verify();\n') f.write(f'}};\n') f.write('\n') f.write(f'void {peripheral.name}::Verify() {{\n') for r in peripheral.registers: if is_dropped(r.name): continue name = reg_name(r.name) f.write(f' static_assert(offsetof({peripheral.name}, _{name}) == ' f'0x{r.address_offset:03x});\n') f.write(f'}}\n') f.write('\n') f.write('#endif // __VERIFY__\n') os.fsync(f) out_path = os.path.join(temp_dir, 'out') cmd = f'gcc {cc_path} -c -o {out_path} -I {include_path}' if FLAGS.dump_verify_command: print(cmd) if os.system(cmd) != 0: print('Verification failed')
def test_cli(self): """Tests launching the CLI.""" r = runfiles.Create() sp = subprocess.Popen( [ "python", r.Rlocation("monorepo_tools/monorepo_tools.zip"), "import", "--individual_repos", r.Rlocation("monorepo_tools/import_into/individual_repos.py"), "--dest_branch", "stitched", "--monorepo_path", os.path.join(REPOS_ROOT, "monorepo") ], stdout = subprocess.PIPE, stderr = subprocess.PIPE) out, err = sp.communicate() if sp.returncode != 0: raise Exception( "non-zero return code {};\nstdout:\n{}\nstderr:\n{}".format( sp.returncode, out.decode('utf8'), err.decode('utf8')))
def runfiles_path(relpath: str) -> Path: # pragma: no cover """Resolve the path to a runfiles data path. Use environment variable PROGRAML_RUNFILES=/path/to/runfiles if running outside of bazel. """ runfiles_path = os.environ.get("PROGRAML_RUNFILES") if runfiles_path: return Path(runfiles_path) / relpath else: try: from rules_python.python.runfiles import runfiles return Path(runfiles.Create().Rlocation( "programl" if relpath == "." else f"programl/{relpath}")) except (ModuleNotFoundError, TypeError): # Special handler for paths to test data. if relpath.startswith("tests/"): return _TESTS_ROOT / relpath return _PACKAGE_ROOT / relpath
def setUpClass(cls): cls.runfiles = runfiles.Create() # Somewhat of an implementation detail, but it works. I think. manifest_file = cls.runfiles.Rlocation( "rules_pkg/tests/install/test_installer_install_script-install-manifest.json" ) with open(manifest_file, 'r') as fh: manifest_data_raw = json.load(fh) cls.manifest_data = {} for entry in manifest_data_raw: entry_struct = manifest.ManifestEntry(*entry) cls.manifest_data[entry_struct.dest] = entry_struct cls.installdir = os.path.join(os.getenv("TEST_TMPDIR"), "installdir") env = {} env.update(cls.runfiles.EnvVars()) subprocess.check_call([ cls.runfiles.Rlocation("rules_pkg/tests/install/test_installer"), "--destdir", cls.installdir, "--verbose", ], env=env)
def _run_benchmark(fixture, rps=1000, duration=30, max_connections=1, max_active_requests=100, request_body_size=0, response_size=1024, concurrency=1): if hasattr(fixture, "proxy_server"): assert (fixture.proxy_server.enableCpuProfiler()) assert (fixture.test_server.enableCpuProfiler()) args = [ fixture.getTestServerRootUri(), "--rps", str(rps), "--duration", str(duration), "--connections", str(max_connections), "--max-active-requests", str(max_active_requests), "--concurrency", str(concurrency), "--request-header", "x-nighthawk-test-server-config:{response_body_size:%s}" % response_size, "--experimental-h1-connection-reuse-strategy", "lru", "--prefetch-connections" ] if request_body_size > 0: args.append("--request-body-size") args.append(str(request_body_size)) parsed_json, _ = fixture.runNighthawkClient(args) counters = fixture.getNighthawkCounterMapFromJson(parsed_json) response_count = counters["benchmark.http_2xx"] request_count = counters["upstream_rq_total"] connection_counter = "upstream_cx_http1_total" # Some arbitrary sanity checks asserts.assertCounterGreaterEqual(counters, "benchmark.http_2xx", (concurrency * rps * duration) * 0.99) asserts.assertGreater(counters["upstream_cx_rx_bytes_total"], response_count * response_size) asserts.assertGreater(counters["upstream_cx_tx_bytes_total"], request_count * request_body_size) asserts.assertCounterEqual(counters, connection_counter, concurrency * max_connections) # Could potentially set thresholds on acceptable latency here. # dump human readable output to logs json_as_string = json.dumps(parsed_json) human_output = fixture.transformNighthawkJson(json_as_string, "human") logging.info(human_output) with open(os.path.join(fixture.test_server.tmpdir, "nighthawk-human.txt"), "w") as f: f.write(human_output) with open(os.path.join(fixture.test_server.tmpdir, "nighthawk.json"), "w") as f: f.write(json_as_string) with open(os.path.join(fixture.test_server.tmpdir, "nighthawk.yaml"), "w") as f: f.write(fixture.transformNighthawkJson(json_as_string, "yaml")) with open(os.path.join(fixture.test_server.tmpdir, "fortio.json"), "w") as f: f.write(fixture.transformNighthawkJson(json_as_string, "fortio")) with open(os.path.join(fixture.test_server.tmpdir, "server_version.txt"), "w") as f: f.write(fixture.test_server.getCliVersionString()) if hasattr(fixture, "proxy_server"): with open( os.path.join(fixture.test_server.tmpdir, "proxy_version.txt"), "w") as f: f.write(fixture.proxy_server.getCliVersionString()) r = runfiles.Create() copyfile( r.Rlocation("nighthawk/benchmarks/test/templates/simple_plot.html"), os.path.join(fixture.test_server.tmpdir, "simple_plot.html"))
class BufTests(TestAllowedChanges, TestBreakingChanges, unittest.TestCase): _buf_path = runfiles.Create().Rlocation("com_github_bufbuild_buf/bin/buf") @classmethod def _run_command_print_error(cls, cmd): response = subprocess.run([cmd], shell=True, capture_output=True, encoding="utf-8") code, out, err = response.returncode, response.stdout, response.stderr if code != 0: raise Exception( f"Error running command {cmd}\nExit code: {code} | stdout: {out} | stderr: {err}" ) @classmethod def setUpClass(cls): try: # make temp dir # buf requires protobuf files to be in a subdirectory of the directory containing the yaml file cls._temp_dir = tempfile.TemporaryDirectory(dir=Path.cwd()) cls._config_file_loc = Path(cls._temp_dir.name, "buf.yaml") # copy in test data testdata_path = Path(Path.cwd(), "tools", "testdata", "api_proto_breaking_change_detector") copytree(testdata_path, cls._temp_dir.name, dirs_exist_ok=True) # copy in buf config bazel_buf_config_loc = Path.cwd().joinpath("external", "envoy_api", "buf.yaml") copyfile(bazel_buf_config_loc, cls._config_file_loc) # pull buf dependencies and initialize git repo with test data files with cd_and_return(cls._temp_dir.name): pull_buf_deps(cls._buf_path, cls._temp_dir.name, config_file_loc=cls._config_file_loc) cls._run_command_print_error('git init') cls._run_command_print_error('git add .') cls._run_command_print_error( "git config user.name 'Bazel Test'") cls._run_command_print_error("git config user.email '<>'") cls._run_command_print_error('git commit -m "Initial commit"') except: cls.tearDownClass() raise @classmethod def tearDownClass(cls): cls._temp_dir.cleanup() def tearDown(self): # undo changes to proto file that were applied in test with cd_and_return(self._temp_dir.name): self._run_command_print_error('git reset --hard') def run_detector_test(self, testname, is_breaking, expects_changes, additional_args=None): """Runs a test case for an arbitrary breaking change detector type""" tests_path = Path(self._temp_dir.name, "breaking" if is_breaking else "allowed") target = Path(tests_path, f"{testname}.proto") changed = Path(tests_path, f"{testname}_changed") # make changes to proto file copyfile(changed, target) # buf breaking detector_obj = BufWrapper(self._temp_dir.name, git_ref="HEAD", git_path=Path(self._temp_dir.name, ".git"), additional_args=additional_args, buf_path=self._buf_path, config_file_loc=self._config_file_loc) detector_obj.run_detector() breaking_response = detector_obj.is_breaking() self.assertEqual(breaking_response, is_breaking) @unittest.skip("PGV field support not yet added to buf") def test_change_pgv_field(self): pass @unittest.skip("PGV message option support not yet added to buf") def test_change_pgv_message(self): pass @unittest.skip("PGV oneof option support not yet added to buf") def test_change_pgv_oneof(self): pass
"""Templated test driver for elaboration tests. Normally one would simply emit a shell script from the elaboration test rule and execute that directly, but (a) making that work for Windows is more complex than it should be, and (b) batch scripts can't access runfiles anyway. This lets us intercept Vivado invocations, anyway. """ import os import subprocess from rules_python.python.runfiles import runfiles ENV = {ENV} EXE = {EXE} ARGS = {ARGS} if __name__ == '__main__': r = runfiles.Create() env = dict(os.environ) env.update(ENV) env.update(r.EnvVars()) subprocess.check_call([EXE, *ARGS], env=env)
def AsResourcePath(filename: os.PathLike) -> os.PathLike: filename = os.fspath(filename) return runfiles.Create().Rlocation(filename)
def gen_main(input_dir, strict, src_func=None): """Main entry point for generation. Args: input_dir: Directory which contains initial input files. strict: Determines if Sphinx warnings should be interpreted as errors. src_func: (optional) Callable of form `f(src_dir)` which will introduce additional source files to `src_dir`. """ sphinx_build = runfiles.Create().Rlocation("sphinx/sphinx-build") assert isfile(sphinx_build), "Please execute via 'bazel run'" parser = argparse.ArgumentParser() parser.add_argument( "--out_dir", type=str, required=True, help="Output directory. Does not have to exist beforehand.") parser.add_argument( "--debug", action="store_true", help="If enabled, leaves intermediate files that are otherwise " "deleted.") args = parser.parse_args() out_dir = args.out_dir if out_dir == "<test>": out_dir = join(os.environ["TEST_TMPDIR"], "doc") if not isabs(out_dir): _die("--out_dir must be absolute path: {}".format(out_dir)) # Use default temp directory, handling both `bazel run` and `bazel test`. tmp_dir = tempfile.mkdtemp(dir=os.environ.get("TEST_TMPDIR")) doctree_dir = join(tmp_dir, "doctrees") src_dir = join(tmp_dir, "src") # Symlink inputs to src dir (so that we can also generate doc modules). mkdir(src_dir) for f in listdir(input_dir): src_f = join(src_dir, f) symlink(join(input_dir, f), src_f) # Optionally generate additional input files as source. if src_func: src_func(src_dir) print("Generating documentation...") if strict: # Turn warnings into errors; else be quiet. warning_args = ["-W", "-N", "-q"] else: warning_args = [ "-N", "-Q", # Be very quiet. "-T", # Traceback (for plugin) ] os.environ["LANG"] = "en_US.UTF-8" check_call([ sphinx_build, "-b", "html", # HTML output. "-a", "-E", # Don't use caching. "-d", doctree_dir ] + warning_args + [ src_dir, # Source dir. out_dir, ]) if not args.debug: rmtree(tmp_dir) else: print("DEBUG: Temporary files: {}".format(tmp_dir))