Skip to content

Instantly share code, notes, and snippets.

@Rexagon
Created August 10, 2024 13:52
Show Gist options
  • Save Rexagon/69cc05dbb6733792ecbe6ecaa8714296 to your computer and use it in GitHub Desktop.
Save Rexagon/69cc05dbb6733792ecbe6ecaa8714296 to your computer and use it in GitHub Desktop.
import os
def process_file(file_path):
if "test" in file_path or "network" in file_path or "target" in file_path:
return
if not file_path.endswith(".rs"):
return
modified_lines = []
with open(file_path, "r", encoding="utf-8") as file:
lines = file.readlines()
modified = False
for line in lines:
stripped_line = line.strip()
if (
".lock()" in stripped_line
or ".lock.await()" in stripped_line
and line.endswith(";")
):
modified_lines.append("let _timer_start = ::std::time::Instant::now();\n")
modified_lines.append(line)
modified_lines.append("let _TIMER_ELAPSED = _timer_start.elapsed();\n")
modified_lines.append("if _TIMER_ELAPSED.as_millis() > 1000 {\n")
modified_lines.append(
' tracing::warn!("Lock took : {:?}", _TIMER_ELAPSED);\n'
)
modified_lines.append("}\n")
modified = True
else:
modified_lines.append(line)
if modified:
with open(file_path, "w", encoding="utf-8") as file:
file.writelines(modified_lines)
def process_directory(directory):
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
process_file(file_path)
# python scripts/wrap_mutex.py
if __name__ == "__main__":
directory_path = "."
process_directory(directory_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment