Browse Source

Show a summary at the end in json format. Turn off color by default (for clean text output such as for redirecting to a file).

master
poikilos 8 months ago
parent
commit
df737c09d4
  1. 41
      pyenliven/mtpatches.py

41
pyenliven/mtpatches.py

@ -10,7 +10,9 @@ mtpatches.py [options]
Options: Options:
--skip-missing Do not list files in heads that are not in sources. --skip-missing Do not list files in heads that are not in sources.
--color Enable console colors such as <ESC character>[32m
''' '''
import json
import os import os
import platform import platform
import shlex import shlex
@ -18,6 +20,7 @@ import shutil
import sys import sys
import subprocess import subprocess
from binaryornot.check import is_binary from binaryornot.check import is_binary
from collections import OrderedDict
class Fore: class Fore:
@ -452,6 +455,7 @@ def usage():
def main(): def main():
skip_missing = False skip_missing = False
enable_color = False
bases = ( bases = (
"/opt/minebest/assemble/bucket_game", "/opt/minebest/assemble/bucket_game",
# "/opt/minebest/mtkit/minetest/src", # "/opt/minebest/mtkit/minetest/src",
@ -465,15 +469,19 @@ def main():
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
if arg == "--skip-missing": if arg == "--skip-missing":
skip_missing = True skip_missing = True
elif arg == "--color":
enable_color = True
else: else:
usage() usage()
echo0("Error: unknown argument {}".format(arg)) echo0("Error: unknown argument {}".format(arg))
return 1 return 1
return check_if_head_files_applied(bases, head_parents, return check_if_head_files_applied(bases, head_parents,
skip_missing=skip_missing) skip_missing=skip_missing,
enable_color=enable_color)
def check_if_head_files_applied(bases, head_parents, skip_missing=False): def check_if_head_files_applied(bases, head_parents, skip_missing=False,
enable_color=False):
"""Check if head files are applied. """Check if head files are applied.
Args: Args:
@ -482,17 +490,29 @@ def check_if_head_files_applied(bases, head_parents, skip_missing=False):
head_parents (list[str]): Folders containing various patches, head_parents (list[str]): Folders containing various patches,
where each sub of each parent is in the form of files to where each sub of each parent is in the form of files to
overlay onto base. overlay onto base.
enable_color (bool): Enable console colors such as
"{escape_character}[32m" for green. Defaults to False.
Returns: Returns:
int: 0 on success. int: 0 on success.
""" """
summary = OrderedDict(
unfinished_patch_count=0,
unpatched_file_count=0,
)
reset_color = ""
color = ""
if enable_color:
reset_color = Fore.RESET
for base_root in bases: for base_root in bases:
if not os.path.isdir(base_root): if not os.path.isdir(base_root):
echo0('Warning: There is no base_root "{}".'.format(base_root)) echo0('Warning: There is no base_root "{}".'.format(base_root))
continue continue
for head_parent in head_parents: for head_parent in head_parents:
echo0("\n# {}".format(head_parent)) echo0("\n# patches={}".format(head_parent))
for head_sub in os.listdir(head_parent): for head_sub in os.listdir(head_parent):
echo0("## patch={}".format(head_sub))
# Identify each head folder as an overlay to "patch" a base. # Identify each head folder as an overlay to "patch" a base.
# *Ignore files* in each head parent! # *Ignore files* in each head parent!
@ -626,15 +646,22 @@ def check_if_head_files_applied(bases, head_parents, skip_missing=False):
diffs = diff_only_head(parallel_base, parallel_head, diffs = diff_only_head(parallel_base, parallel_head,
log_level=-1) log_level=-1)
if len(diffs) > 0:
summary['unfinished_patch_count'] += 1
echo0('* differs from patch "{}": {} file(s)'
''.format(head_parent, len(diffs)))
for diff in diffs: for diff in diffs:
summary['unpatched_file_count'] += 1
base_file = os.path.join(parallel_base, diff['rel']) base_file = os.path.join(parallel_base, diff['rel'])
head_file = os.path.join(parallel_head, diff['rel']) head_file = os.path.join(parallel_head, diff['rel'])
missing = bool(diff.get("new")) missing = bool(diff.get("new"))
if missing: if missing:
if skip_missing: if skip_missing:
continue continue
if enable_color:
color = Fore.GREEN color = Fore.GREEN
else: else:
if enable_color:
color = Fore.YELLOW color = Fore.YELLOW
why = "MISSING" if missing else "differs" why = "MISSING" if missing else "differs"
# echo0(" * {}: {}".format(why, diff)) # echo0(" * {}: {}".format(why, diff))
@ -654,7 +681,9 @@ def check_if_head_files_applied(bases, head_parents, skip_missing=False):
# else isdir # else isdir
# echo0("head_is_binary={}".format(head_is_binary)) # echo0("head_is_binary={}".format(head_is_binary))
head_is_binary = is_binary(head_file) head_is_binary = is_binary(head_file)
difftool = "diffimg-gui" if head_is_binary else "meld" difftool = "diffimage-gui" if head_is_binary else "meld"
# ^ Poikilos' diffimage from rotocanvas
# (*not* the same as nicolashahn' diffimg).
difftool = difftool difftool = difftool
echo0( echo0(
@ -664,10 +693,10 @@ def check_if_head_files_applied(bases, head_parents, skip_missing=False):
head_file, head_file,
]) ])
+ "{} # {} in base (original) vs head (patch) {}" + "{} # {} in base (original) vs head (patch) {}"
"".format(color, why, Fore.RESET) "".format(color, why, reset_color)
) )
# endregion check whether base has it installed # endregion check whether base has it installed
echo0("summary={}".format(json.dumps(summary, indent=2)))
return 0 return 0

Loading…
Cancel
Save