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

[PREVIEW] teuthology: add support for graph walks of QA suites #2012

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
97 changes: 87 additions & 10 deletions teuthology/repo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def current_branch(path: str) -> str:
return result


def enforce_repo_state(repo_url, dest_path, branch, commit=None, remove_on_error=True):
def enforce_repo_state(dest_clone, dest_path, repo_url, branch, commit=None, remove_on_error=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to change initial signature and leave repo_url at first position, and add dest_cone as an optional argument with defaults value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, there will probably be lots of reworking to this code but it was a dirty hack to start because the clones after every QA suite tweak was driving me nuts. That's why it's in this PR.

"""
Use git to either clone or update a given repo, forcing it to switch to the
specified branch.
Expand All @@ -114,25 +114,100 @@ def enforce_repo_state(repo_url, dest_path, branch, commit=None, remove_on_error
# sentinel to track whether the repo has checked out the intended
# version, in addition to being cloned
repo_reset = os.path.join(dest_path, '.fetched_and_reset')
log.info("enforce_repo_state %s %s %s %s %s", dest_clone, dest_path, repo_url, branch, commit)
try:
if not os.path.isdir(dest_path):
clone_repo(repo_url, dest_path, branch, shallow=commit is None)
if not os.path.isdir(dest_clone):
bare_repo(dest_clone)
elif not commit and not is_fresh(sentinel):
set_remote(dest_path, repo_url)
fetch_branch(dest_path, branch)
#set_remote(dest_path, repo_url)
#fetch_branch(dest_path, branch)
touch_file(sentinel)

if commit and os.path.exists(repo_reset):
return
#if commit and os.path.exists(repo_reset):
#return

reset_repo(repo_url, dest_path, branch, commit)
touch_file(repo_reset)
myfetch(dest_clone, repo_url, branch, commit)
myworkspace(dest_clone, dest_path)
#reset_repo(repo_url, dest_path, branch, commit)
#touch_file(repo_reset)
# remove_pyc_files(dest_path)
except (BranchNotFoundError, CommitNotFoundError):
if remove_on_error:
shutil.rmtree(dest_path, ignore_errors=True)
raise

def bare_repo(git_dir):
log.info("bare_repo %s", git_dir)
args = ['git', 'init', '--bare', git_dir]
proc = subprocess.Popen(args)
Copy link
Contributor

@phlogistonjohn phlogistonjohn Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't need a lot of the complexity that Popen provides, I heartily recommend using the simpler subprocess.run function (avialable since 3.5). You can use the check=True parameter to have it raise exceptions on non-zero exit automatically too.

#args,
#stdout=subprocess.PIPE,
#stderr=subprocess.STDOUT)
if proc.wait() != 0:
raise RuntimeError("oops")

def myworkspace(git_dir, workspace_dir):
log.info("myworkspace %s %s", git_dir, workspace_dir)

if os.path.exists(workspace_dir):
args = [
'git',
'log',
'-1',
]
proc = subprocess.Popen(args,cwd=workspace_dir)
if proc.wait() != 0:
raise RuntimeError("oops")
return

args = [
'git',
'worktree',
'add',
#'--detach',
'-B', os.path.basename(workspace_dir),
'--no-track',
'--force',
workspace_dir,
'FETCH_HEAD'
]
proc = subprocess.Popen(args,cwd=git_dir)
#args,
#stdout=subprocess.PIPE,
#stderr=subprocess.STDOUT)
if proc.wait() != 0:
raise RuntimeError("oops")


def myfetch(git_dir, url, branch, commit=None):
log.info("myfetch %s %s %s %s", git_dir, url, branch, commit)
validate_branch(branch)
if commit is not None:
args = ['git', 'log', '-1', commit]
proc = subprocess.Popen(args, cwd=git_dir, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if proc.wait() == 0:
return
args = ['git', 'fetch', url]
if commit is not None:
args.append(commit)
else:
args.append(branch)
proc = subprocess.Popen(args,cwd=git_dir)
#proc = subprocess.Popen(
#args,
#cwd=git_dir,
#)
#stdout=subprocess.PIPE,
#stderr=subprocess.STDOUT)
if proc.wait() != 0:
not_found_str = "fatal: couldn't find remote ref %s" % branch
out = proc.stdout.read().decode()
log.error(out)
if not_found_str in out.lower():
raise BranchNotFoundError(branch)
else:
raise GitError("git fetch failed!")


def clone_repo(repo_url, dest_path, branch, shallow=True):
"""
Expand Down Expand Up @@ -354,6 +429,7 @@ def fetch_repo(url, branch, commit=None, bootstrap=None, lock=True):
os.mkdir(src_base_path)
ref_dir = ref_to_dirname(commit or branch)
dirname = '%s_%s' % (url_to_dirname(url), ref_dir)
dest_clone = os.path.join(src_base_path, url_to_dirname(url))
dest_path = os.path.join(src_base_path, dirname)
# only let one worker create/update the checkout at a time
lock_path = dest_path.rstrip('/') + '.lock'
Expand All @@ -362,7 +438,8 @@ def fetch_repo(url, branch, commit=None, bootstrap=None, lock=True):
try:
while proceed():
try:
enforce_repo_state(url, dest_path, branch, commit)
#enforce_repo_state(url, dest_path, branch, commit)
enforce_repo_state(dest_clone, dest_path, url, branch, commit)
if bootstrap:
sentinel = os.path.join(dest_path, '.bootstrapped')
if commit and os.path.exists(sentinel) or is_fresh(sentinel):
Expand Down