Skip to content

Commit

Permalink
If command was not successful, exit with code 1, ENHANCEMENTS (#97)
Browse files Browse the repository at this point in the history
Enhancing the earlier (accidental) commit df63614 which already contained the core of this change
  • Loading branch information
hannahbast authored Dec 8, 2024
1 parent 11cb02b commit d4e07a5
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 85 deletions.
1 change: 1 addition & 0 deletions .github/workflows/qleverfiles-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
- name: Check that all the files in `src/qlever/Qleverfiles` parse.
working-directory: ${{github.workspace}}/qlever-control
run: |
export QLEVER_ARGCOMPLETE_ENABLED=1
for QLEVERFILE in src/qlever/Qleverfiles/Qleverfile.*; do
echo
echo -e "\x1b[1;34mChecking ${QLEVERFILE}\x1b[0m"
Expand Down
2 changes: 1 addition & 1 deletion src/qlever/Qleverfiles/Qleverfile.dblp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ FORMAT = ttl

[index]
INPUT_FILES = *.gz
MULTI_INPUT_JSON = $$(ls *.gz | xargs -I {} echo '{ "cmd": "zcat {}" }')
MULTI_INPUT_JSON = { "cmd": "zcat {}", "for-each": "*.gz" }
SETTINGS_JSON = { "ascii-prefixes-only": false, "num-triples-per-batch": 5000000, "prefixes-external": [""] }

[server]
Expand Down
3 changes: 2 additions & 1 deletion src/qlever/commands/add_text_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def execute(self, args) -> bool:
# Show the command line.
self.show(add_text_index_cmd, only_show=args.show)
if args.show:
return False
return True

# When running natively, check if the binary exists and works.
if args.system == "native":
Expand All @@ -74,6 +74,7 @@ def execute(self, args) -> bool:
log.error(f"Running \"{args.index_binary}\" failed ({e}), "
f"set `--index-binary` to a different binary or "
f"use `--container_system`")
return False

# Check if text index files already exist.
existing_text_index_files = get_existing_index_files(
Expand Down
2 changes: 1 addition & 1 deletion src/qlever/commands/cache_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def execute(self, args) -> bool:
self.show("\n".join([cache_stats_cmd, cache_settings_cmd]),
only_show=args.show)
if args.show:
return False
return True

# Execute them.
try:
Expand Down
6 changes: 4 additions & 2 deletions src/qlever/commands/clear_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def execute(self, args) -> bool:
f"\"{args.access_token}\"")
self.show(clear_cache_cmd, only_show=args.show)
if args.show:
return False
return True

# Execute the command.
try:
Expand Down Expand Up @@ -76,5 +76,7 @@ def execute(self, args) -> bool:
# Show cache stats.
log.info("")
args.detailed = False
CacheStatsCommand().execute(args)
if not CacheStatsCommand().execute(args):
log.error("Clearing the cache was successful, but showing the "
"cache stats failed {e}")
return True
7 changes: 5 additions & 2 deletions src/qlever/commands/example_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def execute(self, args) -> bool:
only_show=args.show,
)
if args.show:
return False
return True

# Get the example queries.
try:
Expand Down Expand Up @@ -229,8 +229,11 @@ def execute(self, args) -> bool:
if args.clear_cache == "yes":
args.server_url = sparql_endpoint
args.complete = False
clear_cache_successful = False
with mute_log():
ClearCacheCommand().execute(args)
clear_cache_successful = ClearCacheCommand().execute(args)
if not clear_cache_successful:
log.warn("Failed to clear the cache")

# Remove OFFSET and LIMIT (after the last closing bracket).
if args.remove_offset_and_limit or args.limit:
Expand Down
2 changes: 1 addition & 1 deletion src/qlever/commands/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def execute(self, args) -> bool:
# Construct the command line and show it.
self.show(args.get_data_cmd, only_show=args.show)
if args.show:
return False
return True

# Execute the command line.
try:
Expand Down
2 changes: 1 addition & 1 deletion src/qlever/commands/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def execute(self, args) -> bool:
# Show the command line.
self.show(f"{settings_json_cmd}\n{index_cmd}", only_show=args.show)
if args.show:
return False
return True

# When running natively, check if the binary exists and works.
if args.system == "native":
Expand Down
149 changes: 90 additions & 59 deletions src/qlever/commands/index_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,45 @@ def __init__(self):
pass

def description(self) -> str:
return ("Breakdown of the time and space used for the index build")
return "Breakdown of the time and space used for the index build"

def should_have_qleverfile(self) -> bool:
return False

def relevant_qleverfile_arguments(self) -> dict[str: list[str]]:
def relevant_qleverfile_arguments(self) -> dict[str : list[str]]:
return {"data": ["name"]}

def additional_arguments(self, subparser) -> None:
subparser.add_argument("--only-time", action="store_true",
default=False,
help="Show only the time used")
subparser.add_argument("--only-space", action="store_true",
default=False,
help="Show only the space used")
subparser.add_argument("--ignore-text-index", action="store_true",
default=False,
help="Ignore the text index")
subparser.add_argument("--time-unit",
choices=["s", "min", "h", "auto"],
default="auto",
help="The time unit")
subparser.add_argument("--size-unit",
choices=["B", "MB", "GB", "TB", "auto"],
default="auto",
help="The size unit")
subparser.add_argument(
"--only-time",
action="store_true",
default=False,
help="Show only the time used",
)
subparser.add_argument(
"--only-space",
action="store_true",
default=False,
help="Show only the space used",
)
subparser.add_argument(
"--ignore-text-index",
action="store_true",
default=False,
help="Ignore the text index",
)
subparser.add_argument(
"--time-unit",
choices=["s", "min", "h", "auto"],
default="auto",
help="The time unit",
)
subparser.add_argument(
"--size-unit",
choices=["B", "MB", "GB", "TB", "auto"],
default="auto",
help="The size unit",
)

def execute_time(self, args, log_file_name) -> bool:
"""
Expand All @@ -65,8 +78,9 @@ def execute_time(self, args, log_file_name) -> bool:
with open(text_log_file_name, "r") as text_log_file:
lines.extend(text_log_file.readlines())
except Exception as e:
log.error(f"Problem reading text index log file "
f"{text_log_file_name}: {e}")
log.error(
f"Problem reading text index log file " f"{text_log_file_name}: {e}"
)
return False

# Helper function that finds the next line matching the given `regex`,
Expand Down Expand Up @@ -95,12 +109,14 @@ def find_next_line(regex, update_current_line=True):
if regex_match:
try:
return datetime.strptime(
re.match(timestamp_regex, line).group(),
timestamp_format), regex_match
re.match(timestamp_regex, line).group(), timestamp_format
), regex_match
except Exception as e:
log.error(f"Could not parse timestamp of form "
f"\"{timestamp_regex}\" from line "
f" \"{line.rstrip()}\" ({e})")
log.error(
f"Could not parse timestamp of form "
f'"{timestamp_regex}" from line '
f' "{line.rstrip()}" ({e})'
)
# If we get here, we did not find a matching line.
if not update_current_line:
current_line = current_line_backup
Expand All @@ -119,26 +135,32 @@ def find_next_line(regex, update_current_line=True):
# file (old format: "Creating a pair" + names of permutations in
# line "Writing meta data for ..."; new format: name of
# permutations already in line "Creating permutations ...").
perm_begin, _ = find_next_line(r"INFO:\s*Creating a pair",
update_current_line=False)
perm_begin, _ = find_next_line(
r"INFO:\s*Creating a pair", update_current_line=False
)
if perm_begin is None:
perm_begin, perm_info = find_next_line(
r"INFO:\s*Creating permutations ([A-Z]+ and [A-Z]+)",
update_current_line=False)
update_current_line=False,
)
else:
_, perm_info = find_next_line(
r"INFO:\s*Writing meta data for ([A-Z]+ and [A-Z]+)",
update_current_line=False)
update_current_line=False,
)
if perm_info is None:
break
perm_begin_and_info.append((perm_begin, perm_info))
convert_end = (perm_begin_and_info[0][0] if
len(perm_begin_and_info) > 0 else None)
convert_end = (
perm_begin_and_info[0][0] if len(perm_begin_and_info) > 0 else None
)
normal_end, _ = find_next_line(r"INFO:\s*Index build completed")
text_begin, _ = find_next_line(r"INFO:\s*Adding text index",
update_current_line=False)
text_end, _ = find_next_line(r"INFO:\s*Text index build comp",
update_current_line=False)
text_begin, _ = find_next_line(
r"INFO:\s*Adding text index", update_current_line=False
)
text_end, _ = find_next_line(
r"INFO:\s*Text index build comp", update_current_line=False
)
if args.ignore_text_index:
text_begin = text_end = None

Expand All @@ -147,9 +169,11 @@ def find_next_line(regex, update_current_line=True):
log.error("Missing line that index build has started")
return False
if overall_begin and not merge_begin:
log.error("According to the log file, the index build "
"has started, but is still in its first "
"phase (parsing the input)")
log.error(
"According to the log file, the index build "
"has started, but is still in its first "
"phase (parsing the input)"
)
return False

# Helper function that shows the duration for a phase (if the start and
Expand Down Expand Up @@ -187,22 +211,24 @@ def show_duration(heading, start_end_pairs):
show_duration("Convert to global IDs", [(convert_begin, convert_end)])
for i in range(len(perm_begin_and_info)):
perm_begin, perm_info = perm_begin_and_info[i]
perm_end = perm_begin_and_info[i + 1][0] if i + 1 < len(
perm_begin_and_info) else normal_end
perm_info_text = (perm_info.group(1).replace(" and ", " & ")
if perm_info else f"#{i + 1}")
show_duration(f"Permutation {perm_info_text}",
[(perm_begin, perm_end)])
perm_end = (
perm_begin_and_info[i + 1][0]
if i + 1 < len(perm_begin_and_info)
else normal_end
)
perm_info_text = (
perm_info.group(1).replace(" and ", " & ") if perm_info else f"#{i + 1}"
)
show_duration(f"Permutation {perm_info_text}", [(perm_begin, perm_end)])
show_duration("Text index", [(text_begin, text_end)])
if text_begin and text_end:
log.info("")
show_duration("TOTAL time",
[(overall_begin, normal_end),
(text_begin, text_end)])
show_duration(
"TOTAL time", [(overall_begin, normal_end), (text_begin, text_end)]
)
elif normal_end:
log.info("")
show_duration("TOTAL time",
[(overall_begin, normal_end)])
show_duration("TOTAL time", [(overall_begin, normal_end)])
return True

def execute_space(self, args) -> bool:
Expand Down Expand Up @@ -252,24 +278,29 @@ def show_size(heading, size):
return True

def execute(self, args) -> bool:
ret_value = args.show
return_value = True

# The "time" part of the command.
if not args.only_space:
log_file_name = f"{args.name}.index-log.txt"
self.show(f"Breakdown of the time used for "
f"building the index, based on the timestamps for key "
f"lines in \"{log_file_name}\"", only_show=args.show)
self.show(
f"Breakdown of the time used for "
f"building the index, based on the timestamps for key "
f'lines in "{log_file_name}"',
only_show=args.show,
)
if not args.show:
ret_value &= self.execute_time(args, log_file_name)
return_value &= self.execute_time(args, log_file_name)
if not args.only_time:
log.info("")

# The "space" part of the command.
if not args.only_time:
self.show("Breakdown of the space used for building the index",
only_show=args.show)
self.show(
"Breakdown of the space used for building the index",
only_show=args.show,
)
if not args.show:
ret_value &= self.execute_space(args)
return_value &= self.execute_space(args)

return ret_value
return return_value
14 changes: 12 additions & 2 deletions src/qlever/commands/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@ def execute(self, args) -> bool:
log_cmd += f" {log_file}"
self.show(log_cmd, only_show=args.show)
if args.show:
return False
return True

# Execute the command.
log.info(f"Follow log file {log_file}, press Ctrl-C to stop"
f" following (will not stop the server)")
log.info("")
subprocess.run(log_cmd, shell=True)
try:
subprocess.run(log_cmd, shell=True)
return True
except Exception as e:
log.error(e)
return False





2 changes: 1 addition & 1 deletion src/qlever/commands/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def execute(self, args) -> bool:
f" --data-urlencode query={shlex.quote(args.query)}")
self.show(curl_cmd, only_show=args.show)
if args.show:
return False
return True

# Launch query.
try:
Expand Down
2 changes: 1 addition & 1 deletion src/qlever/commands/setup_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def execute(self, args) -> bool:
setup_config_cmd += "> Qleverfile"
self.show(setup_config_cmd, only_show=args.show)
if args.show:
return False
return True

# If there is already a Qleverfile in the current directory, exit.
qleverfile_path = Path("Qleverfile")
Expand Down
Loading

0 comments on commit d4e07a5

Please sign in to comment.