From 3888c4cc9a960f1831c7b8561ad969c460ee099d Mon Sep 17 00:00:00 2001 From: Jan-Paul van der Velden Date: Thu, 3 Jun 2021 15:12:55 +0200 Subject: [PATCH 1/3] Create a bash script and batch file to do similar things on WSL and Windows. --- .gitignore | 5 +++++ .../miniapps/movie-lister/run_movie_lister.bat | 12 ++++++++++++ .../miniapps/movie-lister/run_movie_lister.sh | 12 ++++++++++++ examples/miniapps/movie-lister/setup_venvt.bat | 17 +++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 examples/miniapps/movie-lister/run_movie_lister.bat create mode 100644 examples/miniapps/movie-lister/run_movie_lister.sh create mode 100644 examples/miniapps/movie-lister/setup_venvt.bat diff --git a/.gitignore b/.gitignore index e2195edff..f04b99cb6 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,8 @@ src/dependency_injector/providers/*.so # Workspace for samples .workspace/ +/examples/miniapps/movie-lister/wslenv2-win/Scripts +/examples/miniapps/movie-lister/wslenv2-win +/examples/miniapps/movie-lister/wslenv2/bin +/examples/miniapps/movie-lister/wslenv2/share/python-wheels +/examples/miniapps/movie-lister/wslenv2 diff --git a/examples/miniapps/movie-lister/run_movie_lister.bat b/examples/miniapps/movie-lister/run_movie_lister.bat new file mode 100644 index 000000000..6d64f1b0d --- /dev/null +++ b/examples/miniapps/movie-lister/run_movie_lister.bat @@ -0,0 +1,12 @@ +echo on +set PYTHON37=%UserProfile%\AppData\Local\Programs\Python\Python37\python.exe + +REM set variabless +set ENV_NAME=wslenv2-win +set VENV_DIR=%ENV_NAME%\Scripts +set PYTHON_VENV=%VENV_DIR%\python.exe + +Start /WAIT cmd /k "%VENV_DIR%\activate & cd & %PYTHON_VENV% data/fixtures.py & set MOVIE_FINDER_TYPE=sqlite & %PYTHON_VENV% -m movies & %VENV_DIR%\deactivate & pause & exit 0" + +REM MOVIE_FINDER_TYPE=csv +REM MOVIE_FINDER_TYPE=sqlite diff --git a/examples/miniapps/movie-lister/run_movie_lister.sh b/examples/miniapps/movie-lister/run_movie_lister.sh new file mode 100644 index 000000000..f7b2e6619 --- /dev/null +++ b/examples/miniapps/movie-lister/run_movie_lister.sh @@ -0,0 +1,12 @@ +#!/bin/bash -ef + +rm -rf wslenv2 + +python3 -m venv wslenv2 +. wslenv2/bin/activate + +pip install -r requirements.txt +wslenv2/bin/python data/fixtures.py + +MOVIE_FINDER_TYPE=csv wslenv2/bin/python -m movies +MOVIE_FINDER_TYPE=sqlite wslenv2/bin/python -m movies diff --git a/examples/miniapps/movie-lister/setup_venvt.bat b/examples/miniapps/movie-lister/setup_venvt.bat new file mode 100644 index 000000000..4e45389a3 --- /dev/null +++ b/examples/miniapps/movie-lister/setup_venvt.bat @@ -0,0 +1,17 @@ +echo on +set PYTHON37=%UserProfile%\AppData\Local\Programs\Python\Python37\python.exe + +REM set variabless +set ENV_NAME=wslenv2-win +set VENV_DIR=%ENV_NAME%\Scripts +set PYTHON_VENV=%VENV_DIR%\python.exe + +REM cleanup +rd /s /q %ENV_NAME% + +%PYTHON37% -m venv %ENV_NAME% + +Start /WAIT cmd /k "%VENV_DIR%\activate & %PYTHON_VENV% -m pip install --upgrade pip & %PYTHON_VENV% -m pip install -r .\requirements.txt & %VENV_DIR%\deactivate & pause & exit 0" + +REM MOVIE_FINDER_TYPE=csv +REM MOVIE_FINDER_TYPE=sqlite From 4206c333ae796446d85ac84ddd3247630c7b67be Mon Sep 17 00:00:00 2001 From: Jan-Paul van der Velden Date: Fri, 4 Jun 2021 12:51:18 +0200 Subject: [PATCH 2/3] - Changed the creation of the CSV file so it doesn't add an additional newline, which made the example fail on Windows. - Changed the relative imports to absolute imports so the example works out of the box in VS Code. - Added a shell script and a batch file to run the example on Linux and Windows. --- .gitignore | 4 ++++ examples/miniapps/movie-lister/data/fixtures.py | 5 ++--- examples/miniapps/movie-lister/movies/__main__.py | 4 ++-- examples/miniapps/movie-lister/movies/containers.py | 2 +- examples/miniapps/movie-lister/movies/finders.py | 2 +- examples/miniapps/movie-lister/movies/listers.py | 2 +- examples/miniapps/movie-lister/movies/tests.py | 2 +- examples/miniapps/movie-lister/run_movie_lister.bat | 12 ------------ ...etup_venvt.bat => setup_and_run_movie_lister.bat} | 8 +------- ...movie_lister.sh => setup_and_run_movie_lister.sh} | 4 +++- 10 files changed, 16 insertions(+), 29 deletions(-) delete mode 100644 examples/miniapps/movie-lister/run_movie_lister.bat rename examples/miniapps/movie-lister/{setup_venvt.bat => setup_and_run_movie_lister.bat} (69%) rename examples/miniapps/movie-lister/{run_movie_lister.sh => setup_and_run_movie_lister.sh} (93%) diff --git a/.gitignore b/.gitignore index f04b99cb6..43967294d 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,7 @@ src/dependency_injector/providers/*.so /examples/miniapps/movie-lister/wslenv2/bin /examples/miniapps/movie-lister/wslenv2/share/python-wheels /examples/miniapps/movie-lister/wslenv2 +/examples/miniapps/movie-lister/setup_venv.bat +/examples/miniapps/movie-lister/setup_venv.sh +/examples/miniapps/movie-lister/run_movie_lister.bat +/examples/miniapps/movie-lister/run_movie_lister.sh diff --git a/examples/miniapps/movie-lister/data/fixtures.py b/examples/miniapps/movie-lister/data/fixtures.py index 2870d04d9..f3b829248 100644 --- a/examples/miniapps/movie-lister/data/fixtures.py +++ b/examples/miniapps/movie-lister/data/fixtures.py @@ -18,10 +18,9 @@ def create_csv(movies_data, path): - with open(path, 'w') as opened_file: + with open(path, 'w', newline='') as opened_file: writer = csv.writer(opened_file) - for row in movies_data: - writer.writerow(row) + writer.writerows(movies_data) def create_sqlite(movies_data, path): diff --git a/examples/miniapps/movie-lister/movies/__main__.py b/examples/miniapps/movie-lister/movies/__main__.py index 975618f38..561a6ac58 100644 --- a/examples/miniapps/movie-lister/movies/__main__.py +++ b/examples/miniapps/movie-lister/movies/__main__.py @@ -4,8 +4,8 @@ from dependency_injector.wiring import inject, Provide -from .listers import MovieLister -from .containers import Container +from movies.listers import MovieLister +from movies.containers import Container @inject diff --git a/examples/miniapps/movie-lister/movies/containers.py b/examples/miniapps/movie-lister/movies/containers.py index 9d30fbfb8..4425c6a71 100644 --- a/examples/miniapps/movie-lister/movies/containers.py +++ b/examples/miniapps/movie-lister/movies/containers.py @@ -2,7 +2,7 @@ from dependency_injector import containers, providers -from . import finders, listers, entities +from movies import finders, listers, entities class Container(containers.DeclarativeContainer): diff --git a/examples/miniapps/movie-lister/movies/finders.py b/examples/miniapps/movie-lister/movies/finders.py index 3485d8c38..85601b735 100644 --- a/examples/miniapps/movie-lister/movies/finders.py +++ b/examples/miniapps/movie-lister/movies/finders.py @@ -4,7 +4,7 @@ import sqlite3 from typing import Callable, List -from .entities import Movie +from movies.entities import Movie class MovieFinder: diff --git a/examples/miniapps/movie-lister/movies/listers.py b/examples/miniapps/movie-lister/movies/listers.py index 36d254c20..4da5b40b0 100644 --- a/examples/miniapps/movie-lister/movies/listers.py +++ b/examples/miniapps/movie-lister/movies/listers.py @@ -1,6 +1,6 @@ """Movie listers module.""" -from .finders import MovieFinder +from movies.finders import MovieFinder class MovieLister: diff --git a/examples/miniapps/movie-lister/movies/tests.py b/examples/miniapps/movie-lister/movies/tests.py index eea04c81b..af91a8710 100644 --- a/examples/miniapps/movie-lister/movies/tests.py +++ b/examples/miniapps/movie-lister/movies/tests.py @@ -4,7 +4,7 @@ import pytest -from .containers import Container +from movies.containers import Container @pytest.fixture diff --git a/examples/miniapps/movie-lister/run_movie_lister.bat b/examples/miniapps/movie-lister/run_movie_lister.bat deleted file mode 100644 index 6d64f1b0d..000000000 --- a/examples/miniapps/movie-lister/run_movie_lister.bat +++ /dev/null @@ -1,12 +0,0 @@ -echo on -set PYTHON37=%UserProfile%\AppData\Local\Programs\Python\Python37\python.exe - -REM set variabless -set ENV_NAME=wslenv2-win -set VENV_DIR=%ENV_NAME%\Scripts -set PYTHON_VENV=%VENV_DIR%\python.exe - -Start /WAIT cmd /k "%VENV_DIR%\activate & cd & %PYTHON_VENV% data/fixtures.py & set MOVIE_FINDER_TYPE=sqlite & %PYTHON_VENV% -m movies & %VENV_DIR%\deactivate & pause & exit 0" - -REM MOVIE_FINDER_TYPE=csv -REM MOVIE_FINDER_TYPE=sqlite diff --git a/examples/miniapps/movie-lister/setup_venvt.bat b/examples/miniapps/movie-lister/setup_and_run_movie_lister.bat similarity index 69% rename from examples/miniapps/movie-lister/setup_venvt.bat rename to examples/miniapps/movie-lister/setup_and_run_movie_lister.bat index 4e45389a3..6c6b942b8 100644 --- a/examples/miniapps/movie-lister/setup_venvt.bat +++ b/examples/miniapps/movie-lister/setup_and_run_movie_lister.bat @@ -6,12 +6,6 @@ set ENV_NAME=wslenv2-win set VENV_DIR=%ENV_NAME%\Scripts set PYTHON_VENV=%VENV_DIR%\python.exe -REM cleanup -rd /s /q %ENV_NAME% - %PYTHON37% -m venv %ENV_NAME% -Start /WAIT cmd /k "%VENV_DIR%\activate & %PYTHON_VENV% -m pip install --upgrade pip & %PYTHON_VENV% -m pip install -r .\requirements.txt & %VENV_DIR%\deactivate & pause & exit 0" - -REM MOVIE_FINDER_TYPE=csv -REM MOVIE_FINDER_TYPE=sqlite +Start /WAIT cmd /k "%VENV_DIR%\activate & %PYTHON_VENV% -m pip install --upgrade pip & %PYTHON_VENV% -m pip install -r .\requirements.txt & set MOVIE_FINDER_TYPE=csv& %PYTHON_VENV% -m movies & set MOVIE_FINDER_TYPE=sqlite& %PYTHON_VENV% -m movies&%VENV_DIR%\deactivate & pause & exit 0" diff --git a/examples/miniapps/movie-lister/run_movie_lister.sh b/examples/miniapps/movie-lister/setup_and_run_movie_lister.sh similarity index 93% rename from examples/miniapps/movie-lister/run_movie_lister.sh rename to examples/miniapps/movie-lister/setup_and_run_movie_lister.sh index f7b2e6619..9b34ac6f0 100644 --- a/examples/miniapps/movie-lister/run_movie_lister.sh +++ b/examples/miniapps/movie-lister/setup_and_run_movie_lister.sh @@ -4,9 +4,11 @@ rm -rf wslenv2 python3 -m venv wslenv2 . wslenv2/bin/activate - pip install -r requirements.txt + wslenv2/bin/python data/fixtures.py MOVIE_FINDER_TYPE=csv wslenv2/bin/python -m movies MOVIE_FINDER_TYPE=sqlite wslenv2/bin/python -m movies + +source deactivate From be24fb98aef433ba00197563c3af204ae8fc1ec9 Mon Sep 17 00:00:00 2001 From: Jan-Paul van der Velden Date: Mon, 7 Jun 2021 09:28:29 +0200 Subject: [PATCH 3/3] Removed the reference to the local python installation. Changed script to use multiline command separator for the long command sequence. Added some comment lines. --- .../setup_and_run_movie_lister.bat | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/miniapps/movie-lister/setup_and_run_movie_lister.bat b/examples/miniapps/movie-lister/setup_and_run_movie_lister.bat index 6c6b942b8..8f20daf02 100644 --- a/examples/miniapps/movie-lister/setup_and_run_movie_lister.bat +++ b/examples/miniapps/movie-lister/setup_and_run_movie_lister.bat @@ -1,11 +1,22 @@ echo on -set PYTHON37=%UserProfile%\AppData\Local\Programs\Python\Python37\python.exe +REM The assumption is made that the python executable can be found. -REM set variabless +REM Set variabless set ENV_NAME=wslenv2-win set VENV_DIR=%ENV_NAME%\Scripts set PYTHON_VENV=%VENV_DIR%\python.exe -%PYTHON37% -m venv %ENV_NAME% +REM Create virtual environment +python -m venv %ENV_NAME% + +REM Populate dependencies and run the example +Start /WAIT cmd /k "%VENV_DIR%\activate&^ +%PYTHON_VENV% -m pip install --upgrade pip&^ +%PYTHON_VENV% -m pip install -r .\requirements.txt&^ +set MOVIE_FINDER_TYPE=csv&^ +%PYTHON_VENV% -m movies&^ +set MOVIE_FINDER_TYPE=sqlite&^ +%PYTHON_VENV% -m movies&^ +%VENV_DIR%\deactivate&^ +pause -Start /WAIT cmd /k "%VENV_DIR%\activate & %PYTHON_VENV% -m pip install --upgrade pip & %PYTHON_VENV% -m pip install -r .\requirements.txt & set MOVIE_FINDER_TYPE=csv& %PYTHON_VENV% -m movies & set MOVIE_FINDER_TYPE=sqlite& %PYTHON_VENV% -m movies&%VENV_DIR%\deactivate & pause & exit 0"