Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 Ruff format python files within utils folder #12142

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,4 @@ exclude = [
"tests/test_versioning.py",
"tests/test_writers/**/*",
"tests/utils.py",
"utils/**/*",
]
51 changes: 32 additions & 19 deletions utils/babel_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from babel.util import pathmatch
from jinja2.ext import babel_extract as extract_jinja2

ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", ".."))
ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), '..', '..'))
TEX_DELIMITERS = {
'variable_start_string': '<%=',
'variable_end_string': '%>',
Expand Down Expand Up @@ -100,12 +100,15 @@ def run_extract() -> None:
options = opt_dict
with open(os.path.join(root, filename), 'rb') as fileobj:
for lineno, message, comments, context in extract(
method, fileobj, KEYWORDS, options=options,
method, fileobj, KEYWORDS, options=options
):
filepath = os.path.join(input_path, relative_name)
catalogue.add(
message, None, [(filepath, lineno)],
auto_comments=comments, context=context,
message,
None,
[(filepath, lineno)],
auto_comments=comments,
context=context,
)
break

Expand Down Expand Up @@ -137,7 +140,8 @@ def run_update() -> None:

catalog.update(template)
tmp_name = os.path.join(
os.path.dirname(filename), tempfile.gettempprefix() + os.path.basename(filename),
os.path.dirname(filename),
tempfile.gettempprefix() + os.path.basename(filename),
)
try:
with open(tmp_name, 'wb') as tmpfile:
Expand Down Expand Up @@ -179,8 +183,13 @@ def run_compile() -> None:
for message, errors in catalog.check():
for error in errors:
total_errors += 1
log.error('error: %s:%d: %s\nerror: in message string: %s',
po_file, message.lineno, error, message.string)
log.error(
'error: %s:%d: %s\nerror: in message string: %s',
po_file,
message.lineno,
error,
message.string,
)

mo_file = os.path.join(directory, locale, 'LC_MESSAGES', 'sphinx.mo')
log.info('compiling catalog %s to %s', po_file, mo_file)
Expand All @@ -192,26 +201,30 @@ def run_compile() -> None:
js_catalogue = {}
for message in catalog:
if any(
x[0].endswith(('.js', '.js.jinja', '.js_t', '.html'))
for x in message.locations
x[0].endswith(('.js', '.js.jinja', '.js_t', '.html'))
for x in message.locations
):
msgid = message.id
if isinstance(msgid, (list, tuple)):
msgid = msgid[0]
js_catalogue[msgid] = message.string

obj = json.dumps({
'messages': js_catalogue,
'plural_expr': catalog.plural_expr,
'locale': str(catalog.locale),
}, sort_keys=True, indent=4)
obj = json.dumps(
{
'messages': js_catalogue,
'plural_expr': catalog.plural_expr,
'locale': str(catalog.locale),
},
sort_keys=True,
indent=4,
)
with open(js_file, 'wb') as outfile:
# to ensure lines end with ``\n`` rather than ``\r\n``:
outfile.write(f'Documentation.addTranslations({obj});'.encode())

if total_errors > 0:
log.error('%d errors encountered.', total_errors)
print("Compiling failed.", file=sys.stderr)
print('Compiling failed.', file=sys.stderr)
raise SystemExit(2)


Expand All @@ -232,13 +245,13 @@ def _get_logger():
raise SystemExit(2) from None

os.chdir(ROOT)
if action == "extract":
if action == 'extract':
run_extract()
elif action == "update":
elif action == 'update':
run_update()
elif action == "compile":
elif action == 'compile':
run_compile()
elif action == "all":
elif action == 'all':
run_extract()
run_update()
run_compile()
Expand Down
30 changes: 18 additions & 12 deletions utils/bump_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@

for file in DOCKERFILE_BASE, DOCKERFILE_LATEXPDF:
content = file.read_text(encoding='utf-8')
content = re.sub(rf'{re.escape(OPENCONTAINERS_VERSION_PREFIX)} = "{VERSION_PATTERN}"',
rf'{OPENCONTAINERS_VERSION_PREFIX} = "{VERSION}"',
content)
content = re.sub(rf'{re.escape(SPHINX_VERSION_PREFIX)}{VERSION_PATTERN}',
rf'{SPHINX_VERSION_PREFIX}{VERSION}',
content)
content = re.sub(
rf'{re.escape(OPENCONTAINERS_VERSION_PREFIX)} = "{VERSION_PATTERN}"',
rf'{OPENCONTAINERS_VERSION_PREFIX} = "{VERSION}"',
content,
)
content = re.sub(
rf'{re.escape(SPHINX_VERSION_PREFIX)}{VERSION_PATTERN}',
rf'{SPHINX_VERSION_PREFIX}{VERSION}',
content,
)
file.write_text(content, encoding='utf-8')


def git(*args: str) -> None:
ret = subprocess.run(('git', *args),
capture_output=True,
cwd=DOCKER_ROOT,
check=True,
text=True,
encoding='utf-8')
ret = subprocess.run(
('git', *args),
capture_output=True,
cwd=DOCKER_ROOT,
check=True,
text=True,
encoding='utf-8',
)
print(ret.stdout)
print(ret.stderr, file=sys.stderr)

Expand Down
15 changes: 6 additions & 9 deletions utils/bump_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@
VersionInfo: TypeAlias = tuple[int, int, int, str, int]


def stringify_version(
version_info: VersionInfo, in_develop: bool = True,
) -> str:
def stringify_version(version_info: VersionInfo, in_develop: bool = True) -> str:
version = '.'.join(str(v) for v in version_info[:3])
if not in_develop and version_info[3] != 'final':
version += version_info[3][0] + str(version_info[4])

return version


def bump_version(
path: Path, version_info: VersionInfo, in_develop: bool = True,
) -> None:
def bump_version(path: Path, version_info: VersionInfo, in_develop: bool = True) -> None:
version = stringify_version(version_info, in_develop)

with open(path, encoding='utf-8') as f:
Expand Down Expand Up @@ -177,9 +173,10 @@ def parse_options(argv: Sequence[str]) -> argparse.Namespace:
def main() -> None:
options = parse_options(sys.argv[1:])

with processing("Rewriting sphinx/__init__.py"):
bump_version(package_dir / 'sphinx' / '__init__.py',
options.version, options.in_develop)
with processing('Rewriting sphinx/__init__.py'):
bump_version(
package_dir / 'sphinx' / '__init__.py', options.version, options.in_develop
)

with processing('Rewriting CHANGES'):
changes = Changes(package_dir / 'CHANGES.rst')
Expand Down