James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import os |
| 4 | import shutil |
| 5 | import subprocess |
| 6 | |
| 7 | from upstream_utils import ( |
| 8 | get_repo_root, |
| 9 | clone_repo, |
| 10 | comment_out_invalid_includes, |
| 11 | walk_cwd_and_copy_if, |
| 12 | git_am, |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | def crlf_to_lf(stackwalker_dir): |
| 17 | for root, _, files in os.walk(stackwalker_dir): |
| 18 | if ".git" in root: |
| 19 | continue |
| 20 | |
| 21 | for fname in files: |
| 22 | filename = os.path.join(root, fname) |
| 23 | print(f"Converting CRLF -> LF for {filename}") |
| 24 | with open(filename, "rb") as f: |
| 25 | content = f.read() |
| 26 | content = content.replace(b"\r\n", b"\n") |
| 27 | |
| 28 | with open(filename, "wb") as f: |
| 29 | f.write(content) |
| 30 | |
| 31 | cwd = os.getcwd() |
| 32 | os.chdir(stackwalker_dir) |
| 33 | subprocess.check_call(["git", "add", "-A"]) |
| 34 | subprocess.check_call(["git", "commit", "-m", "Fix line endings"]) |
| 35 | os.chdir(cwd) |
| 36 | |
| 37 | |
| 38 | def main(): |
| 39 | upstream_root = clone_repo( |
| 40 | "https://github.com/JochenKalmbach/StackWalker", |
| 41 | "42e7a6e056a9e7aca911a7e9e54e2e4f90bc2652", |
| 42 | shallow=False, |
| 43 | ) |
| 44 | wpilib_root = get_repo_root() |
| 45 | wpiutil = os.path.join(wpilib_root, "wpiutil") |
| 46 | |
| 47 | # Run CRLF -> LF before trying any patches |
| 48 | crlf_to_lf(upstream_root) |
| 49 | |
| 50 | # Apply patches to upstream Git repo |
| 51 | os.chdir(upstream_root) |
| 52 | for f in [ |
| 53 | "0001-Apply-PR-35.patch", |
| 54 | "0002-Remove-_M_IX86-checks.patch", |
| 55 | "0003-Add-advapi-pragma.patch", |
| 56 | ]: |
| 57 | git_am( |
| 58 | os.path.join(wpilib_root, "upstream_utils/stack_walker_patches", f), |
| 59 | ignore_whitespace=True, |
| 60 | ) |
| 61 | |
| 62 | shutil.copy( |
| 63 | os.path.join("Main", "StackWalker", "StackWalker.h"), |
| 64 | os.path.join(wpiutil, "src/main/native/windows/StackWalker.h"), |
| 65 | ) |
| 66 | |
| 67 | shutil.copy( |
| 68 | os.path.join("Main", "StackWalker", "StackWalker.cpp"), |
| 69 | os.path.join(wpiutil, "src/main/native/windows/StackWalker.cpp"), |
| 70 | ) |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | main() |