Skip to content

Commit

Permalink
Change line length
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanito98 committed Nov 14, 2023
1 parent fda025c commit 3857adb
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 44 deletions.
10 changes: 7 additions & 3 deletions container.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@


@contextlib.contextmanager
def _maybe_open(path: Optional[str], mode: str) -> Iterator[Optional[IO[AnyStr]]]:
def _maybe_open(
path: Optional[str], mode: str
) -> Iterator[Optional[IO[AnyStr]]]:
"""A contextmanager that can open a file, or return None.
This is useful to provide arguments to subprocess.call() and its friends.
Expand All @@ -39,7 +41,8 @@ def getImageName(ci: bool) -> str:

taggedContainerName = f"{imageName}:v1.9.59"
if not subprocess.check_output(
["docker", "image", "ls", "-q", taggedContainerName], universal_newlines=True
["docker", "image", "ls", "-q", taggedContainerName],
universal_newlines=True,
).strip():
logging.info("Downloading Docker image %s...", taggedContainerName)
subprocess.check_call(["docker", "pull", taggedContainerName])
Expand Down Expand Up @@ -170,7 +173,8 @@ def run_command(
stdoutPath, "wb"
) as stdout:
subprocess.run(
["docker", "exec", "--interactive", self.containerId] + list(args),
["docker", "exec", "--interactive", self.containerId]
+ list(args),
stdin=stdin,
stdout=stdout,
stderr=subprocess.PIPE,
Expand Down
42 changes: 32 additions & 10 deletions generateresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
_SUPPORTED_GENERATORS = frozenset(("png", "testplan"))


def _getSolution(p: problems.Problem, *, rootDirectory: str, ci: bool) -> Optional[str]:
def _getSolution(
p: problems.Problem, *, rootDirectory: str, ci: bool
) -> Optional[str]:
"""Gets the solution for the problem."""
solutions = [
f
Expand All @@ -36,7 +38,9 @@ def _getSolution(p: problems.Problem, *, rootDirectory: str, ci: bool) -> Option
return os.path.join(rootDirectory, p.path, "solutions", solutions[0])


def _getInputs(p: problems.Problem, *, rootDirectory: str, ci: bool) -> List[str]:
def _getInputs(
p: problems.Problem, *, rootDirectory: str, ci: bool
) -> List[str]:
"""Gets the list of .in files for the problem."""
inFilenames = [
f
Expand Down Expand Up @@ -113,10 +117,17 @@ def _generateImages(
relativeInFilename = os.path.relpath(inFilename, rootDirectory)
outFilename = f"{os.path.splitext(inFilename)[0]}.out"

logging.debug("%-30s: Generating .pngs for %s", p.title, inFilename)
logging.debug(
"%-30s: Generating .pngs for %s", p.title, inFilename
)
dimMatch = re.search(r"\.(\d*)x(\d*)\.in", inFilename)
if dimMatch:
dimOpts = ["--height", dimMatch.group(1), "--width", dimMatch.group(2)]
dimOpts = [
"--height",
dimMatch.group(1),
"--width",
dimMatch.group(2),
]
else:
dimOpts = []

Expand Down Expand Up @@ -187,10 +198,14 @@ def _main() -> None:
help="Consider all problems, instead of only those that have changed",
)
parser.add_argument(
"--ci", action="store_true", help="Signal that this is being run from the CI."
"--ci",
action="store_true",
help="Signal that this is being run from the CI.",
)
parser.add_argument(
"--force", action="store_true", help="Force re-generating all resources"
"--force",
action="store_true",
help="Force re-generating all resources",
)
parser.add_argument(
"--jobs",
Expand All @@ -208,8 +223,12 @@ def _main() -> None:
"Generates everything by default."
),
)
parser.add_argument("--verbose", action="store_true", help="Verbose logging")
parser.add_argument("problem_paths", metavar="PROBLEM", type=str, nargs="*")
parser.add_argument(
"--verbose", action="store_true", help="Verbose logging"
)
parser.add_argument(
"problem_paths", metavar="PROBLEM", type=str, nargs="*"
)
args = parser.parse_args()

if args.generate - _SUPPORTED_GENERATORS:
Expand All @@ -227,7 +246,9 @@ def _main() -> None:

rootDirectory = problems.repositoryRoot()

with concurrent.futures.ThreadPoolExecutor(max_workers=args.jobs) as executor:
with concurrent.futures.ThreadPoolExecutor(
max_workers=args.jobs
) as executor:
futures: List[concurrent.futures.Future[bool]] = []

for p in problems.problems(
Expand Down Expand Up @@ -257,7 +278,8 @@ def _main() -> None:
)

if not all(
future.result() for future in concurrent.futures.as_completed(futures)
future.result()
for future in concurrent.futures.as_completed(futures)
):
logging.error("Some resources failed to generate")
sys.exit(1)
Expand Down
23 changes: 18 additions & 5 deletions problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ class Problem(NamedTuple):
@staticmethod
def load(problemPath: str, rootDirectory: str) -> "Problem":
"""Load a single proble from the path."""
with open(os.path.join(rootDirectory, problemPath, "settings.json")) as f:
with open(
os.path.join(rootDirectory, problemPath, "settings.json")
) as f:
problemConfig = json.load(f)

return Problem(
path=problemPath, title=problemConfig["title"], config=problemConfig
path=problemPath,
title=problemConfig["title"],
config=problemConfig,
)

def shouldGenerateOutputs(self, *, rootDirectory: str) -> bool:
Expand All @@ -47,7 +51,12 @@ def repositoryRoot() -> str:
"""
return (
subprocess.check_output(
["git", "rev-parse", "--show-superproject-working-tree", "--show-toplevel"],
[
"git",
"rev-parse",
"--show-superproject-working-tree",
"--show-toplevel",
],
universal_newlines=True,
)
.strip()
Expand Down Expand Up @@ -80,7 +89,9 @@ def ci_message(
location.append(f"col={col}")
print(
f'::{kind} {",".join(location)}::'
+ message.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A"),
+ message.replace("%", "%25")
.replace("\r", "%0D")
.replace("\n", "%0A"),
file=sys.stderr,
flush=True,
)
Expand Down Expand Up @@ -164,7 +175,9 @@ def problems(
logging.warning("Problem %s disabled. Skipping.", problem["title"])
continue
configProblems.append(
Problem.load(problemPath=problem["path"], rootDirectory=rootDirectory)
Problem.load(
problemPath=problem["path"], rootDirectory=rootDirectory
)
)

if allProblems:
Expand Down
48 changes: 36 additions & 12 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def _threadInitializer(
) -> None:
"""Set the thread affinity mapping for the current thread."""
with lock:
threadAffinityMapping[threading.get_ident()] = len(threadAffinityMapping)
threadAffinityMapping[threading.get_ident()] = len(
threadAffinityMapping
)


def _testProblem(
Expand Down Expand Up @@ -169,12 +171,16 @@ def _main() -> None:

parser = argparse.ArgumentParser("Run tests")
parser.add_argument(
"--ci", action="store_true", help="Signal that this is being run from the CI."
"--ci",
action="store_true",
help="Signal that this is being run from the CI.",
)
parser.add_argument(
"--all",
action="store_true",
help=("Consider all problems, instead of " "only those that have changed"),
help=(
"Consider all problems, instead of " "only those that have changed"
),
)
parser.add_argument(
"--jobs",
Expand All @@ -183,7 +189,9 @@ def _main() -> None:
default=_availableProcessors(),
help="Number of threads to run concurrently",
)
parser.add_argument("--verbose", action="store_true", help="Verbose logging")
parser.add_argument(
"--verbose", action="store_true", help="Verbose logging"
)
parser.add_argument(
"--results-directory",
default=os.path.join(rootDirectory, "results"),
Expand All @@ -199,7 +207,9 @@ def _main() -> None:
action="store_true",
help=("Don't run tests: " "only download the Docker container"),
)
parser.add_argument("problem_paths", metavar="PROBLEM", type=str, nargs="*")
parser.add_argument(
"problem_paths", metavar="PROBLEM", type=str, nargs="*"
)
args = parser.parse_args()

logging.basicConfig(
Expand Down Expand Up @@ -244,7 +254,9 @@ def _main() -> None:
):
if not filename.endswith(".out"):
continue
os.unlink(os.path.join(rootDirectory, p.path, "cases", filename))
os.unlink(
os.path.join(rootDirectory, p.path, "cases", filename)
)

futures.append(
executor.submit(
Expand Down Expand Up @@ -304,7 +316,10 @@ def _main() -> None:
elif testResult["type"] == "invalid-inputs":
testedFile = os.path.normpath(
os.path.join(
p.path, "tests", "invalid-inputs", testResult["filename"]
p.path,
"tests",
"invalid-inputs",
testResult["filename"],
)
)
expected = {"verdict": "WA"}
Expand Down Expand Up @@ -335,7 +350,9 @@ def _main() -> None:
f"logs at {os.path.relpath(logsDirectory, rootDirectory)}"
)

failureMessages: DefaultDict[str, List[str]] = collections.defaultdict(list)
failureMessages: DefaultDict[
str, List[str]
] = collections.defaultdict(list)

normalizedScore = decimal.Decimal(got["score"])
round(normalizedScore, 15) * 100
Expand Down Expand Up @@ -368,7 +385,9 @@ def _main() -> None:
f'{"-"*20}-+-{"-"*20}-+-{"-"*7}-+-{"-"*7}'
)

failureMessages[testedFile].append("\n".join(groupReportTable))
failureMessages[testedFile].append(
"\n".join(groupReportTable)
)

failedCases = {
c["name"]
Expand Down Expand Up @@ -415,7 +434,8 @@ def _main() -> None:
expectedFailure = err.read().strip()
else:
logging.error(
"Unexpected test result type: " f'{testResult["type"]}'
"Unexpected test result type: "
f'{testResult["type"]}'
)

with open(
Expand Down Expand Up @@ -448,9 +468,13 @@ def _main() -> None:
f"{failureMessage}"
)

failureMessages[associatedFile].append(failureMessage)
failureMessages[associatedFile].append(
failureMessage
)
else:
logging.warning("Logs directory %r not found.", logsDirectory)
logging.warning(
"Logs directory %r not found.", logsDirectory
)

for path, messages in failureMessages.items():
problems.error(
Expand Down
Loading

0 comments on commit 3857adb

Please sign in to comment.