prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Cron job that cleans hf-datasets cache often fails
  > Description: Job datasets-server/prod-datasets-server-job-clean-hf-datasets-cache-28426140 failed to complete. Removing failed job after investigation should clear this alert.
  
  https://huggingface.slack.com/archives/C05UE1R5ZBJ/p1705571880300879
  
  
  
  </issue>
  <code>
  [start of jobs/cache_maintenance/src/cache_maintenance/clean_directory.py]
  1 # SPDX-License-Identifier: Apache-2.0
  2 # Copyright 2023 The HuggingFace Authors.
  3 
  4 import glob
  5 import logging
  6 import os
  7 import shutil
  8 from datetime import datetime, timedelta
  9 from typing import Any
  10 
  11 
  12 def clean_directory(pattern: str, expired_time_interval_seconds: int) -> None:
  13     """
  14     Delete temporary cache directories that match the given pattern.
  15     """
  16     # sanity check
  17     if len(str(pattern)) < 10:
  18         raise RuntimeError(f"Sanity check on pattern failed: len('{pattern}') < 10.")
  19     logging.info(f"looking for all files and directories with pattern {pattern}")
  20     now = datetime.now().replace(tzinfo=None)
  21     errors = 0
  22     total_dirs = 0
  23     total_files = 0
  24 
  25     def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=
  26         logging.error(f"failed to delete directory {path=}")
  27 
  28     for path in glob.glob(pattern):
  29         last_access_time_value = os.path.getatime(path)
  30         last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)
  31         if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:
  32             if os.path.isfile(path):
  33                 logging.info(f"deleting file {path=} {last_access_datetime=}")
  34                 os.remove(path)
  35                 total_files += 1
  36             elif os.path.isdir(path):
  37                 logging.info(f"deleting directory {path=} {last_access_datetime=}")
  38                 shutil.rmtree(path, onerror=rmtree_on_error)
  39                 total_dirs += 1
  40                 errors += os.path.isdir(path)
  41     if errors:
  42         logging.error(f"clean_directory failed to remove {errors} directories at the root of the cache directory.")
  43     if total_files:
  44         logging.info(f"clean_directory removed {total_files} files at the root of the cache directory.")
  45 
  46     logging.info(f"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.")
  47
  [end of jobs/cache_maintenance/src/cache_maintenance/clean_directory.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py
  --- a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py
  +++ b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py
  @@ -21,26 +21,35 @@
       errors = 0
       total_dirs = 0
       total_files = 0
  +    total_disappeared_paths = 0
   
       def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=
           logging.error(f"failed to delete directory {path=}")
   
       for path in glob.glob(pattern):
  -        last_access_time_value = os.path.getatime(path)
  -        last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)
  -        if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:
  -            if os.path.isfile(path):
  -                logging.info(f"deleting file {path=} {last_access_datetime=}")
  -                os.remove(path)
  -                total_files += 1
  -            elif os.path.isdir(path):
  -                logging.info(f"deleting directory {path=} {last_access_datetime=}")
  -                shutil.rmtree(path, onerror=rmtree_on_error)
  -                total_dirs += 1
  -                errors += os.path.isdir(path)
  +        try:
  +            last_access_time_value = os.path.getatime(path)
  +            last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)
  +            if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:
  +                if os.path.isfile(path):
  +                    logging.info(f"deleting file {path=} {last_access_datetime=}")
  +                    os.remove(path)
  +                    total_files += 1
  +                elif os.path.isdir(path):
  +                    logging.info(f"deleting directory {path=} {last_access_datetime=}")
  +                    shutil.rmtree(path, onerror=rmtree_on_error)
  +                    total_dirs += 1
  +                    errors += os.path.isdir(path)
  +        except FileNotFoundError:
  +            logging.error(f"failed to delete {path=} because it has disappeared during the loop")
  +            total_disappeared_paths += 1
       if errors:
           logging.error(f"clean_directory failed to remove {errors} directories at the root of the cache directory.")
       if total_files:
           logging.info(f"clean_directory removed {total_files} files at the root of the cache directory.")
  +    if total_disappeared_paths:
  +        logging.info(
  +            f"clean_directory failed to delete {total_disappeared_paths} paths because they disappeared during the loop."
  +        )
   
       logging.info(f"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.")
  
verification_info: {"golden_diff": "diff --git a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py\n--- a/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py\n+++ b/jobs/cache_maintenance/src/cache_maintenance/clean_directory.py\n@@ -21,26 +21,35 @@\n     errors = 0\n     total_dirs = 0\n     total_files = 0\n+    total_disappeared_paths = 0\n \n     def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=\n         logging.error(f\"failed to delete directory {path=}\")\n \n     for path in glob.glob(pattern):\n-        last_access_time_value = os.path.getatime(path)\n-        last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n-        if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n-            if os.path.isfile(path):\n-                logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n-                os.remove(path)\n-                total_files += 1\n-            elif os.path.isdir(path):\n-                logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n-                shutil.rmtree(path, onerror=rmtree_on_error)\n-                total_dirs += 1\n-                errors += os.path.isdir(path)\n+        try:\n+            last_access_time_value = os.path.getatime(path)\n+            last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n+            if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n+                if os.path.isfile(path):\n+                    logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n+                    os.remove(path)\n+                    total_files += 1\n+                elif os.path.isdir(path):\n+                    logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n+                    shutil.rmtree(path, onerror=rmtree_on_error)\n+                    total_dirs += 1\n+                    errors += os.path.isdir(path)\n+        except FileNotFoundError:\n+            logging.error(f\"failed to delete {path=} because it has disappeared during the loop\")\n+            total_disappeared_paths += 1\n     if errors:\n         logging.error(f\"clean_directory failed to remove {errors} directories at the root of the cache directory.\")\n     if total_files:\n         logging.info(f\"clean_directory removed {total_files} files at the root of the cache directory.\")\n+    if total_disappeared_paths:\n+        logging.info(\n+            f\"clean_directory failed to delete {total_disappeared_paths} paths because they disappeared during the loop.\"\n+        )\n \n     logging.info(f\"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.\")\n", "issue": "Cron job that cleans hf-datasets cache often fails\n> Description: Job datasets-server/prod-datasets-server-job-clean-hf-datasets-cache-28426140 failed to complete. Removing failed job after investigation should clear this alert.\r\n\r\nhttps://huggingface.slack.com/archives/C05UE1R5ZBJ/p1705571880300879\r\n\r\n\n", "before_files": [{"content": "# SPDX-License-Identifier: Apache-2.0\n# Copyright 2023 The HuggingFace Authors.\n\nimport glob\nimport logging\nimport os\nimport shutil\nfrom datetime import datetime, timedelta\nfrom typing import Any\n\n\ndef clean_directory(pattern: str, expired_time_interval_seconds: int) -> None:\n    \"\"\"\n    Delete temporary cache directories that match the given pattern.\n    \"\"\"\n    # sanity check\n    if len(str(pattern)) < 10:\n        raise RuntimeError(f\"Sanity check on pattern failed: len('{pattern}') < 10.\")\n    logging.info(f\"looking for all files and directories with pattern {pattern}\")\n    now = datetime.now().replace(tzinfo=None)\n    errors = 0\n    total_dirs = 0\n    total_files = 0\n\n    def rmtree_on_error(function: Any, path: str, excinfo: Any) -> None:  # noqa: ARG001, args needed for onerror=\n        logging.error(f\"failed to delete directory {path=}\")\n\n    for path in glob.glob(pattern):\n        last_access_time_value = os.path.getatime(path)\n        last_access_datetime = datetime.fromtimestamp(last_access_time_value).replace(tzinfo=None)\n        if last_access_datetime + timedelta(seconds=expired_time_interval_seconds) <= now:\n            if os.path.isfile(path):\n                logging.info(f\"deleting file {path=} {last_access_datetime=}\")\n                os.remove(path)\n                total_files += 1\n            elif os.path.isdir(path):\n                logging.info(f\"deleting directory {path=} {last_access_datetime=}\")\n                shutil.rmtree(path, onerror=rmtree_on_error)\n                total_dirs += 1\n                errors += os.path.isdir(path)\n    if errors:\n        logging.error(f\"clean_directory failed to remove {errors} directories at the root of the cache directory.\")\n    if total_files:\n        logging.info(f\"clean_directory removed {total_files} files at the root of the cache directory.\")\n\n    logging.info(f\"clean_directory removed {total_dirs - errors} directories at the root of the cache directory.\")\n", "path": "jobs/cache_maintenance/src/cache_maintenance/clean_directory.py"}]}