Skip to content

Commit

Permalink
fix: modernize python code (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored May 17, 2024
1 parent 711f08a commit fad9295
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 407 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint_and_format_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
- uses: chartboost/ruff-action@e18ae971ccee1b2d7bbef113930f00c670b78da4 # v1.0.0
name: Lint with Ruff
with:
version: 0.4.2
version: 0.4.4
28 changes: 19 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
'lint.select' = [
[project]
name = "ada-url"
requires-python = ">=3.12"

[tool.ruff]
line-length = 120
target-version = "py312"

[tool.ruff.format]
quote-style = "single"
indent-style = "space"
docstring-code-format = true

[tool.ruff.lint]
select = [
"C90", # McCabe cyclomatic complexity
"E", # pycodestyle
"F", # Pyflakes
Expand All @@ -17,12 +31,8 @@
"YTT", # flake8-2020
"ANN" # flake8-annotations
]
exclude = [
"docs",
"tests",
]
'lint.ignore' = [
"E722" # Do not use bare `except`
ignore = [
"E722", # Do not use bare `except`
"ANN101", # Missing type annotation for self in method
"TID252", # Prefer absolute imports over relative imports from parent modules
]
line-length = 120
target-version = "py312"
91 changes: 48 additions & 43 deletions singleheader/amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,40 @@
import re
import shutil
import datetime

if sys.version_info[0] < 3:
sys.stdout.write("Sorry, requires Python 3.x or better\n")
sys.stdout.write('Sorry, requires Python 3.x or better\n')
sys.exit(1)

SCRIPTPATH = os.path.dirname(os.path.abspath(sys.argv[0]))
PROJECTPATH = os.path.dirname(SCRIPTPATH)
print(f"SCRIPTPATH={SCRIPTPATH} PROJECTPATH={PROJECTPATH}")
print(f'SCRIPTPATH={SCRIPTPATH} PROJECTPATH={PROJECTPATH}')

if "AMALGAMATE_SOURCE_PATH" not in os.environ:
AMALGAMATE_SOURCE_PATH = os.path.join(PROJECTPATH, "src")
if 'AMALGAMATE_SOURCE_PATH' not in os.environ:
AMALGAMATE_SOURCE_PATH = os.path.join(PROJECTPATH, 'src')
else:
AMALGAMATE_SOURCE_PATH = os.environ["AMALGAMATE_SOURCE_PATH"]
if "AMALGAMATE_INCLUDE_PATH" not in os.environ:
AMALGAMATE_INCLUDE_PATH = os.path.join(PROJECTPATH, "include")
AMALGAMATE_SOURCE_PATH = os.environ['AMALGAMATE_SOURCE_PATH']
if 'AMALGAMATE_INCLUDE_PATH' not in os.environ:
AMALGAMATE_INCLUDE_PATH = os.path.join(PROJECTPATH, 'include')
else:
AMALGAMATE_INCLUDE_PATH = os.environ["AMALGAMATE_INCLUDE_PATH"]
if "AMALGAMATE_OUTPUT_PATH" not in os.environ:
AMALGAMATE_INCLUDE_PATH = os.environ['AMALGAMATE_INCLUDE_PATH']
if 'AMALGAMATE_OUTPUT_PATH' not in os.environ:
AMALGAMATE_OUTPUT_PATH = os.path.join(SCRIPTPATH)
else:
AMALGAMATE_OUTPUT_PATH = os.environ["AMALGAMATE_OUTPUT_PATH"]
AMALGAMATE_OUTPUT_PATH = os.environ['AMALGAMATE_OUTPUT_PATH']

# this list excludes the "src/generic headers"
ALLCFILES = ["ada.cpp"]
ALLCFILES = ['ada.cpp']

# order matters
ALLCHEADERS = ["ada.h"]
ALLCHEADERS = ['ada.h']

found_includes = []

current_implementation=''
current_implementation = ''

def doinclude(fid: str, file: str, line: str, origin: str) -> None:

def doinclude(fid: str, file: str, line: str, origin: str) -> None:
p = os.path.join(AMALGAMATE_INCLUDE_PATH, file)
pi = os.path.join(AMALGAMATE_SOURCE_PATH, file)

Expand All @@ -61,22 +62,23 @@ def doinclude(fid: str, file: str, line: str, origin: str) -> None:
pass
else:
# If we don't recognize it, just emit the #include
print("unrecognized:", file, " from ", line, " in ", origin)
print('unrecognized:', file, ' from ', line, ' in ', origin)
print(line, file=fid)


def dofile(fid: str, prepath: str, filename: str) -> None:
file = os.path.join(prepath, filename)
RELFILE = os.path.relpath(file, PROJECTPATH)
# Last lines are always ignored. Files should end by an empty lines.
print(f"/* begin file {RELFILE} */", file=fid)
print(f'/* begin file {RELFILE} */', file=fid)
includepattern = re.compile('\\s*#\\s*include "(.*)"')
with open(file, 'r') as fid2:
for line in fid2:
line = line.rstrip('\n')
s = includepattern.search(line)
if s:
includedfile = s.group(1)
if includedfile == "ada.h" and filename == "ada.cpp":
if includedfile == 'ada.h' and filename == 'ada.cpp':
print(line, file=fid)
continue

Expand All @@ -86,7 +88,7 @@ def dofile(fid: str, prepath: str, filename: str) -> None:
doinclude(fid, includedfile, line, filename)
else:
print(line, file=fid)
print(f"/* end file {RELFILE} */", file=fid)
print(f'/* end file {RELFILE} */', file=fid)


# Get the generation date from git, so the output is reproducible.
Expand All @@ -95,51 +97,54 @@ def dofile(fid: str, prepath: str, filename: str) -> None:
# Forcing it to be UTC is difficult, because it needs to be portable
# between gnu date and busybox date.
try:
timestamp = subprocess.run(['git', 'show', '-s', '--format=%ci', 'HEAD'],
stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
timestamp = (
subprocess.run(['git', 'show', '-s', '--format=%ci', 'HEAD'], stdout=subprocess.PIPE)
.stdout.decode('utf-8')
.strip()
)
except Exception:
print("git not found, timestamp based on current time")
print('git not found, timestamp based on current time')
timestamp = str(datetime.datetime.now())
print(f"timestamp is {timestamp}")
print(f'timestamp is {timestamp}')

os.makedirs(AMALGAMATE_OUTPUT_PATH, exist_ok=True)
AMAL_H = os.path.join(AMALGAMATE_OUTPUT_PATH, "ada.h")
AMAL_C = os.path.join(AMALGAMATE_OUTPUT_PATH, "ada.cpp")
DEMOCPP = os.path.join(AMALGAMATE_OUTPUT_PATH, "cpp")
README = os.path.join(AMALGAMATE_OUTPUT_PATH, "README.md")
AMAL_H = os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.h')
AMAL_C = os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.cpp')
DEMOCPP = os.path.join(AMALGAMATE_OUTPUT_PATH, 'cpp')
README = os.path.join(AMALGAMATE_OUTPUT_PATH, 'README.md')

print(f"Creating {AMAL_H}")
print(f'Creating {AMAL_H}')
amal_h = open(AMAL_H, 'w')
print(f"/* auto-generated on {timestamp}. Do not edit! */", file=amal_h)
print(f'/* auto-generated on {timestamp}. Do not edit! */', file=amal_h)
for h in ALLCHEADERS:
doinclude(amal_h, h, f"ERROR {h} not found", h)
doinclude(amal_h, h, f'ERROR {h} not found', h)

amal_h.close()
print()
print()
print(f"Creating {AMAL_C}")
print(f'Creating {AMAL_C}')
amal_c = open(AMAL_C, 'w')
print(f"/* auto-generated on {timestamp}. Do not edit! */", file=amal_c)
print(f'/* auto-generated on {timestamp}. Do not edit! */', file=amal_c)
for c in ALLCFILES:
doinclude(amal_c, c, f"ERROR {c} not found", c)
doinclude(amal_c, c, f'ERROR {c} not found', c)

amal_c.close()

# copy the README and DEMOCPP
if SCRIPTPATH != AMALGAMATE_OUTPUT_PATH:
shutil.copy2(os.path.join(SCRIPTPATH,"demo.cpp"),AMALGAMATE_OUTPUT_PATH)
shutil.copy2(os.path.join(SCRIPTPATH,"demo.c"),AMALGAMATE_OUTPUT_PATH)
shutil.copy2(os.path.join(SCRIPTPATH,"README.md"),AMALGAMATE_OUTPUT_PATH)
shutil.copy2(os.path.join(SCRIPTPATH, 'demo.cpp'), AMALGAMATE_OUTPUT_PATH)
shutil.copy2(os.path.join(SCRIPTPATH, 'demo.c'), AMALGAMATE_OUTPUT_PATH)
shutil.copy2(os.path.join(SCRIPTPATH, 'README.md'), AMALGAMATE_OUTPUT_PATH)

shutil.copy2(os.path.join(AMALGAMATE_INCLUDE_PATH,"ada_c.h"),AMALGAMATE_OUTPUT_PATH)
shutil.copy2(os.path.join(AMALGAMATE_INCLUDE_PATH, 'ada_c.h'), AMALGAMATE_OUTPUT_PATH)

zf = zipfile.ZipFile(os.path.join(AMALGAMATE_OUTPUT_PATH,'singleheader.zip'), 'w', zipfile.ZIP_DEFLATED)
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH,"ada.cpp"), "ada.cpp")
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH,"ada.h"), "ada.h")
zf.write(os.path.join(AMALGAMATE_INCLUDE_PATH,"ada_c.h"), "ada_c.h")
zf = zipfile.ZipFile(os.path.join(AMALGAMATE_OUTPUT_PATH, 'singleheader.zip'), 'w', zipfile.ZIP_DEFLATED)
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.cpp'), 'ada.cpp')
zf.write(os.path.join(AMALGAMATE_OUTPUT_PATH, 'ada.h'), 'ada.h')
zf.write(os.path.join(AMALGAMATE_INCLUDE_PATH, 'ada_c.h'), 'ada_c.h')


print("Done with all files generation.")
print('Done with all files generation.')

print(f"Files have been written to directory: {AMALGAMATE_OUTPUT_PATH}/")
print("Done with all files generation.")
print(f'Files have been written to directory: {AMALGAMATE_OUTPUT_PATH}/')
print('Done with all files generation.')
20 changes: 9 additions & 11 deletions tools/release/create_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
from github import Github
import lib.release as release

WORK_DIR = os.path.dirname(os.path.abspath(__file__)).replace("/tools/release", "")
WORK_DIR = os.path.dirname(os.path.abspath(__file__)).replace('/tools/release', '')

NEXT_TAG = os.environ["NEXT_RELEASE_TAG"]
REPO_NAME = os.environ["GITHUB_REPOSITORY"]
TOKEN = os.environ["GITHUB_TOKEN"]
NEXT_TAG = os.environ['NEXT_RELEASE_TAG']
REPO_NAME = os.environ['GITHUB_REPOSITORY']
TOKEN = os.environ['GITHUB_TOKEN']
if not NEXT_TAG or not REPO_NAME or not TOKEN:
raise Exception(
"Bad environment variables. Invalid GITHUB_REPOSITORY, GITHUB_TOKEN or NEXT_RELEASE_TAG"
)
raise Exception('Bad environment variables. Invalid GITHUB_REPOSITORY, GITHUB_TOKEN or NEXT_RELEASE_TAG')

g = Github(TOKEN)
repo = g.get_repo(REPO_NAME)
Expand All @@ -22,7 +20,7 @@
release.create_release(repo, NEXT_TAG, release_notes)

release = repo.get_release(NEXT_TAG)
release.upload_asset("singleheader/ada.cpp")
release.upload_asset("singleheader/ada.h")
release.upload_asset("singleheader/ada_c.h")
release.upload_asset("singleheader/singleheader.zip")
release.upload_asset('singleheader/ada.cpp')
release.upload_asset('singleheader/ada.h')
release.upload_asset('singleheader/ada_c.h')
release.upload_asset('singleheader/singleheader.zip')
Loading

0 comments on commit fad9295

Please sign in to comment.