mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Automatically remove import comments on files.
This is desired because now imports are automatically sorted and these header comments often get misplaced. Fixes #57979
This commit is contained in:
parent
e58f38f879
commit
65e12043eb
4 changed files with 70 additions and 4 deletions
|
@ -703,6 +703,19 @@ repos:
|
||||||
salt/client/ssh/ssh_py_shim.py
|
salt/client/ssh/ssh_py_shim.py
|
||||||
)$
|
)$
|
||||||
|
|
||||||
|
- repo: https://github.com/saltstack/mirrors-nox
|
||||||
|
rev: v2020.8.22
|
||||||
|
hooks:
|
||||||
|
- id: nox
|
||||||
|
alias: remove-import-comments
|
||||||
|
name: Remove import comments
|
||||||
|
files: .*\.py$
|
||||||
|
args:
|
||||||
|
- -e
|
||||||
|
- invoke-pre-commit
|
||||||
|
- --
|
||||||
|
- imports.remove-comments
|
||||||
|
|
||||||
- repo: https://github.com/timothycrosley/isort
|
- repo: https://github.com/timothycrosley/isort
|
||||||
rev: "1e78a9acf3110e1f9721feb591f89a451fc9876a"
|
rev: "1e78a9acf3110e1f9721feb591f89a451fc9876a"
|
||||||
hooks:
|
hooks:
|
||||||
|
|
|
@ -5,7 +5,7 @@ if (env.CHANGE_ID) {
|
||||||
pre_commit_skips = ''
|
pre_commit_skips = ''
|
||||||
} else {
|
} else {
|
||||||
// This is a branch build
|
// This is a branch build
|
||||||
pre_commit_skips = 'pyupgrade'
|
pre_commit_skips = 'pyupgrade,remove-import-comments'
|
||||||
}
|
}
|
||||||
|
|
||||||
runPreCommit(
|
runPreCommit(
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
from invoke import Collection # pylint: disable=3rd-party-module-not-gated
|
from invoke import Collection # pylint: disable=3rd-party-module-not-gated
|
||||||
|
|
||||||
from . import docs, filemap, loader
|
from . import docs, filemap, imports, loader
|
||||||
|
|
||||||
ns = Collection()
|
ns = Collection()
|
||||||
ns.add_collection(Collection.from_module(docs, name="docs"), name="docs")
|
ns.add_collection(Collection.from_module(docs, name="docs"), name="docs")
|
||||||
ns.add_collection(Collection.from_module(loader, name="loader"), name="loader")
|
ns.add_collection(Collection.from_module(loader, name="loader"), name="loader")
|
||||||
ns.add_collection(Collection.from_module(filemap, name="filemap"), name="filemap")
|
ns.add_collection(Collection.from_module(filemap, name="filemap"), name="filemap")
|
||||||
|
ns.add_collection(Collection.from_module(imports, name="imports"), name="imports")
|
||||||
|
|
54
tasks/imports.py
Normal file
54
tasks/imports.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
"""
|
||||||
|
tasks.imports
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Imports related tasks
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
|
|
||||||
|
from invoke import task # pylint: disable=3rd-party-module-not-gated
|
||||||
|
from tasks import utils
|
||||||
|
|
||||||
|
CODE_DIR = pathlib.Path(__file__).resolve().parent.parent
|
||||||
|
SALT_CODE_DIR = CODE_DIR / "salt"
|
||||||
|
|
||||||
|
|
||||||
|
@task(iterable=["files"], positional=["files"])
|
||||||
|
def remove_comments(ctx, files):
|
||||||
|
"""
|
||||||
|
Remove import comments, 'Import Python libs', 'Import salt libs', etc
|
||||||
|
"""
|
||||||
|
# CD into Salt's repo root directory
|
||||||
|
ctx.cd(CODE_DIR)
|
||||||
|
|
||||||
|
# Unfortunately invoke does not support nargs.
|
||||||
|
# We migth have been passed --files="foo.py bar.py"
|
||||||
|
# Turn that into a list of paths
|
||||||
|
_files = []
|
||||||
|
for path in files:
|
||||||
|
if not path:
|
||||||
|
continue
|
||||||
|
_files.extend(path.split())
|
||||||
|
if not _files:
|
||||||
|
utils.exit_invoke(0)
|
||||||
|
|
||||||
|
_files = [
|
||||||
|
pathlib.Path(fname).resolve() for fname in _files if fname.endswith(".py")
|
||||||
|
]
|
||||||
|
|
||||||
|
fixes = 0
|
||||||
|
exitcode = 0
|
||||||
|
comments_regex = re.compile(r"^# ([I|i])mports? .*(([L|l])ibs?)?\n", re.MULTILINE)
|
||||||
|
for path in _files:
|
||||||
|
contents = path.read_text()
|
||||||
|
fixed = comments_regex.sub("", contents)
|
||||||
|
if fixed == contents:
|
||||||
|
continue
|
||||||
|
fixes += 1
|
||||||
|
exitcode = 1
|
||||||
|
path.write_text(fixed)
|
||||||
|
if exitcode:
|
||||||
|
utils.error("Fixed {} files", fixes)
|
||||||
|
utils.exit_invoke(exitcode)
|
Loading…
Add table
Reference in a new issue