-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_python_project.py
155 lines (112 loc) · 3.96 KB
/
create_python_project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
import argparse
from pathlib import Path
import subprocess
from urllib.request import urlopen
DOCKERFILE_CONTENT = """
FROM python:3.13-slim-bullseye
RUN useradd --create-home --home-dir /app --shell /bin/bash app
WORKDIR /app
COPY requirements ./requirements
RUN pip install --disable-pip-version-check --no-cache-dir -r requirements/{}.txt
COPY . .
USER app
"""
DOCKERIGNORE_SOURCE = 'https://raw.githubusercontent.com/GoogleCloudPlatform/getting-started-python/main/optional-kubernetes-engine/.dockerignore' # noqa: E501
DOCKER_COMPOSE_CONTENT = """
services:
app:
build:
context: .
dockerfile: docker/Dockerfile.dev
volumes:
- .:/app
tty: true
command: python -m src.main
"""
DEV_REQUIREMENTS_CONTENT = """
-r base.txt
flake8
flake8-print
flake8-multiline-containers
flake8-builtins
flake8-import-order
flake8-commas
flake8-quotes
flake8-broken-line
flake8-clean-block
flake8-walrus
"""
FLAKE8_CONTENT = """
[flake8]
max-line-length = 100
import-order-style = google
application-import-names = src
exclude = venv
"""
PYRIGHT_CONTENT = """
{
"venvPath": ".",
"venv": "venv"
}
"""
GITIGNORE_SOURCE = 'https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore'
GIT_BRANCH = 'develop'
GIT_COMMIT = 'initial commit'
def main() -> None:
parser = argparse.ArgumentParser(description='Create project directory structure')
parser.add_argument('project_name', type=str, help='Name of the project')
args = parser.parse_args()
project_dir = Path(args.project_name)
create_src_dir(project_dir)
_create_requirements_dir(project_dir)
_create_docker_dir(project_dir)
_create_dockerignore(project_dir)
_create_docker_compose(project_dir)
_create_flake8_config(project_dir)
_create_pyright_config(project_dir)
_create_gitignore(project_dir)
_init_git(project_dir)
def create_src_dir(project_dir: Path) -> None:
code_dir = project_dir / 'src'
code_dir.mkdir(parents=True)
(code_dir / '__init__.py').touch()
(code_dir / 'main.py').touch()
def _create_requirements_dir(project_dir: Path) -> None:
requirements_dir = project_dir / 'requirements'
requirements_dir.mkdir()
(requirements_dir / 'base.txt').touch()
with open(requirements_dir / 'dev.txt', 'w') as file:
file.write(DEV_REQUIREMENTS_CONTENT.lstrip())
def _create_docker_dir(project_dir: Path) -> None:
docker_dir = project_dir / 'docker'
docker_dir.mkdir()
with open(docker_dir / 'Dockerfile', 'w') as file:
file.write(DOCKERFILE_CONTENT.format('base').lstrip())
with open(docker_dir / 'Dockerfile.dev', 'w') as file:
file.write(DOCKERFILE_CONTENT.format('dev').lstrip())
def _create_dockerignore(project_dir: Path) -> None:
with urlopen(DOCKERIGNORE_SOURCE) as response:
gitignore_content = response.read().decode('utf-8')
with open(project_dir / '.dockerignore', 'w') as file:
file.write(gitignore_content)
def _create_docker_compose(project_dir: Path) -> None:
with open(project_dir / 'docker-compose.yml', 'w') as file:
file.write(DOCKER_COMPOSE_CONTENT.lstrip())
def _create_flake8_config(project_dir: Path) -> None:
with open(project_dir / '.flake8', 'w') as file:
file.write(FLAKE8_CONTENT.lstrip())
def _create_pyright_config(project_dir: Path) -> None:
with open(project_dir / 'pyrightconfig.json', 'w') as file:
file.write(PYRIGHT_CONTENT.lstrip())
def _create_gitignore(project_dir: Path) -> None:
with urlopen(GITIGNORE_SOURCE) as response:
gitignore_content = response.read().decode('utf-8')
with open(project_dir / '.gitignore', 'w') as file:
file.write(gitignore_content)
def _init_git(project_dir: Path) -> None:
subprocess.run(['git', 'init', project_dir, '--initial-branch', GIT_BRANCH])
subprocess.run(['git', '-C', project_dir, 'add', '.'])
subprocess.run(['git', '-C', project_dir, 'commit', '-m', GIT_COMMIT])
if __name__ == '__main__':
main()