Skip to content
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

Workspace 152 #153

Merged
merged 15 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix over writing repo
  • Loading branch information
--global committed Oct 21, 2024
commit ab8ce082b625d33121299cdebfbb11b797925b08
41 changes: 31 additions & 10 deletions .submodules/setup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from argparse import ArgumentParser, Namespace
from dataclasses import dataclass
from pathlib import Path
from shutil import rmtree
from subprocess import CalledProcessError
from time import sleep

Expand Down Expand Up @@ -61,6 +62,17 @@ def get_namespace() -> Namespace:
action="store_true",
dest="skip_login",
default=False,
help="Skip login if already logged in.",
)
arg_parser.add_argument(
"--overwrite-clone",
action="store_true",
dest="overwrite_clone",
default=False,
help=(
"Deletes each clone's current directory if they have one and clones"
" each repo."
),
)

return arg_parser.parse_args()
Expand Down Expand Up @@ -99,20 +111,14 @@ def read_submodules() -> t.Dict[str, Submodule]:


def login_to_github():
"""Log into GitHub with the CLI and setup Git to use the CLI as a credential
helper.
"""Log into GitHub with the CLI.

https://cli.github.com/manual/gh_auth_login
https://cli.github.com/manual/gh_auth_setup-git
"""
subprocess.run(
["gh", "auth", "login", "--web"],
check=True,
)
subprocess.run(
["gh", "auth", "setup-git"],
check=True,
)


def fork_repo(url: str):
Expand Down Expand Up @@ -149,25 +155,32 @@ def fork_repo(url: str):
return True


def clone_repo(name: str, path: str):
def clone_repo(name: str, path: str, overwrite: bool):
# pylint: disable=line-too-long
"""Clone a repo from GitHub.

https://cli.github.com/manual/gh_repo_clone

Args:
name: The name of the repo to clone.
path: The paths to clone the repo to.
overwrite: A flag designating whether to delete the repo's current directory if it exists and clone the repo in the directory.

Returns:
A flag designating whether the repo was successfully cloned.
"""
# pylint: enable=line-too-long
print(Style.BRIGHT + "Cloning repo..." + Style.RESET_ALL)

repo_dir = str(BASE_DIR / path)

if os.path.isdir(repo_dir) and os.listdir(repo_dir):
print(Style.BRIGHT + repo_dir + Style.RESET_ALL + " already exists.")

return True
if overwrite:
rmtree(repo_dir)
else:
return True

retry_delay, max_retries = 1, 5
for retry_index in range(max_retries):
Expand All @@ -179,13 +192,17 @@ def clone_repo(name: str, path: str):

return True
except CalledProcessError:
if os.path.isdir(repo_dir):
rmtree(repo_dir)

print(
Style.BRIGHT
+ Fore.YELLOW
+ f"Retrying clone in {retry_delay} seconds."
+ f" Attempt {retry_index + 1}/{max_retries}."
+ Style.RESET_ALL
)

sleep(retry_delay)
retry_delay *= 2

Expand Down Expand Up @@ -250,7 +267,11 @@ def main() -> None:

cloned_repo = False
if forked_repo:
cloned_repo = clone_repo(name, submodule.path)
cloned_repo = clone_repo(
name,
submodule.path,
namespace.overwrite_clone,
)

view_repo(name)

Expand Down