Skip to content

Commit

Permalink
[WIP] Require Python 3.6 (f-strings) and fix pylints
Browse files Browse the repository at this point in the history
So far only done scripts directory, will do the rest if this is
considered good
  • Loading branch information
def- committed Jul 27, 2022
1 parent 53e9fd3 commit 7379a64
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ jobs:
- name: Pylint
run: |
pylint --version
find . -type f -name "*.py" -not -path './ddnet-libs/*' -print0 | xargs -0 pylint
find . -type f -name "*.py" -not -path './ddnet-libs/*' -not -path './googletest-src/*' -print0 | xargs -0 pylint
2 changes: 1 addition & 1 deletion datasrc/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def gen_network_header():
print(network.RawHeader)

for e in network.Enums:
for line in create_enum_table(["%s_%s"%(e.name, v) for v in e.values], 'NUM_%sS'%e.name): # pylint: disable=E1101
for line in create_enum_table(["%s_%s"%(e.name, v) for v in e.values], 'NUM_%sS'%e.name): # pylint: disable=no-member
print(line)
print("")

Expand Down
18 changes: 6 additions & 12 deletions other/dmgsettings.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# -*- coding: utf-8 -*-
# pylint: disable=E0602
# pylint: disable=undefined-variable
from __future__ import unicode_literals
import os.path
import sys

# Before Python 3.4, use biplist; afterwards, use plistlib
if sys.version_info < (3, 4):
import biplist # pylint: disable=E0401
def read_plist(path):
return biplist.readPlist(path)
else:
import plistlib # pylint: disable=E0401
def read_plist(path):
with open(path, 'rb') as f:
return plistlib.load(f)
import plistlib
def read_plist(path):
with open(path, 'rb') as f:
return plistlib.load(f)

#
# Example settings file for dmgbuild
Expand Down Expand Up @@ -52,7 +46,7 @@ def icon_from_app(app_path):
# volume_name = 'DDNet'

# Volume format (see hdiutil create -help)
format = defines.get('format', 'UDBZ') # pylint: disable=W0622
format = defines.get('format', 'UDBZ') # pylint: disable=redefined-builtin

# Compression level (if relevant)
compression_level = 9
Expand Down
6 changes: 3 additions & 3 deletions scripts/check_header_guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def check_file(filename):
if filename in EXCEPTIONS:
return False
error = False
with open(filename) as file:
with open(filename, encoding="utf-8") as file:
for line in file:
if line == "// This file can be included several times.\n":
break
Expand All @@ -27,10 +27,10 @@ def check_file(filename):
if line.startswith("#ifndef"):
if line[:-1] != header_guard:
error = True
print("Wrong header guard in {}, is: {}, should be: {}".format(filename, line[:-1], header_guard))
print(f"Wrong header guard in {filename}, is: {line[:-1]}, should be: {header_guard}")
else:
error = True
print("Missing header guard in {}, should be: {}".format(filename, header_guard))
print(f"Missing header guard in {filename}, should be: {header_guard}")
break
return error

Expand Down
6 changes: 3 additions & 3 deletions scripts/check_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def check_variable_name(qualifiers, typ, name):
return None
prefix = "".join([qualifiers, "_" if qualifiers else "", typ])
if not name.startswith(prefix):
return "should start with {!r}".format(prefix)
return f"should start with {prefix!r}"
if name in ALLOW:
return None
name = name[len(prefix):]
if not name[0].isupper():
if prefix:
return "should start with an uppercase letter after the prefix {!r}".format(prefix)
return f"should start with an uppercase letter after the prefix {prefix!r}"
return "should start with an uppercase letter"
return None

Expand All @@ -65,7 +65,7 @@ def main():
error = check_name(i["kind"], i["qualifiers"], i["type"], i["name"])
if error:
unclean = True
print("{}:{}:{}: {}: {}".format(i["file"], i["line"], i["column"], i["name"], error))
print(f"{['file']}:{i['line']}:{i['column']}: {i['name']}: {error}")
return unclean

if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions scripts/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
os.chdir(os.path.dirname(__file__) + "/..")

def hash_bytes(b):
return "0x{}".format(hashlib.sha256(b).hexdigest()[:8])
return f"0x{hashlib.sha256(b).hexdigest()[:8]}"

def hash_file(filename):
with open(filename, "rb") as f:
Expand All @@ -18,7 +18,7 @@ def main():
p.add_argument("extra_file", metavar="EXTRA_FILE", help="File containing extra strings to be hashed")
args = p.parse_args()

with open(args.list_file) as f:
with open(args.list_file, encoding="utf-8") as f:
files = f.read().splitlines()
with open(args.extra_file, "rb") as f:
extra = f.read().splitlines()
Expand All @@ -31,10 +31,10 @@ def main():
void CChecksumData::InitFiles()
{
""", end="")
print("\tm_NumFiles = {};".format(len(hashes_files)))
print("\tm_NumExtra = {};".format(len(hashes_extra)))
print(f"\tm_NumFiles = {len(hashes_files)};")
print(f"\tm_NumExtra = {len(hashes_extra)};")
for i, h in enumerate(hashes):
print("\tm_aFiles[0x{:03x}] = {};".format(i, h))
print(f"\tm_aFiles[0x{i:03x}] = {h};")
print("}")

if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions scripts/extract_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ class ParseError(RuntimeError):
def process_source_file(out, file, extra_args, break_on):
args = extra_args + ["-Isrc"]
if file.endswith(".c"):
header = "{}.h".format(file[:-2])
header = f"{file[:-2]}.h"
elif file.endswith(".cpp"):
header = "{}.h".format(file[:-4])
header = f"{file[:-4]}.h"
else:
raise ValueError("unrecognized source file: {}".format(file))
raise ValueError(f"unrecognized source file: {file}")

index = clang.cindex.Index.create()
unit = index.parse(file, args=args)
errors = list(unit.diagnostics)
if errors:
for error in errors:
print("{}: {}".format(file, error.format()), file=sys.stderr)
print(f"{file}: {error.format()}", file=sys.stderr)
print(args, file=sys.stderr)
raise ParseError("failed parsing {}".format(file))
raise ParseError(f"failed parsing {file}")

filter_files = frozenset([file, header])

Expand Down Expand Up @@ -140,7 +140,7 @@ def process_source_file(out, file, extra_args, break_on):
"name": node.spelling,
})
if node.spelling == break_on:
breakpoint()
breakpoint() # pylint: disable=forgotten-debug-statement

def main():
p = argparse.ArgumentParser(description="Extracts identifier data from a Teeworlds source file and its header, outputting the data as CSV to stdout")
Expand Down
8 changes: 4 additions & 4 deletions scripts/fix_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def filter_cpp(filenames):
def find_clang_format(version):
for binary in (
"clang-format",
"clang-format-{}".format(version),
"/opt/clang-format-static/clang-format-{}".format(version)):
f"clang-format-{version}",
f"/opt/clang-format-static/clang-format-{version}"):
try:
out = subprocess.check_output([binary, "--version"])
except FileNotFoundError:
continue
if "clang-format version {}.".format(version) in out.decode("utf-8"):
if f"clang-format version {version}." in out.decode("utf-8"):
return binary
print("Found no clang-format {}".format(version))
print(f"Found no clang-format {version}")
sys.exit(-1)

clang_format_bin = find_clang_format(10)
Expand Down
16 changes: 8 additions & 8 deletions scripts/generate_fake_curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_curl_calls(path):
if (filename.endswith(".cpp") or
filename.endswith(".c") or
filename.endswith(".h")):
with open(os.path.join(directory, filename)) as f:
with open(os.path.join(directory, filename), encoding="utf-8") as f:
contents = f.read()
names = names.union(CURL_RE.findall(contents))
return names
Expand All @@ -27,11 +27,11 @@ def assembly_source(names):
names = sorted(names)
result = []
for name in names:
result.append(".type {},@function".format(name))
result.append(f".type {name},@function")
for name in names:
result.append(".global {}".format(name))
result.append(f".global {name}")
for name in names:
result.append("{}:".format(name))
result.append(f"{name}:")
return "\n".join(result + [""])

DEFAULT_OUTPUT="libcurl.so"
Expand All @@ -40,8 +40,8 @@ def assembly_source(names):
def main():
p = argparse.ArgumentParser(description="Create a stub shared object for linking")
p.add_argument("-k", "--keep", action="store_true", help="Keep the intermediary assembly file")
p.add_argument("--output", help="Output filename (default: {})".format(DEFAULT_OUTPUT), default=DEFAULT_OUTPUT)
p.add_argument("--soname", help="soname of the produced shared object (default: {})".format(DEFAULT_SONAME), default=DEFAULT_SONAME)
p.add_argument("--output", help=f"Output filename (default: {DEFAULT_OUTPUT})", default=DEFAULT_OUTPUT)
p.add_argument("--soname", help=f"soname of the produced shared object (default: {DEFAULT_SONAME})", default=DEFAULT_SONAME)
p.add_argument("--functions", metavar="FUNCTION", nargs="+", help="Function symbols that should be put into the shared object (default: look for curl_* names in the source code)")
p.add_argument("--link-args", help="Colon-separated list of additional linking arguments")
args = p.parse_args()
Expand All @@ -56,15 +56,15 @@ def main():

with tempfile.NamedTemporaryFile("w", suffix=".s", delete=not args.keep) as f:
if args.keep:
print("using {} as temporary file".format(f.name))
print(f"using {f.name} as temporary file")
f.write(assembly_source(functions))
f.flush()
subprocess.check_call([
"cc",
] + extra_link_args + [
"-shared",
"-nostdlib", # don't need to link to libc
"-Wl,-soname,{}".format(args.soname),
f"-Wl,-soname,{args.soname}",
"-o", args.output,
f.name,
])
Expand Down
15 changes: 8 additions & 7 deletions scripts/generate_unicode_confusables_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def generate_decompositions():
ud = unicode.data()
con = unicode.confusables()

category = lambda x: {unicode.unhex(u["Value"]) for u in ud if u["General_Category"].startswith(x)}
def category(x):
return {unicode.unhex(u["Value"]) for u in ud if u["General_Category"].startswith(x)}

# TODO: Is this correct? They changed the decompositioning format
nfd = {unicode.unhex(u["Value"]): unicode.unhex_sequence(u["Decomposition_Type"]) for u in ud}
Expand Down Expand Up @@ -62,8 +63,8 @@ def gen_header(decompositions, len_set):
""")
print("enum")
print("{")
print("\tNUM_DECOMP_LENGTHS = {},".format(len(len_set)))
print("\tNUM_DECOMPS = {},".format(len(decompositions)))
print(f"\tNUM_DECOMP_LENGTHS = {len(len_set)},")
print(f"\tNUM_DECOMPS = {len(decompositions)},")
print("};")
print()

Expand All @@ -81,13 +82,13 @@ def gen_data(decompositions, decomposition_set, decomposition_offsets, len_set):

print("const uint8_t decomp_lengths[NUM_DECOMP_LENGTHS] = {")
for l in len_set:
print("\t{},".format(l))
print(f"\t{l},")
print("};")
print()

print("const int32_t decomp_chars[NUM_DECOMPS] = {")
for k in sorted(decompositions):
print("\t0x{:x},".format(k))
print(f"\t0x{k:x},")
print("};")
print()

Expand All @@ -96,14 +97,14 @@ def gen_data(decompositions, decomposition_set, decomposition_offsets, len_set):
d = decompositions[k]
i = decomposition_set.index(tuple(d))
l = len_set.index(len(d))
print("\t{{{}, {}}},".format(decomposition_offsets[i], l))
print(f"\t{{{decomposition_offsets[i]}, {l}}},")
print("};")
print()

print("const int32_t decomp_data[] = {")
for d in decomposition_set:
for c in d:
print("\t0x{:x},".format(c))
print(f"\t0x{c:x},")
print("};")

def main():
Expand Down
8 changes: 4 additions & 4 deletions scripts/generate_unicode_tolower.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def generate_cases():
return [(unicode.unhex(u["Value"]), unicode.unhex(u["Simple_Lowercase_Mapping"])) for u in ud if u["Simple_Lowercase_Mapping"]]

def gen_header(cases):
print("""\
print(f"""\
#include <stdint.h>
struct UPPER_LOWER
Expand All @@ -26,10 +26,10 @@ def gen_header(cases):
enum
{{
\tNUM_TOLOWER = {},
\tNUM_TOLOWER = {len(cases)},
}};
extern const struct UPPER_LOWER tolowermap[];""".format(len(cases)))
extern const struct UPPER_LOWER tolowermap[];""")

def gen_data(cases):
print("""\
Expand All @@ -39,7 +39,7 @@ def gen_data(cases):
const struct UPPER_LOWER tolowermap[] = {""")
for upper_code, lower_code in cases:
print("\t{{{}, {}}},".format(upper_code, lower_code))
print(f"\t{{{upper_code}, {lower_code}}},")
print("};")

def main():
Expand Down
11 changes: 3 additions & 8 deletions scripts/git_revision.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import errno
import subprocess
try:
from subprocess import DEVNULL
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
try:
git_hash = subprocess.check_output(["git", "rev-parse", "--short=16", "HEAD"], stderr=DEVNULL).decode().strip()
definition = '"{}"'.format(git_hash)
git_hash = subprocess.check_output(["git", "rev-parse", "--short=16", "HEAD"], stderr=subprocess.DEVNULL).decode().strip()
definition = f'"{git_hash}"'
except FileNotFoundError as e:
if e.errno != errno.ENOENT:
raise
definition = "0"
except subprocess.CalledProcessError:
definition = "0"
print("const char *GIT_SHORTREV_HASH = {};".format(definition))
print(f"const char *GIT_SHORTREV_HASH = {definition};")
20 changes: 10 additions & 10 deletions scripts/hash_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def hash_password(password):

def auth_add_p_line(username, level, pwhash, salt):
if level not in ('admin', 'mod', 'moderator', 'helper'):
print("Warning: level ({}) not one of admin, mod or helper.".format(level), file=sys.stderr)
if repr(level) != "'{}'".format(level):
print("Warning: level ({}) contains weird symbols, config line is possibly malformed.".format(level), file=sys.stderr)
if repr(username) != "'{}'".format(username):
print("Warning: username ({}) contains weird symbols, config line is possibly malformed.".format(username), file=sys.stderr)
print(f"Warning: level ({level}) not one of admin, mod or helper.", file=sys.stderr)
if repr(level) != f"'{level}'":
print(f"Warning: level ({level}) contains weird symbols, config line is possibly malformed.", file=sys.stderr)
if repr(username) != f"'{username}'":
print(f"Warning: username ({username}) contains weird symbols, config line is possibly malformed.", file=sys.stderr)
username = username.replace('"', '\\"')
if ' ' in username or ';' in username:
username = '"{}"'.format(username)
return "auth_add_p {} {} {} {}".format(username, level, pwhash, salt)
username = f'"{username}"'
return f"auth_add_p {username} {level} {pwhash} {salt}"

def auth_add_p_line_from_pw(username, level, password):
if len(password) < 8:
Expand Down Expand Up @@ -57,13 +57,13 @@ def main():
use_stdio = args.config is None or args.config == '-'
if use_stdio:
if args.config is None:
input_file = open(os.devnull)
input_file = open(os.devnull, encoding="utf-8")
else:
input_file = sys.stdin
output_file = sys.stdout
else:
input_file = open(args.config)
output_file = tempfile.NamedTemporaryFile('w', dir=os.getcwd(), prefix="{}.".format(args.config), delete=False)
input_file = open(args.config, encoding="utf-8") # pylint: disable=consider-using-with
output_file = tempfile.NamedTemporaryFile('w', dir=os.getcwd(), prefix=f"{args.config}.", delete=False) # pylint: disable=consider-using-with

for line in input_file:
parsed = parse_line(line)
Expand Down
Loading

0 comments on commit 7379a64

Please sign in to comment.