Skip to content

Commit

Permalink
Produce an OpenStudio SDK CMakeLists.txt patch
Browse files Browse the repository at this point in the history
I'm way too tired of manually doing it
  • Loading branch information
jmarrec committed Apr 17, 2024
1 parent d04ac45 commit f1b95fb
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/checksums.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ jobs:
tag: ${{ env.TAG_NAME }}
overwrite: true
file_glob: true

- name: Create an OpenStudio CMakeLists.txt diff
shell: bash
run: |
python -m pip install requests
python create_cmakelist_diff.py --tag-name ${{ env.TAG_NAME }} --md5-file md5sums.txt
- name: Upload CMakeLists diff to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: '.tmp_diff/CMakeLists.txt.patch'
tag: ${{ env.TAG_NAME }}
overwrite: true
file_glob: false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
/build/
/openstudio-gems/

.tmp_diff
md5sums.txt
sha256sums.txt

# ignore tar.gz files
*.tar.gz

Expand Down
127 changes: 127 additions & 0 deletions create_cmakelist_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import argparse
from pathlib import Path
import os
import re
import shutil
import subprocess
from typing import List, Optional, Tuple

import requests

RE_DATE = re.compile(r'\d{8}')

def validate_file(arg):
if (filepath := Path(arg).expanduser()).is_file():
return filepath
else:
raise FileNotFoundError(arg)


def get_openstudio_cmakelist():
r = requests.get('https://raw.githubusercontent.com/NREL/OpenStudio/develop/CMakeLists.txt')
r.raise_for_status()
cmake_lines = r.text.splitlines()
return cmake_lines


def read_md5sums(md5_file: Optional[Path] = None, tag_name: Optional[str] = None) -> dict:
assert not (md5_file is None and tag_name is None)
if md5_file is not None:
print(f"Reading from {md5_file}")
md5sums_lines = md5_file.read_text().splitlines()
else:
url = f"https://github.com/jmarrec/openstudio-gems/releases/download/{tag_name}/md5sums.txt"
print(f"Trying to read from release at '{url}'")
r = requests.get(url)
r.raise_for_status()
md5sums_lines = r.text.splitlines()

md5sums = {}
for x in md5sums_lines:
md5sha, zip_name = x.split(' ')
md5sums[RE_DATE.sub('*', zip_name)] = {'name': zip_name, 'md5sha': md5sha}
return md5sums


def get_block_location(cmake_lines: List[str]) -> Tuple[int, int]:
start_i = None
end_i = None
for i, line in enumerate(cmake_lines):
if start_i is None and 'OPENSTUDIO_GEMS_BASEURL' in line:
start_i = i
continue
if start_i is not None:
if 'OPENSTUDIO_GEMS_ZIP_LOCAL_PATH' in line:
end_i = i
break
assert start_i is not None and end_i is not None
return [start_i, end_i]


def create_new_cmakelists(cmake_lines: List[str], md5sums: dict, tag_name: str) -> List[str]:
# Make a copy
cmake_mod_lines = cmake_lines[:]

start_i, end_i = get_block_location(cmake_lines=cmake_lines)
for i, line in enumerate(cmake_mod_lines):
if i < start_i or i > end_i:
continue
if 'OPENSTUDIO_GEMS_BASEURL' in line and 'github' in line:
last_tag_name = line.split('"')[1].split('/')[-1]
cmake_mod_lines[i] = line.replace(last_tag_name, tag_name)
if 'set(OPENSTUDIO_GEMS_ZIP_FILENAME' in line:
zip_name = line.split('"')[1]
lookup = RE_DATE.sub('*', zip_name)
if lookup not in md5sums:
zip_name = None
continue
d = md5sums[lookup]
cmake_mod_lines[i] = line.replace(zip_name, d['name'])
sha_line = cmake_mod_lines[i + 1]
assert 'set(OPENSTUDIO_GEMS_ZIP_EXPECTED_MD5' in sha_line
old_sha = sha_line.split('"')[1]
cmake_mod_lines[i + 1] = sha_line.replace(old_sha, d['md5sha'])
return cmake_mod_lines


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create an OpenStudio CMakeLists.txt with updated gems")

parser.add_argument("--tag-name", help="Use a specific tag name")
parser.add_argument("--md5-file", type=validate_file, help="Path to a md5sum.txt on disk, will download it from an existing release otherwise")


args = parser.parse_args()
print(args)
tag_name = args.tag_name
if tag_name is None:
if 'TAG_NAME' not in os.environ:
raise ValueError("Specify --tag-name TAG_NAME or set it in environment")
tag_name = os.environ['TAG_NAME']
print(f"You did not specify --tag-name, grabbing it from environment variable TAG_NAME='{tag_name}'")

md5sums = read_md5sums(md5_file=args.md5_file, tag_name=tag_name)
cmake_lines = get_openstudio_cmakelist()
cmake_mod_lines = create_new_cmakelists(cmake_lines=cmake_lines, md5sums=md5sums, tag_name=tag_name)
diff_dir = Path('.tmp_diff')
if diff_dir.exists():
shutil.rmtree(diff_dir)
diff_dir.mkdir()
ori_path = (diff_dir / 'CMakeLists_ori.txt')
ori_path.write_text("\n".join(cmake_lines))
new_path = (diff_dir / 'CMakeLists_new.txt')
new_path.write_text("\n".join(cmake_mod_lines))

print('{:=^80s}'.format(" Diff "))
r = subprocess.run(["diff", "-u", str(ori_path), str(new_path)], stdout=subprocess.PIPE, encoding='utf-8',
universal_newlines=True)
unified_diff = r.stdout
unified_diff = unified_diff.replace(str(ori_path), "CMakeLists.txt").replace(str(new_path), "CMakeLists.txt")
print(unified_diff)
print("=" * 80)
(diff_dir / 'CMakeLists.txt.patch').write_text(unified_diff)

print('\n\n')
print('{:=^80s}'.format(" For Copy Pasting "))
start_i, end_i = get_block_location(cmake_lines=cmake_lines)
print("\n".join(cmake_mod_lines[start_i:end_i]))

0 comments on commit f1b95fb

Please sign in to comment.