forked from expo/troggle
refactor & git author work
This commit is contained in:
130
core/utils.py
130
core/utils.py
@@ -127,8 +127,8 @@ def make_new_expo_dir(year):
|
||||
content = f"<html><head><title>{ff}</title></head><body><h1>{ff}</h1>{t}</body></html>"
|
||||
p = Path(year_dir, ff+".html")
|
||||
if not p.is_file():
|
||||
writetrogglefile(p, content, commit_msg="Auto new year file creation")
|
||||
|
||||
write_and_commit( [(p, content, "utf8")], f"Auto new year {ff} file creation", "Auto New Year <make_new_expo_dir@troggle.expo>")
|
||||
|
||||
|
||||
def current_expo():
|
||||
"""Returns the current expo year, but also checks if the most recent expo year is the same
|
||||
@@ -233,7 +233,7 @@ def git_add(filename, cwd, commands=[]):
|
||||
|
||||
# what is the purpose of this 'git diff' ? To prevent merge conflicts happening I guess,
|
||||
# so we do not have to reverse a 'git add'
|
||||
print(f"git diff {filename}")
|
||||
print(f"git diff {filename} in {cwd}")
|
||||
cmd_diff = [git, "diff", filename]
|
||||
commands.append(cmd_diff)
|
||||
cp_diff = subprocess.run(cmd_diff, cwd=cwd, capture_output=True, text=True)
|
||||
@@ -243,7 +243,7 @@ def git_add(filename, cwd, commands=[]):
|
||||
f"CANNOT git ADD on server for this file {filename}.\n\n" + msgdata
|
||||
)
|
||||
|
||||
print(f"git add {filename}")
|
||||
print(f"git add {filename} in {cwd}")
|
||||
cmd_add = [git, "add", filename]
|
||||
commands.append(cmd_add)
|
||||
cp_add = subprocess.run(cmd_add, cwd=cwd, capture_output=True, text=True)
|
||||
@@ -267,6 +267,11 @@ def git_commit(cwd, message, editor, commands=[]):
|
||||
cp_commit = subprocess.run(cmd_commit, cwd=cwd, capture_output=True, text=True)
|
||||
# This produces return code = 1 if it commits OK, but when the local repo still needs to be pushed to origin/repo
|
||||
# which will be the case when running a test troggle system on a development machine
|
||||
|
||||
# Several ways of testing if the commit failed
|
||||
#This produces return code = 1 if it commits OK, but when the repo still needs to be pushed to origin/expoweb
|
||||
# if (not cp_commit.stdout) or len(cp_commit.stdout) < 2 or cp_commit.stdout.split("\n")[-2] != "nothing to commit, working tree clean":
|
||||
|
||||
if cp_commit.returncode == 1 and cp_commit.stdout == DEV_OK: # only good for 1 commit ahead of origin/repo
|
||||
pass
|
||||
else:
|
||||
@@ -303,20 +308,23 @@ def add_commit(fname, message, editor=None):
|
||||
raise WriteAndCommitError(msg)
|
||||
|
||||
|
||||
def write_and_commit(files, message, editor=None):
|
||||
"""Writes the content to the filepath and adds and commits the file to git. If this fails, a WriteAndCommitError is raised.
|
||||
def write_and_commit(files, message, editor):
|
||||
"""For each file in files, it writes the content to the filepath
|
||||
and adds and commits the file to git.
|
||||
filepath, content, encoding = file
|
||||
If this fails, a WriteAndCommitError is raised.
|
||||
|
||||
This needs refactoring to just write and then call add_commit()
|
||||
This needs refactoring to just write to the filesystem and then call git_add()
|
||||
for each file, and then git_commit() at the end.
|
||||
|
||||
message - the "-m" comment field for the git commit
|
||||
editor - the "--author" field for the git commit
|
||||
"""
|
||||
# GIT see also core/views/uploads.py dwgupload()
|
||||
# GIT see also core/views/expo.py editexpopage()
|
||||
git = settings.GIT
|
||||
commands = []
|
||||
if editor:
|
||||
editor = git_string(editor)
|
||||
else:
|
||||
# cannot happen as form verification has this as an obligatory field
|
||||
editor = "Write_and_commit <automaton@potatohut.expo>"
|
||||
editor = git_string(editor)
|
||||
try:
|
||||
for filepath, content, encoding in files:
|
||||
cwd = filepath.parent
|
||||
@@ -345,56 +353,10 @@ def write_and_commit(files, message, editor=None):
|
||||
raise WriteAndCommitError(
|
||||
f"CANNOT write this file {filepath}. Ask a nerd to fix this: {e}"
|
||||
)
|
||||
|
||||
# what is the purpose of this 'git diff' ? To prevent merge conflicts happening I guess,
|
||||
# so we do not have to reverse a 'git add'
|
||||
cmd_diff = [git, "diff", filename]
|
||||
cp_diff = subprocess.run(cmd_diff, cwd=cwd, capture_output=True, text=True)
|
||||
commands.append(cmd_diff)
|
||||
if cp_diff.returncode == 0:
|
||||
cmd_add = [git, "add", filename]
|
||||
cp_add = subprocess.run(cmd_add, cwd=cwd, capture_output=True, text=True)
|
||||
commands.append(cmd_add)
|
||||
git_add_returncode = ""
|
||||
if cp_add.returncode != 0:
|
||||
git_add_returncode = cp_add.returncode
|
||||
msgdata = (
|
||||
"Ask a nerd to fix this.\n\nstderr:\n"
|
||||
+ cp_add.stderr
|
||||
+ "\n\nstdout:\n"
|
||||
+ cp_add.stdout
|
||||
+ "\n\nreturn code: "
|
||||
+ str(cp_add.returncode)
|
||||
)
|
||||
raise WriteAndCommitError(
|
||||
f"PROBLEM with git on server for {filename}. Edits saved but [possibly] not added to git.\n\n"
|
||||
+ msgdata
|
||||
)
|
||||
else:
|
||||
print(f"No change {filepath}")
|
||||
filepaths = [filepath for filepath, content, encoding in files]
|
||||
# message = message + " " + str(filepaths)
|
||||
|
||||
commands = git_add(filename, cwd, commands)
|
||||
|
||||
commands = git_commit(cwd, message, editor, commands)
|
||||
|
||||
if False:
|
||||
cmd_status = [git, "status"] # + filepaths
|
||||
cp_status = subprocess.run(cmd_status, cwd=cwd, capture_output=True, text=True)
|
||||
commands.append(cp_status)
|
||||
#This produces return code = 1 if it commits OK, but when the repo still needs to be pushed to origin/expoweb
|
||||
if (not cp_status.stdout) or len(cp_status.stdout) < 2 or cp_status.stdout.split("\n")[-2] != "nothing to commit, working tree clean":
|
||||
msgdata = (
|
||||
str(commands) +
|
||||
"Ask a nerd to fix this.\n\n"
|
||||
+ "Stderr: " + cp_status.stderr
|
||||
+ "\n\n"
|
||||
+ "Stdout: " + cp_status.stdout
|
||||
+ "\n\nreturn code: " + str(cp_status.returncode)
|
||||
)
|
||||
raise WriteAndCommitError(
|
||||
f"Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed. Git status not clean.\n\n"
|
||||
+ msgdata
|
||||
)
|
||||
except subprocess.SubprocessError:
|
||||
raise WriteAndCommitError(
|
||||
f"CANNOT git on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this."
|
||||
@@ -412,54 +374,6 @@ class WriteAndCommitError(Exception):
|
||||
return f"WriteAndCommitError: {self.message}"
|
||||
|
||||
|
||||
def writetrogglefile(filepath, filecontent, commit_msg=None):
|
||||
"""
|
||||
REPLACE with call to write_and_commit + any necessary setup
|
||||
used only by cave editor in
|
||||
core/models/caves.py
|
||||
and by
|
||||
make_new_expo_dir(year)
|
||||
but NOT by core/views/caves.py
|
||||
|
||||
Commit the new saved file to git
|
||||
|
||||
"""
|
||||
# GIT see also core/views/expo.py editexpopage()
|
||||
# GIT see also core/views/uploads.py dwgupload()
|
||||
filepath = Path(filepath)
|
||||
cwd = filepath.parent
|
||||
filename = filepath.name
|
||||
git = settings.GIT
|
||||
|
||||
# do not trap exceptions, pass them up to the view that called this function
|
||||
print(f"WRITING{cwd}---{filename} ")
|
||||
with open(filepath, "w") as f:
|
||||
f.write(filecontent)
|
||||
# os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
|
||||
sp = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True, check=True, text=True)
|
||||
if sp.returncode != 0:
|
||||
out = sp.stdout
|
||||
if len(out) > 160:
|
||||
out = out[:75] + "\n <Long output curtailed>\n" + out[-75:]
|
||||
print(f"git ADD {cwd}:\n\n" + str(sp.stderr) + "\n\n" + out + "\n\nreturn code: " + str(sp.returncode))
|
||||
|
||||
if not commit_msg:
|
||||
commit_msg = f"Troggle online: cave or entrance edit -{filename}"
|
||||
sp = subprocess.run(
|
||||
[git, "commit", "-m", commit_msg],
|
||||
cwd=cwd,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
text=True,
|
||||
)
|
||||
if sp.returncode != 0:
|
||||
out = sp.stdout
|
||||
if len(out) > 160:
|
||||
out = out[:75] + "\n <Long output curtailed>\n" + out[-75:]
|
||||
print(f"git COMMIT {cwd}:\n\n" + str(sp.stderr) + "\n\n" + out + "\n\nreturn code: " + str(sp.returncode))
|
||||
# not catching and re-raising any exceptions yet, inc. the stderr etc.,. We should do that.
|
||||
|
||||
|
||||
"""The following is a Bard converted version of Radosts's MIT copyrighted Javascript on 2023-10-27
|
||||
with hand-editing.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user