Skip to content

Commit

Permalink
[generate_version] fix to pass black (the pre version had a single qu…
Browse files Browse the repository at this point in the history
…ote ``'``)
  • Loading branch information
autumnjolitz committed Jun 18, 2024
1 parent 5e5059f commit 838fb31
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions generate_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import os
import subprocess
import shlex
from contextlib import suppress
from typing import Any
from contextlib import suppress, closing

try:
from packaging.version import parse as parse_version
Expand All @@ -11,6 +12,8 @@

here = os.path.dirname(os.path.abspath(__file__))

VERSION_FILE = os.path.join(here, "instruct/about.py")

with open(os.path.join(here, "CURRENT_VERSION.txt")) as fh:
for line in fh:
if line.strip().startswith("#"):
Expand All @@ -32,12 +35,23 @@
parsed_version = parse_version(full_version)


def quote(s: str) -> str:
def quote(s: Any) -> str:
if not isinstance(s, str):
return repr(s).replace("'", '"')
return f'"{s}"'


def write_about_and_emit_version():
with open(os.path.join(here, "instruct/about.py"), "w") as fh:
def write_about_and_emit_version(filename_or_stream: str = VERSION_FILE):
if filename_or_stream == "-":
stream = sys.stderr
elif isinstance(filename_or_stream, str):
stream = open(filename_or_stream, "w")
elif hasattr(filename_or_stream, "write"):
stream = filename_or_stream
else:
raise TypeError(filename_or_stream)
prerelease_version = repr(parsed_version.pre).replace("'", '"')
with closing(stream) as fh:
fh.write(
f"""# Autogenerated from `attr: generate_version.write_about_and_emit_version`
# it is run on setup.py/pip installs!
Expand Down Expand Up @@ -75,11 +89,20 @@ def public(self) -> str:
{parsed_version.major!r},
{parsed_version.minor!r},
{parsed_version.micro!r},
{parsed_version.pre!r},
{quote(parsed_version.pre) if parsed_version.pre else None},
{parsed_version.post!r},
{quote(parsed_version.local) or None},
{quote(parsed_version.local) if parsed_version.local else None},
)
__commit__: Optional[str] = {quote(parsed_version.local) or None}
"""
)
return version


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filename", default="-")
args = parser.parse_args()
write_about_and_emit_version(args.filename)

0 comments on commit 838fb31

Please sign in to comment.