Skip to content

Commit

Permalink
Merge pull request #320 from rest-for-physics/pullforeach
Browse files Browse the repository at this point in the history
Adding foreach to pull-submodules.py
  • Loading branch information
jgalan authored Nov 9, 2022
2 parents 324eeed + 7efeef0 commit 3999b01
Showing 1 changed file with 48 additions and 67 deletions.
115 changes: 48 additions & 67 deletions pull-submodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def main():
if (not exclude and url.find("github") != -1) or (
url.find("lfna.unizar.es") != -1 and lfna) or (
url.find("gitlab.pandax.sjtu.edu.cn") != -1 and sjtu):
print("Pulling: ", end="")
print("Updating submodule: ", end="")
print(fullpath.rstrip(), end='')
# init
p = subprocess.run(f"cd {root} && git submodule init {submodule}", #
p = subprocess.run(f"cd {root} && git submodule update --init {submodule}", #
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
Expand All @@ -179,78 +179,59 @@ def main():
if p.stdout.decode("utf-8").find("checkout") >= 0:
print(p.stdout.decode("utf-8"))
errorOutput = p.stderr.decode("utf-8")
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1:
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1 or errorOutput.find("ERROR") != -1 or errorOutput.find("fatal") != -1:
print("[\033[91m Failed \x1b[0m]")
if debug:
print("Message: ")
print(errorOutput)
continue
# if 'force', override the changes with git reset
if force:
p = subprocess.run(f"cd {root}/{submodule} && git reset --hard", #
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
print(p.stdout.decode("utf-8"))
print(p.stderr.decode("utf-8"))
errorOutput = p.stderr.decode("utf-8")
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1:
print("[\033[91m Failed \x1b[0m]")
if debug:
print("Message: ")
print(errorOutput)
continue
# update submodule
p = subprocess.run(f"cd {root} && git submodule update {submodule}", #
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
print(p.stdout.decode("utf-8"))
print(p.stderr.decode("utf-8"))
if p.stdout.decode("utf-8").find("checkout") >= 0:
print(p.stdout.decode("utf-8"))
errorOutput = p.stderr.decode("utf-8")
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1:
print("[\033[91m Failed \x1b[0m]")
if debug:
print("Message: ")
print(errorOutput)
continue
# if latest, pull the latest commit instead of the one recorded in the main repo
if latest:
branchExistsPcs = subprocess.run(
f"git ls-remote --heads {url} {frameworkBranchName} | wc -l", #
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

branchToPull = "master"
if branchExistsPcs.stdout.decode("utf-8").rstrip("\n") != "0":
branchToPull = frameworkBranchName
print(f" --> Pulling branch: {branchToPull}", end='')

p = subprocess.run(
f"cd {root}/{submodule} && git fetch && git checkout {branchToPull} && git pull origin {branchToPull}", #
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
print(p.stdout.decode("utf-8"))
print(p.stderr.decode("utf-8"))
errorOutput = p.stderr.decode("utf-8")
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1:
print("[\033[91m Failed \x1b[0m]")
if debug:
print("Message: ")
print(errorOutput)
continue
# get commit id
p = subprocess.run(f"cd {root}/{submodule} && git rev-parse HEAD", #
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
print("[\033[92m OK \x1b[0m]")

if errorOutput.find("failed") == -1 and errorOutput.find("error") == -1:
print("[\033[92m OK \x1b[0m] (" + p.stdout.decode("utf-8")[0:7] + ")")
# back to the git framework directory
p = subprocess.run(f"cd {root}", #
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# if 'force', override the changes with git reset
if force:
print("Forcing reset: ", end="")
p = subprocess.run(f"git submodule foreach 'git reset --hard'", #
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
print(p.stdout.decode("utf-8"))
print(p.stderr.decode("utf-8"))
errorOutput = p.stderr.decode("utf-8")
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1 or errorOutput.find("ERROR") != -1 or errorOutput.find("fatal") != -1:
print("[\033[91m Failed \x1b[0m]")
if debug:
print("Message: ")
print(errorOutput)
else:
print("[\033[92m OK \x1b[0m]")

# if latest, pull the latest commit instead of the one recorded in the main repo
if latest:
print("Pulling submodules: ", end="")
p = subprocess.run(f"git submodule foreach 'git fetch; if [ -z \"$(git ls-remote --heads origin {frameworkBranchName})\" ]; then git checkout master; else git checkout {frameworkBranchName};fi;git pull'", #
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
print(p.stdout.decode("utf-8"))
print(p.stderr.decode("utf-8"))
errorOutput = p.stderr.decode("utf-8")
if errorOutput.find("failed") != -1 or errorOutput.find("error") != -1 or errorOutput.find("ERROR") != -1 or errorOutput.find("fatal") != -1:
print("[\033[91m Failed \x1b[0m]")
if debug:
print("Message: ")
print(errorOutput)
else:
print("[\033[92m OK \x1b[0m]")
# get commit id
p = subprocess.run(f"git submodule foreach 'git rev-parse HEAD'", #
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(p.stdout.decode("utf-8"))

if clean:
subprocess.run("git clean -xfd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.run("git reset --hard", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.run(f"git clean -xfd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.run(f"git reset --hard", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


if __name__ == "__main__":
Expand Down

0 comments on commit 3999b01

Please sign in to comment.