Cleanup: use main functions to allow importing scripts

Support importing scripts without running their logic to
allow basic validation (see #130746).

Parts of !131037 were used.

Co-authored-by: Bastien Montagne <bastien@blender.org>
This commit is contained in:
Campbell Barton
2024-11-29 15:21:01 +11:00
parent b295fc9a9c
commit 273f48cd53
10 changed files with 331 additions and 265 deletions

View File

@@ -21,95 +21,101 @@ def get_string(cmd):
return subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf8').strip()
# Parse arguments.
mode = None
base_branch = 'main'
if len(sys.argv) >= 2:
# Note that recursive conflict resolution strategy has to reversed in rebase compared to merge.
# See https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--m
if sys.argv[1] == '--rebase':
mode = 'rebase'
recursive_format_commit_merge_options = '-Xignore-all-space -Xtheirs'
elif sys.argv[1] == '--merge':
mode = 'merge'
recursive_format_commit_merge_options = '-Xignore-all-space -Xours'
if len(sys.argv) == 4:
if sys.argv[2] == '--base_branch':
base_branch = sys.argv[3]
def main() -> int:
# Parse arguments.
mode = None
base_branch = 'main'
if len(sys.argv) >= 2:
# Note that recursive conflict resolution strategy has to reversed in rebase compared to merge.
# See https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--m
if sys.argv[1] == '--rebase':
mode = 'rebase'
recursive_format_commit_merge_options = '-Xignore-all-space -Xtheirs'
elif sys.argv[1] == '--merge':
mode = 'merge'
recursive_format_commit_merge_options = '-Xignore-all-space -Xours'
if len(sys.argv) == 4:
if sys.argv[2] == '--base_branch':
base_branch = sys.argv[3]
if mode is None:
print("Merge or rebase Blender main (or another base branch) into a branch in 3 steps,")
print("to automatically merge clang-format changes.")
print("")
print(" --rebase Perform equivalent of 'git rebase main'")
print(" --merge Perform equivalent of 'git merge main'")
print("")
print("Optional arguments:")
print(" --base_branch <branch name> Use given branch instead of main")
print(" (assuming that base branch has already been updated")
print(" and has the initial clang-format commit).")
sys.exit(0)
if mode is None:
print("Merge or rebase Blender main (or another base branch) into a branch in 3 steps,")
print("to automatically merge clang-format changes.")
print("")
print(" --rebase Perform equivalent of 'git rebase main'")
print(" --merge Perform equivalent of 'git merge main'")
print("")
print("Optional arguments:")
print(" --base_branch <branch name> Use given branch instead of main")
print(" (assuming that base branch has already been updated")
print(" and has the initial clang-format commit).")
return 0
# Verify we are in the right directory.
root_path = get_string(['git', 'rev-parse', '--show-superproject-working-tree'])
if os.path.realpath(root_path) != os.path.realpath(os.getcwd()):
print("BLENDER MERGE: must run from blender repository root directory")
sys.exit(1)
# Verify we are in the right directory.
root_path = get_string(['git', 'rev-parse', '--show-superproject-working-tree'])
if os.path.realpath(root_path) != os.path.realpath(os.getcwd()):
print("BLENDER MERGE: must run from blender repository root directory")
return 1
# Abort if a rebase is still progress.
rebase_merge = get_string(['git', 'rev-parse', '--git-path', 'rebase-merge'])
rebase_apply = get_string(['git', 'rev-parse', '--git-path', 'rebase-apply'])
merge_head = get_string(['git', 'rev-parse', '--git-path', 'MERGE_HEAD'])
if os.path.exists(rebase_merge) or \
os.path.exists(rebase_apply) or \
os.path.exists(merge_head):
print("BLENDER MERGE: rebase or merge in progress, complete it first")
sys.exit(1)
# Abort if a rebase is still progress.
rebase_merge = get_string(['git', 'rev-parse', '--git-path', 'rebase-merge'])
rebase_apply = get_string(['git', 'rev-parse', '--git-path', 'rebase-apply'])
merge_head = get_string(['git', 'rev-parse', '--git-path', 'MERGE_HEAD'])
if os.path.exists(rebase_merge) or \
os.path.exists(rebase_apply) or \
os.path.exists(merge_head):
print("BLENDER MERGE: rebase or merge in progress, complete it first")
return 1
# Abort if uncommitted changes.
changes = get_string(['git', 'status', '--porcelain', '--untracked-files=no'])
if len(changes) != 0:
print("BLENDER MERGE: detected uncommitted changes, can't run")
sys.exit(1)
# Abort if uncommitted changes.
changes = get_string(['git', 'status', '--porcelain', '--untracked-files=no'])
if len(changes) != 0:
print("BLENDER MERGE: detected uncommitted changes, can't run")
return 1
# Setup command, with commit message for merge commits.
if mode == 'rebase':
mode_cmd = 'rebase'
else:
branch = get_string(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
mode_cmd = 'merge --no-edit -m "Merge \'' + base_branch + '\' into \'' + branch + '\'"'
# Setup command, with commit message for merge commits.
if mode == 'rebase':
mode_cmd = 'rebase'
else:
branch = get_string(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
mode_cmd = 'merge --no-edit -m "Merge \'' + base_branch + '\' into \'' + branch + '\'"'
# Rebase up to the clang-format commit.
code = os.system('git merge-base --is-ancestor ' + pre_format_commit + ' HEAD')
if code != 0:
code = os.system('git ' + mode_cmd + ' ' + pre_format_commit)
# Rebase up to the clang-format commit.
code = os.system('git merge-base --is-ancestor ' + pre_format_commit + ' HEAD')
if code != 0:
print("BLENDER MERGE: resolve conflicts, complete " + mode + " and run again")
sys.exit(code)
code = os.system('git ' + mode_cmd + ' ' + pre_format_commit)
if code != 0:
print("BLENDER MERGE: resolve conflicts, complete " + mode + " and run again")
return code
# Rebase clang-format commit.
code = os.system('git merge-base --is-ancestor ' + format_commits[-1] + ' HEAD')
if code != 0:
os.system('git ' + mode_cmd + ' ' + recursive_format_commit_merge_options + ' ' + format_commits[-1])
paths = get_string(('git', '--no-pager', 'diff', '--name-only', format_commits[-1])).replace('\n', ' ')
if sys.platform == 'win32' and len(paths) > 8000:
# Windows command-line does not accept more than 8191 chars.
os.system('make format')
else:
os.system('make format PATHS="' + paths + '"')
os.system('git add -u')
count = int(get_string(['git', 'rev-list', '--count', '' + format_commits[-1] + '..HEAD']))
if count == 1 or mode == 'merge':
# Amend if we just have a single commit or are merging.
os.system('git commit --amend --no-edit')
else:
# Otherwise create a commit for formatting.
os.system('git commit -m "Cleanup: apply clang format"')
# Rebase clang-format commit.
code = os.system('git merge-base --is-ancestor ' + format_commits[-1] + ' HEAD')
if code != 0:
os.system('git ' + mode_cmd + ' ' + recursive_format_commit_merge_options + ' ' + format_commits[-1])
paths = get_string(('git', '--no-pager', 'diff', '--name-only', format_commits[-1])).replace('\n', ' ')
if sys.platform == 'win32' and len(paths) > 8000:
# Windows command-line does not accept more than 8191 chars.
os.system('make format')
else:
os.system('make format PATHS="' + paths + '"')
os.system('git add -u')
count = int(get_string(['git', 'rev-list', '--count', '' + format_commits[-1] + '..HEAD']))
if count == 1 or mode == 'merge':
# Amend if we just have a single commit or are merging.
os.system('git commit --amend --no-edit')
else:
# Otherwise create a commit for formatting.
os.system('git commit -m "Cleanup: apply clang format"')
# Rebase remaining commits
code = os.system('git ' + mode_cmd + ' ' + base_branch)
if code != 0:
print("BLENDER MERGE: resolve conflicts, complete " + mode + " and you're done")
else:
print("BLENDER MERGE: done")
sys.exit(code)
# Rebase remaining commits
code = os.system('git ' + mode_cmd + ' ' + base_branch)
if code != 0:
print("BLENDER MERGE: resolve conflicts, complete " + mode + " and you're done")
else:
print("BLENDER MERGE: done")
return code
if __name__ == "__main__":
sys.exit(main())

View File

@@ -6,6 +6,7 @@
# Created by Robert Wenzlaff (Det. Thorn).
# Oct. 30, 2003
import sys
from tkinter import (
Button,
Canvas,
@@ -288,9 +289,15 @@ class App:
################## Main App #######################
root = Tk()
def main() -> int:
root = Tk()
app = App(root)
root.title("Cursor Maker")
app = App(root)
root.title("Cursor Maker")
root.mainloop()
root.mainloop()
return 0
if __name__ == "__main__":
sys.exit(main())