Skip to content

Commit 10a090a

Browse files
authoredNov 21, 2023
Merge pull request #16 from jcmuel/fix/add_timeout_error
Add TimeoutError, which is not covered by URLError and HTTPError.
2 parents 3e50cf1 + ee5c134 commit 10a090a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed
 

‎antlr4_tool_runner.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import jdk # requires install-jdk package
1313

14-
1514
mvn_repo: str
1615
homedir: Path
1716

@@ -24,15 +23,16 @@ def initialize_paths():
2423

2524
def latest_version():
2625
try:
27-
with urlopen(f"https://search.maven.org/solrsearch/select?q=a:antlr4-master+g:org.antlr", timeout=10) as response:
26+
with urlopen(f"https://search.maven.org/solrsearch/select?q=a:antlr4-master+g:org.antlr",
27+
timeout=10) as response:
2828
s = response.read().decode("UTF-8")
2929
searchResult = json.loads(s)['response']
3030
# searchResult = s.json()['response']
3131
antlr_info = searchResult['docs'][0]
3232
# print(json.dump(searchResult))
3333
latest = antlr_info['latestVersion']
3434
return latest
35-
except (error.URLError, error.HTTPError) as e:
35+
except (error.URLError, error.HTTPError, TimeoutError):
3636
print("Could not get latest version number, attempting to fall back to latest downloaded version...")
3737
version_dirs = list(filter(lambda directory: re.match(r"[0-9]+\.[0-9]+\.[0-9]+", directory), os.listdir(mvn_repo)))
3838
version_dirs.sort(reverse=True)
@@ -43,6 +43,7 @@ def latest_version():
4343
print(f"Found version '{latest_version_dir}', this version may be out of date")
4444
return latest_version_dir
4545

46+
4647
def antlr4_jar(version):
4748
jar = os.path.join(mvn_repo, version, f'antlr4-{version}-complete.jar')
4849
if not os.path.exists(jar):
@@ -53,11 +54,12 @@ def antlr4_jar(version):
5354
def download_antlr4(jar, version):
5455
s = None
5556
try:
56-
with urlopen(f"https://repo1.maven.org/maven2/org/antlr/antlr4/{version}/antlr4-{version}-complete.jar", timeout=60) as response:
57+
with urlopen(f"https://repo1.maven.org/maven2/org/antlr/antlr4/{version}/antlr4-{version}-complete.jar",
58+
timeout=60) as response:
5759
print(f"Downloading antlr4-{version}-complete.jar")
5860
os.makedirs(os.path.join(mvn_repo, version), exist_ok=True)
5961
s = response.read()
60-
except (error.URLError, error.HTTPError) as e:
62+
except (error.URLError, error.HTTPError, TimeoutError):
6163
print(f"Could not find antlr4-{version}-complete.jar at maven.org")
6264

6365
if s is None:
@@ -86,7 +88,7 @@ def install_jre(java_version='11'):
8688
return java
8789

8890
r = input(f"ANTLR tool needs Java to run; install Java JRE 11 yes/no (default yes)? ")
89-
if r.strip().lower() not in {'yes','y',''}:
91+
if r.strip().lower() not in {'yes', 'y', ''}:
9092
exit(1)
9193
install_dir = jdk.install(java_version, jre=True)
9294
print(f"Installed Java in {install_dir}; remove that dir to uninstall")
@@ -118,12 +120,12 @@ def process_args():
118120
parser = argparse.ArgumentParser(
119121
add_help=False, usage="%(prog)s [-v VERSION] [%(prog)s options]"
120122
)
121-
# Note, that the space after `-v` is needed so we don't pick up other arguments begining with `v`, like `-visitor`
123+
# Note, that the space after `-v` is needed, so we don't pick up other arguments beginning with `v`, like `-visitor`
122124
parser.add_argument("-v ", dest="version", default=None)
123125
args, unparsed_args = parser.parse_known_args()
124126

125127
return unparsed_args, (
126-
args.version or os.environ.get("ANTLR4_TOOLS_ANTLR_VERSION") or latest_version()
128+
args.version or os.environ.get("ANTLR4_TOOLS_ANTLR_VERSION") or latest_version()
127129
)
128130

129131

@@ -132,7 +134,7 @@ def run_cli(entrypoint):
132134
args, version = process_args()
133135
jar, java = install_jre_and_antlr(version)
134136

135-
cp = subprocess.run([java, '-cp', jar, entrypoint]+args)
137+
cp = subprocess.run([java, '-cp', jar, entrypoint] + args)
136138
sys.exit(cp.returncode)
137139

138140

0 commit comments

Comments
 (0)