aboutsummaryrefslogtreecommitdiffstats
path: root/bin/common/srtool_patcher.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/common/srtool_patcher.py')
-rwxr-xr-xbin/common/srtool_patcher.py93
1 files changed, 73 insertions, 20 deletions
diff --git a/bin/common/srtool_patcher.py b/bin/common/srtool_patcher.py
index bda941fc..17032750 100755
--- a/bin/common/srtool_patcher.py
+++ b/bin/common/srtool_patcher.py
@@ -68,10 +68,39 @@ DISABLE_TAG = 'DISABLE'
# Extract a clean version of the file without the the custom sections,
# and generate a patch file of those sections
#
+# States:
+# 'LOOKING' : scan the lines for an extension beginning, otherwise output lines
+# 'FOUND_EXTEND' : scan the lines for an extension ending, otherwise skip lines
+# 'FOUND_EXCLUDE' : scan the lines for an exclude ending, otherwise uncomment the lines
+# 'BLANK' : scan for a trailing blank line after an extension, skip if found
+#
+
+def _uncomment_line(line):
+ def remove_delimiters(line,lead,tail):
+ pos = line.find(lead)
+ if 0 <= pos:
+ line = line[0:pos] + line[pos+len(lead):]
+ if tail:
+ pos = line.rfind(tail)
+ if 0 <= pos:
+ line = line[0:pos] + line[pos+len(tail):]
+ return(line)
+ test_line = line.strip()
+ if test_line.startswith('#'):
+ return remove_delimiters(line,'#','')
+ if test_line.startswith('<!--'):
+ return remove_delimiters(line,'<!--','-->')
+ if test_line.startswith('/*'):
+ return remove_delimiters(line,'/*','*/')
+ if test_line.startswith('"__'):
+ return remove_delimiters(line,'__','')
+ return(line)
def extract_custom_patch(custom_file,clean_file,patch_file,label,patcher_dir,options):
- tag_begin = "%s_EXTENSION_BEGIN" % label
- tag_end = "%s_EXTENSION_END" % label
+ tag_exclude_begin = "%s_EXTENSION_EXCLUDE_BEGIN" % label
+ tag_exclude_end = "%s_EXTENSION_EXCLUDE_END" % label
+ tag_extend_begin = "%s_EXTENSION_BEGIN" % label
+ tag_extend_end = "%s_EXTENSION_END" % label
ret = 0
# Insure patcher working directory is ready
@@ -89,30 +118,54 @@ def extract_custom_patch(custom_file,clean_file,patch_file,label,patcher_dir,opt
with open(custom_file, 'r') as fs:
with open(clean_file, 'w') as fd:
- state = "find"
+ state = "LOOKING"
+ leading_blank_line = False
for line in fs:
#print("LINE(%s):%s" % (state,line.strip()))
- if state == "find":
- if 0 < line.find(tag_begin):
- #print("START:$line")
- state = "found"
+ # Process state
+ if state == "LOOKING":
+ if 0 < line.find(tag_exclude_begin):
+ #print("START_FOUND_EXCLUDE:%s" % line)
+ state = "FOUND_EXCLUDE"
+ continue
+ if 0 < line.find(tag_extend_begin):
+ #print("START_FOUND_EXTEND:%s" % line)
+ state = "FOUND_EXTEND"
+ continue
+ elif state == "FOUND_EXCLUDE":
+ if 0 < line.find(tag_exclude_end):
+ #print("STOP_FOUND_EXCLUDE:%s" % line)
+ state = "LOOKING"
continue
- elif state == "found":
- if 0 < line.find(tag_end):
- #print("STOP:$line")
- state = "blank"
+ elif state == "FOUND_EXTEND":
+ if 0 < line.find(tag_extend_end):
+ #print("STOP_FOUND_EXTEND:%s" % line)
+ if leading_blank_line:
+ # Remove trailing blank line
+ state = "BLANK"
+ else:
+ state = "LOOKING"
continue
- elif state == "blank":
- state = "find"
+ elif state == "BLANK":
+ state = "LOOKING"
# if next line after stop is blank, hide that also
if not line.strip():
continue
- # Normal line?
- if state != "found":
+
+ # Output line?
+ if state == "FOUND_EXCLUDE":
+ # Restore excluded line
+ line = _uncomment_line(line)
+ fd.write('%s' % line)
+ elif state != "FOUND_EXTEND":
+ # Normal line
fd.write('%s' % line)
+ leading_blank_line = not line.strip()
+
# Did we end cleanly?
- if state != "find":
+ if state != "LOOKING":
print("ERROR: START not STOPPED (%s)" % state)
+
if INPLACE_TAG in options:
cmd = "git diff %s > %s" % (custom_file,patch_file)
print(cmd)
@@ -207,15 +260,14 @@ def diff_original(custom_file,original_file,patch_file,label,patcher_dir,options
#
def clean_inplace(custom_file,original_file,patch_file,label,patcher_dir,options):
- print("FOO1:%s,%s" % (INPLACE_TAG,options))
# Skip if not in-place
if not INPLACE_TAG in options:
return 0
- print("FOO2")
# Only continue if 'original' file has custom patches, in case this
# command is run multiple times by accident
- cmd = "grep %s %s" % (label,original_file)
+ print("* Confirm in-place file has customization")
+ cmd = "grep %s %s > /dev/null 2>&1" % (label,original_file)
ret = os.system(cmd)
if 0 != ret:
print("ERROR:SKIP: original file '%s' does not have custom tags '%s'" % (original_file,label))
@@ -352,7 +404,8 @@ def main(argv):
ret = 0
for custom_file,original_file,patch_file,options in file_list:
- #print("PATCH_FILE:%s,%s,%s,%s,%s,%s" % (custom_file,original_file,patch_file,options,patcher_dir,options))
+ if verbose:
+ print("*** PATCHER_FILE:%s,%s,%s,%s,%s" % (custom_file,original_file,patch_file,patcher_dir,options))
if 'merge_original' == args.command:
validate_file(custom_file,'Custom')
validate_file(original_file,'Original')