Skip to content

Commit

Permalink
Add python 3 versions of PT python scripts (#2930)
Browse files Browse the repository at this point in the history
Co-authored-by: Raymond Luong <[email protected]>
  • Loading branch information
Nateowami and RaymondLuong3 authored Jan 3, 2025
1 parent 6f3e06b commit 53fa502
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 95 deletions.
65 changes: 23 additions & 42 deletions src/SIL.XForge.Scripture/ParatextMerge.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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])
Expand All @@ -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)
93 changes: 40 additions & 53 deletions src/SIL.XForge.Scripture/changedChapter.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53fa502

Please sign in to comment.