Skip to content

Instantly share code, notes, and snippets.

@costa86
Last active May 31, 2023 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save costa86/dc3b3c407cba65499bb2200eecb11496 to your computer and use it in GitHub Desktop.
Save costa86/dc3b3c407cba65499bb2200eecb11496 to your computer and use it in GitHub Desktop.
python project creator
import os
from datetime import datetime
current_year = datetime.now().year
project_path = "/home/costa/coding/python"
author = "Lourenço Costa"
new_project = ""
gitignore_content = """
# Ignore compiled Python files
*.pyc
# Ignore files generated by popular Python IDEs and editors
/.vscode/
/.idea/
# Ignore the virtual environment directory
venv/
# Ignore logs, cache files and other build artifacts
*.log
__pycache__/
*.cache
*.egg-info/
dist/
build/
*.pyo
*.bak
*.swp
*.tmp
*.so
*.out
*.pyd
"""
license_content = f"""MIT License
[Copyright (c) {current_year} {author}]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
requirements_content = "pylint\nmypy"
makefile_content = """
REQ := requirements.txt
VE := "virtual environment"
VENV_PATH := venv/bin/activate
MF := main.py
VENV_NAME := venv
help:
@echo "USAGE: make <command>"
@echo "[INFO] All commands will be executed in a new $(VE) named $(VENV_NAME)"
@echo "AVAILABLE COMMANDS:"
@echo "----"
@echo "run: Run $(MF)"
@echo "add lib=<library_name>: Install library and add it to $(REQ)"
@echo "clear: Delete all libraries and clear contents of $(REQ)"
@echo "list: List installed libraries"
@echo "del lib=<library_name>: Uninstall library and remove it from $(REQ)"
@echo "install: Install dependencies in $(REQ)"
@echo "sync: Syncronize libraries in $(REQ) with their actual installed version"
@echo "validate: Run mypy (check types) and pylint (check stylistic guidelines) \
for all Python files in the current directory"
@echo "----"
@echo "ABOUT THE CREATED FILES:"
@echo "----"
@echo ".pylint: Settings for pylint's CLI"
@echo ".gitignore: Files and directories to be ignored by git version control"
@echo "LICENSE: Legal information about the usage of the project"
@echo "$(MF): A Python file to start the project with"
@echo "README.md: Used to document the project"
@echo "$(REQ): Holds third-party dependencies/libraries for the project"
upgrade_pip:
@. $(VENV_PATH) && pip install --upgrade pip
venv:
@echo "[INFO] Creating new $(VE)"
@if [ ! -d $(VENV_NAME) ]; then python -m venv $(VENV_NAME) && \
echo "[INFO] New $(VE) created"; fi
@if [ ! -d $(REQ) ]; then touch $(REQ); fi
@$(MAKE) upgrade_pip
sort:
@sort $(REQ) -o $(REQ)
run: venv
@if [ ! -f $(MF) ]; then echo "print('Hello, world!')" > $(MF); fi
@. $(VENV_PATH) && python $(MF)
add: venv
@if [ -z "$(lib)" ]; then \
echo "[ERROR] library name (lib=<library_name>) is required"; \
elif ! grep -q "^$(lib)$$" $(REQ); then \
echo $(lib) >> $(REQ); \
echo "[INFO] Added $(lib) to $(REQ)"; \
. $(VENV_PATH) && pip install $(lib); \
else \
echo "[WARNING] $(lib) is already in $(REQ)"; \
fi
@$(MAKE) sort
list: venv
@. $(VENV_PATH) && pip list
@$(MAKE) sort
clear: venv
@if [ ! -s $(REQ) ]; then \
echo "No library requirements found in $(REQ). Clearing skipped."; \
else \
echo "Deleting all libraries and clearing contents of $(REQ)..."; \
echo > $(REQ); \
. $(VENV_PATH) && pip freeze | xargs pip uninstall -y; \
fi
validate: venv
@. $(VENV_PATH) && pylint *.py && mypy *.py
del: venv
@if [ -z "$(lib)" ]; then \
echo "[ERROR] library name (lib=<library_name>) is required."; \
elif ! grep -q "^$(lib)$$" $(REQ); then \
echo "[WARNING] $(lib) is not in $(REQ)"; \
else \
sed -i "/^$(lib)$$/d" $(REQ); \
. $(VENV_PATH) && pip uninstall -y $(lib); \
echo "[INFO] Removed $(lib) from $(REQ) and uninstalled it"; \
fi
install: venv
@if [ ! -s $(REQ) ]; then \
echo "No library requirements found in $(REQ). Installation skipped."; \
else \
echo "Installing librarys from $(REQ)..."; \
. $(VENV_PATH) && pip install -r $(REQ); \
$(MAKE) sort; \
fi
sync: venv
@if [ ! -s $(REQ) ]; then \
echo "No library requirements found in $(REQ). Synchronization skipped."; \
else \
echo "Checking pip libraries..."; \
. $(VENV_PATH) && pip freeze | tr A-Z a-z > installed_libraries.txt; \
echo "Matching libraries in $(REQ) with installed libraries..."; \
cp $(REQ) new_requirements.txt && { \
while read requirement; do \
match=$$(grep -i "^$$requirement==" installed_libraries.txt); \
if [ -n "$$match" ]; then \
sed -i "/^$$requirement\$$/d" new_requirements.txt && echo "$$match" >> new_requirements.txt; \
fi; \
done < $(REQ); \
} && mv new_requirements.txt $(REQ); \
$(MAKE) sort; \
rm installed_libraries.txt; \
echo "Synchronized libraries in $(REQ) with their installed versions in $(VE)."; \
fi
"""
pylint_content = """
[MESSAGES_CONTROL]
disable=C0114,C0116
[REPORTS]
output-format=colorized
"""
def create_file(file_name: str, content: str) -> None:
with open(file_name, "w") as f:
f.write(content)
os.chdir(project_path)
while 1:
print("Current projects: ")
projects = os.listdir(project_path)
print(projects)
new_project = input("Project name: ")
if new_project not in projects and len(new_project) > 3:
break
print("Pick another name")
os.mkdir(new_project)
os.chdir(new_project)
readme_content = f"""# {new_project}
## Description
"""
for name, content in [
(".gitignore", gitignore_content),
("LICENSE", license_content),
("README.md", readme_content),
("Makefile", makefile_content),
(".pylintrc", pylint_content),
("requirements.txt", requirements_content),
("main.py", "print('hello, world!')"),
]:
create_file(name, content)
os.system("code .")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment