Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Tidy up expire.py

Russ Garrette58136ec3724
diff --git a/tegola/expire.py b/tegola/expire.py
index c3b310a..c687521 100644
--- a/tegola/expire.py
+++ b/tegola/expire.py
@@ -1,11 +1,14 @@
-# expire.py - reads expiry files output by Imposm and tells Tegola to purge the affected tiles
-# Expiry files are output by the `-expiretiles-dir` Imposm option.
+"""
+Watches for expiry files output by Imposm and tells Tegola to purge the affected tiles.
+Expiry files are output by the `-expiretiles-dir` Imposm option.
+"""
from pathlib import Path
import subprocess
import os
import click
from inotify.adapters import InotifyTree
import logging
+from datetime import datetime
log = logging.getLogger(__name__)
@@ -33,14 +36,29 @@ def expire(tile_list: Path, tegola_config: str, dry_run: bool):
os.remove(tile_list)
+def expire_path(expire_dir: Path, tegola_config: str, dry_run: bool):
+ path_list = expire_dir.glob("**/*.tiles")
+ for tile_list in path_list:
+ expire(tile_list, tegola_config, dry_run)
+
+
+def current_dir() -> str:
+ return datetime.now().strftime("%Y%m%d")
+
+
def clean_empty_dirs(expire_dir: Path, dry_run: bool):
+ """Delete empty directories, except the one for the current day."""
log.info("Cleaning empty directories in %s", expire_dir)
- if dry_run:
- return
+
for path in expire_dir.iterdir():
- if path.is_dir() and not any(path.iterdir()):
+ if (
+ path.is_dir()
+ and not any(path.iterdir())
+ and path.parts[-1] != current_dir()
+ ):
print("Removing directory", path)
- path.rmdir()
+ if not dry_run:
+ path.rmdir()
@click.command()
@@ -50,24 +68,19 @@ def clean_empty_dirs(expire_dir: Path, dry_run: bool):
def main(expire_dir, tegola_config, dry_run):
log.info("Starting...")
expire_dir = Path(expire_dir)
-
- path_list = expire_dir.glob("**/*.tiles")
- for tile_list in path_list:
- expire(tile_list, tegola_config, dry_run)
-
+ expire_path(expire_dir, tegola_config, dry_run)
clean_empty_dirs(expire_dir, dry_run)
log.info("Watching for new changes...")
inotify = InotifyTree(str(expire_dir))
event_count = 0
for _, type_names, path, filename in inotify.event_gen(yield_nones=False):
- log.info("filename: %s, path: %s, type_names: %s", filename, path, type_names)
if not ("IN_MOVED_TO" in type_names and filename.endswith(".tiles")):
continue
- log.debug("Received IN_MOVED_TO for tile file %s, path %s", filename, path)
-
- expire(Path(path) / filename, tegola_config, dry_run)
+ log.info("Received IN_MOVED_TO for tile file %s", filename)
+ # Still look for every file in the path in case we've missed any inotify events.
+ expire_path(expire_dir)
# Cleanup empty dirs on every 10th event
if event_count % 10 == 0: