def parse_build_file(self, build_file): # type: (str) -> None if not os.path.isfile(build_file): return if self._verbose: print( "Parsing %s (%d bytes)" % (build_file, os.path.getsize(build_file)), file=sys.stderr, ) dir = os.path.dirname(build_file) bp = build_parser.BuildParser() if self._verbose: t0 = time.time() try: bp.parse_file(build_file) except SyntaxError as err: sys.stderr.write("whatpyver: Syntax error ignored in build file\n") sys.stderr.write("%s:%s:%s\n" % ( os.path.relpath(build_file), err.lineno, err.text.rstrip() if err.text is not None else "", )) return finally: if self._verbose: t1 = time.time() print("Parsing took %.1f msec" % ((t1 - t0) * 1000)) self._build_file_parsers[build_file] = bp rules = bp.get_rules_by_types(RULE_TYPES) if not any(rule.attr_map.get("srcs") for rule in rules): # If the BUILD file is empty or lacks srcs, it trivially supports py2/py3 # this helps support intermediate directories w/ only __init__ self._py2_files.add(os.path.join(dir, "__init__.py")) self._py3_files.add(os.path.join(dir, "__init__.py")) for rule in rules: # NOTE: These defaults may change when build_tools/py/py.bzl changes. # python2_compatible is used by dbx_py_binary # python_version is used by py_binary # srcs_version is used by py_library py2 = False py3 = (rule.attr_map.get("python3_compatible", True) and rule.attr_map.get("python_version", "PY3") != "PY2" and rule.attr_map.get("srcs_version", "PY3") != "PY2ONLY") for src in build_parser.maybe_expand_attribute( rule.attr_map.get("srcs", [])): src = os.path.join(dir, src) # Explicitly add __init__.py files, since those are typically not included # in BUILD files, but mypy relies on them for module existence, particularly # when follow_imports=skip in the mypy.ini if py2: self._py2_files.add(src) self._py2_files.add(os.path.join(dir, "__init__.py")) if py3: self._py3_files.add(src) self._py3_files.add(os.path.join(dir, "__init__.py"))
def parse_build_file(self, build_file): # type: (str) -> None if not os.path.isfile(build_file): return if self._verbose: print( "Parsing %s (%d bytes)" % (build_file, os.path.getsize(build_file)), file=sys.stderr, ) dir = os.path.dirname(build_file) bp = build_parser.BuildParser() if self._verbose: t0 = time.time() try: bp.parse_file(build_file) except SyntaxError as err: sys.stderr.write("whatpyver: Syntax error ignored in build file\n") sys.stderr.write("%s:%s:%s\n" % ( os.path.relpath(build_file), err.lineno, err.text.rstrip() if err.text is not None else "", )) return finally: if self._verbose: t1 = time.time() print("Parsing took %.1f msec" % ((t1 - t0) * 1000)) self._build_file_parsers[build_file] = bp for rule in bp.get_rules_by_types(RULE_TYPES): # NOTE: These defaults may change when build_tools/py/py.bzl changes. # python2_compatible is used by dbx_py_binary # python_version is used by py_binary # srcs_version is used by py_library py2 = (rule.attr_map.get( "python2_compatible", rule.rule_type not in RULE_TYPES_THAT_DEFAULT_PY3_ONLY, ) or rule.attr_map.get("python_version", "PY3") == "PY2" or rule.attr_map.get( "srcs_version", "PY3") in ("PY2", "PY2ONLY", "PY2AND3")) py3 = (rule.attr_map.get("python3_compatible", True) and rule.attr_map.get("python_version", "PY3") != "PY2" and rule.attr_map.get("srcs_version", "PY3") != "PY2ONLY") for src in build_parser.maybe_expand_attribute( rule.attr_map.get("srcs", [])): src = os.path.join(dir, src) if py2: self._py2_files.add(src) if py3: self._py3_files.add(src)
def parse_build_file(self, build_file): # type: (str) -> None if not os.path.isfile(build_file): return if self._verbose: print( "Parsing %s (%d bytes)" % (build_file, os.path.getsize(build_file)), file=sys.stderr, ) dir = os.path.dirname(build_file) bp = build_parser.BuildParser() if self._verbose: t0 = time.time() try: bp.parse_file(build_file) except SyntaxError as err: sys.stderr.write("whatpyver: Syntax error ignored in build file\n") sys.stderr.write("%s:%s:%s\n" % ( os.path.relpath(build_file), err.lineno, err.text.rstrip() if err.text is not None else "", )) return finally: if self._verbose: t1 = time.time() print("Parsing took %.1f msec" % ((t1 - t0) * 1000)) self._build_file_parsers[build_file] = bp for rule in bp.get_rules_by_types(RULE_TYPES): # NOTE: These defaults may change when build_tools/py/py.bzl changes. py2 = rule.attr_map.get("python2_compatible", True) py3 = rule.attr_map.get( "python3_compatible", rule.rule_type not in RULE_TYPES_WITHOUT_PY3_SUPPORT, ) for src in rule.attr_map.get("srcs", []): src = os.path.join(dir, src) if py2: self._py2_files.add(src) if py3: self._py3_files.add(src)
def cmd_pkg(args, bazel_args, mode_args): workspace_dir = bazel_utils.find_workspace() curdir = os.getcwd() os.chdir(workspace_dir) # Each target must be of type dbx_pkg_* just for sanity. for target_str in args.targets: target = bazel_utils.BazelTarget(target_str) try: bp = build_parser.BuildParser() bp.parse_file(os.path.join(workspace_dir, target.build_file)) rule = bp.get_rule(target.name) except (IOError, KeyError) as e: sys.exit("No such target: " + target_str + " " + str(e)) run_rule(args, bazel_args, mode_args, target, rule) outputs = bazel_utils.outputs_for_label(args.bazel_path, target.label) print("bzl target", target.label, "up-to-date:") for f in outputs: print(" " + f) os.chdir(curdir)