From 95668daff2f934e66c8f1eb1ec3cb0aae63e6ebd Mon Sep 17 00:00:00 2001 From: Raymond Luong Date: Fri, 3 Jan 2025 12:07:07 -0700 Subject: [PATCH] Add python 3 versions of PT python scripts --- src/SIL.XForge.Scripture/ParatextMerge.py | 65 ++++++--------- src/SIL.XForge.Scripture/changedChapter.py | 93 ++++++++++------------ 2 files changed, 63 insertions(+), 95 deletions(-) diff --git a/src/SIL.XForge.Scripture/ParatextMerge.py b/src/SIL.XForge.Scripture/ParatextMerge.py index b32a94d717..ebe01f1f50 100644 --- a/src/SIL.XForge.Scripture/ParatextMerge.py +++ b/src/SIL.XForge.Scripture/ParatextMerge.py @@ -1,7 +1,6 @@ -import os, uuid +import sys, os, uuid from shutil import copyfile - def merge(ui, repo, args, **kwargs): global fullNameMine global fullNameParent @@ -12,10 +11,10 @@ def merge(ui, repo, args, **kwargs): global mergeToken if len(kwargs) != 1: return True - hgRelativeDirName = ".hg" - mergeRelativeDir = "merge" - mergeListFile = "Merges.txt" - mergeToken = "#Merge" + hgRelativeDirName = b'.hg' + mergeRelativeDir = b'merge' + mergeListFile = b'Merges.txt' + mergeToken = b'#Merge' fullNameMine = os.path.abspath(args[0]) fullNameParent = os.path.abspath(args[1]) fullNameTheirs = os.path.abspath(args[2]) @@ -29,55 +28,37 @@ def FindRepositoryRoot(ui): global mergeDir global repositoryDir d = os.path.dirname(fullNameMine) - while d and d != "/": + root = os.path.abspath(os.sep.encode()) + while d and d != root: if os.path.isdir(os.path.join(d, hgRelativeDirName)): repositoryDir = d mergeDir = os.path.join(repositoryDir, mergeRelativeDir) return True - d = os.path.abspath(os.path.join(d, os.pardir)) + d = os.path.abspath(os.path.join(d, os.pardir.encode())) - ui.warn("File " + fullNameMine + " is not in a repository. No .hg found.") + ui.warn(b'File ' + fullNameMine + b' is not in a repository. No .hg found.') return False def PrintInfo(ui): - ui.write( - "Mine : " - + fullNameMine - + "\nParent: " - + fullNameParent - + "\nTheirs:" - + fullNameTheirs - + "\nRoot : " - + repositoryDir - + "\nMerge: " - + mergeDir - ) + ui.write(b'Mine : ' + fullNameMine + b'\nParent: ' + fullNameParent + b'\nTheirs:' + fullNameTheirs + b'\nRoot : ' + repositoryDir + b'\nMerge: ' + mergeDir) def Run(ui): if not os.path.isdir(mergeDir): os.makedirs(mergeDir) - relativePathMine = fullNameMine[len(repositoryDir) + 1 :] - mergePathParent = os.path.join(mergeRelativeDir, str(uuid.uuid4())) - mergePathTheirs = os.path.join(mergeRelativeDir, str(uuid.uuid4())) - ui.debug( - "Relative File Names\nMine :" - + relativePathMine - + "\nParent: " - + mergePathParent - + "\nTheirs: " - + mergePathTheirs - + "\n" - ) - ui.debug("Moving parent.\n") + relativePathMine = fullNameMine[len(repositoryDir) + 1:] + mergePathParent = os.path.join(mergeRelativeDir, str(uuid.uuid4()).encode()) + mergePathTheirs = os.path.join(mergeRelativeDir, str(uuid.uuid4()).encode()) + ui.debug(b'Relative File Names\nMine :' + relativePathMine + b'\nParent: ' + mergePathParent + b'\nTheirs: ' + mergePathTheirs + b'\n') + ui.debug(b'Moving parent.\n') copyfile(fullNameParent, os.path.join(repositoryDir, mergePathParent)) - ui.debug("Moving Theirs.\n") + ui.debug(b'Moving Theirs.\n') copyfile(fullNameTheirs, os.path.join(repositoryDir, mergePathTheirs)) - ui.debug("Done moving.\n") - with open(os.path.join(mergeDir, mergeListFile), "a", 0) as (text_file): - text_file.write(mergeToken + "\n") - text_file.write(relativePathMine + "\n") - text_file.write(mergePathParent + "\n") - text_file.write(mergePathTheirs + "\n") - os.fsync(text_file) + ui.debug(b'Done moving.\n') + with open(os.path.join(mergeDir, mergeListFile), 'ab', 0) as (text_file): + text_file.write(mergeToken + b'\n') + text_file.write(relativePathMine + b'\n') + text_file.write(mergePathParent + b'\n') + text_file.write(mergePathTheirs + b'\n') + os.fsync(text_file) \ No newline at end of file diff --git a/src/SIL.XForge.Scripture/changedChapter.py b/src/SIL.XForge.Scripture/changedChapter.py index 0a4e2e3b99..a1d3d281e4 100644 --- a/src/SIL.XForge.Scripture/changedChapter.py +++ b/src/SIL.XForge.Scripture/changedChapter.py @@ -1,93 +1,80 @@ # Mercurial extension to provide the 'hg changedChapter' command -"""command to list all changesets which changed the specified chapter""" +'''command to list all changesets which changed the specified chapter''' from mercurial import commands, registrar from mercurial.i18n import _ +from mercurial.node import short import re cmdtable = {} command = registrar.command(cmdtable) - - -@command( - "changedChapter", - [("c", "chap", "", _("show revision changing the chapter"))] + commands.globalopts, - _("hg changedChapter [-c CHAPTER] [FILE]"), -) +@command(b'changedChapter', + [(b'c', b'chap', b'', _(b'show revision changing the chapter'))] + commands.globalopts, + _(b'hg changedChapter [-c CHAPTER] [FILE]')) def changedChapter(ui, repo, file_=None, **opts): - parts = opts.get("chap").split(".") + parts = opts.get('chap').split(b'.') chapter = parts[0] - if len(parts[1].split("-")) == 2: - (v1, v2) = parts[1].split("-") + if len(parts[1].split(b'-')) == 2: + (v1, v2) = parts[1].split(b'-') else: # verse bridges present, just look for changed chapter text - v1 = "" - v2 = "" - - fl = repo.file(file_) # get filelog - - lastVerseText = "" # track verse text seen - + v1 = b'' + v2 = b'' + + fl = repo.file(file_) # get filelog + lastVerseText = b'' # track verse text seen + for rev in fl: - bookText = fl.revision(fl.node(rev)) - chapterText = getChapter(chapter, bookText) - if chapterText == "": - continue - - if v1 != "": + bookText = fl.revision(fl.node(rev)) + chapterText = getChapter(chapter, bookText) + if chapterText == b'': continue + + if v1 != b'': verseText = getVerses(v1, v2, chapterText) else: verseText = chapterText - - if verseText == lastVerseText: - continue + + if verseText == lastVerseText: continue lastVerseText = verseText - - ui.write(str(int(fl.linkrev(rev))) + "\r\n") + + ui.write(str(int(fl.linkrev(rev))).encode() + b'\r\n') ui.flush() - # Extract the text of the specificed chapter from the text of the book - def getChapter(chapter, text): - parts = re.split(r"(\\c\s+\S+)", text) + parts = re.split(b'(\\\\c\\s+\\S+)', text) i = findIndex(parts, chapter) - if i == -1: - return "" - - if chapter == "1": - return parts[i - 1] + parts[i] + parts[i + 1] - - return parts[i] + parts[i + 1] - + if i == -1: return b'' + + if chapter == b'1': + return parts[i-1] + parts[i] + parts[i+1] + + return parts[i] + parts[i+1] def getVerses(v1, v2, text): - parts = re.split(r"(\\v\s+\S+)", text) + parts = re.split(b'(\\\\v\\s+\\S+)', text) i = findIndex(parts, v1) - if i == -1: - return text + if i == -1: return text j = findIndex(parts, v2) - if j == -1: - return text - - result = "" + if j == -1: return text + + result = b'' if i == 1: result = parts[0] - + while i <= j: - result += parts[i] + parts[i + 1] + result += parts[i] + parts[i+1] i = i + 2 - + return result - - + def findIndex(parts, match): for i in range(1, len(parts), 2): pieces = parts[i].split() if pieces[1] == match: return i - + return -1