1
0
Fork 0
mirror of https://github.com/ArtemSBulgakov/buildozer-action.git synced 2024-08-16 10:09:52 +02:00

Format files with pre-commit hook

This commit is contained in:
Artem Bulgakov 2020-07-29 16:19:30 +03:00
parent 5770a69f88
commit d1bccff610
4 changed files with 91 additions and 14 deletions

View file

@ -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}"
)

18
.pre-commit-config.yaml Normal file
View file

@ -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"]

View file

@ -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}")

33
pyproject.toml Normal file
View file

@ -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