From d1bccff610e835346d940ba3b2f18ed2007ce0ad Mon Sep 17 00:00:00 2001 From: Artem Bulgakov Date: Wed, 29 Jul 2020 16:19:30 +0300 Subject: [PATCH] Format files with pre-commit hook --- .ci/move_binary.py | 40 ++++++++++++++++++++++++++++++---------- .pre-commit-config.yaml | 18 ++++++++++++++++++ entrypoint.py | 14 ++++++++++---- pyproject.toml | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.ci/move_binary.py b/.ci/move_binary.py index 9ef4e42..ff35169 100755 --- a/.ci/move_binary.py +++ b/.ci/move_binary.py @@ -1,9 +1,9 @@ #!/bin/python3 import os -from os import environ as env -import sys import shutil import subprocess +import sys +from os import environ as env binary_filename = os.path.abspath(sys.argv[1]) master_repository_directory = os.path.abspath(sys.argv[2]) @@ -14,10 +14,20 @@ directory = sys.argv[4] os.chdir(master_repository_directory) filename = os.path.basename(binary_filename) -commit_hash = subprocess.check_output(["git", "rev-parse", "--verify", "--short", "HEAD"]).decode("utf-8").strip() -commit_subject = subprocess.check_output(["git", "log", "-1", "--pretty=format:%s"]).decode("utf-8").strip() +commit_hash = ( + subprocess.check_output(["git", "rev-parse", "--verify", "--short", "HEAD"]) + .decode("utf-8") + .strip() +) +commit_subject = ( + subprocess.check_output(["git", "log", "-1", "--pretty=format:%s"]) + .decode("utf-8") + .strip() +) -is_tag = env["GITHUB_EVENT_NAME"] == "push" and env["GITHUB_REF"].startswith("refs/tags") +is_tag = env["GITHUB_EVENT_NAME"] == "push" and env["GITHUB_REF"].startswith( + "refs/tags" +) if not is_tag: is_pr = env["GITHUB_REF"].startswith("refs/pull") if is_pr: @@ -30,8 +40,12 @@ if not is_tag: filename = "-".join([*filename_split[:2], middle, *filename_split[2:]]) # Set author info to the latest commit author -author_name = subprocess.check_output(["git", "log", "-1", "--pretty=format:%an"]).decode("utf-8") -author_email = subprocess.check_output(["git", "log", "-1", "--pretty=format:%ae"]).decode("utf-8") +author_name = subprocess.check_output( + ["git", "log", "-1", "--pretty=format:%an"] +).decode("utf-8") +author_email = subprocess.check_output( + ["git", "log", "-1", "--pretty=format:%ae"] +).decode("utf-8") # Move file os.chdir(data_repository_directory) @@ -41,9 +55,15 @@ shutil.copy(binary_filename, os.path.join(directory, filename)) # Push changes subprocess.check_call(["git", "config", "user.name", author_name]) subprocess.check_call(["git", "config", "user.email", author_email]) -subprocess.check_call(["git", "pull", "--ff-only"]) # Ensure that there is no changes +subprocess.check_call( + ["git", "pull", "--ff-only"] +) # Ensure that there is no changes subprocess.check_call(["git", "add", os.path.join(directory, filename)]) -subprocess.check_call(["git", "commit", "-m", f"Add binary for {commit_hash}: \"{commit_subject}\""]) +subprocess.check_call( + ["git", "commit", "-m", f'Add binary for {commit_hash}: "{commit_subject}"'] +) subprocess.check_call(["git", "push"]) -print(f"Binary file: {env['GITHUB_SERVER_URL']}/{env['GITHUB_REPOSITORY']}/blob/{data_repository}/{directory}/{filename}") +print( + f"Binary file: {env['GITHUB_SERVER_URL']}/{env['GITHUB_REPOSITORY']}/blob/{data_repository}/{directory}/{filename}" +) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..7b5625f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,18 @@ +# Pre-commit hooks. +# python3 -m pip install pre-commit +# pre-commit install + +repos: + + # Format Python + - repo: https://github.com/psf/black + rev: stable + hooks: + - id: black + + # Sort imports + - repo: https://github.com/timothycrosley/isort + rev: 4.3.21 + hooks: + - id: isort + additional_dependencies: ["toml"] diff --git a/entrypoint.py b/entrypoint.py index 73a1552..2183d99 100755 --- a/entrypoint.py +++ b/entrypoint.py @@ -11,8 +11,8 @@ order. """ import os -from os import environ as env import subprocess +from os import environ as env def main(): @@ -134,12 +134,18 @@ def run_command(command): def set_output(repository_root, workdir): if not os.path.exists("bin"): - print("::error::Output directory does not exist. See Buildozer log for error") + print( + "::error::Output directory does not exist. See Buildozer log for error" + ) exit(1) filename = [ - file for file in os.listdir("bin") if os.path.isfile(os.path.join("bin", file)) + file + for file in os.listdir("bin") + if os.path.isfile(os.path.join("bin", file)) ][0] - path = os.path.normpath(os.path.join(repository_root, workdir, "bin", filename)) + path = os.path.normpath( + os.path.join(repository_root, workdir, "bin", filename) + ) print(f"::set-output name=filename::{path}") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..07e07c4 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,33 @@ +# Format Python +[tool.black] +line-length = 80 +target-version = ['py35', 'py36', 'py37', 'py38'] +include = '(\.pyi?$|\.spec$)' +exclude = ''' +( + /( + \.eggs + | \.git + | \.hg + | \.mypy_cache + | \.tox + | \.venv + | _build + | buck-out + | build + | dist + )/ + | buildozer\.spec +) +''' + +# Sort imports +# Settings to fit Black formatting +# Taken from https://github.com/timothycrosley/isort/issues/694 +[tool.isort] +line_length = 80 +include_trailing_comma = true +multi_line_output = 3 +use_parantheses = true +force_grid_wrap = 0 +ensure_newline_before_comments = true