From 33a36ed15ddbf2200d226ac805f75c0bd20b9717 Mon Sep 17 00:00:00 2001 From: Ali Yaman Date: Thu, 7 Nov 2024 09:49:22 +0100 Subject: [PATCH] Refactor pre-commit configuration and update setup.py for consistency --- .pre-commit-config.yaml | 63 +++------- djeasy/client/management.py | 240 ++++++++++++++++++++++++++---------- setup.py | 52 ++++---- 3 files changed, 217 insertions(+), 138 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fd0aee0..27d633b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,55 +1,26 @@ repos: - - repo: https://github.com/asottile/pyupgrade - rev: v2.37.1 - hooks: - - id: pyupgrade - language_version: python3.8 - - - repo: https://github.com/adamchainz/django-upgrade - rev: '1.7.0' - hooks: - - id: django-upgrade - args: [ --target-version, "3.1" ] - - - repo: https://github.com/pycqa/isort - rev: 5.10.1 - hooks: - - id: isort - name: isort (python) - - - repo: https://github.com/python/black - rev: '22.6.0' - hooks: - - id: black - exclude: ^.*\b(migrations)\b.*$ - language_version: python3.8 - args: [ - --skip-string-normalization, - -l 120, - ] - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.3.0 + rev: v5.0.0 hooks: + - id: check-added-large-files + - id: check-toml + - id: check-yaml + args: + - --unsafe - id: end-of-file-fixer - - id: requirements-txt-fixer - - id: detect-private-key - - id: check-executables-have-shebangs - - id: check-case-conflict - id: trailing-whitespace - - id: check-symlinks - - id: flake8 - - repo: https://github.com/PyCQA/pylint - rev: v2.14.5 + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.7.2 hooks: - - id: pylint - name: pylint - entry: pylint - language: system - exclude: ^.*\b(migrations)\b.*$ - types: [ python ] + - id: ruff args: - - --errors-only - - --rcfile=.pylintrc - - --load-plugins=pylint_django + - --fix + - id: ruff-format + + - repo: https://github.com/AliYmn/conventional-commits-check + rev: v2.0.5 + hooks: + - id: conventional-commits-check + stages: [commit-msg] diff --git a/djeasy/client/management.py b/djeasy/client/management.py index c5cffff..7883f02 100644 --- a/djeasy/client/management.py +++ b/djeasy/client/management.py @@ -18,7 +18,14 @@ class EasyInstall: """Package Install and Settings""" def __init__( - self, project_name, server_name_or_ip, static_url, gunicorn_file, nginx_file, project_file, virtualenv_file + self, + project_name, + server_name_or_ip, + static_url, + gunicorn_file, + nginx_file, + project_file, + virtualenv_file, ): """ :param project_name: Django project folder @@ -44,95 +51,140 @@ def __init__( def __call__(self, *args, **kwargs): """Packages Loader""" - for package in self.data['package']: - cprint("{}".format(package['message']), 'white', 'on_red', attrs=['bold']) - subprocess.call(package['name'], shell=True) + for package in self.data["package"]: + cprint("{}".format(package["message"]), "white", "on_red", attrs=["bold"]) + subprocess.call(package["name"], shell=True) # Restart. subprocess.call("sudo systemctl daemon reload", shell=True) subprocess.call("sudo systemctl restart nginx", shell=True) # Successfull package - cprint("The packages have been successfully installed.", 'white', 'on_red', attrs=['bold']) + cprint( + "The packages have been successfully installed.", + "white", + "on_red", + attrs=["bold"], + ) def __add__(self): """It records Gunicorn vs nginx files.""" # gunicorn file save and move with open("{}/client/file/gunicorn.service".format(BASE_DIR)) as gunicorn_files: - gunicorn = gunicorn_files.read().format(self.project_name, self.project_file, self.virtualenv_file) - file_gunicorn = open('{}/package/{}.service'.format(BASE_DIR, self.gunicorn_file), 'w') + gunicorn = gunicorn_files.read().format( + self.project_name, self.project_file, self.virtualenv_file + ) + file_gunicorn = open( + "{}/package/{}.service".format(BASE_DIR, self.gunicorn_file), "w" + ) file_gunicorn.write(gunicorn) file_gunicorn.flush() file_gunicorn.close() - cprint("{}.service file created.".format(self.gunicorn_file), 'white', 'on_red', attrs=['bold']) + cprint( + "{}.service file created.".format(self.gunicorn_file), + "white", + "on_red", + attrs=["bold"], + ) # File move operations subprocess.call( - "sudo cp {}/package/{}.service /etc/systemd/system/".format(BASE_DIR, self.gunicorn_file), shell=True + "sudo cp {}/package/{}.service /etc/systemd/system/".format( + BASE_DIR, self.gunicorn_file + ), + shell=True, ) # Gunicorn recording and restart - subprocess.call("sudo systemctl start {0} & sudo systemctl enable {0}".format(self.gunicorn_file), shell=True) - cprint("Gunicorn was registered and restarted.", 'white', 'on_red', attrs=['bold']) + subprocess.call( + "sudo systemctl start {0} & sudo systemctl enable {0}".format( + self.gunicorn_file + ), + shell=True, + ) + cprint( + "Gunicorn was registered and restarted.", "white", "on_red", attrs=["bold"] + ) # nginx file save and move with open("{}/client/file/nginx".format(BASE_DIR)) as nginx_files: nginx = nginx_files.read().format( - self.server_name_or_ip, self.static_url, self.project_file, self.project_name + self.server_name_or_ip, + self.static_url, + self.project_file, + self.project_name, ) - nginx_file = nginx.replace('[', '{').replace(']', '}') + nginx_file = nginx.replace("[", "{").replace("]", "}") - file_nignx = open("{}/package/{}".format(BASE_DIR, self.nginx_file), 'w') + file_nignx = open("{}/package/{}".format(BASE_DIR, self.nginx_file), "w") file_nignx.write(nginx_file) file_nignx.flush() file_nignx.close() - cprint("{} file created.".format(self.nginx_file), 'white', 'on_red', attrs=['bold']) + cprint( + "{} file created.".format(self.nginx_file), + "white", + "on_red", + attrs=["bold"], + ) # File move operations subprocess.call( - "sudo cp {}/package/{} /etc/nginx/sites-available/".format(BASE_DIR, self.nginx_file), shell=True + "sudo cp {}/package/{} /etc/nginx/sites-available/".format( + BASE_DIR, self.nginx_file + ), + shell=True, ) # Nginx recording and restart subprocess.call( - "sudo ln -s /etc/nginx/sites-available/{} /etc/nginx/sites-enabled".format(self.nginx_file), shell=True + "sudo ln -s /etc/nginx/sites-available/{} /etc/nginx/sites-enabled".format( + self.nginx_file + ), + shell=True, ) def __copy__(self): """Gunicorn and nginx setting files""" # Gunicorn - for gunicorn_package in self.data['gunicorn']: - cprint(gunicorn_package['message'], 'white', 'on_red', attrs=['bold']) - subprocess.call(gunicorn_package['name'], shell=True) + for gunicorn_package in self.data["gunicorn"]: + cprint(gunicorn_package["message"], "white", "on_red", attrs=["bold"]) + subprocess.call(gunicorn_package["name"], shell=True) - cprint("Gunicorn successful!", 'white', 'on_red', attrs=['bold']) + cprint("Gunicorn successful!", "white", "on_red", attrs=["bold"]) # Nginx - for nginx_package in self.data['nginx']: - cprint(nginx_package['message'], 'white', 'on_red', attrs=['bold']) - subprocess.call(nginx_package['name'], shell=True) + for nginx_package in self.data["nginx"]: + cprint(nginx_package["message"], "white", "on_red", attrs=["bold"]) + subprocess.call(nginx_package["name"], shell=True) - cprint("Nginx successful!", 'white', 'on_red', attrs=['bold']) + cprint("Nginx successful!", "white", "on_red", attrs=["bold"]) def requirements(self): """requirements.txt install""" subprocess.call( - '{}/bin/pip3 install -r {}/requirements.txt'.format(self.virtualenv_file, self.project_file), shell=True + "{}/bin/pip3 install -r {}/requirements.txt".format( + self.virtualenv_file, self.project_file + ), + shell=True, ) - subprocess.call('{}/bin/pip3 install gunicorn'.format(self.virtualenv_file), shell=True) - cprint("requirements.txt successfully loaded.!", 'white', 'on_red', attrs=['bold']) + subprocess.call( + "{}/bin/pip3 install gunicorn".format(self.virtualenv_file), shell=True + ) + cprint( + "requirements.txt successfully loaded.!", "white", "on_red", attrs=["bold"] + ) def save(self): """Records information.""" - with open('{}/client/file/server.info'.format(BASE_DIR)) as server_file: + with open("{}/client/file/server.info".format(BASE_DIR)) as server_file: server_file = server_file.read().format( self.project_name, self.server_name_or_ip, @@ -143,23 +195,30 @@ def save(self): self.virtualenv_file, ) - file_fix = server_file.replace('[', '{').replace(']', '}') - file = open('/home/{}.json'.format(self.project_name), 'w') + file_fix = server_file.replace("[", "{").replace("]", "}") + file = open("/home/{}.json".format(self.project_name), "w") file.write(file_fix) file.flush() file.close() - cprint("/home/{}.json file created...".format(self.project_name), 'white', 'on_red', attrs=['bold']) - cprint("All successful!", 'white', 'on_red', attrs=['bold']) + cprint( + "/home/{}.json file created...".format(self.project_name), + "white", + "on_red", + attrs=["bold"], + ) + cprint("All successful!", "white", "on_red", attrs=["bold"]) - subprocess.call('sudo chmod -R 777 /home/{}.json'.format(self.project_name), shell=True) + subprocess.call( + "sudo chmod -R 777 /home/{}.json".format(self.project_name), shell=True + ) def nginx_restart(): """Nginx Restart""" subprocess.call("sudo service nginx restart", shell=True) subprocess.call("sudo systemctl restart nginx", shell=True) - cprint("Nginx has been successfully restarted.", 'white', 'on_red', attrs=['bold']) + cprint("Nginx has been successfully restarted.", "white", "on_red", attrs=["bold"]) def gunicorn_restart(project_name): @@ -168,9 +227,13 @@ def gunicorn_restart(project_name): with open("/home/{}.json".format(project_name)) as gunicorn_file: data = json.load(gunicorn_file) - subprocess.call("sudo systemctl restart {}".format(data['gunicorn_file']), shell=True) + subprocess.call( + "sudo systemctl restart {}".format(data["gunicorn_file"]), shell=True + ) - cprint("Gunicorn has been successfully restarted.", 'white', 'on_red', attrs=['bold']) + cprint( + "Gunicorn has been successfully restarted.", "white", "on_red", attrs=["bold"] + ) def RunEasy(): @@ -178,87 +241,135 @@ def RunEasy(): # server_ip_or_domain while True: - cprint("Please type in the server ip or domain address.", 'white', 'on_red', attrs=['bold']) - server_name_or_ip = str(input('server ip or domain = ')) + cprint( + "Please type in the server ip or domain address.", + "white", + "on_red", + attrs=["bold"], + ) + server_name_or_ip = str(input("server ip or domain = ")) if server_name_or_ip == "": - cprint("Please do not leave blank, try again...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please do not leave blank, try again...", + "white", + "on_red", + attrs=["bold"], + ) continue else: if domain(server_name_or_ip) or ipv4(server_name_or_ip): break else: - cprint("Please enter a valid address...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please enter a valid address...", "white", "on_red", attrs=["bold"] + ) continue # Static Url while True: - cprint("Write your STATIC_URL (Django Settings.py)", 'white', 'on_red', attrs=['bold']) - static_url = str(input('STATIC_URL = ')) + cprint( + "Write your STATIC_URL (Django Settings.py)", + "white", + "on_red", + attrs=["bold"], + ) + static_url = str(input("STATIC_URL = ")) if static_url == "": - cprint("Please do not leave blank, try again...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please do not leave blank, try again...", + "white", + "on_red", + attrs=["bold"], + ) continue else: break # guniorn file name while True: - cprint("Write your gunicorn file name", 'white', 'on_red', attrs=['bold']) - gunicorn_file = str(input('Gunicorn File name = ')) + cprint("Write your gunicorn file name", "white", "on_red", attrs=["bold"]) + gunicorn_file = str(input("Gunicorn File name = ")) if gunicorn_file == "": - cprint("Please do not leave blank, try again...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please do not leave blank, try again...", + "white", + "on_red", + attrs=["bold"], + ) continue else: break # nginx file while True: - cprint("Write your nginx file name", 'white', 'on_red', attrs=['bold']) - nginx_file = str(input('Nginx File name = ')) + cprint("Write your nginx file name", "white", "on_red", attrs=["bold"]) + nginx_file = str(input("Nginx File name = ")) if nginx_file == "": - cprint("Please do not leave blank, try again...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please do not leave blank, try again...", + "white", + "on_red", + attrs=["bold"], + ) continue else: - cprint("Write your virtualenv file path", 'white', 'on_red', attrs=['bold']) - cprint("Example : /home/DjangoEnv", 'white', 'on_red', attrs=['bold']) + cprint("Write your virtualenv file path", "white", "on_red", attrs=["bold"]) + cprint("Example : /home/DjangoEnv", "white", "on_red", attrs=["bold"]) break # virtualenv file while True: - virtualenv_file = str(input('Virtualenv File path = ')) + virtualenv_file = str(input("Virtualenv File path = ")) if virtualenv_file == "": - cprint("Please do not leave blank, try again...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please do not leave blank, try again...", + "white", + "on_red", + attrs=["bold"], + ) continue else: if os.path.isdir(virtualenv_file): break else: - cprint("No such file or directory", 'white', 'on_red', attrs=['bold']) + cprint("No such file or directory", "white", "on_red", attrs=["bold"]) continue # project file while True: - cprint("Write your Project file path", 'white', 'on_red', attrs=['bold']) - cprint("Example : /home/Blog", 'white', 'on_red', attrs=['bold']) - project_file = str(input('Project File path = ')) + cprint("Write your Project file path", "white", "on_red", attrs=["bold"]) + cprint("Example : /home/Blog", "white", "on_red", attrs=["bold"]) + project_file = str(input("Project File path = ")) if project_file == "": - cprint("Please do not leave blank, try again...", 'white', 'on_red', attrs=['bold']) + cprint( + "Please do not leave blank, try again...", + "white", + "on_red", + attrs=["bold"], + ) continue else: if os.path.isdir(project_file): break else: - cprint("No such file or directory", 'white', 'on_red', attrs=['bold']) + cprint("No such file or directory", "white", "on_red", attrs=["bold"]) continue - project_name = str(project_file).split('/')[len(str(project_file).split('/')) - 1] + project_name = str(project_file).split("/")[len(str(project_file).split("/")) - 1] easy = EasyInstall( - project_name, server_name_or_ip, static_url, gunicorn_file, nginx_file, project_file, virtualenv_file + project_name, + server_name_or_ip, + static_url, + gunicorn_file, + nginx_file, + project_file, + virtualenv_file, ) easy.__call__() easy.__add__() @@ -286,7 +397,6 @@ def main(): """ if (len(sys.argv)) == 2: - if str(sys.argv[1]) == "--create": RunEasy() @@ -297,9 +407,7 @@ def main(): print("Command not found\n", message) else: - if (len(sys.argv)) > 2: - if str(sys.argv[2]) == "--gunicorn": gunicorn_restart(sys.argv[1]) @@ -310,5 +418,5 @@ def main(): print(message) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/setup.py b/setup.py index 5967683..2ac8893 100644 --- a/setup.py +++ b/setup.py @@ -3,10 +3,10 @@ from setuptools import setup if sys.version_info[0] < 3: - with open('README.md') as f: + with open("README.md") as f: README = f.read() else: - with open('README.md', encoding='utf-8') as f: + with open("README.md", encoding="utf-8") as f: README = f.read() setup( @@ -19,34 +19,34 @@ long_description_content_type="text/markdown", long_description=README, keywords="django deploy on server", - packages=['djeasy', 'djeasy/client', 'djeasy/package'], - url='https://github.com/AliYmn/djeasy', - download_url='https://github.com/AliYmn/djeasy', + packages=["djeasy", "djeasy/client", "djeasy/package"], + url="https://github.com/AliYmn/djeasy", + download_url="https://github.com/AliYmn/djeasy", install_requires=[ - 'termcolor', - 'validators', + "termcolor", + "validators", ], include_package_data=True, zip_safe=False, classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Web Environment', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: MIT License', - 'Operating System :: OS Independent', - 'Programming Language :: Python :: 3.0', - 'Programming Language :: Python :: 3.1', - 'Programming Language :: Python :: 3.2', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: 3.10', - 'Programming Language :: Python :: 3.11', - 'Programming Language :: Python :: 3.12', + "Development Status :: 3 - Alpha", + "Environment :: Web Environment", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3.0", + "Programming Language :: Python :: 3.1", + "Programming Language :: Python :: 3.2", + "Programming Language :: Python :: 3.3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", ], - entry_points={'console_scripts': ['djeasy=djeasy.client.management:main']}, + entry_points={"console_scripts": ["djeasy=djeasy.client.management:main"]}, )