Skip to content

Commit

Permalink
Add last modified time to the file artifacts
Browse files Browse the repository at this point in the history
For future convenience, this change adds last modified time
to each file artifact. In that way, Precaution can potentially
avoid re-analysis of files that haven't changed since the
last analysis.

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Oct 21, 2024
1 parent 3c79016 commit a3ddc98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,13 @@ def discover_files(targets: list[str], recursive: bool):
not preignore_mgr.is_ignored(file_path)
and pathlib.Path(path).suffix in FILE_EXTS
):
artifact = Artifact(path)
if repo:
artifact.uri = file_to_url(
uri = file_to_url(
owner, repo, branch, target, root, file
)
artifact = Artifact(path, uri)
else:
artifact = Artifact(path)
artifacts.append(artifact)
else:
files = os.listdir(path=target)
Expand Down
14 changes: 14 additions & 0 deletions precli/core/artifact.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright 2024 Secure Sauce LLC
# SPDX-License-Identifier: BUSL-1.1
import os
from datetime import datetime
from datetime import timezone
from typing import Optional


Expand All @@ -12,6 +15,12 @@ def __init__(self, file_name: str, uri: Optional[str] = None):
self._encoding = "utf-8"
self._language = None

if file_name != "-" or not uri:
modified_time = os.path.getmtime(file_name)
self._last_modified = datetime.fromtimestamp(
modified_time, tz=timezone.utc
)

@property
def file_name(self) -> str:
"""The name of the file."""
Expand Down Expand Up @@ -61,3 +70,8 @@ def language(self) -> Optional[str]:
def language(self, language: str):
"""Set the programming language."""
self._language = language

@property
def last_modified(self) -> datetime:
"""The last modified time in UTC for this artifact."""
return self._last_modified

0 comments on commit a3ddc98

Please sign in to comment.