Skip to content

Commit 5daa1ca

Browse files
authored
Merge branch 'sihrc:main' into main
2 parents d66db0f + d1bac42 commit 5daa1ca

File tree

12 files changed

+151
-106
lines changed

12 files changed

+151
-106
lines changed

.pre-commit-config.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- repo: https://github.com/PyCQA/autoflake
9+
rev: v2.2.0
10+
hooks:
11+
- id: autoflake
12+
- repo: https://github.com/pycqa/isort
13+
rev: 5.12.0
14+
hooks:
15+
- id: isort
16+
name: isort (python)
17+
args: ["--profile", "black"]
18+
- repo: https://github.com/psf/black
19+
rev: 23.7.0
20+
hooks:
21+
- id: black
22+
- repo: https://github.com/pre-commit/mirrors-mypy
23+
rev: "v1.4.1" # Use the sha / tag you want to point at
24+
hooks:
25+
- id: mypy
26+
pass_filenames: false
27+
args: ["--config-file", "pyproject.toml"]

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,45 @@
22

33
This is a [cookiecutter](https://github.com/cookiecutter/cookiecutter) template used to generate Python 3.10+ poetry projects.
44

5-
65
## Features
6+
77
#### Poetry
8+
89
local directory installation for vscode interpreter magic.
910

1011
#### Docker
12+
1113
Dockerfile with python 3.10 base image and package installation
1214

1315
#### Pre-commit
16+
1417
Python's Isort for import formatting
1518
Python's Black for strict formatting
1619

1720
#### vscode settings (optional)
21+
1822
VSCode settings that use black and the local poetry python package for auto-completes and module discovery
1923

2024
#### dotenv loading (optional)
21-
For application type packages that have .env provided runtime configurations
2225

26+
For application type packages that have .env provided runtime configurations
2327

2428
## Installation
29+
2530
```bash
2631
pip3 install cookiecutter
2732
```
2833

2934
## Usage
35+
3036
```bash
3137
cookiecutter gh:IndicoDataSolutions/cookiecutter-poetry-py3-10
3238
```
3339

3440
The cookiecutter generation hooks will automatically setup all features and dependency necessary for this project.
3541

3642
## Pre-commit Hooks
43+
3744
```yaml
3845
repos:
3946
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -53,3 +60,26 @@ repos:
5360
hooks:
5461
- id: black
5562
```
63+
64+
## Private Repos
65+
66+
There is an option to enable ssh mounting to the docker build process that allows poetry to install private github repositories added to the pyproject.toml like so:
67+
68+
```toml
69+
lib_name = {git = "ssh://git@github.com/user_or_org/lib_name.git", branch = "main", tag="0.1.0", version=0.1.0}
70+
```
71+
72+
It requires `SSH_AUTH_SOCK` environment variable to be set, which can be done with the following command:
73+
74+
```bash
75+
eval $(ssh-agent)
76+
```
77+
78+
It also requires the keys you want to use for SSH access to be tracked in the ssh agent. If you don't have these setup, you can add them like so:
79+
80+
```bash
81+
ssh-add -L # lists keys currently added
82+
ssh-add [optional key]
83+
```
84+
85+
In order to have ssh-agent & keys automatically added, you can follow the instructions [here](https://unix.stackexchange.com/a/90869)

cookiecutter.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"author": "sudocoder dev",
3-
"email": "dev@sudocodes.dev",
4-
"project_name": "Poetry Python 3.10 Library",
5-
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_') }}",
6-
"project_description": "Cut from cookiecutter-poetry-py3-10",
7-
"version": "0.1.0",
8-
"use_vscode": "y",
9-
"use_dotenv": "y"
10-
}
2+
"author": "Cookie Loving Engineer",
3+
"email": "ilovecookies@imanengineer.io",
4+
"project_name": "Poetry Python 3.10 Library",
5+
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_') }}",
6+
"project_description": "Cut from cookiecutter-poetry-py3-10",
7+
"version": "0.1.0"
8+
}

hooks/post_gen_project.py

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
11
#! /usr/bin/env python
2-
import io
32
import json
43
import logging
5-
import os
64
import sys
75
import typing as t
8-
from contextlib import redirect_stdout
9-
from multiprocessing import Process
106
from pathlib import Path
117

12-
from poetry.console import Application
8+
import sh
139

1410
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
1511

1612

1713
def run_poetry_command(*args_list: t.Iterable[str]):
18-
app = Application()
19-
app.config.set_terminate_after_run(False)
20-
output_buffer = io.StringIO()
21-
22-
with redirect_stdout(output_buffer):
23-
sys.argv[1:] = args_list
24-
logging.info(f"Poetry command: {' '. join(args_list)}")
25-
app.run()
26-
27-
stdout = output_buffer.getvalue()
28-
29-
# remove exit code
30-
return stdout.split()
14+
return str(sh.poetry(*args_list))
3115

3216

3317
def try_remove(path: t.Union[str, Path]):
3418
# Coerce into path obj
35-
path: Path = Path(path)
19+
path = Path(path)
3620

3721
# Delete either file or dir (true if exists)
3822
if path.is_file():
@@ -58,7 +42,7 @@ def update_json(
5842
Returns:
5943
settings_json (t.Dict): updated settings dictionary
6044
"""
61-
path: Path = Path(path)
45+
path = Path(path)
6246
if not path.is_file():
6347
raise ValueError(f"Invalid path to json file {path}")
6448

@@ -74,49 +58,25 @@ def update_json(
7458

7559

7660
def main():
77-
os.system("git init")
78-
os.system("git checkout -b main")
61+
sh.git("init")
62+
sh.git.checkout("-b", "main")
7963

8064
# Commands to run
8165
for command in (
82-
# ("config", "virtualenvs.in-project", "true", "--local"),
66+
(
67+
"config",
68+
"virtualenvs.in-project",
69+
"true",
70+
),
71+
("add", "--dev", "pytest", "black", "isort", "ipdb"),
8372
("update",),
8473
("install",),
8574
):
8675
run_poetry_command(*command)
8776

88-
# TODO: Handle vscode / dotenv options
89-
if "{{ cookiecutter.use_vscode }}" == "y":
90-
logging.info("Setting up vscode python interpreter path")
91-
92-
# Fetch path to python interpreter for this poetry env
93-
path = run_poetry_command("env", "info", "--path")[0]
94-
95-
logging.info(f"Setting python interpreter path to {path}")
96-
97-
# Add environment python interpreter to vscode settings.json
98-
updated_settings = update_json(
99-
Path(".vscode", "settings.json"), {"python.defaultInterpreterPath": path}
100-
)
101-
logging.info(f"vscode settings successfully updated {updated_settings}")
102-
else:
103-
logging.info("No vscode: deleting .vscode settings directory")
104-
# Remove .vscode/
105-
try_remove(".vscode")
106-
107-
if "{{ cookiecutter.use_dotenv }}" != "y":
108-
# Remove .env & config.py
109-
logging.info("No dotenv: deleting boilerplate .env and config.py")
110-
try_remove("{{ cookiecutter.project_slug }}", "config.py")
111-
112-
with open(".gitignore", "a") as f:
113-
f.write(".vscode/\n")
114-
115-
p = Process(target=run_poetry_command, args=("run", "pre-commit", "install", "-f"))
116-
p.start()
117-
p.join()
118-
119-
os.system("git add -A; git commit -m 'Cut a cookie from a cookiecutter'")
77+
getattr(sh, "pre-commit").install("-f")
78+
sh.git.add("-A")
79+
sh.git.commit("-m", "'Cut a cookie from a cookiecutter'")
12080

12181

12282
if __name__ == "__main__":
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.git/
2+
3+
build
4+
dist
5+
*.egg-info
6+
*.egg/
7+
*.pyc
8+
*.swp
9+
10+
.tox
11+
.coverage
12+
html/*
13+
__pycache__
14+
15+
# Compiled Documentation
16+
docs/_build

{{cookiecutter.project_slug}}/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
poetry.lock
106+
.vscode/
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.3.0
3+
rev: v4.4.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
8+
- repo: https://github.com/PyCQA/autoflake
9+
rev: v2.2.0
10+
hooks:
11+
- id: autoflake
812
- repo: https://github.com/pycqa/isort
9-
rev: 5.10.1
13+
rev: 5.12.0
1014
hooks:
1115
- id: isort
1216
name: isort (python)
1317
args: ["--profile", "black"]
1418
- repo: https://github.com/psf/black
15-
rev: 22.3.0
19+
rev: 23.7.0
1620
hooks:
1721
- id: black
22+
- repo: https://github.com/pre-commit/mirrors-mypy
23+
rev: "v1.4.1" # Use the sha / tag you want to point at
24+
hooks:
25+
- id: mypy
26+
pass_filenames: false
27+
args: ["--config-file", "pyproject.toml"]
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
FROM python:3.10
1+
FROM python:3.10.6 as builder
2+
3+
ENV PATH="/root/.local/bin:${PATH}"
4+
5+
RUN curl -sSL https://install.python-poetry.org | python3 - && \
6+
poetry self update --preview && \
7+
poetry config virtualenvs.in-project true
8+
9+
10+
COPY pyproject.toml poetry.lock /venv/
11+
12+
WORKDIR /venv/
13+
14+
RUN poetry install
15+
16+
FROM python:3.10.6
217

318
LABEL version="{{cookiecutter.version}}"
419
LABEL author="{{cookiecutter.author}}"
520
LABEL email="{{cookiecutter.email}}"
621
LABEL description="{{cookiecutter.project_description}}"
722

8-
ENV PATH=/{{cookiecutter.project_slug}}/bin:/root/.poetry/bin:${PATH}
23+
ENV PYTHONPATH=/{{cookiecutter.project_slug}} PATH=/venv/.venv/bin:/{{cookiecutter.project_slug}}/bin:/{{cookiecutter.project_slug}}/scripts:${PATH}
24+
COPY --from=builder /venv /venv
925

10-
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -
26+
RUN apt update
1127

12-
COPY . /{{cookiecutter.project_slug}}
13-
WORKDIR /{{cookiecutter.project_slug}}
28+
WORKDIR /{{ cookiecutter.project_slug }}
29+
COPY . /{{ cookiecutter.project_slug }}
1430

15-
# no need for virtualenv in docker
16-
RUN poetry config virtualenvs.create false \
17-
&& poetry install --no-interaction --no-ansi
1831

19-
CMD ["bash"]
32+
ENTRYPOINT [ "bash" ]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: "3"
2+
3+
services:
4+
{{ cookiecutter.project_slug }}:
5+
build: .
6+
container_name: {{ cookiecutter.project_slug }}
7+
env_file: .env
8+
tty: true
9+
entrypoint: ash
10+
volumes:
11+
- .:/{{ cookiecutter.project_slug }}

{{cookiecutter.project_slug}}/pyproject.toml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ name = "{{ cookiecutter.project_name }}"
33
version = "{{ cookiecutter.version }}"
44
description = "{{cookiecutter.project_description}}"
55
authors = ["{{cookiecutter.author}} <{{cookiecutter.email}}>"]
6-
packages = [ {include = "{{ cookiecutter.project_slug }}"}]
76

87
[tool.poetry.dependencies]
98
python = "^3.10"
10-
setuptools = "^57.4.0"
11-
pytest = {version = "^6.2.4", extras = ["test"]}
12-
{% if cookiecutter.use_dotenv == 'y' %}python-dotenv = "^0.19.2"{%- endif %}
139

14-
15-
[tool.poetry.dev-dependencies]
16-
pytest = {version = "^6.2.4"}
17-
black = {version = "^22.3.0", allow-prereleases = true}
18-
isort = {version = "^5.10.1", allow-prereleases = true}
19-
ipdb = {version = "^0.13.9", allow-prereleases = true}
10+
[tool.poetry.group.dev.dependencies]
11+
ipdb = "^0.13.13"
12+
black = "^23.9.1"
13+
isort = "^5.12.0"
14+
pytest = "^7.4.2"
15+
pytest-asyncio = "^0.21.1"
2016

2117
[tool.pytest.ini_options]
2218
addopts = "-ra -q"

0 commit comments

Comments
 (0)