2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-04-14 04:34:20 +01:00

Wallet edit now using refactored git add/commit system

This commit is contained in:
2024-12-29 19:35:20 +00:00
parent a3c7f165b9
commit e3a9498656
3 changed files with 34 additions and 75 deletions

View File

@@ -210,7 +210,7 @@ def get_cookie(request):
return editor
def git_string(author_string):
"""Rewrites the supplied editor string intoa git-complient author string
"""Rewrites the supplied editor string into a git-complient author string
Uses a regular expression for a git-compatible author string written mostly by Copilot
valid example "John Doe <john.doe@example.com>"
"""
@@ -259,7 +259,7 @@ def git_add(filename, cwd, commands=[]):
def git_commit(cwd, message, editor, commands=[]):
"""Commits whatever has been Staged by git in this directory
"""Commits whatever has been Staged by git in this directory 'cwd'
"""
git = settings.GIT
print(f"git commit in {cwd}")
@@ -277,25 +277,23 @@ def git_commit(cwd, message, editor, commands=[]):
# 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.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
if cp_commit.returncode == 1 and cp_commit.stdout == DEV_OK: # only good for 1 commit ahead of origin/repo
pass
else:
if cp_commit.returncode != 0 and not cp_commit.stdout.strip().endswith(
"nothing to commit, working tree clean"
):
msgdata = f'--Ask a nerd to fix this problem in add_commit().\n--{cp_commit.stderr}\n--"{cp_commit.stdout}"\n--return code:{str(cp_commit.returncode)}'
print(msgdata)
raise WriteAndCommitError(
f"Error code with git on server in this directory: {cwd}. Edits saved, added to git, but NOT committed.\n\n"
+ msgdata
)
if cp_commit.returncode != 0 and not cp_commit.stdout.strip().endswith(
"nothing to commit, working tree clean"
):
msgdata = f'--Ask a nerd to fix this problem in git_commit().\n--{cp_commit.stderr}\n--"{cp_commit.stdout}"\n--return code:{str(cp_commit.returncode)}'
print(msgdata)
raise WriteAndCommitError(
f"Error code with git on server in this directory: {cwd}. Edits saved, added to git, but NOT committed.\n\n"
+ msgdata
)
return commands
def add_commit(fname, message, editor=None):
"""Only used to commit a survex file edited and saved in view/survex.py"""
def add_commit(fname, message, editor):
"""Used to commit a survex file edited and saved in view/survex.py
and also contents.json for an edited wallet"""
cwd = fname.parent
filename = fname.name
commands = []
@@ -307,6 +305,7 @@ def add_commit(fname, message, editor=None):
editor = "Anathema Device <a.device@potatohut.expo>"
try:
# print(f"add_commit: {editor=} {filename=} {cwd=}")
commands = git_add(filename, cwd, commands)
commands = git_commit(cwd, message, editor, commands)

View File

@@ -19,12 +19,13 @@ from troggle.core.models.logbooks import LogbookEntry # , PersonLogEntry
from troggle.core.models.survex import SurvexBlock, SurvexFile, SurvexPersonRole
from troggle.core.models.troggle import DataIssue, Expedition
from troggle.core.models.wallets import YEAR_RANGE, Wallet, make_valid_date
from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, current_expo, get_cookie, git_string, sanitize_name, write_and_commit
from troggle.core.utils import COOKIE_MAX_AGE, WriteAndCommitError, add_commit, current_expo, get_cookie, git_add, \
git_commit, git_string, sanitize_name, write_and_commit
from troggle.core.views.auth import login_required_if_public
from troggle.core.views.caves import get_cave_leniently, getCave
from troggle.core.views.scans import caveifywallet, oldwallet
from troggle.core.views.uploads import FilesForm
from troggle.parsers.scans import contentsjson
from troggle.parsers.scans import CONTENTSJSON
"""Main wallet editing form, which includes scan file upload into the wallet
"""
@@ -504,60 +505,19 @@ def walletedit(request, path=None):
return w
def commit_json(waldata):
# CHANGE THIS to use git_commit() from troggle.utils
destfolder = contents_path.parent
dr_add = subprocess.run([git, "add", contentsjson], cwd=destfolder, capture_output=True, text=True)
if dr_add.returncode != 0:
msgdata = (
"Ask a nerd to fix this.\n--"
+ dr_add.stderr
+ "\n--"
+ dr_add.stdout
+ "\n--return code: "
+ str(dr_add.returncode)
)
message = (
f"CANNOT git on server for this file {contentsjson}. Edits saved but not added to git.\n\n" + msgdata
)
print(message)
return render(request, "errors/generic.html", {"message": message})
print(f"commit_json \n..{editor=} \n..{contents_path=}")
if "cave" in waldata:
label = str(waldata["cave"]).replace("[","").replace("]","")
else:
if socket.gethostname() != "expo":
comment = f"on dev machine '{socket.gethostname()}' "
if "name" in waldata:
label = waldata["name"]
else:
comment = ""
if "cave" in waldata:
label = waldata["cave"]
else:
if "name" in waldata:
label = waldata["name"]
else:
label = ""
dr_commit = subprocess.run(
[git, "commit", "-m", f"JSON update wallet {wallet} {label} {comment}"],
cwd=destfolder,
capture_output=True,
text=True,
)
# This produces return code = 1 if it commits OK
if dr_commit.returncode != 0:
msgdata = (
"Ask a nerd to fix this.\n\nstderr: "
+ dr_commit.stderr
+ "\n\nstdout: "
+ dr_commit.stdout
+ "\n\nreturn code: "
+ str(dr_commit.returncode)
)
message = (
f"Error code with git on server for this {contentsjson}. File is added to git, but NOT committed.\n"
+ msgdata
)
print(message)
return render(request, "errors/generic.html", {"message": message})
label = ""
message = f"JSON update wallet {wallet} {label}"
add_commit(contents_path, message, editor)
def get_logbook_trips():
return None
@@ -706,7 +666,7 @@ def walletedit(request, path=None):
wurl = f"/walletedit/{wallet}".replace("#", ":")
wallet = wallet.replace(":", "#")
dirpath = Path(settings.SCANS_ROOT, year, wallet)
contents_path = Path(settings.DRAWINGS_DATA, "walletjson") / year / wallet / contentsjson
contents_path = Path(settings.DRAWINGS_DATA, "walletjson") / year / wallet / CONTENTSJSON
fresh_wallet = False

View File

@@ -12,7 +12,7 @@ todo - update the survexblock scanswallet field to refer to the wallet, if it is
But we can't do that, we only have the survexfile, not the blcok. But we could set it for ALL the blocks? Hmm.
"""
contentsjson = "contents.json"
CONTENTSJSON = "contents.json"
git = settings.GIT
@@ -160,7 +160,7 @@ def load_all_scans():
for yeardir in contents_path.iterdir():
if yeardir.is_dir():
for walletpath in yeardir.iterdir():
if Path(walletpath, contentsjson).is_file():
if Path(walletpath, CONTENTSJSON).is_file():
walletname = walletpath.name
if walletname not in wallets: