Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@ def modify_portal_properties(repo_url):
inp.write(repo_url+"/blob/master/LICENSE.md")


def check_precommit_module() -> None:
if subprocess.run(["python", "-m", "pip", "show", "pre-commit"]).returncode != 0:
print('\n[COOKIECUTTER][ERROR]: Pre-commit module is not installed! Installing..')
subprocess.run(["python", "-m", "pip", "install", "pre-commit"])
else:
print('\n[COOKIECUTTER][INFO]: Module pre-commit is already installed. Proceeding...')

print('\n[COOKIECUTTER][INFO]: Installing pre-commit hooks...')
subprocess.run(["pre-commit", "install"])


def check_virtualenv_module() -> None:
if subprocess.run(["python", "-m", "virtualenv", "--version"]).returncode != 0:
print('\n[COOKIECUTTER][ERROR]: Virtualenv module is not installed! Installing...')
subprocess.run(["python", "-m", "pip", "install", "virtualenv"])
check_virtualenv_module()
else:
print('\n[COOKIECUTTER][INFO]: Module virtualenv is already installed. Proceeding...')


def create_venv_and_install_libraries() -> None:
print('\n[COOKIECUTTER][INFO]: Creating virtual environment...')
subprocess.run(["python", "-m", "virtualenv", "venv"])
print('\n[COOKIECUTTER][INFO]: Virtual environment created.')

if os.name.lower().startswith('nt'):
pip_exec = os.path.join('venv', 'Scripts', 'pip')

else:
pip_exec = os.path.join('venv', 'bin', 'pip')

print('\n[COOKIECUTTER][INFO]: Installing libraries...')
subprocess.run([pip_exec, "install", "-r", "requirements.txt"])
print('\n[COOKIECUTTER][INFO]: Libraries installed. Proceeding...')


# remove redundant files and directories
platform = '{{ cookiecutter.template_variant }}'
repo_url = '{{ cookiecutter.repository_url }}'

Expand All @@ -38,23 +75,36 @@ def modify_portal_properties(repo_url):

def handle_error(err_out):
if err_out:
print(f"Command failed with error: {err_out}")
print(f"\n[COOKIECUTTER][ERROR]: Command failed with error: {err_out}")
exit(1)

# initialize GitHub repository
print("Initializing github repository")
print("\n[COOKIECUTTER][INFO]: Initializing github repository")
subprocess.run(["git", "init"])

if repo_url:
print(f'\nSetting up remote to {repo_url}')
print(f'\n[COOKIECUTTER][INFO]: Setting up remote to {repo_url}')
subprocess.run(["git", "remote", "add", "origin", repo_url])

print("\nAdding first commit")
print("\n[COOKIECUTTER][INFO]: Adding first commit")
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", '"Initial commit"'])

if not repo_url:
print(
'\n WARNING: No repository_url was set. To set the remote to your repository please use following command:\n '
'\n [COOKIECUTTER][WARNING]: No repository_url was set. To set the remote to your repository please use following command:\n '
'git remote add '
'origin PATH_TO_YOUR_REPOSITORY')


# virtualenv setup process
print("\n[COOKIECUTTER][INFO]: Pre-commit module checking...")
check_precommit_module()
print("\n[COOKIECUTTER][INFO]: Virtual environment setup process starting...")
check_virtualenv_module()
print("\n[COOKIECUTTER][INFO]: Virtual environment module was checked. Proceeding...")
create_venv_and_install_libraries()
print("\n[COOKIECUTTER][INFO]: Virtual environment setup process completed.")

project_name = '{{ cookiecutter.repository_folder_name }}'
print(f"\n[COOKIECUTTER][INFO]: Project \"{project_name}\" initialized successfully!")
32 changes: 32 additions & 0 deletions {{cookiecutter.repository_folder_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
args:
- --max-line-length=120
exclude:
- .git
- __pycache__
- tests
- example
- venv

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml

# F812: list comprehension redefines ...
# H101: Use TODO(NAME)
# H202: assertRaises Exception too broad
# H233: Python 3.x incompatible use of print operator
# H301: one import per line
# H306: imports not in alphabetical order (time, os)
# H401: docstring should not start with a space
# H403: multi line docstrings should end on a new line
# H404: multi line docstring should start without a leading new line
# H405: multi line docstring summary not separated with an empty line
# H501: Do not use self.__dict__ for string formatting