2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-01-18 23:42:49 +00:00

err messages for newbies and change to subprocess.run() from os.system()

This commit is contained in:
2025-12-17 19:45:10 +00:00
parent 677bf92565
commit 67e154ab83

View File

@@ -2,6 +2,7 @@ import datetime
import difflib
import os
import re
import subprocess
from collections import namedtuple
from pathlib import Path
@@ -16,6 +17,7 @@ import troggle.settings as settings
from troggle.core.models.caves import Cave, GetCaveLookup
from troggle.core.models.logbooks import LogbookEntry
from troggle.core.models.survex import SurvexBlock, SurvexFile #, SurvexDirectory
from troggle.core.models.troggle import DataIssue
from troggle.core.models.wallets import Wallet
from troggle.core.utils import (
get_cookie_max_age,
@@ -142,14 +144,15 @@ class SvxForm(forms.Form):
errmsg = "Error: Spaces are not possible in filenames.\n\nRename the file."
return errmsg
if re.search(r"\[|\]|<--", rcode):
errmsg = "Error: remove all []s from the text.\nEverything inside [] are only template guidance.\n\n"
errmsg += "All [] must be edited out and replaced with real data before you can save this file.\n"
errmsg = "Error: remove all []s and '<--' from the text.\n Everything inside [] are only template guidance.\n\n"
errmsg += " All [] must be edited out and replaced with real data before you can save this file.\n"
errmsg += " All '; <-- ' reminder text must also be removed.\nNOT saved."
return errmsg
mbeginend = re.search(r"(?s)\*begin\s+(\w+).*?\*end\s+(\w+)", rcode)
if not mbeginend:
return "Error: no begin/end block here"
return "Error: no begin/end block here. NOT saved."
if mbeginend.group(1) != mbeginend.group(2):
return "Error: mismatching begin/end labels"
return "Error: mismatching begin/end labels. NOT saved."
try:
# we only store survex files in Unix line-ending style, even if the code is running on Windows
@@ -174,28 +177,37 @@ class SvxForm(forms.Form):
return msg + "\nBUT PARSING failed. Do a completely new databaseReset."
def Process(self):
# refactor this to use pathlib
message =""
print(">>>>....\n....Processing\n")
froox = os.fspath(SVXPATH / (self.data["filename"] + ".svx"))
froog = os.fspath(SVXPATH / (self.data["filename"] + ".log"))
cwd = os.getcwd()
os.chdir(os.path.split(froox)[0])
os.system(settings.CAVERN + " --log " + froox)
os.chdir(cwd)
# Update this to use the new syntax..
# sp = subprocess.run([settings.CAVERN, "--log", f'--output={outputdir}', f'{fullpath}.svx'],
# capture_output=True, check=False, text=True)
# if sp.returncode != 0:
# message = f' ! Error running {settings.CAVERN}: {fullpath}'
# DataIssue.objects.create(parser='entrances', message=message)
# print(message)
# print(f'stderr:\n\n' + str(sp.stderr) + '\n\n' + str(sp.stdout) + '\n\nreturn code: ' + str(sp.returncode))
filepatherr = Path(SVXPATH / str(self.data["filename"] + ".err"))
if filepatherr.is_file():
if filepatherr.stat().st_size == 0:
filepatherr.unlink() # delete empty closure error file
froox = SVXPATH / f"{self.data['filename']}.svx"
froog = SVXPATH / f"{self.data['filename']}.log"
frooerr = SVXPATH / f"{self.data["filename"]}.err"
froogdir = froox.parent
cwd = Path.cwd() # current working directory
os.chdir(froox.parent) # cd to above froox
# os.system(settings.CAVERN + " --log " + f"{froox}")
# Updated to use the new subprocess method
command = [settings.CAVERN, "--log", f"{froox}"]
sp = subprocess.run(
command,
cwd=froogdir, capture_output=True, text=True, check=False
) # raises CalledProcessError if command fails and check=True
if sp.returncode != 0:
message = f' ! Error running {settings.CAVERN}: {froox}'
DataIssue.objects.create(parser='survex', message=message)
print(message)
print(f"sp = subprocess.run([{settings.CAVERN} --log {froox}], cwd={froogdir}"
f"\n\nstderr:\n\n{str(sp.stderr)}\n\nstdout:{str(sp.stdout)}\n\nreturn code: {str(sp.returncode)}")
os.chdir(cwd) # Restore working directory
if frooerr.is_file():
if frooerr.stat().st_size == 0:
frooerr.unlink() # delete empty closure error file
fin = open(froog, "r", encoding="utf8")
log = fin.read()
@@ -210,7 +222,7 @@ class SvxForm(forms.Form):
"Calculating statistics...\n\n",
]:
log = log.replace(s, "")
return log
return log+message
def svx(request, survex_file):