mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2026-04-02 06:21:01 +01:00
err messages for newbies and change to subprocess.run() from os.system()
This commit is contained in:
@@ -2,6 +2,7 @@ import datetime
|
|||||||
import difflib
|
import difflib
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from pathlib import Path
|
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.caves import Cave, GetCaveLookup
|
||||||
from troggle.core.models.logbooks import LogbookEntry
|
from troggle.core.models.logbooks import LogbookEntry
|
||||||
from troggle.core.models.survex import SurvexBlock, SurvexFile #, SurvexDirectory
|
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.models.wallets import Wallet
|
||||||
from troggle.core.utils import (
|
from troggle.core.utils import (
|
||||||
get_cookie_max_age,
|
get_cookie_max_age,
|
||||||
@@ -142,14 +144,15 @@ class SvxForm(forms.Form):
|
|||||||
errmsg = "Error: Spaces are not possible in filenames.\n\nRename the file."
|
errmsg = "Error: Spaces are not possible in filenames.\n\nRename the file."
|
||||||
return errmsg
|
return errmsg
|
||||||
if re.search(r"\[|\]|<--", rcode):
|
if re.search(r"\[|\]|<--", rcode):
|
||||||
errmsg = "Error: remove all []s from the text.\nEverything inside [] are only template guidance.\n\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 [] 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
|
return errmsg
|
||||||
mbeginend = re.search(r"(?s)\*begin\s+(\w+).*?\*end\s+(\w+)", rcode)
|
mbeginend = re.search(r"(?s)\*begin\s+(\w+).*?\*end\s+(\w+)", rcode)
|
||||||
if not mbeginend:
|
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):
|
if mbeginend.group(1) != mbeginend.group(2):
|
||||||
return "Error: mismatching begin/end labels"
|
return "Error: mismatching begin/end labels. NOT saved."
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# we only store survex files in Unix line-ending style, even if the code is running on Windows
|
# 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."
|
return msg + "\nBUT PARSING failed. Do a completely new databaseReset."
|
||||||
|
|
||||||
def Process(self):
|
def Process(self):
|
||||||
# refactor this to use pathlib
|
message =""
|
||||||
|
|
||||||
print(">>>>....\n....Processing\n")
|
print(">>>>....\n....Processing\n")
|
||||||
froox = os.fspath(SVXPATH / (self.data["filename"] + ".svx"))
|
froox = SVXPATH / f"{self.data['filename']}.svx"
|
||||||
froog = os.fspath(SVXPATH / (self.data["filename"] + ".log"))
|
froog = SVXPATH / f"{self.data['filename']}.log"
|
||||||
cwd = os.getcwd()
|
frooerr = SVXPATH / f"{self.data["filename"]}.err"
|
||||||
os.chdir(os.path.split(froox)[0])
|
froogdir = froox.parent
|
||||||
os.system(settings.CAVERN + " --log " + froox)
|
|
||||||
os.chdir(cwd)
|
|
||||||
|
|
||||||
# Update this to use the new syntax..
|
cwd = Path.cwd() # current working directory
|
||||||
# sp = subprocess.run([settings.CAVERN, "--log", f'--output={outputdir}', f'{fullpath}.svx'],
|
os.chdir(froox.parent) # cd to above froox
|
||||||
# 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"))
|
# os.system(settings.CAVERN + " --log " + f"{froox}")
|
||||||
if filepatherr.is_file():
|
# Updated to use the new subprocess method
|
||||||
if filepatherr.stat().st_size == 0:
|
command = [settings.CAVERN, "--log", f"{froox}"]
|
||||||
filepatherr.unlink() # delete empty closure error file
|
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")
|
fin = open(froog, "r", encoding="utf8")
|
||||||
log = fin.read()
|
log = fin.read()
|
||||||
@@ -210,7 +222,7 @@ class SvxForm(forms.Form):
|
|||||||
"Calculating statistics...\n\n",
|
"Calculating statistics...\n\n",
|
||||||
]:
|
]:
|
||||||
log = log.replace(s, "")
|
log = log.replace(s, "")
|
||||||
return log
|
return log+message
|
||||||
|
|
||||||
|
|
||||||
def svx(request, survex_file):
|
def svx(request, survex_file):
|
||||||
|
|||||||
Reference in New Issue
Block a user