diff options
author | Karl Berry <karl@freefriends.org> | 2018-12-17 21:34:06 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2018-12-17 21:34:06 +0000 |
commit | c4541e083cf32e2c9a372f5eb53277e416d866ac (patch) | |
tree | f1e745b732afa6bf95a2358263a39d45749872c5 /Master/texmf-dist/scripts | |
parent | 6b1a8094f0a744aa5cd6bf002eab62c21fcee76a (diff) |
changes (17dec18)
git-svn-id: svn://tug.org/texlive/trunk@49442 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts')
-rwxr-xr-x | Master/texmf-dist/scripts/changes/delcmdchanges.bash | 249 | ||||
-rwxr-xr-x | Master/texmf-dist/scripts/changes/pyMergeChanges.py | 277 |
2 files changed, 277 insertions, 249 deletions
diff --git a/Master/texmf-dist/scripts/changes/delcmdchanges.bash b/Master/texmf-dist/scripts/changes/delcmdchanges.bash deleted file mode 100755 index 6f18194ee7b..00000000000 --- a/Master/texmf-dist/scripts/changes/delcmdchanges.bash +++ /dev/null @@ -1,249 +0,0 @@ -#!/bin/bash - -################################################################################ -# -# Copyright (C) 2011 Silvano Chiaradonna (ISTI-CNR) -# -# Macro: delcmdchanges.bash -# -# License: -# This program is free software; you can redistribute -# it and/or modify it under the terms of the -# GNU General Public License as published by the Free -# Software Foundation; either version 2 of the License, -# or (at your option) any later version. -# -# This program is distributed in the hope that it will -# be useful, but WITHOUT ANY WARRANTY; without even the -# implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public -# License for more details. -# -# You should have received a copy of the GNU General -# Public License along with this program; if not, -# write to the Free Software Foundation, Inc., 59 -# Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# An on-line copy of the GNU General Public License can -# be downloaded from the FSF web page at: -# http://www.gnu.org/copyleft/gpl.html -# -# Version: 1.0 -# -# Purpose: -# This script deletes all changes-commands of the package changes.sty, accepting changes. If option -i is specified you will be prompted to accept or reject changes interactively. -# -# Usage: -# delcmdchanges.bash [-i] [-h] <inputfile> <outputfile> -# -# Requirements: -# linux systems -# -################################################################################ - -usage() -{ - cat << EOF -usage: $0 [options] <inputfile> <outputfile> - -This script deletes all changes-commands of the package changes.sty, accepting changes. If option -i is specified you will be prompted to accept or reject changes interactively. - -options: - -h show this message - -i interactive mode -EOF -} - -#-----------------------MAIN---------------- - -while getopts ":ih" opt; do - case $opt in - i) - imode="1" - shift - ;; - h) - usage - exit - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - usage - exit - ;; - esac -done - -if [ ! -e "$1" ]; then - echo "Input file not found: $1" >&2 - exit -fi - -INFILE="$1" -OUTFILE="$2" - -rm -f "${OUTFILE}" -touch "${OUTFILE}" - -awk -v im=${imode} -v fn=${OUTFILE} ' -# remove commands (accepting changes): -# \added[<authorid>][<remark>]{<new text>} -# \deleted[<authorid>][<remark>]{<deleted text>} -# \replaced[<authorid>][<remark>]{<new text>}{<old text>} - - BEGIN { EOT = SUBSEP } - -{ line[NR] = $0 } - -END { - do { - buf0 = "" - found = 0 - added = 0 - deleted = 0 - replaced = 0 - while (!found && (c = getc()) != EOT) { # parsing commands - buf0 = buf0 c - if( match(buf0, /\\added[[:space:]]*\[[^\]]*\][[:space:]]*\[[^\]]*\][[:space:]]*{/) || - match(buf0, /\\added[[:space:]]*\[[^\]]*\][[:space:]]*{/) || - match(buf0, /\\added[[:space:]]*{/) ) { # found command \added[<authorid>][<remark>]{<new text>} - added = 1 - found = 1 - balance = 1 - } else - if( match(buf0, /\\deleted[[:space:]]*\[[^\]]*\][[:space:]]*\[[^\]]*\][[:space:]]*{/) || - match(buf0, /\\deleted[[:space:]]*\[[^\]]*\][[:space:]]*{/) || - match(buf0, /\\deleted[[:space:]]*{/) ) { # found command \deleted[<authorid>][<remark>]{<deleted text>} - deleted = 1 - found = 1 - balance = 1 - } else - if( match(buf0, /\\replaced[[:space:]]*\[[^\]]*\][[:space:]]*\[[^\]]*\][[:space:]]*{/) || - match(buf0, /\\replaced[[:space:]]*\[[^\]]*\][[:space:]]*{/) || - match(buf0, /\\replaced[[:space:]]*{/) ) { # found command \replaced[<authorid>][<remark>]{<new text>}{<old text>} - replaced = 1 - found = 1 - balance = 1 - } - } - if( RLENGTH == -1 ) # command not found - pbuf0 = buf0 - else { # command found - pbuf0 = substr(buf0, 1, RSTART-1) - cmd = substr(buf0, RSTART, RLENGTH) # the command - } - printf("%s", pbuf0) >> fn # print text preceding command - if (c == EOT) - break - buf = "" - if( added ) { # command \added[][]{} - while (found && balance > 0 && (c = getc()) != EOT) # parsing <new text> - buf = buf c # <new text> - cmd = cmd buf # the command - if ( im ) { # interactive mode - printf("\nat line: %d\n%s\n", nr,cmd) - printf("Accept added text, Reject added text, Ignore or Finish accepting changes? [A|r|i|f]") - getline imoderes < "/dev/tty" - if( imoderes != "r" && imoderes != "i" ) { # accept added text - pbuf = substr(buf, 1, length(buf)-1) # remove ending curly brace "}" - printf("%s", pbuf) >> fn # print <new text> - if( imoderes == "f" ) # finish accepting changes - im = 0 # normal mode - } else - if( imoderes == "i" ) # ignore cmd - printf("%s", cmd) >> fn - } else { # accept change - pbuf = substr(buf, 1, length(buf)-1) # remove ending curly brace "}" - printf("%s", pbuf) >> fn # print <new text> - } - } # \added[][]{} - else - if( deleted ) { # command \deleted[][]{} - while (found && balance > 0 && (c = getc()) != EOT) # parsing <deleted text> - buf = buf c # <deleted text> to skip - cmd = cmd buf # the command - if ( im ) { # interactive mode - printf("\nat line: %d\n%s\n", nr,cmd) - printf("Accept to remove text, Reject deletion, Ignore or Finish accepting changes? [A|r|i|f]") - getline imoderes < "/dev/tty" - if( imoderes == "r" ) { # reject deletion - pbuf = substr(buf, 1, length(buf)-1) # remove ending curly brace "}" - printf("%s", pbuf) >> fn # print <deleted text> - } else - if( imoderes == "i" ) # ignore cmd - printf("%s", cmd) >> fn - else # accept changes - if( imoderes == "f" ) # finish accepting changes - im = 0 # normal mode - } # else accept changes - } # \deleted[][]{} - else - if ( replaced ) { # command \replaced[][]{}{} - while (replaced && balance > 0 && (c = getc()) != EOT) # parsing <new text> - buf = buf c # <new text> - cmd = cmd buf # the command - pbufnt = substr(buf, 1, length(buf)-1) # new text - buf = "" - skip=0 - while ( !skip && (c = getc()) != EOT ) { # skip first curly brace "{" - buf = buf c - if ( c !~ /[[:space:]]/ ) - skip = 1 - } - cmd = cmd buf # the command - if ( c != "{" ) { # found "{" - printf("\nOps! Syntax error at line %d of input file: \"%s\"\nPlease fix command \\replaced[][]{}{}: \"{\" is expected.\n\n", nr+1, cmd) - break - } - buf = "" - balance = 1 # reset balance - while (replaced && balance > 0 && (c = getc()) != EOT) # parsing <old text> - buf = buf c # <old text> - cmd = cmd buf # the command - pbufot = substr(buf, 1, length(buf)) # old text - if ( im ) { # interactive mode - printf("\nat line: %d\n%s\n", nr,cmd) - printf("Accept change, Reject change, Ignore or Finish accepting changes? [A|r|i|f]") - getline imoderes < "/dev/tty" - if( imoderes == "r" ) { # reject change - printf("%s", pbufot) >> fn # print <old text> - } else - if( imoderes == "i" ) { # ignore cmd - printf("%s", cmd) >> fn - } else { # accept change - printf("%s", pbufnt) >> fn # print <new text> - if( imoderes == "f" ) # finish accepting changes - im = 0 # normal mode - } - } else { # accept change - printf("%s", pbufnt) >> fn # print <new text> - } - } # \replaced[][]{}{} - } while(c != EOT) -} - -function getc() -{ - if (!nr) { - nr = 1 - nc = 1 - } - - if (nr > NR) - return EOT - if (nc > length(line[nr])) { - ++nr - nc = 1 - return "\n" - } - c = substr(line[nr], nc, 1) - ++nc - if (c == "{") - ++balance - else if (c == "}") - --balance - if (balance < 0) - balance = 0 - return c -} ' "${INFILE}" - diff --git a/Master/texmf-dist/scripts/changes/pyMergeChanges.py b/Master/texmf-dist/scripts/changes/pyMergeChanges.py new file mode 100755 index 00000000000..37209bdc649 --- /dev/null +++ b/Master/texmf-dist/scripts/changes/pyMergeChanges.py @@ -0,0 +1,277 @@ +#!/usr/bin/python +""" + pymergechanges: Merge commits made with changes package into text + Copyright (C) 2018 Y. Cui + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +pymergechanges: Merge commits made with changes package into text + +Requires Python version 3; Tested on Python 3.6.6 + +Supported commands: added, deleted, replaced, highlight +Usage: python pyMergeChanges.py [-arh] <Input File> <Output File> +<Output File> will be overwritten and must be different than <Input File>. +Options: + -a: accept all added, deleted and replaced + -r: reject all added, deleted and replaced + -h: remove all highlights +If no option is given, runs interactively. + +Created on Wed Dec 5 20:28:40 2018 + +""" + +import sys +import re +import codecs + +def parse_param(parstr): + if all(p in parstr for p in ('a', 'r')): + print('You cannot accept and reject at the same time.') + sys.exit(1) + parout = '' + for par in parstr[1:]: + if par == 'a': + print('Accepting all added, deleted and replaced') + parout += 'a' + elif par == 'r': + print('Rejecting all added, deleted and replaced') + parout += 'r' + elif par == 'h': + print('Removing all highlights.') + parout += 'h' + else: + print('Unknown parameter: ' + par) + sys.exit(1) + if not parout: + parout = 'i' + return parout + +def ask1(): # accept, reject, keep, break + if PARAMS == 'i': + while True: + ans = input('[a]ccept or [r]eject or [k]eep or [b]reak ? ').lower() + if ans == 'a': + print('Accepted.') + break + elif ans == 'r': + print('Rejected.') + break + elif ans == 'k': + print('Kept.') + break + elif ans == 'b': + print('Alright.') + break + elif any(p == 'a' for p in PARAMS): + print('Accepted.') + ans = 'a' + elif any(p == 'r' for p in PARAMS): + print('Rejected.') + ans = 'r' + else: + ans = 'k' + return ans + +def ask2(): # remove, keep, break + if PARAMS == 'i': + while True: + ans = input('[r]emove or [k]eep or [b]reak ? ').lower() + if ans == 'r': + print('Removed.') + break + elif ans == 'k': + print('Kept.') + break + elif ans == 'b': + print('Alright.') + break + return ans + elif any(p == 'h' for p in PARAMS): + print('Removed.') + ans = 'r' + else: + print('Kept.') + ans = 'k' + return ans + +def trim_space(line, pos): # trim two consecutive white spaces + if line[pos-1:pos+1] == ' ': + line = line[:pos] + line[pos+1:] + return line + +# BEGIN OF SCRIPT + +if len(sys.argv) not in [3, 4]: + print(__doc__) + sys.exit(1) + +# parse input parameters +if len(sys.argv) == 3: + print('Running in interactive mode. ') + INPUTFILE, OUTPUTFILE = sys.argv[1:] + PARAMS = "i" +if len(sys.argv) == 4: + PARAMLIST, INPUTFILE, OUTPUTFILE = sys.argv[1:] + PARAMS = parse_param(PARAMLIST) + +if INPUTFILE == OUTPUTFILE: + print('Input File and Output File must be different.') + sys.exit(1) + +RE_ADDED = re.compile(r'\\added(\[[^\]]*\]){0,1}\{([^\}]*)\}') +RE_DELETED = re.compile(r'\\deleted(\[[^\]]*\]){0,1}\{([^\}]*)\}') +RE_REPLACED = re.compile(r'\\replaced(\[[^\]]*\]){0,1}\{([^\}]*)\}\{([^\}]*)\}') +RE_HIGHLIGHT = re.compile(r'\\highlight(\[[^\]]*\]){0,1}\{([^\}]*)\}') +RE_COMMENT = re.compile(r'\\comment(\[[^\]]*\]){0,1}\{([^\}]*)\}') + +codecs.open(OUTPUTFILE, mode='w', encoding='utf8').close() +with codecs.open(INPUTFILE, mode='r', encoding='utf8') as fin, \ +codecs.open(OUTPUTFILE, mode='a', encoding='utf8') as fout: + LINE_COUNT = 0 + FLAG_FAST_BREAK = False # if you want to halt merging for some reason + for linein in fin: + if FLAG_FAST_BREAK: + fout.write(linein) + continue + LINE_COUNT += 1 + lineout = linein + matchAdded = RE_ADDED.search(lineout) + matchDeleted = RE_DELETED.search(lineout) + matchReplaced = RE_REPLACED.search(lineout) + matchHighlight = RE_HIGHLIGHT.search(lineout) + matchComment = RE_COMMENT.search(lineout) + flagHasCommit = matchAdded or matchDeleted or matchReplaced or matchHighlight or matchComment + if flagHasCommit: + print('\n******** In Line %i :\n %s' % (LINE_COUNT, lineout)) + next_pos = 0 + while matchAdded: + if FLAG_FAST_BREAK: + break + print('\n** add commit ** \n' + matchAdded.group()) + answer = ask1() + if answer == 'a': + lineout = (lineout[:matchAdded.start(0)] + + matchAdded.group(2) + lineout[matchAdded.end(0):]) + lineout = trim_space(lineout, matchAdded.start(0) + + len(matchAdded.group(2))) + lineout = trim_space(lineout, matchAdded.start(0)) + elif answer == 'r': + lineout = (lineout[:matchAdded.start(0)] + lineout[matchAdded.end(0):]) + lineout = trim_space(lineout, matchAdded.start(0)) + elif answer == 'k': + lineout = lineout + next_pos = matchAdded.end(0) + elif answer == 'b': + FLAG_FAST_BREAK = True + break + matchAdded = RE_ADDED.search(lineout, next_pos) # redo matching for updated text + + next_pos = 0 + while matchDeleted: + if FLAG_FAST_BREAK: + break + print('\n** delete commit ** \n' + matchDeleted.group()) + answer = ask1() + if answer == 'a': + lineout = (lineout[:matchDeleted.start(0)] + lineout[matchDeleted.end(0):]) + lineout = trim_space(lineout, matchDeleted.start(0)) + elif answer == 'r': + lineout = (lineout[:matchDeleted.start(0)] + matchDeleted.group(2) + + lineout[matchDeleted.end(0):]) + elif answer == 'k': + lineout = lineout + next_pos = matchDeleted.end(0) + elif answer == 'b': + FLAG_FAST_BREAK = True + break + matchDeleted = RE_DELETED.search(lineout, next_pos) + + next_pos = 0 + while matchReplaced: + if FLAG_FAST_BREAK: + break + print('\n** replace commit ** \n' + matchReplaced.group()) + answer = ask1() + if answer == 'a': + lineout = (lineout[:matchReplaced.start(0)] + + matchReplaced.group(2) + lineout[matchReplaced.end(0):]) + lineout = trim_space(lineout, matchReplaced.start(0) + + len(matchReplaced.group(2))) + lineout = trim_space(lineout, matchReplaced.start(0)) + elif answer == 'r': + lineout = (lineout[:matchReplaced.start(0)] + + matchReplaced.group(3) + lineout[matchReplaced.end(0):]) + lineout = trim_space(lineout, matchReplaced.start(0) + + len(matchReplaced.group(3))) + lineout = trim_space(lineout, matchReplaced.start(0)) + elif answer == 'k': + lineout = lineout + next_pos = matchReplaced.end(0) + elif answer == 'b': + FLAG_FAST_BREAK = True + break + matchReplaced = RE_REPLACED.search(lineout, next_pos) + + next_pos = 0 + while matchHighlight: + if FLAG_FAST_BREAK: + break + print('\n** highlight commit ** \n' + matchHighlight.group()) + answer = ask2() + if answer == 'r': + lineout = (lineout[:matchHighlight.start(0)] + + matchHighlight.group(2) + lineout[matchHighlight.end(0):]) + lineout = trim_space(lineout, matchHighlight.start(0) + + len(matchHighlight.group(2))) + lineout = trim_space(lineout, matchHighlight.start(0)) + elif answer == 'k': + lineout = lineout + next_pos = matchHighlight.end(0) + elif answer == 'b': + FLAG_FAST_BREAK = True + break + matchHighlight = RE_HIGHLIGHT.search(lineout, next_pos) + + next_pos = 0 + while matchComment: + if FLAG_FAST_BREAK: + break + print('\n** comment commit ** \n' + matchComment.group()) + answer = ask2() + if answer == 'r': + lineout = (lineout[:matchComment.start(0)] + + matchComment.group(2) + lineout[matchComment.end(0):]) + lineout = trim_space(lineout, matchComment.start(0) + + len(matchComment.group(2))) + lineout = trim_space(lineout, matchComment.start(0)) + elif answer == 'k': + lineout = lineout + next_pos = matchComment.end(0) + elif answer == 'b': + FLAG_FAST_BREAK = True + break + matchComment = RE_COMMENT.search(lineout, next_pos) + + if lineout.isspace(): + print('\nResult is empty line and not stored.') + else: + print('\nResult: \n' + lineout + '\n') + fout.write(lineout) + + else: + fout.write(linein) +# +# END OF SCRIPT |