-
Notifications
You must be signed in to change notification settings - Fork 8
GitHub extraction #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
GitHub extraction #122
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5eadf4
move files around
james-bruten-mo 0f624bd
update suite_report
james-bruten-mo 96a4755
black
james-bruten-mo 6a1bf53
Merge branch 'main' into github_extraction
james-bruten-mo 9165ebf
Update github_scripts/get_git_sources.py
james-bruten-mo 43b1ff9
Update github_scripts/suite_data.py
james-bruten-mo f0c6279
add option for using https
james-bruten-mo f743d70
remove debug prints
james-bruten-mo dc0cf64
Merge branch 'main' into github_extraction
james-bruten-mo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # *****************************COPYRIGHT******************************* | ||
| # (C) Crown copyright Met Office. All rights reserved. | ||
| # For further details please refer to the file COPYRIGHT.txt | ||
| # which you should have received as part of this distribution. | ||
| # *****************************COPYRIGHT******************************* | ||
| """ | ||
| Clone sources for a rose-stem run for use with git bdiff module in scripts | ||
| """ | ||
|
|
||
| import re | ||
| import subprocess | ||
| from typing import Optional | ||
| from pathlib import Path | ||
| from shutil import rmtree | ||
|
|
||
|
|
||
| def run_command( | ||
| command: str, rval: bool = False | ||
| ) -> Optional[subprocess.CompletedProcess]: | ||
| """ | ||
| Run a subprocess command and return the result object | ||
| Inputs: | ||
| - command, str with command to run | ||
| Outputs: | ||
| - result object from subprocess.run | ||
| """ | ||
| command = command.split() | ||
| result = subprocess.run( | ||
| command, | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=300, | ||
| shell=False, | ||
| check=False, | ||
| ) | ||
| if result.returncode: | ||
| print(result.stdout, end="\n\n\n") | ||
| raise RuntimeError( | ||
| f"[FAIL] Issue found running command {command}\n\n{result.stderr}" | ||
| ) | ||
| if rval: | ||
| return result | ||
|
|
||
|
|
||
| def clone_repo_mirror( | ||
| source: str, repo_ref: str, parent: str, mirror_loc: Path, loc: Path | ||
| ) -> None: | ||
| """ | ||
| Clone a repo source using a local git mirror. | ||
| Assume the mirror is set up as per the Met Office | ||
| """ | ||
|
|
||
| # Remove if this clone already exists | ||
| if loc.exists(): | ||
| rmtree(loc) | ||
|
|
||
| command = f"git clone {mirror_loc} {loc}" | ||
| run_command(command) | ||
|
|
||
| # If not provided a ref, return | ||
| if not repo_ref: | ||
| return | ||
|
|
||
| source = source.removeprefix("git@github.com:") | ||
| user = source.split("/")[0] | ||
| # Check that the user is different to the Upstream User | ||
| if user in parent.split("/")[0]: | ||
| user = None | ||
|
|
||
| # If the ref is a hash then we don't need the fork user as part of the fetch. | ||
| # Equally, if the user is the Upstream User, it's not needed | ||
| if re.match(r"^\s*([0-9a-f]{40})\s*$", repo_ref) or not user: | ||
| fetch = repo_ref | ||
| else: | ||
| fetch = f"{user}/{repo_ref}" | ||
| commands = ( | ||
| f"git -C {loc} fetch origin {fetch}", | ||
| f"git -C {loc} checkout FETCH_HEAD", | ||
| ) | ||
| for command in commands: | ||
| run_command(command) | ||
|
|
||
|
|
||
| def clone_repo(repo_source: str, repo_ref: str, loc: Path) -> None: | ||
| """ | ||
| Clone the repo and checkout the provided ref | ||
| Only if a remote source | ||
| """ | ||
|
|
||
| # Remove if this clone already exists | ||
| if loc.exists(): | ||
| rmtree(loc) | ||
|
|
||
| # Create a clean clone location | ||
| loc.mkdir(parents=True) | ||
|
|
||
| commands = ( | ||
| f"git -C {loc} init", | ||
| f"git -C {loc} remote add origin {repo_source}", | ||
| f"git -C {loc} fetch origin {repo_ref}", | ||
| f"git -C {loc} checkout FETCH_HEAD", | ||
| ) | ||
| for command in commands: | ||
| run_command(command) | ||
|
|
||
|
|
||
| def sync_repo(repo_source: str, repo_ref: str, loc: Path) -> None: | ||
| """ | ||
| Rsync a local git clone and checkout the provided ref | ||
| """ | ||
|
|
||
| # Remove if this clone already exists | ||
| if loc.exists(): | ||
| rmtree(loc) | ||
|
|
||
| # Create a clean clone location | ||
| loc.mkdir(parents=True) | ||
|
|
||
| # Trailing slash required for rsync | ||
| command = f"rsync -av {repo_source}/ {loc}" | ||
| run_command(command) | ||
| if repo_ref: | ||
| command = f"git -C {loc} checkout {repo_ref}" | ||
| run_command(command) | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #!/usr/bin/env python3 | ||
| # *****************************COPYRIGHT******************************* | ||
| # (C) Crown copyright Met Office. All rights reserved. | ||
| # For further details please refer to the file COPYRIGHT.txt | ||
| # which you should have received as part of this distribution. | ||
| # *****************************COPYRIGHT******************************* | ||
| """ | ||
| Clone sources for a rose-stem run for use with git bdiff module in scripts | ||
| Only intended for use with rose-stem suites that have provided appropriate environment | ||
| variables | ||
| """ | ||
|
|
||
| import os | ||
| from datetime import datetime | ||
| from pathlib import Path | ||
| from ast import literal_eval | ||
| from get_git_sources import clone_repo, clone_repo_mirror, sync_repo | ||
| from typing import Dict | ||
|
|
||
|
|
||
| def main() -> None: | ||
|
|
||
| clone_loc = Path(os.environ["SOURCE_DIRECTORY"]) | ||
|
|
||
| dependencies: Dict = literal_eval(os.environ["DEPENDENCIES"]) | ||
|
|
||
| for dependency, values in dependencies.items(): | ||
|
|
||
| print(f"Extracting {dependency} at time {datetime.now()}") | ||
|
|
||
| loc = clone_loc / dependency | ||
|
|
||
| if ".git" in values["source"]: | ||
| if os.environ["USE_MIRRORS"] == "True": | ||
| mirror_loc = Path(os.environ["GIT_MIRROR_LOC"]) / values["parent"] | ||
| clone_repo_mirror( | ||
| values["source"], values["ref"], values["parent"], mirror_loc, loc | ||
| ) | ||
| else: | ||
| clone_repo(values["source"], values["ref"], loc) | ||
| else: | ||
| sync_repo(values["source"], values["ref"], loc) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.