Skip to content

Add pre-commit #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
31 changes: 29 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,36 @@ jobs:
run: echo "maven_bundle_url=$(./scripts/maven-bundle-url.sh | tail -n 1)" >> $GITHUB_OUTPUT
shell: bash

pre_commit:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Java 21
id: java21
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install pre-commit
run: python -m pip install --upgrade pip pre-commit
shell: bash

- name: run pre-commit
run: pre-commit run --all-files
shell: bash

build:
runs-on: ${{ matrix.os }}
needs: maven_bundle_url
# pre_commit not needed, but is in 'needs' to avoid wasting CI cycles in case it doesn't pass
needs: [maven_bundle_url, pre_commit]
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
Expand Down Expand Up @@ -207,4 +234,4 @@ jobs:

- name: Run Maven tests
run: mvn --batch-mode -s settings.xml -Dgradle.java.home=$GRADLE_JAVA_HOME ${{ matrix.maven_args }}
shell: bash
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/javainterfacegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
with:
name: javainterfacegen-${{ matrix.os_name }}-${{ env.ARCH }}
path: |
javainterfacegen/target/javainterfacegen
javainterfacegen/target/javainterfacegen
2 changes: 1 addition & 1 deletion .github/workflows/tests-release-jbang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
mvn --batch-mode exec:java@integration-tests \
-Dproject.polyglot.version=24.1.2 \
-Dintegration.tests.args="test_jbang_integration.py \
--jbang-graalpy-version=24.2.1"
--jbang-graalpy-version=24.2.1"
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-xml
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- id: mixed-line-ending
args: ['--fix=no']
- repo: local
hooks:
- id: maven-validate-spotless
name: Maven validate & spotless:apply
entry: ./mvnw
args: ['validate', '-DskipJavainterfacegen=true', 'spotless:apply']
language: system
pass_filenames: false
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ verbosity level.
## Changing version

- property `revision` in top level `pom.xml`
- property `graalpy.version` in `graalpy-archetype-polyglot-app/src/main/resources/archetype-resources/pom.xml` (TODO: propagate from revision)
- property `graalpy.version` in `graalpy-archetype-polyglot-app/src/main/resources/archetype-resources/pom.xml` (TODO: propagate from revision)
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
</archetype-descriptor>
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void main(String[] args) {
} catch (IOException e) {
throw new RuntimeException(e);
}

context.eval(source);

// retrieve the python PyHello class
Expand All @@ -70,7 +70,7 @@ public static void main(String[] args) {
// and cast it to the Hello interface which matches PyHello
Hello hello = pyHello.as(Hello.class);
hello.hello("java");

} catch (PolyglotException e) {
if (e.isExit()) {
System.exit(e.getExitStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# the jdk used to run the gradle demon has to be <= 22
# this will be replaced by the integration tests:
# pass -Dgradle.java.home to Maven or --gradle-java-home to the Python test driver
org.gradle.java.home={GRADLE_JAVA_HOME}
org.gradle.java.home={GRADLE_JAVA_HOME}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ distributionUrl=https://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ public class GraalPy {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def hello(self, txt):
print(colored_text)

# We export the PyHello class to Java as our explicit interface with the Java side
polyglot.export_value("PyHello", PyHello)
polyglot.export_value("PyHello", PyHello)
1 change: 0 additions & 1 deletion integration-tests/gradle/scripts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ application {
val r = tasks.run.get()
r.enableAssertions = true
r.outputs.upToDateWhen {false}

6 changes: 3 additions & 3 deletions integration-tests/jbang/EmptyPIPComments.j
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:26.0.0}
//PIP
// one blank after PIP
//PIP
//PIP
// three blanks after PIP
//PIP
//PIP

public class EmptyPIPComments {
public static void main(String[] args) {
}
}
}
2 changes: 1 addition & 1 deletion integration-tests/jbang/EmptyPythonResourceComment.j
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
public class EmptyPythonResourceComment {
public static void main(String[] args) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

//DEPS org.graalvm.python:jbang:${env.GRAALPY_VERSION:26.0.0}
// resource dir with blanks
//PYTHON_RESOURCES_DIRECTORY
//PYTHON_RESOURCES_DIRECTORY

public class EmptyPythonResourceCommentWithBlanks {
public static void main(String[] args) {
}
}
}
2 changes: 1 addition & 1 deletion integration-tests/jbang/NoPackagesResourcesDir.j
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
public class NoPackagesResourcesDir {
public static void main(String[] args) {
}
}
}
2 changes: 1 addition & 1 deletion integration-tests/jbang/TwoPythonResourceComments.j
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
public class TwoPythonResourceComments {
public static void main(String[] args) {
}
}
}
2 changes: 1 addition & 1 deletion integration-tests/native-image.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# some test gates have only 16Gb and
# native image then allocates only 8Gb which isn't enough
Args = -J-Xms12g
Args = -J-Xms12g
2 changes: 1 addition & 1 deletion integration-tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@
util.jbang_graalpy_version = args.jbang_graalpy_version if args.jbang_graalpy_version else args.graalpy_version
util.gradle_java_home = args.gradle_java_home

unittest.main(argv=[sys.argv[0]] + remaining_args, module=None, exit=True)
unittest.main(argv=[sys.argv[0]] + remaining_args, module=None, exit=True)
3 changes: 1 addition & 2 deletions integration-tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def run_cmd(cmd, env, cwd=None, print_out=False, logger:LoggerBase=NullLogger())
logger = StdOutLogger(logger)
out = []
out.append(f"Executing:\n {cmd=}\n")

logger.log(f"Executing command: {' '.join(cmd)}")
process = subprocess.Popen(cmd, env=env, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, text=True, errors='backslashreplace')
for line in iter(process.stdout.readline, ""):
Expand Down Expand Up @@ -220,4 +220,3 @@ def replace_main_body(filename, new_main_body):
f.write(new_main_body)
f.write(' }\n')
f.write('}\n')

18 changes: 9 additions & 9 deletions javainterfacegen/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
This tool generates Java interfaces from Python files. It relies on the Pyhton
Mypy package, which parses Python source code and provides an attributed AST
This tool generates Java interfaces from Python files. It relies on the Pyhton
Mypy package, which parses Python source code and provides an attributed AST
with type information.

Currently, running Mypy on GraalVM is slow, so the program uses cached results
generated after the first run. These caches should not be added to version
Currently, running Mypy on GraalVM is slow, so the program uses cached results
generated after the first run. These caches should not be added to version
control (git) because they contain absolute file paths.

Although it's a Maven project, a binary launcher named `javainterfacegen` is
generated in the `bin` folder during the build process. You can build it
as part of the GraalPython project by running
Although it's a Maven project, a binary launcher named `javainterfacegen` is
generated in the `bin` folder during the build process. You can build it
as part of the GraalPython project by running
```bash
mx build
```
from the top-level graalpython repository folder. The `javainterfacegen`
executable will be located in `graal/sdk/latest_graalvm_home/bin/`.

Alternatively, you can build it directly in this repository using Maven with
Alternatively, you can build it directly in this repository using Maven with
the following command:
```bash
mvn package -DskipTests
Expand All @@ -29,4 +29,4 @@ mvn exec:java -Dexec.mainClass="org.graalvm.python.javainterfacegen.Main" -Dexec
For debugging, you need to increase the stack size:
```bash
MAVEN_OPTS="-Xss64M" mvnDebug exec:java -Dexec.mainClass="org.graalvm.python.javainterfacegen.Main" -Dexec.args="./examples/pygal/pygal.yml"
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def set_mypy_cache_folder(folder_path: str) -> None:

def extract_type_info(file_paths: list[str]):
sources, options = process_options(file_paths)


options.incremental = False
options.fine_grained_incremental=False
Expand All @@ -65,8 +65,8 @@ def extract_type_info(file_paths: list[str]):
options.skip_version_check=True
options.cache_dir= mypy_cache_dir



fscache = FileSystemCache()

result = build(sources, options=options, fscache = fscache)
Expand All @@ -75,7 +75,7 @@ def extract_type_info(file_paths: list[str]):
# print(f"{result.types}");
# print(f"{result.used_cache}");
return result

def getNodesForFile(file_path: str) -> mypy.nodes.MypyFile :
result = extract_type_info([file_path])
absPath = os.path.abspath(file_path)
Expand All @@ -90,45 +90,45 @@ def serialize_result(paths: list[str], cache_file_path:str)->dict[str, MypyFile]
result = extract_type_info(paths)

ast_dict = {file: result.files[file].serialize() for file in result.files}

directory = os.path.dirname(cache_file_path)
if directory and not os.path.exists(directory):
os.makedirs(directory)

with open(cache_file_path, 'w') as f:
json.dump(ast_dict, f, default=str, indent=2)

return result.files

def serialize_mypyfile(mypyfile, output_file:str) -> None:
def serialize_mypyfile(mypyfile, output_file:str) -> None:
import json
d = {mypyfile['name']: mypyfile}
with open(output_file, 'w') as f:
json.dump(d, f, default=str, indent=2)


def serialize_mypyfile(mypyfile:MypyFile, output_file:str) -> None:
def serialize_mypyfile(mypyfile:MypyFile, output_file:str) -> None:
import json
d = {mypyfile.fullname: mypyfile.serialize()}
d = {mypyfile.fullname: mypyfile.serialize()}
with open(output_file, 'w') as f:
json.dump(d, f, default=str, indent=2)

def serialize_mypyFile_toStr(mypyfile: MypyFile) -> str:
import json
d = {mypyfile.fullname: mypyfile.serialize()}
d = {mypyfile.fullname: mypyfile.serialize()}
return json.dumps(d, default=str, indent=2)

def serialize_ast(ast:str, output_file:str)->None:
import json
with open(output_file, 'w') as f:
json.dump(ast.serialize(), f)

def load_ast(json_filename):
import json
import time

with open(json_filename, 'r') as f:
ast_data = json.load(f)
ast_data = json.load(f)

start_time = time.time()
mypyFile = mypy.nodes.MypyFile.deserialize(ast_data)
Expand All @@ -154,10 +154,10 @@ def create_mypyfile(data:str)->dict[str, MypyFile]:
def load_result(json_filename:str, previous: dict[str, MypyFile] = None) -> dict[str, MypyFile]:
import json
import time

with open(json_filename, 'r') as f:
ast_data = json.load(f)

ast_nodes = {file: mypy.nodes.MypyFile.deserialize(ast) for file, ast in ast_data.items()}

for file, mypy_file in ast_nodes.items():
Expand Down Expand Up @@ -216,13 +216,13 @@ def test_only_posArgs(*args):
def test_only_kwArgs(**kwargs):
print("calling test_only_kwArgs(**kwargs):")
print(f" {kwargs=}")


def test_posArgs_kwArgs(*args, **kwArgs):
print("calling test_posArgs_kwArgs(*args, **kwArgs):")
print(f" {args=}")
print(f" {kwArgs=}")


def test_pos_posArgs(arg1, *args):
print("calling test_pos_posArgs(arg1, *args):")
Expand All @@ -239,7 +239,7 @@ def test_pos_posArgs_kwArgs(arg1, *args, **kwArgs):
print(f" {arg1=}")
print(f" {args=}")
print(f" {kwArgs=}")

def test_pos3_posArgs_kwArgs(arg1, arg2, arg3, *args, **kwArgs):
print("calling test_pos_posArgs_kwArgs(arg1, *args, **kwArgs):")
print(f" {arg1=}")
Expand All @@ -254,7 +254,7 @@ def test_posArgs_named_args(*args, named1, named2=10):
print(f" {args=}")
print(f" {named1=}")
print(f" {named2=}")


def test_posarg(a, b, c):
print("calling test_posarg(a, b, c):")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
org.graalvm.python.javainterfacegen.generator.impl.FunctionGeneratorImpl
org.graalvm.python.javainterfacegen.generator.impl.JustInterfacesGeneratorImpl
org.graalvm.python.javainterfacegen.generator.impl.JustInterfacesGeneratorImpl
Loading
Loading