Skip to content

Commit

Permalink
add _is_file_pushed
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Su <[email protected]>
  • Loading branch information
pingsutw committed Sep 4, 2024
1 parent e5f079c commit 309a5a0
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions flytekit/tools/translator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import subprocess
import typing
from collections import OrderedDict
from dataclasses import dataclass
from typing import Callable, Dict, List, Optional, Tuple, Union

import requests
from flyteidl.admin import schedule_pb2

from flytekit import ImageSpec, PythonFunctionTask, SourceCode, logger
Expand Down Expand Up @@ -845,18 +845,45 @@ def _get_git_link(module: str, settings: SerializationSettings) -> Optional[str]

from flytekit.remote.remote import _get_git_root

if _is_file_pushed(settings.source_root, module):
_get_git_root(settings.source_root)

return None


def _is_file_pushed(source_root: str, filename: str) -> bool:
"""
Check if a specific file has been pushed to the remote repository.
Args:
source_root: The root directory of the source code.
filename: The name of the file to check.
Returns:
True if the file has been pushed, False otherwise.
"""
try:
git_link = "https://" + settings.git_repo + module.removeprefix(_get_git_root(settings.source_root))
response = requests.get(git_link)
except Exception as e:
logger.debug(f"Failing to get source code link: {e}")
return None
# Check if the file is tracked by Git
check_file_command = ["git", "ls-files", filename]
result = subprocess.run(
check_file_command, cwd=source_root, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)

if response.status_code != 200:
logger.debug(f"Source code link not found: {git_link}")
return None
if not result.stdout.strip():
return False

return git_link
# Compare the file version in the current branch with the remote branch
diff_command = ["git", "diff", "--name-only", "@{u}", filename]
diff_result = subprocess.run(
diff_command, cwd=source_root, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)

# If there is no output from diff, the file has been pushed
return not bool(diff_result.stdout.strip())

except Exception as e:
logger.debug(f"Error while checking the file's push status: {str(e)}")
return False


def gather_dependent_entities(
Expand Down

0 comments on commit 309a5a0

Please sign in to comment.