Skip to content

Commit

Permalink
Git deliver tmp directory fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Barabanov committed Sep 18, 2023
1 parent f1e977a commit 09453ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions hworker/deliver/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import os
from pathlib import Path
from tempfile import gettempdir

import git

Expand All @@ -19,7 +20,7 @@ def local_path(student_id: str) -> str:
:return: local repo path
"""
git_directory = get_git_directory()
return os.path.join(git_directory, student_id)
return os.path.join(gettempdir(), git_directory, student_id)


def clone(repo: str) -> None:
Expand Down Expand Up @@ -66,33 +67,34 @@ def update_all() -> None:
pull(repo)


def get_homework_content(repo: git.Repo, root: str) -> dict:
def get_homework_content(repo: git.Repo, root: Path) -> dict:
"""Extracts tests, solution and URLS from homework and pack into dict
:param root: local path to homework
:return: dict with "prog", "tests" and "urls" keys
"""
get_logger(__name__).debug(f"Getting {root} content")
Root = Path(root)
content = {
path.relative_to(Root).as_posix(): FileObject(
path.relative_to(root).as_posix(): FileObject(
content=path.read_bytes(), timestamp=repo.git.log("-1", "--format=%ct", "--date=default", "--", path)
)
for path in Root.rglob("*")
if path.is_file() and f"{os.sep}." not in str(path.relative_to(Root.parent))
for path in root.rglob("*")
if path.is_file() and f"{os.sep}." not in str(path.relative_to(root.parent))
}
return content


def get_commits(repo: git.Repo, path: str) -> list[tuple[str, str]]:
def get_commits(repo: git.Repo, path: Path) -> list[tuple[str, str]]:
"""Get list of all commits (with timestamps) for a given directory
:param repo: repo local path
:param path: homework local path
:return: list of (commit hash, timestamp) pairs
"""
get_logger(__name__).debug(f"Getting {path} commits")
commits = [tuple(_.split()) for _ in repo.git.log("--format=%H %ct", "--date=default", "--", path).split("\n")]
commits = [
tuple(_.split()) for _ in repo.git.log("--format=%H %ct", "--date=default", "--", path.as_posix()).split("\n")
]
return commits


Expand All @@ -106,10 +108,8 @@ def download_all() -> None:
repo = git.Repo(local_path(student_id))
for task in get_tasks_list():
repo.git.checkout(repo.heads[0])
if os.path.isdir(
(task_path := os.path.join(local_path(student_id), get_task_info(task).get("deliver_ID", "")))
):
commits = get_commits(repo, task_path)
if os.path.isdir((task_path := Path(local_path(student_id), get_task_info(task).get("deliver_ID", "")))):
commits = [_ for _ in get_commits(repo, task_path) if _]
for commit in commits:
repo.git.checkout(commit[0])
content = get_homework_content(repo, task_path)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_deliver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def example_git_repo(tmp_path_factory):
(test_path / config.get_remote_name()).write_bytes(b"UserN:TaskN\n")
repo.git.add(".")
repo.git.commit(message="test commit")
return repo, str(repo_path)
return repo, repo_path


class TestDeliverGit:
Expand Down

0 comments on commit 09453ea

Please sign in to comment.