-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_init.py
More file actions
106 lines (85 loc) · 2.63 KB
/
Copy pathproject_init.py
File metadata and controls
106 lines (85 loc) · 2.63 KB
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
"""
Project Setup Automator
-----------------------
Creates a new Python project with venv, .gitignore,
.env, README, git init and first commit in one command.
Usage: python newproject.py
"""
import os
import sys
import subprocess
#----config-----------------------------------------
GITIGNORE = """.env
venv/
__pycache__/
*.pyc
.vscode/
"""
ENV = """# Add your API keys here
# EX_API_KEY=key_value
"""
README = """# {name}
## setup
```bash
python -m venv venv
venv\\Scripts\\activate
pip install -r requirements.txt
```
## Usage
## License
MIT
"""
#-------------------------------------------------------
def run(cmd, cwd=None):
subprocess.run(cmd, shell=True, cwd=cwd, check=True)
def create_project(name, packages):
base = os.path.join(os.getcwd(), name)
## 1. create dir
os.makedirs(base, exist_ok=True)
print(f"Created folder: {name}")
## 2. create files
with open(os.path.join(base, ".gitignore"), 'w') as f:
f.write(GITIGNORE)
with open(os.path.join(base, ".env"), 'w') as f:
f.write(ENV)
with open(os.path.join(base, "README.md"), 'w') as f:
f.write(README.format(name=name))
with open(os.path.join(base, "main.py"), 'w') as f:
f.write(f"#{name}\n")
with open(os.path.join(base, "requirements.txt"), 'w') as f:
f.write("# Add packages here or run: pip freeze > requirements.txt\n")
print("Created .gitignore, .env, README.md, main.py, requirements.txt")
## 3. create venv
run("python -m venv venv", cwd=base)
print("Created venv")
## 4. Install packages
if packages:
python = os.path.join(base, "venv", "Scripts", "python.exe")
run(f'"{python}" -m pip install {" ".join(packages)}', cwd=base)
run(f'"{python}" -m pip freeze > requirements.txt', cwd=base)
print(f'Installed packages: [{", ".join(packages)}]')
## 5. git init + first commit
run("git init", cwd=base)
run("git add .", cwd=base)
run('git commit -m "init project structure"', cwd=base)
print("Git initialized + first commit done")
print(f"""
Project ready: '{name}'
-----------------------------------
Next steps:
$ cd {name}
-> Create Github repo and run:
$ git remote add origin <url>
$ git push -u origin main
-----------------------------------
""")
def main():
name = input("Project name: ").strip()
if not name:
print("Name can't be empty.")
sys.exit(1)
pkgs = input("Packages to be installed (space separated, Enter to skip:)").strip()
pkgs = pkgs.split() if pkgs else []
create_project(name, pkgs)
if __name__=="__main__":
main()