Skip to content
Snippets Groups Projects
Commit 89b94495 authored by Gitlab CI's avatar Gitlab CI Committed by Chenjia Zhang
Browse files
parent 0b233b09
No related branches found
No related tags found
1 merge request!348Modify the functions in FileFromDirac
Pipeline #6291331 passed
This commit is part of merge request !348. Comments created here will be created in the context of that merge request.
......@@ -111,9 +111,9 @@ def get_access_urls_data(bkkpath, sites_to_remove=[], max_files=500):
# ensure files are always in same order
file_list.sort()
# if any(file.endswith("raw") for file in file_list):
# raise NotImplementedError(
# "File ending with 'raw' found, please write a script manually")
# if any(file.endswith("raw") for file in file_list):
# raise NotImplementedError(
# "File ending with 'raw' found, please write a script manually")
print(
"#### Checking output of . /cvmfs/lhcb.cern.ch/lib/LbEnv --quiet; lb-dirac dirac-dms-lfn-accessURL --Terminal"
......@@ -159,6 +159,7 @@ def get_access_urls_data(bkkpath, sites_to_remove=[], max_files=500):
# TODO warn if some of the first N files was not resolved to a URL
# since then one would get numerically different results.
if __name__ == "__main__":
# bkk_path = ("/MC/Upgrade/Beam7000GeV-Upgrade-MagDown-Nu7.6-25ns-Pythia8"
# "/Sim10-Up08/Digi15-Up04")
......
......@@ -25,8 +25,7 @@ from datetime import date
COPYRIGHT_SIGNATURE = re.compile(r"\bcopyright\b", re.I)
CHECKED_FILES = re.compile(
r".*(\.(i?[ch](pp|xx|c)?|cc|hh|py|cuh?|C|cmake|[yx]ml|qm[ts]|dtd|xsd|ent|bat|[cz]?sh|js|jsx|css|html?)|"
r"CMakeLists.txt|Jenkinsfile)$"
)
r"CMakeLists.txt|Jenkinsfile)$")
COPYRIGHT_STATEMENT = """
(c) Copyright {} CERN for the benefit of the LHCb Collaboration
......@@ -54,8 +53,7 @@ licenses = {"GPL-3.0-only": GPL3_STATEMENT, "Apache-2.0": APACHE2_STATEMENT}
# see https://www.python.org/dev/peps/pep-0263 for the regex
ENCODING_DECLARATION = re.compile(
r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)".encode()
)
r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)".encode())
CLANG_FORMAT_VERSION = "8"
YAPF_VERSION = "0.24.0"
......@@ -74,7 +72,8 @@ def to_check(path):
"""
Check if path is meant to contain a copyright statement.
"""
return os.path.isfile(path) and (bool(CHECKED_FILES.match(path)) or is_script(path))
return os.path.isfile(path) and (bool(CHECKED_FILES.match(path))
or is_script(path))
def is_empty(path):
......@@ -108,31 +107,21 @@ def get_files(reference=None):
from subprocess import check_output
if reference is None:
all = (
path.decode()
for path in check_output(["git", "ls-files", "-z"])
.rstrip(b"\x00")
.split(b"\x00")
)
all = (path.decode() for path in check_output(
["git", "ls-files", "-z"]).rstrip(b"\x00").split(b"\x00"))
else:
prefix_len = len(check_output(["git", "rev-parse", "--show-prefix"]).strip())
all = (
path[prefix_len:].decode()
for path in check_output(
[
"git",
"diff",
"--name-only",
"--no-renames",
"--diff-filter=MA",
"-z",
reference + "...",
".",
]
)
.rstrip(b"\x00")
.split(b"\x00")
)
prefix_len = len(
check_output(["git", "rev-parse", "--show-prefix"]).strip())
all = (path[prefix_len:].decode() for path in check_output([
"git",
"diff",
"--name-only",
"--no-renames",
"--diff-filter=MA",
"-z",
reference + "...",
".",
]).rstrip(b"\x00").split(b"\x00"))
return (path for path in all if to_check(path))
......@@ -144,29 +133,21 @@ def report(filenames, inverted=False, target=None, license="GPL-3.0-only"):
"""
print(
("The following {} files {}contain a copyright statement:\n- ").format(
len(filenames), "" if inverted else "do not "
),
len(filenames), "" if inverted else "do not "),
end="",
)
print("\n- ".join(filenames))
if not inverted:
license_arg = (
" --license={}".format(license) if license != "GPL-3.0-only" else ""
)
license_arg = (" --license={}".format(license)
if license != "GPL-3.0-only" else "")
if target:
print(
"\nYou can fix the {0} files without copyright statement "
"with:\n\n $ lb-check-copyright --porcelain {1}{2} "
"| xargs -r lb-add-copyright{2}\n".format(
len(filenames), target, license_arg
)
)
print("\nYou can fix the {0} files without copyright statement "
"with:\n\n $ lb-check-copyright --porcelain {1}{2} "
"| xargs -r lb-add-copyright{2}\n".format(
len(filenames), target, license_arg))
else:
print(
"\nyou can fix them with the command lb-add-copyright{}\n".format(
license_arg
)
)
print("\nyou can fix them with the command lb-add-copyright{}\n".
format(license_arg))
def to_comment(text, lang_family="#", width=80):
......@@ -227,12 +208,11 @@ def lang_family(path):
if re.match(r".*\.(xml|xsd|dtd|html?|qm[ts]|ent)$", path):
return "xml"
elif re.match(
r"(.*\.(i?[ch](pp|xx|c)?|cuh?|cc|hh|C|opts|js|jsx|css)|" r"Jenkinsfile)$", path
):
r"(.*\.(i?[ch](pp|xx|c)?|cuh?|cc|hh|C|opts|js|jsx|css)|"
r"Jenkinsfile)$", path):
return "c"
elif re.match(r".*\.py$", path) or re.match(
r"^#!.*python", open(path).readline(120)
):
elif re.match(r".*\.py$", path) or re.match(r"^#!.*python",
open(path).readline(120)):
return "py"
else:
return "#"
......@@ -248,7 +228,10 @@ def find_encoding_declaration_line(lines, limit=2):
return i
def add_copyright_to_file(path, year=None, license_fn=None, add_license="GPL-3.0-only"):
def add_copyright_to_file(path,
year=None,
license_fn=None,
add_license="GPL-3.0-only"):
"""
Add copyright statement to the given file for the specified year (or range
of years). If the year argument is not specified, the current year is
......@@ -302,9 +285,10 @@ def get_git_root(path):
if not os.path.isdir(path):
path = os.path.dirname(path)
p = Popen(
["git", "rev-parse", "--show-toplevel"], cwd=path, stdout=PIPE, stderr=PIPE
)
p = Popen(["git", "rev-parse", "--show-toplevel"],
cwd=path,
stdout=PIPE,
stderr=PIPE)
out, _ = p.communicate()
if p.returncode == 0:
return out.strip()
......@@ -333,7 +317,8 @@ def ensure_clang_format_style(path):
path = os.path.abspath(path)
global _found_clang_format_dirs
if not any(os.path.commonprefix([d, path]) for d in _found_clang_format_dirs):
if not any(
os.path.commonprefix([d, path]) for d in _found_clang_format_dirs):
base = find_clang_format(path)
if base:
debug("found .clang-format in %s", base)
......@@ -364,14 +349,12 @@ class CommandNotFound(RuntimeError):
def get_clang_format_cmd(version=CLANG_FORMAT_VERSION):
cmd = find_command(
cmd.format(version)
for cmd in [
cmd.format(version) for cmd in [
"clang-format-{}",
"lcg-clang-format-{}",
"lcg-clang-format-{}.0",
"lcg-clang-format-{}.0.0",
]
)
])
if not cmd:
raise CommandNotFound("clang-format-%s not found" % version)
return cmd
......@@ -386,8 +369,7 @@ def get_yapf_format_cmd(version=YAPF_VERSION):
found_version = check_output([cmd, "--version"]).split()[-1].decode()
if found_version != version:
raise CommandNotFound(
"wrong yapf version %s (%s required)" % (found_version, version)
)
"wrong yapf version %s (%s required)" % (found_version, version))
return cmd
......@@ -402,9 +384,8 @@ class Formatter:
'lang'.
"""
if lang == "c":
assert self.clang_format_cmd, (
"tried to format C/C++ file but " "clang-format is not available"
)
assert self.clang_format_cmd, ("tried to format C/C++ file but "
"clang-format is not available")
ensure_clang_format_style(path)
return [
self.clang_format_cmd,
......@@ -413,9 +394,8 @@ class Formatter:
"-assume-filename=" + path,
]
elif lang == "py":
assert self.yapf_cmd, (
"tried to format Python file but " "yapf is not available"
)
assert self.yapf_cmd, ("tried to format Python file but "
"yapf is not available")
return [self.yapf_cmd]
else:
assert False, "invalid language %r" % lang
......@@ -459,12 +439,10 @@ class Formatter:
def check_copyright():
from argparse import ArgumentParser
parser = ArgumentParser(
description="""
parser = ArgumentParser(description="""
Check that each git tracked source file in the current directory contains a
copyright statement.
"""
)
""")
parser.add_argument(
"reference",
nargs="?",
......@@ -506,14 +484,12 @@ def check_copyright():
args = parser.parse_args()
missing = [
path
for path in get_files(args.reference)
path for path in get_files(args.reference)
# we only deal with non-empty files and we report as "missing"
# those without copyright, unless `args.inverted` is True, in which
# case we invert the answer of has_copyright, to report the file _with_
# copyright notice
if not is_empty(path)
and not (args.inverted ^ has_copyright(path))
if not is_empty(path) and not (args.inverted ^ has_copyright(path))
and not any(pattern.search(path) for pattern in args.exclude)
]
if missing:
......@@ -529,15 +505,12 @@ def add_copyright():
from argparse import ArgumentParser
parser = ArgumentParser(
description="Add standard LHCb copyright statement to files."
)
description="Add standard LHCb copyright statement to files.")
parser.add_argument("files", nargs="+", help="files to modify")
parser.add_argument(
"--year", help="copyright year specification (default: current year)"
)
"--year", help="copyright year specification (default: current year)")
parser.add_argument(
"--license-fn", help="Name of the license file (default: COPYING)"
)
"--license-fn", help="Name of the license file (default: COPYING)")
parser.add_argument(
"--license",
default="GPL-3.0-only",
......@@ -552,7 +525,8 @@ def add_copyright():
parser.add_argument(
"--pre-commit",
action="store_true",
help="Print modified files but don't print warnings (for pre-commit hook)",
help=
"Print modified files but don't print warnings (for pre-commit hook)",
)
args = parser.parse_args()
......@@ -560,17 +534,17 @@ def add_copyright():
for path in args.files:
if not args.force and not to_check(path):
if not args.pre_commit:
print(
"warning: cannot add copyright to {} (file type not "
"supported)".format(path)
)
print("warning: cannot add copyright to {} (file type not "
"supported)".format(path))
elif has_copyright(path):
if not args.pre_commit:
print("warning: {} already has a copyright statement".format(path))
print("warning: {} already has a copyright statement".format(
path))
else:
if args.pre_commit:
print("Adding copyright notice to", path)
add_copyright_to_file(path, args.year, args.license_fn, args.license)
add_copyright_to_file(path, args.year, args.license_fn,
args.license)
def format():
......@@ -586,11 +560,11 @@ def format():
)
parser.add_argument(
"--clang-format-version",
help="version of clang-format to use " "(default: %(default)s)",
help="version of clang-format to use "
"(default: %(default)s)",
)
parser.add_argument(
"--yapf-version", help="version of yapf to use (default: %(default)s)"
)
"--yapf-version", help="version of yapf to use (default: %(default)s)")
parser.add_argument(
"--verbose",
action="store_const",
......@@ -606,8 +580,7 @@ def format():
help="print debug messages",
)
parser.add_argument(
"-n", "--dry-run", action="store_true", help="do not modify the files"
)
"-n", "--dry-run", action="store_true", help="do not modify the files")
parser.add_argument(
"--reference",
help="check/format only the files select the files that have changed "
......@@ -624,7 +597,8 @@ def format():
"--pipe",
metavar="LANGUAGE",
choices=FORMATTABLE_LANGUAGES,
help="format from stdin to stdout (allowed values: %s)" % FORMATTABLE_LANGUAGES,
help="format from stdin to stdout (allowed values: %s)" %
FORMATTABLE_LANGUAGES,
)
parser.set_defaults(
files=[],
......@@ -649,10 +623,8 @@ def format():
if args.format_patch:
if len(args.files) > 1:
parser.error(
"wrong number of arguments: at most one argument "
"must be provided when using --format-patch"
)
parser.error("wrong number of arguments: at most one argument "
"must be provided when using --format-patch")
elif args.files:
args.reference = args.files.pop()
......@@ -667,27 +639,27 @@ def format():
info("using clang-format: %s", clang_format_cmd)
except CommandNotFound as err:
(parser.error if args.pipe == "c" else warning)(
"%s: C/C++ formatting not available" % err
)
"%s: C/C++ formatting not available" % err)
try:
yapf_cmd = get_yapf_format_cmd(args.yapf_version)
info("using yapf: %s", yapf_cmd)
except CommandNotFound as err:
(parser.error if args.pipe == "py" else warning)(
"%s: Python formatting not available" % err
)
"%s: Python formatting not available" % err)
def can_format(path):
if to_check(path):
lang = lang_family(path)
if (clang_format_cmd and lang == "c") or (yapf_cmd and lang == "py"):
if (clang_format_cmd and lang == "c") or (yapf_cmd
and lang == "py"):
return lang
return None
if not args.pipe:
if not args.files:
args.files = (f for f in get_files(args.reference) if can_format(f))
args.files = (f for f in get_files(args.reference)
if can_format(f))
if args.pipe:
import sys
......@@ -715,16 +687,14 @@ def format():
output = formatter(input, path, lang)
if args.format_patch:
patch.extend(
l
if l.endswith("\n")
else (l + "\n\\ No newline at end of file\n")
l if l.endswith("\n") else (
l + "\n\\ No newline at end of file\n")
for l in unified_diff(
input.decode("utf-8").splitlines(True),
output.decode("utf-8").splitlines(True),
os.path.join("a", path),
os.path.join("b", path),
)
)
))
elif output != input:
if args.dry_run:
print(path, "should be changed")
......@@ -733,7 +703,8 @@ def format():
with open(path, "wb") as f:
f.write(output)
except CalledProcessError as err:
warning("could not format %r: %s\n%s", path, err, err.output.rstrip())
warning("could not format %r: %s\n%s", path, err,
err.output.rstrip())
except UnicodeDecodeError as err:
error("invalid encoding in %r: %s", path, err)
encoding_errors.append(path)
......@@ -760,18 +731,14 @@ def format():
msg.add_header("From", "Gitlab CI <noreply@cern.ch>")
msg.add_header("Date", formatdate())
msg.add_header("Subject", "[PATCH] Fixed formatting")
payload = "\n".join(
[
"patch generated by {}".format(
"{CI_PROJECT_URL}/-/jobs/{CI_JOB_ID}".format(**os.environ)
if "CI" in os.environ
else "standalone job"
),
"",
"",
"".join(patch),
]
)
payload = "\n".join([
"patch generated by {}".format(
"{CI_PROJECT_URL}/-/jobs/{CI_JOB_ID}".format(
**os.environ) if "CI" in os.environ else "standalone job"),
"",
"",
"".join(patch),
])
try:
payload.encode("ascii")
except UnicodeEncodeError:
......@@ -784,8 +751,7 @@ def format():
print(msg)
else:
if os.path.dirname(args.format_patch) and not os.path.isdir(
os.path.dirname(args.format_patch)
):
os.path.dirname(args.format_patch)):
os.makedirs(os.path.dirname(args.format_patch))
with open(args.format_patch, "wb") as patchfile:
patchfile.write(bytes(msg))
......@@ -796,11 +762,9 @@ def format():
sep="\n",
)
if "CI" in os.environ:
print(
" curl {CI_PROJECT_URL}/-/jobs/{CI_JOB_ID}/"
"artifacts/raw/{0} | "
"git am".format(args.format_patch, **os.environ)
)
print(" curl {CI_PROJECT_URL}/-/jobs/{CI_JOB_ID}/"
"artifacts/raw/{0} | "
"git am".format(args.format_patch, **os.environ))
else:
print(" git am {}".format(args.format_patch))
print("", "=======================================", sep="\n")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment