From 6d34acba095be3eb8447de377ff299e50404725b Mon Sep 17 00:00:00 2001 From: Anonymous4045 Date: Mon, 9 Sep 2024 00:22:36 -0500 Subject: [PATCH 1/2] Add run command This commit adds the `run` command. This will do the following: - Check if the file is in the right format. If not, it tells what to change, but still tries to run anyway. - Extract the zip file to a temporary directory. - Set up a virtual environment. - Install requirements. - Display the readme with rich. - Run the game. --- pyweek.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 1 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/pyweek.py b/pyweek.py index 1f1b8a2..53f55f1 100644 --- a/pyweek.py +++ b/pyweek.py @@ -2,6 +2,7 @@ Download and verify entries for a given challenge: """ +import subprocess import sys import os import re @@ -9,10 +10,13 @@ import time from packaging import version import zipfile +import tempfile import requests import click import progressbar +from rich.console import Console +from rich.markdown import Markdown __version__ = '0.5.3' PYWEEK_URL = 'https://pyweek.org' @@ -135,6 +139,9 @@ def download(challenge, directory): def verify(file: Path): """Determines if a given zip file is in the proper format.""" + in_correct_format(file) + +def in_correct_format(file: Path): errors = 0 def error(msg, critical=False): @@ -207,8 +214,50 @@ def error(msg, critical=False): if errors: error(f"{errors} error{"s" if errors > 1 else ""} occurred while verifying file {file}.") + return False + else: + click.echo(click.style(f"File {file} is in a valid format.", fg='green')) + return True + +@cli.command() +@click.argument( + 'file', + type=Path, +) +def run(file: Path): + """Extract and run the specified game from the zip file.""" + + if not in_correct_format(file): + click.echo("\nImproperly formatted file. Attempting to run anyway...\n") else: - click.echo(click.style(f"File {file} is valid.", fg='green')) + click.echo("\nSetting up venv...\n") + + # Extract the zip file to a temporary directory + temp_dir = Path(tempfile.mkdtemp()) + with zipfile.ZipFile(file, 'r') as zipped_file: + zipped_file.extractall(temp_dir) + top_level_dir = next(temp_dir.iterdir()) + + # Set up the venv + venv_dir = temp_dir / 'venv' + subprocess.run([sys.executable, '-m', 'venv', venv_dir]) + python = venv_dir / 'bin' / 'python' + pip = venv_dir / 'bin' / 'pip' + subprocess.run([pip, 'install', '-r', temp_dir / top_level_dir / 'requirements.txt']) + + # Show the README rendered in the terminal + readme = temp_dir / top_level_dir / 'README.md' + contents = readme.read_text() + + console = Console() + click.echo("\nREADME.md") + console.print(Markdown(contents)) + + input("\nPress Enter after reading to play...") + + # Run the game + run_game = temp_dir / top_level_dir / 'run_game.py' + subprocess.run([python, run_game]) CHUNK_SIZE = 10240 diff --git a/requirements.txt b/requirements.txt index 7204e2b..c324193 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ requests progressbar2 packaging colorama +rich From fbf7370b03e32836571e574917174a3cb3da65c4 Mon Sep 17 00:00:00 2001 From: Anonymous4045 Date: Mon, 9 Sep 2024 00:39:48 -0500 Subject: [PATCH 2/2] Add better checks for running malformatted files This commit changes the run command behavior to better handle unformatted zip files. --- pyweek.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/pyweek.py b/pyweek.py index 53f55f1..ef57af1 100644 --- a/pyweek.py +++ b/pyweek.py @@ -238,25 +238,39 @@ def run(file: Path): zipped_file.extractall(temp_dir) top_level_dir = next(temp_dir.iterdir()) + # No pint in trying if there is no run_game.py + run_game = temp_dir / top_level_dir / 'run_game.py' + if not run_game.exists(): + click.echo("No run_game.py file found. Exiting...") + sys.exit(1) + # Set up the venv venv_dir = temp_dir / 'venv' subprocess.run([sys.executable, '-m', 'venv', venv_dir]) python = venv_dir / 'bin' / 'python' pip = venv_dir / 'bin' / 'pip' - subprocess.run([pip, 'install', '-r', temp_dir / top_level_dir / 'requirements.txt']) + + # Check if the requirements.txt file exists + requirements = temp_dir / top_level_dir / 'requirements.txt' + if not requirements.exists(): + click.echo("No requirements.txt file found. Attempting to run anyway...\n") + else: + click.echo("Installing requirements...\n") + subprocess.run([pip, 'install', '-r', requirements]) # Show the README rendered in the terminal readme = temp_dir / top_level_dir / 'README.md' - contents = readme.read_text() - - console = Console() - click.echo("\nREADME.md") - console.print(Markdown(contents)) - - input("\nPress Enter after reading to play...") + if not readme.exists(): + click.echo("No README.md file found. Attempting to run anyway...\n") + input("\nPress Enter to play...") + else: + contents = readme.read_text() + console = Console() + click.echo("\nREADME.md") + console.print(Markdown(contents)) + input("\nPress Enter after reading to play...") # Run the game - run_game = temp_dir / top_level_dir / 'run_game.py' subprocess.run([python, run_game])