Skip to content

Commit 3a66c84

Browse files
New scripting environment for integration with the monorepo (#2748)
THIS IS DEFINITELY WIP! I've screwed up my Cloud Workspace trying to make this work, so I haven't really tested scripts/copy-ast.sh. I did test scripts/copy-remote-ast.sh whereby I run the script from my local machine. AST tests pass after this. --------- Co-authored-by: Varnika Budati <varnika.budati@snowflake.com>
1 parent 15037ef commit 3a66c84

File tree

10 files changed

+83
-147
lines changed

10 files changed

+83
-147
lines changed

scripts/copy-ast.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Compute the source directory. We honor the MONOREPO_DIR environment variable if set,
4+
# and default to the standard Cloud Workspace location otherwise.
5+
DEFAULT_SRC_DIR=$HOME/Snowflake/trunk
6+
MONOREPO_DIR="${MONOREPO_DIR:-$DEFAULT_SRC_DIR}"
7+
if [ ! -d "$MONOREPO_DIR" ]; then
8+
echo "Source directory '${MONOREPO_DIR}' doesn't exist"
9+
exit 1
10+
fi
11+
12+
# To allow this script to run from any subdirectory within snowpark-python, we use git rev-parse.
13+
SNOWPARK_ROOT=$(git rev-parse --show-toplevel)
14+
15+
# Build the targets.
16+
pushd $MONOREPO_DIR
17+
bazel build //Snowpark/ast:ast_proto
18+
bazel build //Snowpark/unparser
19+
popd
20+
21+
# Copy the AST.
22+
cp $MONOREPO_DIR/bazel-bin/Snowpark/ast/ast.proto $SNOWPARK_ROOT/src/snowflake/snowpark/_internal/proto/ast.proto
23+
24+
# Generate Python from ast.proto.
25+
pushd $SNOWPARK_ROOT
26+
python -m tox -e protoc
27+
popd

scripts/copy-remote-ast.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# This script assumes the target Cloud Workspace specified as the command-line argument has the build target.
4+
# To make sure this is the case, run bazel build //Snowpark/ast:ast_proto and bazel build //Snowpark/unparser.
5+
6+
# N.B. The calling environment further requires:
7+
# export MONOREPO_DIR=$TMPDIR
8+
9+
# To allow this script to run from any subdirectory within snowpark-python, we use git rev-parse.
10+
SNOWPARK_ROOT=$(git rev-parse --show-toplevel)
11+
12+
if [ ! -d "$TMPDIR" ]; then
13+
echo "TMPDIR not defined"
14+
exit 1
15+
fi
16+
17+
scp $1:~/Snowflake/trunk/bazel-bin/Snowpark/ast/ast.proto $SNOWPARK_ROOT/src/snowflake/snowpark/_internal/proto/ast.proto
18+
19+
mkdir -p $TMPDIR/bazel-bin/Snowpark/unparser/unparser.runfiles
20+
scp -r $1:~/Snowflake/trunk/bazel-bin/Snowpark/unparser/unparser.runfiles/ $TMPDIR/bazel-bin/Snowpark/unparser/unparser.runfiles/
21+
22+
scp $1:~/Snowflake/trunk/bazel-bin/Snowpark/unparser/unparser-lib.jar $TMPDIR/bazel-bin/Snowpark/unparser/
23+
scp $1:~/Snowflake/trunk/bazel-bin/Snowpark/unparser/unparser.jar $TMPDIR/bazel-bin/Snowpark/unparser/
24+
25+
pushd $SNOWPARK_ROOT
26+
python -m tox -e protoc
27+
popd

src/snowflake/snowpark/_internal/proto/update-from-devvm.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/snowflake/snowpark/_internal/proto/update-from-local.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/ast/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pytest --update-expectations tests/ast
3535
For these tests to work, the Unparser must be built in the monorepo:
3636
```bash
3737
cd my-monorepo-path
38-
cd Snowflake/unparser
39-
sbt assembly
38+
bazel build //Snowpark/unparser
4039
```
4140

42-
The location of the Unparser can be set either via the environment variable `SNOWPARK_UNPARSER_JAR` or via the _pytest_ commandline argument `--unparser-jar=<path>`.
41+
The location of the monorepo must be supplied in the environment variable `MONOREPO_DIR`.
42+
Alternatively, use `scripts/copy-remote-ast.sh` to copy the necessary artifacts from a cloud workspace.

tests/ast/ast_test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def render(ast_base64: Union[str, List[str]], unparser_jar: Optional[str]) -> st
3333
"""Uses the unparser to render the AST."""
3434
assert (
3535
unparser_jar
36-
), "A valid Unparser JAR path must be supplied either via --unparser-jar=<path> or the environment variable SNOWPARK_UNPARSER_JAR"
36+
), "A valid Unparser JAR path must be supplied either via --unparser-jar=<path> or the environment variable MONOREPO_DIR"
3737

3838
if isinstance(ast_base64, str):
3939
ast_base64 = [ast_base64]
@@ -43,7 +43,7 @@ def render(ast_base64: Union[str, List[str]], unparser_jar: Optional[str]) -> st
4343
"java",
4444
"-cp",
4545
unparser_jar,
46-
"com.snowflake.snowpark.experimental.unparser.UnparserCli",
46+
"com.snowflake.snowpark.unparser.UnparserCli",
4747
",".join(
4848
ast_base64
4949
), # base64 strings will not contain , so pass multiple batches comma-separated.

tests/ast/conftest.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111

1212

1313
def default_unparser_path():
14-
explicit = os.getenv("SNOWPARK_UNPARSER_JAR")
15-
default_default = f"{os.getenv('HOME')}/Snowflake/trunk/Snowpark/unparser/target/scala-2.13/unparser-assembly-0.1.jar"
16-
return explicit or default_default
14+
explicit = os.getenv("MONOREPO_DIR")
15+
default_default = os.path.join(os.getenv("HOME"), "Snowflake/trunk")
16+
base_dir = explicit or default_default
17+
unparser_dir = os.path.join(base_dir, "bazel-bin/Snowpark/unparser")
18+
19+
# Grab all *.jar files from the subtree.
20+
jars = []
21+
for path, _, files in os.walk(unparser_dir):
22+
jars.extend(
23+
[os.path.join(path, file) for file in files if file.endswith(".jar")]
24+
)
25+
return ":".join(jars)
1726

1827

1928
def pytest_addoption(parser):
@@ -22,7 +31,7 @@ def pytest_addoption(parser):
2231
action="store",
2332
default=default_unparser_path(),
2433
type=str,
25-
help="Path to the Unparser JAR built in the monorepo. To build it, run `sbt assembly` from the unparser directory.",
34+
help="Path to the Unparser JAR built in the monorepo.",
2635
)
2736
parser.addoption(
2837
"--update-expectations",
@@ -33,15 +42,18 @@ def pytest_addoption(parser):
3342

3443

3544
def pytest_configure(config):
36-
pytest.unparser_jar = config.getoption("--unparser-jar")
37-
if not os.path.exists(pytest.unparser_jar):
38-
pytest.unparser_jar = None
45+
unparser_jar = config.getoption("--unparser-jar")
46+
pytest.unparser_jar = (
47+
unparser_jar
48+
if all(os.path.exists(file) for file in unparser_jar.split(":"))
49+
else None
50+
)
3951
pytest.update_expectations = config.getoption("--update-expectations")
4052

4153
if pytest.unparser_jar is None and pytest.update_expectations:
4254
raise RuntimeError(
43-
f"Unparser JAR not found at {pytest.unparser_jar}. "
44-
f"Please set the correct path with --unparser-jar or SNOWPARK_UNPARSER_JAR."
55+
f"Unparser JAR not found at {unparser_jar}. "
56+
f"Please set the correct path with --unparser-jar or the MONOREPO_DIR environment variable."
4557
)
4658

4759

tests/ast/test_ast_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_ast(session, tables, test_case):
193193
if pytest.update_expectations:
194194
assert pytest.unparser_jar, (
195195
"Can only update expectations with unparser jar set. Either run the test with --unparser-jar=<path> or"
196-
" update the environment variable SNOWPARK_UNPARSER_JAR."
196+
" update the environment variable MONOREPO_DIR."
197197
)
198198
with open(DATA_DIR / test_case.filename, "w", encoding="utf-8") as f:
199199
f.writelines(

tests/ast/update-unparser.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

tests/conftest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pytest
1313

1414
from snowflake.snowpark._internal.utils import COMPATIBLE_WITH_MODIN, warning_dict
15+
from .ast.conftest import default_unparser_path
1516

1617
logging.getLogger("snowflake.connector").setLevel(logging.ERROR)
1718

@@ -45,12 +46,6 @@ def dict_exporter():
4546
opentelemetry_installed = False
4647

4748

48-
def default_unparser_path():
49-
explicit = os.getenv("SNOWPARK_UNPARSER_JAR")
50-
default_default = f"{os.getenv('HOME')}/Snowflake/trunk/Snowpark/unparser/target/scala-2.13/unparser-assembly-0.1.jar"
51-
return explicit or default_default
52-
53-
5449
def is_excluded_frontend_file(path):
5550
for excluded in excluded_frontend_files:
5651
if str(path).endswith(excluded):
@@ -76,7 +71,7 @@ def pytest_addoption(parser, pluginmanager):
7671
action="store",
7772
default=default_unparser_path(),
7873
type=str,
79-
help="Path to the Unparser JAR built in the monorepo. To build it, run `sbt assembly` from the unparser directory.",
74+
help="Path to the Unparser JAR built in the monorepo.",
8075
)
8176

8277

0 commit comments

Comments
 (0)