Skip to content

Commit ff67411

Browse files
Redo setup script
1 parent 9769d8a commit ff67411

File tree

5 files changed

+111
-28
lines changed

5 files changed

+111
-28
lines changed

README.md

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
11
# Cookiecutter Python Package
22

3-
A [Cookiecutter](https://github.com/cookiecutter/cookiecutter) template for a Python package with Poetry as the dependency manager.
3+
A [Cookiecutter](https://github.com/cookiecutter/cookiecutter) blank template for a project.
44

55
**NOTE:** Only Python 3.8+ is supported.
66

77
---
88

99
## Features
10-
- Hooks: Pre-commit
11-
- Formatters and Linters: Black, Flake8, Flake8-bugbear, Isort, and Lizard
10+
- Package manager: pip
11+
- Formatters and Linters: Ruff
1212
- Testing Frameworks (Optional): Pytest, Coverage, and CovDefaults
1313

1414
## Usage
1515

16-
- Since this template uses Poetry as the dependancy manager, install poetry from `https://python-poetry.org/docs/#installation`
17-
18-
- Install the `cookiecutter` library.
19-
20-
```python
21-
pip install cookiecutter
22-
```
23-
OR
24-
```python
25-
python -m pip install cookiecutter
26-
```
27-
28-
- Run the command:
29-
```python
30-
cookiecutter https://github.com/gurashish1singh/cookiecutter-python.git
31-
```
32-
OR
33-
```python
34-
python -m cookiecutter https://github.com/gurashish1singh/cookiecutter-python.git
35-
```
16+
- bash setup.sh
17+
3618
- This template uses post-project generation hooks to:
3719
- Initialize a git repository (with default branch as main), IF the working directory is not already a git repository.
3820

3921
**NOTE**: You will have to create a repositry on remote if it doesn't already exist before running the cookiecutter command.
40-
- Create a Poetry virtualenv
41-
- Install all dependencies
42-
- Install the pre-commit and pre-push hooks
22+
- Create a python virtual environment and activate it
23+
- Install ruff
24+
- Include an initial lint github workflow

hooks/post_gen_project.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
VENV_NAME = ".venv"
1616
VIRTUAL_ENV = "VIRTUAL_ENV"
1717

18-
19-
# Setting up environment
2018
SUBPROCESS_PARAMS = {
2119
"stdout": subprocess.PIPE,
2220
"stderr": subprocess.PIPE,

setup/__init__.py

Whitespace-only changes.

setup/setup.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import pathlib
5+
import subprocess
6+
import sys
7+
import venv
8+
9+
ERROR_MSG = "Error occured while running command"
10+
PRETTY_LINES = "*" * 80
11+
VENV_NAME = ".venv"
12+
VIRTUAL_ENV = "VIRTUAL_ENV"
13+
14+
SUBPROCESS_PARAMS = {
15+
"stdout": subprocess.PIPE,
16+
"stderr": subprocess.PIPE,
17+
"check": True,
18+
}
19+
20+
21+
def main() -> int:
22+
return_code_one = setup_environment()
23+
return 0 or return_code_one
24+
25+
26+
def setup_environment() -> int:
27+
return_code = 0
28+
try:
29+
# Always create a new environment in the project dir
30+
python_venv_path = _create_new_environment()
31+
system_type, python_executable_path, python_activate_script_path = _get_python_paths(venv_path=python_venv_path)
32+
_activate_environment(system_type, python_activate_script_path)
33+
except Exception:
34+
return_code = -1
35+
return_code = _install_requirements(python_executable_path)
36+
return 0 or return_code
37+
38+
39+
def _create_new_environment() -> str:
40+
parent_dir = pathlib.Path(os.getcwd())
41+
python_venv_path = str(parent_dir / VENV_NAME)
42+
43+
print(PRETTY_LINES)
44+
print(f"Attempting to create a new virtual env at {python_venv_path}")
45+
try:
46+
venv.create(env_dir=python_venv_path, with_pip=True)
47+
except Exception:
48+
print("An unexpected error has occured")
49+
raise
50+
51+
print(f"Successfully created virtualenv at: {python_venv_path!r}")
52+
return python_venv_path
53+
54+
55+
def _get_python_paths(venv_path: str) -> tuple[str, str, str]:
56+
sys_type = sys.platform
57+
if sys_type == "win32":
58+
python_executable_path = pathlib.Path(venv_path, "Scripts", "python.exe")
59+
activate_script_path = pathlib.Path(venv_path, "Scripts", "activate")
60+
elif sys_type in ("darwin", "linux", "linux2"):
61+
python_executable_path = pathlib.Path(venv_path, "bin", "python")
62+
activate_script_path = pathlib.Path(venv_path, "bin", "activate")
63+
else:
64+
raise OSError(f"Unsupported platform: {sys_type!r}")
65+
return sys_type, str(python_executable_path), str(activate_script_path)
66+
67+
68+
def _activate_environment(system_type: str, activate_script_path: str) -> None:
69+
if system_type == "win32":
70+
subprocess.call(["cmd.exe", "c", activate_script_path])
71+
elif system_type in ("darwin", "linux", "linux2"):
72+
subprocess.call(f"source {activate_script_path}", shell=True)
73+
74+
print("Successfully activated virtualenv.")
75+
print(f"\n{PRETTY_LINES}")
76+
77+
78+
def _install_requirements(python_executable_path: str) -> int:
79+
print("Installing all dependencies from the requirements.txt file.\n")
80+
try:
81+
subprocess.run(
82+
[python_executable_path, "-m", "pip", "install", "-r", "requirements.txt"],
83+
**SUBPROCESS_PARAMS,
84+
)
85+
if "{{ cookiecutter.project_slug }}":
86+
subprocess.run(
87+
[python_executable_path, "-m", "pip", "install", "-r", "dev-requirements.txt"],
88+
**SUBPROCESS_PARAMS,
89+
)
90+
except subprocess.CalledProcessError as e:
91+
print(ERROR_MSG, e)
92+
return e.returncode
93+
94+
print(PRETTY_LINES)
95+
return 0
96+
97+
98+
if __name__ == "__main__":
99+
main()

setup/setup.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -eou pipefail
3+
4+
python setup/setup.py

0 commit comments

Comments
 (0)