Skip to content

Commit b259142

Browse files
authored
Merge pull request #759 from hx2A/installjdk
Install jdk tools, and `python -m py5_tools` feature
2 parents faf8b3f + cd932d6 commit b259142

10 files changed

Lines changed: 193 additions & 14 deletions

py5-resources/py5-module/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ py5-utils = "py5_tools.tools.py5utils:main"
6666
run_sketch = "py5_tools.tools.run_sketch:main"
6767
py5-run-sketch = "py5_tools.tools.run_sketch:main"
6868
py5-live-coding = "py5_tools.tools.live_coding:main"
69+
py5-install-jdk = "py5_tools.tools.install_jdk:main"
6970

7071
[project.urls]
7172
"Bug Tracker" = "https://github.com/py5coding/py5generator/issues"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# *****************************************************************************
2+
#
3+
# Part of the py5 library
4+
# Copyright (C) 2020-2026 Jim Schmitz
5+
#
6+
# This library is free software: you can redistribute it and/or modify it
7+
# under the terms of the GNU Lesser General Public License as published by
8+
# the Free Software Foundation, either version 2.1 of the License, or (at
9+
# your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful, but
12+
# WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
14+
# General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with this library. If not, see <https://www.gnu.org/licenses/>.
18+
#
19+
# *****************************************************************************
20+
import importlib
21+
import sys
22+
23+
# Map the clean CLI subcommand to the actual module name in py5_tools.tools
24+
TOOLS_MAP = {
25+
"cmd": "py5cmd",
26+
"translate-imported2module": "py5translate_imported2module",
27+
"translate-module2imported": "py5translate_module2imported",
28+
"translate-processingpy2imported": "py5translate_processingpy2imported",
29+
"utils": "py5utils",
30+
"run-sketch": "run_sketch",
31+
"live-coding": "live_coding",
32+
"install-jdk": "install_jdk",
33+
}
34+
35+
36+
def main():
37+
args = sys.argv[1:]
38+
39+
# Check if the user is asking for general help or ran without arguments
40+
if not args or args[0] in {"-h", "--help"}:
41+
print("Usage: python -m py5_tools <command> [args...]\n")
42+
print("Available commands:")
43+
for cmd in TOOLS_MAP:
44+
print(f" {cmd}")
45+
sys.exit(0)
46+
47+
# Get the first argument to figure out what the user wants
48+
command = args[0]
49+
remaining_args = args[1:]
50+
51+
if command not in TOOLS_MAP:
52+
print(f"Error: Unknown command '{command}'\n", file=sys.stderr)
53+
print("Available commands:", file=sys.stderr)
54+
for cmd in TOOLS_MAP:
55+
print(f" {cmd}", file=sys.stderr)
56+
sys.exit(1)
57+
58+
module_name = TOOLS_MAP[command]
59+
full_module_name = f"py5_tools.tools.{module_name}"
60+
61+
try:
62+
# Dynamically load the requested tool module
63+
module = importlib.import_module(full_module_name)
64+
except ImportError as e:
65+
print(f"Error loading {full_module_name}: {e}", file=sys.stderr)
66+
sys.exit(1)
67+
68+
# Import the argparse instance from the module
69+
if not hasattr(module, "parser"):
70+
print(
71+
f"Error: {full_module_name} does not expose a 'parser' instance.",
72+
file=sys.stderr,
73+
)
74+
sys.exit(1)
75+
76+
# Use the instance to parse the remaining command line arguments
77+
parsed_args = module.parser.parse_args(remaining_args)
78+
79+
if hasattr(module, "main"):
80+
module.main(parsed_args)
81+
else:
82+
print(
83+
f"Error: {full_module_name} does not have a 'main' function.",
84+
file=sys.stderr,
85+
)
86+
sys.exit(1)
87+
88+
89+
if __name__ == "__main__":
90+
main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# *****************************************************************************
2+
#
3+
# Part of the py5 library
4+
# Copyright (C) 2020-2026 Jim Schmitz
5+
#
6+
# This library is free software: you can redistribute it and/or modify it
7+
# under the terms of the GNU Lesser General Public License as published by
8+
# the Free Software Foundation, either version 2.1 of the License, or (at
9+
# your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful, but
12+
# WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
14+
# General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with this library. If not, see <https://www.gnu.org/licenses/>.
18+
#
19+
# *****************************************************************************
20+
import argparse
21+
import sys
22+
23+
parser = argparse.ArgumentParser(description="Install Java Development Kit")
24+
parser.add_argument(
25+
"-j",
26+
"--java-version",
27+
action="store",
28+
dest="java_version",
29+
default=21,
30+
type=int,
31+
help="Java Version (must be 17 or greater, defaults to 21)",
32+
)
33+
parser.add_argument(
34+
"--jre",
35+
action="store_true",
36+
dest="jre",
37+
default=False,
38+
help="Install Java Runtime Environment (JRE) instead of Java Development Kit (JDK)",
39+
)
40+
41+
42+
def main(args=None):
43+
args = args or parser.parse_args()
44+
45+
try:
46+
import jdk
47+
except ImportError:
48+
print(
49+
"Please first install the python library `install-jdk` using the command `pip install install-jdk`",
50+
file=sys.stderr,
51+
)
52+
return
53+
54+
java_version = args.java_version
55+
jre = args.jre
56+
57+
installing = "Java Runtime Environment" if jre else "Java Development Kit"
58+
59+
if java_version < 17:
60+
print(
61+
f"Java version must be 17 or greater, please specify a different version using the -j option"
62+
)
63+
return
64+
65+
try:
66+
print(f"Installing {installing} version {java_version}...")
67+
print(
68+
f"{installing} version {java_version} installed to {jdk.install(java_version, jre=jre)}"
69+
)
70+
except jdk.JdkError as e:
71+
print(
72+
f"Failed to install {installing} version {java_version}: {e}",
73+
file=sys.stderr,
74+
)
75+
print(
76+
"Make sure you have a working internet and that have not installed this version of Java already.",
77+
file=sys.stderr,
78+
)
79+
80+
81+
if __name__ == "__main__":
82+
main()

py5-resources/py5-module/src/py5_tools/tools/live_coding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
)
7575

7676

77-
def main():
78-
args = parser.parse_args()
77+
def main(args=None):
78+
args = args or parser.parse_args()
7979

8080
sketch_path = Path(args.sketch_path).resolve()
8181
archive_dir = Path(args.archive_dir).resolve()

py5-resources/py5-module/src/py5_tools/tools/py5cmd.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ def postloop(self):
169169
print()
170170

171171

172-
def main():
173-
args = parser.parse_args()
172+
def main(args=None):
173+
args = args or parser.parse_args()
174+
174175
py5cmd = Py5Cmd()
175176
py5cmd.cmdloop()
176177

py5-resources/py5-module/src/py5_tools/tools/py5translate_imported2module.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
parser.add_argument(action="store", dest="dest", help="path to module mode code")
3030

3131

32-
def main():
33-
args = parser.parse_args()
32+
def main(args=None):
33+
args = args or parser.parse_args()
34+
3435
src = Path(args.src)
3536
dest = Path(args.dest)
3637

py5-resources/py5-module/src/py5_tools/tools/py5translate_module2imported.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
parser.add_argument(action="store", dest="dest", help="path to imported mode code")
3030

3131

32-
def main():
33-
args = parser.parse_args()
32+
def main(args=None):
33+
args = args or parser.parse_args()
34+
3435
src = Path(args.src)
3536
dest = Path(args.dest)
3637

py5-resources/py5-module/src/py5_tools/tools/py5translate_processingpy2imported.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
parser.add_argument(action="store", dest="dest", help="path to imported mode code")
3030

3131

32-
def main():
33-
args = parser.parse_args()
32+
def main(args=None):
33+
args = args or parser.parse_args()
34+
3435
src = Path(args.src)
3536
dest = Path(args.dest)
3637

py5-resources/py5-module/src/py5_tools/tools/py5utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
)
3232

3333

34-
def main():
35-
args = parser.parse_args()
34+
def main(args=None):
35+
args = args or parser.parse_args()
36+
3637
py5_tools.utilities.generate_utilities_framework(args.output_dir)
3738

3839

py5-resources/py5-module/src/py5_tools/tools/run_sketch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@
5858
)
5959

6060

61-
def main():
62-
args = parser.parse_args()
61+
def main(args=None):
62+
args = args or parser.parse_args()
63+
6364
imported.run_code(
6465
args.sketch_path,
6566
classpath=args.classpath,

0 commit comments

Comments
 (0)