Skip to content

Commit

Permalink
Merge pull request #35 from roblabla/ruff
Browse files Browse the repository at this point in the history
Reformat all python scripts with ruff, add ruff to pre-commit
  • Loading branch information
roblabla authored Nov 21, 2023
2 parents 1781224 + ba00fd8 commit 0ab369b
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 199 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ repos:
rev: 'v17.0.4'
hooks:
- id: clang-format
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.6
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
180 changes: 115 additions & 65 deletions scripts/create_devenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

SCRIPTS_DIR = Path(__file__).parent


def run_generic_extract(msi_file_path: Path, output_dir: Path) -> int:
return subprocess.check_call(["7z", "x", "-y", str(msi_file_path)], cwd=output_dir)

Expand All @@ -21,7 +22,10 @@ def run_msiextract(msi_file_path: Path, output_dir: Path) -> int:


def run_msiextract_win32(msi_file_path: Path, output_dir: Path) -> int:
return subprocess.check_call(["msiexec", "/a", str(msi_file_path), "/qb", f"TARGETDIR={output_dir}"], cwd=output_dir)
return subprocess.check_call(
["msiexec", "/a", str(msi_file_path), "/qb", f"TARGETDIR={output_dir}"],
cwd=output_dir,
)


def run_windows_program(args, add_env=None, cwd=None):
Expand Down Expand Up @@ -106,32 +110,46 @@ def check_file(path: Path, message: str) -> Path:

def parse_arguments() -> Namespace:
parser = ArgumentParser(description="Prepare devenv")
parser.add_argument("--only", action='append', choices=['vs', 'dx8', 'py', 'pragma', 'cygwin'], help="Only run certain steps. Possible values are vs, dx8, py, pragma and cygwin.")
parser.add_argument(
"--only",
action="append",
choices=["vs", "dx8", "py", "pragma", "cygwin"],
help="Only run certain steps. Possible values are vs, dx8, py, pragma and cygwin.",
)
parser.add_argument("dl_cache_path", help="Path to download the requirements in")
parser.add_argument("output_path", help="The output directory")
parser.add_argument("--download", action='store_true', help="Only download the components, don't install them.")
parser.add_argument(
"--download",
action="store_true",
help="Only download the components, don't install them.",
)

return parser.parse_args()


def get_sha256(path):
h = hashlib.new('sha256')
h = hashlib.new("sha256")
with open(path, "rb") as f:
h.update(f.read())
return h.hexdigest()


units = {"GB": 1024**3, "MB": 1024**2, "KiB": 1024**1}


def parse_size(size):
for unit, val in units.items():
if size > val:
return format(float(size) / val, '.2f') + unit
return format(float(size) / val, ".2f") + unit

return str(size) + "B"

clear_line_sequence = '' if sys.platform == 'win32' else '\033[2K'

clear_line_sequence = "" if sys.platform == "win32" else "\033[2K"

last_refresh = datetime.now()


def progress_bar(blocks_transfered, block_size, total_bytes):
global last_refresh

Expand All @@ -144,78 +162,90 @@ def progress_bar(blocks_transfered, block_size, total_bytes):
bytes_transfered = blocks_transfered * block_size

x = int(size * bytes_transfered / total_bytes)
clear_line_sequence = '' if sys.platform == 'win32' else '\033[2K'
print("{}[{}{}] {}/{}".format(clear_line_sequence, "#"*x, "."*(size-x), parse_size(bytes_transfered), parse_size(total_bytes)),
end='\r', file=sys.stdout, flush=True)
clear_line_sequence = "" if sys.platform == "win32" else "\033[2K"
print(
"{}[{}{}] {}/{}".format(
clear_line_sequence,
"#" * x,
"." * (size - x),
parse_size(bytes_transfered),
parse_size(total_bytes),
),
end="\r",
file=sys.stdout,
flush=True,
)
last_refresh = datetime.now()


def download_requirement(dl_cache_path, requirement):
path = dl_cache_path / requirement['filename']
if path.exists() and get_sha256(path) == requirement['sha256']:
path = dl_cache_path / requirement["filename"]
if path.exists() and get_sha256(path) == requirement["sha256"]:
return

print("Downloading " + requirement['name'])
urllib.request.urlretrieve(requirement['url'], path, progress_bar)
print(clear_line_sequence, end='', flush=True, file=sys.stdout)
print("Downloading " + requirement["name"])
urllib.request.urlretrieve(requirement["url"], path, progress_bar)
print(clear_line_sequence, end="", flush=True, file=sys.stdout)
hash = get_sha256(path)
if hash != requirement['sha256']:
raise Exception("Download failed: Got hash " + hash + ", expected " + requirement['sha256'])
if hash != requirement["sha256"]:
raise Exception(
"Download failed: Got hash " + hash + ", expected " + requirement["sha256"]
)


def download_requirements(dl_cache_path, steps):
requirements = [
{
'name': 'Direct X 8.0',
'only': 'dx8',
'url': 'https://archive.org/download/dx8sdk/dx8sdk.exe',
'filename': 'dx8sdk.exe',
'sha256': '719f8fe4f02af5f435aac4a90bf9ef958210e6bd1d1e9715f26d13b10a73cb6c',
"name": "Direct X 8.0",
"only": "dx8",
"url": "https://archive.org/download/dx8sdk/dx8sdk.exe",
"filename": "dx8sdk.exe",
"sha256": "719f8fe4f02af5f435aac4a90bf9ef958210e6bd1d1e9715f26d13b10a73cb6c",
},
{
'name': 'Visual Studio .NET 2002 Professional Edition',
'only': 'vs',
'url': 'https://archive.org/download/en_vs.net_pro_full/en_vs.net_pro_full.exe',
'filename': 'en_vs.net_pro_full.exe',
'sha256': '440949f3d152ee0375050c2961fc3c94786780b5aae7f6a861a5837e03bf2dac',
"name": "Visual Studio .NET 2002 Professional Edition",
"only": "vs",
"url": "https://archive.org/download/en_vs.net_pro_full/en_vs.net_pro_full.exe",
"filename": "en_vs.net_pro_full.exe",
"sha256": "440949f3d152ee0375050c2961fc3c94786780b5aae7f6a861a5837e03bf2dac",
},
{
'name': 'Python 3.4.4',
'only': 'py',
'url': 'https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi',
'filename': 'python-3.4.4.msi',
'sha256': '46c8f9f63cf02987e8bf23934b2f471e1868b24748c5bb551efcf4863b43ca6c',
"name": "Python 3.4.4",
"only": "py",
"url": "https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi",
"filename": "python-3.4.4.msi",
"sha256": "46c8f9f63cf02987e8bf23934b2f471e1868b24748c5bb551efcf4863b43ca6c",
},
{
'name': 'Visual C++ 10.0 Runtime',
'only': 'py',
'url': 'https://download.microsoft.com/download/1/6/5/165255E7-1014-4D0A-B094-B6A430A6BFFC/vcredist_x86.exe',
'filename': 'vcredist_x86.exe',
'sha256': '99dce3c841cc6028560830f7866c9ce2928c98cf3256892ef8e6cf755147b0d8',
"name": "Visual C++ 10.0 Runtime",
"only": "py",
"url": "https://download.microsoft.com/download/1/6/5/165255E7-1014-4D0A-B094-B6A430A6BFFC/vcredist_x86.exe",
"filename": "vcredist_x86.exe",
"sha256": "99dce3c841cc6028560830f7866c9ce2928c98cf3256892ef8e6cf755147b0d8",
},
{
'name': 'Cygwin',
'only': 'cygwin',
"name": "Cygwin",
"only": "cygwin",
# On darwin, for whatever reason, the 32-bit installer fails. Let's
# just grab the 64-bit installer instead.
'condition': sys.platform == 'darwin',
'url': 'http://ctm.crouchingtigerhiddenfruitbat.org/pub/cygwin/setup/snapshots/setup-x86_64-2.874.exe',
'filename': 'cygwin-setup-2.874.exe',
'sha256': '58f9f42f5dbd52c5e3ecd24e537603ee8897ea15176b7acdc34afcef83e5c19a',
"condition": sys.platform == "darwin",
"url": "http://ctm.crouchingtigerhiddenfruitbat.org/pub/cygwin/setup/snapshots/setup-x86_64-2.874.exe",
"filename": "cygwin-setup-2.874.exe",
"sha256": "58f9f42f5dbd52c5e3ecd24e537603ee8897ea15176b7acdc34afcef83e5c19a",
},
{
'name': 'Cygwin',
'only': 'cygwin',
'condition': sys.platform != 'darwin',
'url': 'http://ctm.crouchingtigerhiddenfruitbat.org/pub/cygwin/setup/snapshots/setup-x86-2.874.exe',
'filename': 'cygwin-setup-2.874.exe',
'sha256': 'a79e4f57ce98a4d4bacb8fbb66fcea3de92ef30b34ab8b76e11c8bd3b426fd31',
"name": "Cygwin",
"only": "cygwin",
"condition": sys.platform != "darwin",
"url": "http://ctm.crouchingtigerhiddenfruitbat.org/pub/cygwin/setup/snapshots/setup-x86-2.874.exe",
"filename": "cygwin-setup-2.874.exe",
"sha256": "a79e4f57ce98a4d4bacb8fbb66fcea3de92ef30b34ab8b76e11c8bd3b426fd31",
},
]

for requirement in requirements:
if requirement['only'] in steps:
if 'condition' not in requirement or requirement['condition']:
if requirement["only"] in steps:
if "condition" not in requirement or requirement["condition"]:
download_requirement(dl_cache_path, requirement)


Expand All @@ -236,10 +266,15 @@ def install_compiler_sdk(installer_path, tmp_dir, tmp2_dir, output_path):
for compiler_directory_part in compiler_directories:
dst_required_directory_path = output_path / compiler_directory_part
src_required_directory_path = tmp_dir / compiler_directory_part
shutil.copytree(src_required_directory_path, dst_required_directory_path, dirs_exist_ok=True)
shutil.copytree(
src_required_directory_path, dst_required_directory_path, dirs_exist_ok=True
)

msvcr70_dll_src_path = tmp_dir / "MSVCR70.DLL"
shutil.copy(msvcr70_dll_src_path, output_path / "PROGRAM FILES/MICROSOFT VISUAL STUDIO .NET/VC7/BIN")
shutil.copy(
msvcr70_dll_src_path,
output_path / "PROGRAM FILES/MICROSOFT VISUAL STUDIO .NET/VC7/BIN",
)

# Extract and grab Windows SDK
tmp2_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -249,7 +284,9 @@ def install_compiler_sdk(installer_path, tmp_dir, tmp2_dir, output_path):
for sdk_directory_part in sdk_directories:
dst_required_directory_path = output_path / sdk_directory_part
src_required_directory_path = tmp2_dir / sdk_directory_part
shutil.copytree(src_required_directory_path, dst_required_directory_path, dirs_exist_ok=True)
shutil.copytree(
src_required_directory_path, dst_required_directory_path, dirs_exist_ok=True
)

shutil.rmtree(tmp2_dir, ignore_errors=True)

Expand Down Expand Up @@ -294,7 +331,9 @@ def install_directx8(dx8sdk_installer_path, tmp_dir, output_path):
shutil.rmtree(tmp_dir, ignore_errors=True)


def install_python(python_installer_path, vcredist_installer_path, tmp_dir, output_path):
def install_python(
python_installer_path, vcredist_installer_path, tmp_dir, output_path
):
print("Installing Python")
shutil.rmtree(tmp_dir, ignore_errors=True)
tmp_dir.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -345,9 +384,18 @@ def install_cygwin(cygwin_installer_path, tmp_dir, output_path):
def install_pragma_var_order(tmp_dir, output_path):
print("Installing pragma_var_order")
tmp_dir.mkdir(parents=True, exist_ok=True)
run_windows_program([str(SCRIPTS_DIR / "th06run.bat"), "CL.EXE", str(SCRIPTS_DIR / "pragma_var_order.cpp"), "/ohackery.dll", "/link", "/DLL"], add_env={
'DEVENV_PREFIX': str(output_path)
}, cwd=str(tmp_dir))
run_windows_program(
[
str(SCRIPTS_DIR / "th06run.bat"),
"CL.EXE",
str(SCRIPTS_DIR / "pragma_var_order.cpp"),
"/ohackery.dll",
"/link",
"/DLL",
],
add_env={"DEVENV_PREFIX": str(output_path)},
cwd=str(tmp_dir),
)
VC7 = output_path / "PROGRAM FILES/MICROSOFT VISUAL STUDIO .NET/VC7"
if not (VC7 / "BIN/C1XXOrig.DLL").exists():
shutil.move(VC7 / "BIN/C1XX.DLL", VC7 / "BIN/C1XXOrig.DLL")
Expand All @@ -363,7 +411,7 @@ def main(args: Namespace) -> int:
tmp2_dir = output_path / "tmp2"

if args.only is None or len(args.only) == 0:
steps = set(['vs', 'dx8', 'py', 'pragma', 'cygwin'])
steps = set(["vs", "dx8", "py", "pragma", "cygwin"])
else:
steps = set(args.only)

Expand All @@ -380,19 +428,21 @@ def main(args: Namespace) -> int:
vcredist_installer_path = dl_cache_path / "vcredist_x86.exe"
cygwin_installer_path = dl_cache_path / "cygwin-setup-2.874.exe"

if 'vs' in steps:
if "vs" in steps:
install_compiler_sdk(installer_path, tmp_dir, tmp2_dir, output_path)
if 'dx8' in steps:
if "dx8" in steps:
install_directx8(dx8sdk_installer_path, tmp_dir, output_path)
if 'py' in steps:
install_python(python_installer_path, vcredist_installer_path, tmp_dir, output_path)
if 'pragma' in steps:
if "py" in steps:
install_python(
python_installer_path, vcredist_installer_path, tmp_dir, output_path
)
if "pragma" in steps:
install_pragma_var_order(tmp_dir, output_path)
if 'cygwin' in steps:
if "cygwin" in steps:
install_cygwin(cygwin_installer_path, tmp_dir, output_path)

return 0


if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main(parse_arguments()))
47 changes: 28 additions & 19 deletions scripts/diff_all_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,57 @@

from generate_function_diff import generate_function_diff


def main():
base_dir = Path(__file__).parent.parent
config_dir = base_dir / 'config'
config_dir = base_dir / "config"

implemented_csv = config_dir / 'implemented.csv'
implemented_csv = config_dir / "implemented.csv"

with open(implemented_csv) as f:
vals = []
for row in csv.reader(f):
orig, reimpl = generate_function_diff(row[0])
vals.append({
'name': row[0],
'diff': "\n".join(difflib.unified_diff(orig.split('\n'), reimpl.split('\n'), 'Original', 'Reimplementation', lineterm='')),
'ratio': difflib.SequenceMatcher(None, orig, reimpl).ratio(),
})

vals.append(
{
"name": row[0],
"diff": "\n".join(
difflib.unified_diff(
orig.split("\n"),
reimpl.split("\n"),
"Original",
"Reimplementation",
lineterm="",
)
),
"ratio": difflib.SequenceMatcher(None, orig, reimpl).ratio(),
}
)

print("# Report")
print("")
print(f"name | result")
print(f"-----|-------")
print("name | result")
print("-----|-------")
for val in vals:
name = val['name']
id = val['name'].lower().replace(":", "__")
if val['ratio'] != 1:
name = f'[{name}](#user-content-{id})'
name = val["name"]
id = val["name"].lower().replace(":", "__")
if val["ratio"] != 1:
name = f"[{name}](#user-content-{id})"
print(f"{name} | {val['ratio'] * 100:.2f}%")


for val in vals:
if val['ratio'] != 1:
id = val['name'].lower().replace(":", "__")
if val["ratio"] != 1:
id = val["name"].lower().replace(":", "__")
print("")
print("")
print(f'<details id="{id}"><summary><h2>{val["name"]}</h2></summary>')
print("")
print("```diff")
print(val['diff'])
print(val["diff"])
print("```")
print("")
print("</details>")


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit 0ab369b

Please sign in to comment.