Skip to content

Instantly share code, notes, and snippets.

@iydon
Created June 24, 2022 22:41
Show Gist options
  • Save iydon/ced91b5347c77b317e27e8863dca4d9c to your computer and use it in GitHub Desktop.
Save iydon/ced91b5347c77b317e27e8863dca4d9c to your computer and use it in GitHub Desktop.
Download and merge different versions of OpenFOAM
__root__ = __import__('pathlib').Path(__file__).parent
import pathlib as p
import shutil
import subprocess
import typing as t
Path = t.Union[str, p.Path]
class Git:
def __init__(self, cwd: Path) -> None:
self.cwd = p.Path(cwd)
self.cwd.mkdir(parents=True, exist_ok=True)
@classmethod
def from_path(cls, cwd: Path) -> 'Git':
return cls(cwd)
def add(self, *args: str) -> 'Git':
self._git('add', *args)
return self
def clone(self, *args: str) -> 'Git':
self._git('clone', *args)
return self
def commit(self, message: str) -> 'Git':
self._git('commit', '-m', message)
return self
def gc(self) -> 'Git':
self._git('gc')
return self
def init(self) -> 'Git':
if not (self.cwd/'.git').exists():
self._git('init')
return self
def tag(self, name: str) -> 'Git':
self._git('tag', name)
return self
def _run(self, *args: str) -> subprocess.CompletedProcess:
return subprocess.run(args, cwd=self.cwd)
def _git(self, *args: str) -> subprocess.CompletedProcess:
return self._run('git', *args)
def _clean(self) -> None:
for path in self.cwd.iterdir():
if path.name != '.git':
if path.is_file():
path.unlink()
elif path.is_dir():
shutil.rmtree(path)
else:
raise Exception(f'Path "{path}" is neither a file nor a directory')
if __name__ == '__main__':
src, dst = __root__/'version', __root__/'repository'
versions = {
'2.3.x', '2.4.x', '3.0.x',
'4.x', '5.x',
'6', '7', '8',
}
if not src.exists(): # step 1: download different versions of OpenFOAM
git = Git.from_path(src)
for version in versions:
git.clone(
'--depth', '1',
f'https://github.com/OpenFOAM/OpenFOAM-{version}',
version,
)
for name in {'.git', 'doc', 'test', 'tutorials'}:
path = src / version / name
if path.exists() and path.is_dir():
shutil.rmtree(path)
print('Please make changes to the version directory and then run this script again')
elif not dst.exists(): # step 2: merge different versions of OpenFOAM
git = Git.from_path(dst).init()
for version in sorted(versions):
git._clean()
shutil.copytree(src/version, dst, dirs_exist_ok=True)
git \
.add('.') \
.commit(f':bookmark: OpenFOAM-{version}') \
.tag(version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment