wait.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import time
  2. from datetime import datetime, timedelta
  3. from pathlib import Path
  4. import typer
  5. def main(filename: str, tolerance: int = 12, duration: int = 6):
  6. now_ts = datetime.now()
  7. max_ts = now_ts + timedelta(hours=duration)
  8. file_mtime_ts = datetime.fromtimestamp(Path(filename).stat().st_mtime)
  9. print(f"Warte auf Datei {filename}")
  10. print("Aktuelle Uhrzeit: " + now_ts.strftime("%d.%m.%Y %H:%M:%S"))
  11. print("Aenderungsdatum: " + file_mtime_ts.strftime("%d.%m.%Y %H:%M:%S"))
  12. print("Maximale Wartezeit: " + max_ts.strftime("%d.%m.%Y %H:%M:%S"))
  13. print("")
  14. while now_ts < max_ts:
  15. now_ts = datetime.now()
  16. file_mtime_ts = datetime.fromtimestamp(Path(filename).stat().st_mtime)
  17. oldest_ts = now_ts - timedelta(hours=tolerance)
  18. newest_ts = now_ts - timedelta(minutes=10)
  19. if file_mtime_ts >= oldest_ts and file_mtime_ts <= newest_ts:
  20. print(f"\nDatei {filename} ist bereit")
  21. return
  22. print(".", end="")
  23. time.sleep(60)
  24. print(f"\n!! Datei {filename} ist nicht aktuell. Warten beendet !!")
  25. exit(1)
  26. if __name__ == "__main__":
  27. typer.run(main)