19 from typing
import List, Iterator
20 from pathlib
import Path
21 from contextlib
import contextmanager
25 from watchdog.events
import FileSystemEventHandler, FileSystemEvent
26 from watchdog.observers
import Observer
28 from ...entities
import TaskRun
31 IGNORED_FILES = [
"_coretex.py"]
34 class FileEventHandler(FileSystemEventHandler):
36 def __init__(self) -> None:
39 self.artifactPaths: List[Path] = []
41 def on_created(self, event: FileSystemEvent) ->
None:
42 if event.is_directory:
45 filePath = Path(event.src_path)
47 if filePath.parent.joinpath(
".coretexignore").exists():
50 if filePath.name
in IGNORED_FILES:
53 logging.getLogger(
"coretex").debug(f
">> [Coretex] File created at path \"{filePath}\", adding to artifacts list")
54 self.artifactPaths.append(filePath)
58 def track(taskRun: TaskRun) -> Iterator[FileEventHandler]:
60 root = Path.cwd()
if taskRun.isLocal
else taskRun.taskPath
64 observer.setName(
"ArtifactTracker")
66 logging.getLogger(
"coretexpylib").debug(f
">> [Coretex] Tracking files created inside \"{root}\"")
68 eventHandler = FileEventHandler()
69 observer.schedule(eventHandler, root, recursive =
True)
73 if "inotify watch limit reached" in str(e):
74 logging.getLogger(
"coretexpylib").error(
75 f
"{e}. Consider increasing the inotify limit by putting \"fs.inotify.max_user_watches=524288\" \
76 (or any other value you prefer) into your sysctl settings"
79 logging.getLogger(
"coretexpylib").error(f
"Failed to start watchdog observer with error: {e}")
86 for index, artifactPath
in enumerate(eventHandler.artifactPaths):
87 logging.getLogger(
"coretexpylib").debug(f
">> [Coretex] Uploading {index + 1}/{len(eventHandler.artifactPaths)} - \"{artifactPath}\"")
90 artifact = taskRun.createArtifact(artifactPath, str(artifactPath.relative_to(root)))
91 if artifact
is not None:
92 logging.getLogger(
"coretexpylib").debug(f
"\tSuccessfully uploaded artifact")
94 logging.getLogger(
"coretexpylib").debug(f
"\tFailed to upload artifact")
95 except Exception
as e:
96 logging.getLogger(
"coretexpylib").error(f
"\tError while creating artifact: {e}")
97 logging.getLogger(
"coretexpylib").debug(f
"\tError while creating artifact: {e}", exc_info = e)