Skip to content

Commit

Permalink
Delete projects when finished, move dir to be timestamped + shallow copy
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperlees committed May 16, 2020
1 parent 6a2c786 commit cb4bce3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/black_primer/cli.py
Expand Up @@ -3,7 +3,8 @@
import asyncio
import logging
import sys
from os import cpu_count, getpid
from datetime import datetime
from os import cpu_count
from pathlib import Path
from shutil import rmtree, which
from tempfile import gettempdir
Expand All @@ -15,7 +16,8 @@


DEFAULT_CONFIG = Path(__file__).parent / "primer.json"
DEFAULT_WORKDIR = Path(gettempdir()) / f"primer.{getpid()}"
_timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
DEFAULT_WORKDIR = Path(gettempdir()) / f"primer.{_timestamp}"
LOG = logging.getLogger(__name__)


Expand Down
24 changes: 19 additions & 5 deletions src/black_primer/lib.py
@@ -1,10 +1,12 @@
#!/usr/bin/env python3

from __future__ import annotations

import asyncio
import json
import logging
from pathlib import Path
from shutil import which
from shutil import rmtree, which
from subprocess import CalledProcessError
from sys import version_info
from typing import Any, Dict, NamedTuple, Optional, Sequence, Tuple
Expand Down Expand Up @@ -101,7 +103,7 @@ async def black_run(
_stdout, _stderr = await _gen_check_output(cmd, cwd=repo_path)
except asyncio.TimeoutError:
results.stats["failed"] += 1
LOG.error(f"Running black for {repo_path} timedout ({cmd})")
LOG.error(f"Running black for {repo_path} timed out ({cmd})")
except CalledProcessError as cpe:
# TODO: This might need to be tuned and made smarter for higher signal
if not project_config["expect_formatting_changes"] and cpe.returncode == 1:
Expand All @@ -113,7 +115,11 @@ async def black_run(


async def git_checkout_or_rebase(
work_path: Path, project_config: Dict[str, Any], rebase: bool = False
work_path: Path,
project_config: Dict[str, Any],
rebase: bool = False,
*,
depth: int = 1,
) -> Optional[Path]:
"""git Clone project or rebase"""
git_bin = str(which("git"))
Expand All @@ -125,7 +131,7 @@ async def git_checkout_or_rebase(
path_parts = repo_url_parts.path[1:].split("/", maxsplit=1)

repo_path: Path = work_path / path_parts[1].replace(".git", "")
cmd = [git_bin, "clone", project_config["git_clone_url"]]
cmd = [git_bin, "clone", "--depth", str(depth), project_config["git_clone_url"]]
cwd = work_path
if repo_path.exists() and rebase:
cmd = [git_bin, "pull", "--rebase"]
Expand Down Expand Up @@ -167,8 +173,10 @@ async def project_runner(
results: Results,
long_checkouts: bool = False,
rebase: bool = False,
keep: bool = False,
) -> None:
"""Checkout project and run black on it + record result"""
loop = asyncio.get_event_loop()
py_version = f"{version_info[0]}.{version_info[1]}"
while True:
try:
Expand Down Expand Up @@ -205,6 +213,10 @@ async def project_runner(
continue
await black_run(repo_path, project_config, results)

if not keep:
LOG.debug(f"Removing {repo_path}")
await loop.run_in_executor(None, rmtree, repo_path)


async def process_queue(
config_file: str,
Expand Down Expand Up @@ -237,7 +249,9 @@ async def process_queue(
# Wait until we finish running all the projects before analyzing
await asyncio.gather(
*[
project_runner(i, config, queue, work_path, results, long_checkouts, rebase)
project_runner(
i, config, queue, work_path, results, long_checkouts, rebase, keep
)
for i in range(workers)
]
)
Expand Down

0 comments on commit cb4bce3

Please sign in to comment.