Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"shared/yeast",
"shared/yeast-macros",
"shared/yeast-schema",
"python/extractor/yeast-python",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to see (parts of) the Python extractor get included in the workspace 🤩

"ruby/extractor",
"unified/extractor",
"unified/swift-syntax-rs",
Expand Down
39 changes: 39 additions & 0 deletions misc/bazel/3rdparty/rust_workspace_deps/crates.bzl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ codeql_pkg_files(
name = "extractor-arch",
exes = [
"//python/extractor/tsg-python",
"//python/extractor/yeast-python",
],
prefix = "tools/{CODEQL_PLATFORM}",
)
Expand Down
23 changes: 23 additions & 0 deletions python/extractor/semmle/python/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import semmle.python.parser.tokenizer
import semmle.python.parser.tsg_parser
import semmle.python.parser.yeast_parser
import re
import os
from blib2to3.pgen2 import tokenize
Expand Down Expand Up @@ -121,6 +122,28 @@ def old_py_ast(self):

@property
def py_ast(self):
if self._py_ast is None and not os.environ.get(
"CODEQL_PYTHON_DISABLE_YEAST_PARSER"
):
with timers["yeast_py_ast"]:
self.logger.debug("Trying yeast parser on %s", self.path)
try:
self._py_ast = semmle.python.parser.yeast_parser.parse(
self.path, self.source, self.logger
)
except Exception as ex:
self.logger.debug(
"Yeast parser failed on %s: %s", self.path, ex
)
self._py_ast = None
if self._py_ast is not None:
self.logger.debug("Yeast parser successful on %s", self.path)
self.diagnostics_writer.record_tree_sitter_parser()
return self._py_ast
elif self._py_ast is None:
self.logger.debug(
"Yeast parser disabled, skipping parse attempt for %s", self.path
)
try:
# If the `CODEQL_PYTHON_DISABLE_OLD_PARSER` flag is present, we do not try to use the
# old parser, and instead jump straight to the exception handler.
Expand Down
83 changes: 83 additions & 0 deletions python/extractor/semmle/python/parser/yeast_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Functions used for parsing Python files with YEAST.

import json
import os
import subprocess


_SUPPORTED_PLATFORMS = frozenset(("linux64", "linux-arm64", "osx64", "win64"))


def _is_within(path, root):
try:
return os.path.commonpath((root, path)) == root
except ValueError:
return False


def _packaged_command(extractor_root, platform):
if platform not in _SUPPORTED_PLATFORMS:
raise ValueError("Unsupported CodeQL platform {!r}".format(platform))

tools_root = os.path.realpath(os.path.join(extractor_root, "tools"))
tools = os.path.realpath(os.path.join(tools_root, platform))
if not _is_within(tools, tools_root):
raise ValueError("Platform tools directory escapes extractor tools root")

ext = ".exe" if platform == "win64" else ""
executable = os.path.realpath(os.path.join(tools, "yeast-python" + ext))
if not _is_within(executable, tools):
raise ValueError("Parser executable escapes platform tools directory")
return [executable]


_SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
_SOURCE_TREE_ROOT = os.path.realpath(
os.path.join(_SCRIPT_PATH, "../../../yeast-python")
)
_SOURCE_TREE_COMMAND = [
"cargo",
"run",
"--quiet",
"--release",
"--manifest-path=" + os.path.join(_SOURCE_TREE_ROOT, "Cargo.toml"),
]


def _parser_command():
if "CODEQL_EXTRACTOR_PYTHON_ROOT" in os.environ:
return (
_packaged_command(
os.environ["CODEQL_EXTRACTOR_PYTHON_ROOT"],
os.environ["CODEQL_PLATFORM"],
),
[],
None,
)
return (
_SOURCE_TREE_COMMAND,
["--"],
_SOURCE_TREE_ROOT,
)


def parse(path, source, _logger):
command, command_separator, cwd = _parser_command()
command_args = command + command_separator + [os.path.abspath(path)]
process = subprocess.Popen(
Comment thread
tausbn marked this conversation as resolved.
Resolved
command_args,
cwd=cwd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = process.communicate(source.encode("utf-8"))
if process.returncode:
raise subprocess.CalledProcessError(
process.returncode, command_args, stdout, stderr
)

result = json.loads(stdout)
if result == {"status": "not_parsed"}:
return None
raise ValueError("Unsupported yeast parser response {!r}".format(result))
Loading
Loading