mirror of
https://github.com/naruxde/revpipyload.git
synced 2025-11-08 15:13:52 +01:00
Upload nur einzelne gzip Dateien mit Dateinamen - prüft Pfad auf plcworkdir
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
# (c) Sven Sager, License: LGPLv3
|
# (c) Sven Sager, License: LGPLv3
|
||||||
#
|
#
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import gzip
|
||||||
import proginit
|
import proginit
|
||||||
import os
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
@@ -15,7 +16,7 @@ import subprocess
|
|||||||
import tarfile
|
import tarfile
|
||||||
import zipfile
|
import zipfile
|
||||||
from concurrent import futures
|
from concurrent import futures
|
||||||
from shutil import rmtree, copyfile
|
from shutil import rmtree
|
||||||
from tempfile import mktemp
|
from tempfile import mktemp
|
||||||
from threading import Thread, Event
|
from threading import Thread, Event
|
||||||
from time import sleep, asctime
|
from time import sleep, asctime
|
||||||
@@ -224,7 +225,9 @@ class RevPiPlc(Thread):
|
|||||||
# Prozess beenden
|
# Prozess beenden
|
||||||
count = 0
|
count = 0
|
||||||
proginit.logger.info("term plc program {}".format(self._program))
|
proginit.logger.info("term plc program {}".format(self._program))
|
||||||
|
|
||||||
# TODO: Prüfen ob es überhautp noch läuft
|
# TODO: Prüfen ob es überhautp noch läuft
|
||||||
|
|
||||||
self._procplc.terminate()
|
self._procplc.terminate()
|
||||||
while self._procplc.poll() is None and count < 10:
|
while self._procplc.poll() is None and count < 10:
|
||||||
count += 1
|
count += 1
|
||||||
@@ -293,18 +296,22 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
# Konfiguration verarbeiten
|
# Konfiguration verarbeiten
|
||||||
self.autoreload = \
|
self.autoreload = \
|
||||||
int(self.globalconfig["DEFAULT"].get("autoreload", 1))
|
int(self.globalconfig["DEFAULT"].get("autoreload", 1))
|
||||||
self.autostart = int(self.globalconfig["DEFAULT"].get("autostart", 0))
|
self.autostart = \
|
||||||
self.plcprog = self.globalconfig["DEFAULT"].get("plcprogram", None)
|
int(self.globalconfig["DEFAULT"].get("autostart", 0))
|
||||||
self.plcworkdir = self.globalconfig["DEFAULT"].get(
|
self.plcprog = \
|
||||||
"plcworkdir", "/var/lib/revpipyload")
|
self.globalconfig["DEFAULT"].get("plcprogram", None)
|
||||||
self.plcslave = int(self.globalconfig["DEFAULT"].get("plcslave", 0))
|
self.plcworkdir = \
|
||||||
self.pythonver = int(
|
self.globalconfig["DEFAULT"].get("plcworkdir", "/var/lib/revpipyload")
|
||||||
self.globalconfig["DEFAULT"].get("pythonversion", 3))
|
self.plcslave = \
|
||||||
self.xmlrpc = int(self.globalconfig["DEFAULT"].get("xmlrpc", 1))
|
int(self.globalconfig["DEFAULT"].get("plcslave", 0))
|
||||||
self.zerooneerror = int(
|
self.pythonver = \
|
||||||
self.globalconfig["DEFAULT"].get("zeroonerror", 1))
|
int(self.globalconfig["DEFAULT"].get("pythonversion", 3))
|
||||||
self.zeroonexit = int(
|
self.xmlrpc = \
|
||||||
self.globalconfig["DEFAULT"].get("zeroonexit", 1))
|
int(self.globalconfig["DEFAULT"].get("xmlrpc", 1))
|
||||||
|
self.zerooneerror = \
|
||||||
|
int(self.globalconfig["DEFAULT"].get("zeroonerror", 1))
|
||||||
|
self.zeroonexit = \
|
||||||
|
int(self.globalconfig["DEFAULT"].get("zeroonexit", 1))
|
||||||
|
|
||||||
# Workdirectory wechseln
|
# Workdirectory wechseln
|
||||||
os.chdir(self.plcworkdir)
|
os.chdir(self.plcworkdir)
|
||||||
@@ -332,8 +339,7 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
|
|
||||||
self.xsrv.register_function(self.xml_getconfig, "get_config")
|
self.xsrv.register_function(self.xml_getconfig, "get_config")
|
||||||
self.xsrv.register_function(self.xml_getfilelist, "get_filelist")
|
self.xsrv.register_function(self.xml_getfilelist, "get_filelist")
|
||||||
self.xsrv.register_function(
|
self.xsrv.register_function(self.xml_getpictoryrsc, "get_pictoryrsc")
|
||||||
self.xml_getpictoryrsc, "get_pictoryrsc")
|
|
||||||
self.xsrv.register_function(self.xml_getprocimg, "get_procimg")
|
self.xsrv.register_function(self.xml_getprocimg, "get_procimg")
|
||||||
self.xsrv.register_function(self.xml_plcdownload, "plcdownload")
|
self.xsrv.register_function(self.xml_plcdownload, "plcdownload")
|
||||||
self.xsrv.register_function(self.xml_plcexitcode, "plcexitcode")
|
self.xsrv.register_function(self.xml_plcexitcode, "plcexitcode")
|
||||||
@@ -341,12 +347,11 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
self.xsrv.register_function(self.xml_plcstart, "plcstart")
|
self.xsrv.register_function(self.xml_plcstart, "plcstart")
|
||||||
self.xsrv.register_function(self.xml_plcstop, "plcstop")
|
self.xsrv.register_function(self.xml_plcstop, "plcstop")
|
||||||
self.xsrv.register_function(self.xml_plcupload, "plcupload")
|
self.xsrv.register_function(self.xml_plcupload, "plcupload")
|
||||||
self.xsrv.register_function(
|
self.xsrv.register_function(self.xml_plcuploadclean, "plcuploadclean")
|
||||||
self.xml_plcuploadclean, "plcuploadclean")
|
|
||||||
self.xsrv.register_function(self.xml_reload, "reload")
|
self.xsrv.register_function(self.xml_reload, "reload")
|
||||||
self.xsrv.register_function(self.xml_setconfig, "set_config")
|
self.xsrv.register_function(self.xml_setconfig, "set_config")
|
||||||
self.xsrv.register_function(
|
self.xsrv.register_function(self.xml_setpictoryrsc, "set_pictoryrsc")
|
||||||
self.xml_setpictoryrsc, "set_pictoryrsc")
|
|
||||||
self.xsrv.register_function(lambda: pyloadverion, "version")
|
self.xsrv.register_function(lambda: pyloadverion, "version")
|
||||||
proginit.logger.debug("created xmlrpc server")
|
proginit.logger.debug("created xmlrpc server")
|
||||||
|
|
||||||
@@ -391,7 +396,9 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
@param mode: Packart 'tar' oder 'zip'
|
@param mode: Packart 'tar' oder 'zip'
|
||||||
@param pictory: piCtory Konfiguration mit einpacken"""
|
@param pictory: piCtory Konfiguration mit einpacken"""
|
||||||
filename = mktemp(suffix=".packed", prefix="plc")
|
filename = mktemp(suffix=".packed", prefix="plc")
|
||||||
|
|
||||||
# TODO: Fehlerabfang
|
# TODO: Fehlerabfang
|
||||||
|
|
||||||
if mode == "zip":
|
if mode == "zip":
|
||||||
fh_pack = zipfile.ZipFile(filename, mode="w")
|
fh_pack = zipfile.ZipFile(filename, mode="w")
|
||||||
wd = os.walk("./")
|
wd = os.walk("./")
|
||||||
@@ -492,7 +499,9 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
|
|
||||||
def xml_plcdownload(self, mode="tar", pictory=False):
|
def xml_plcdownload(self, mode="tar", pictory=False):
|
||||||
proginit.logger.debug("xmlrpc call plcdownload")
|
proginit.logger.debug("xmlrpc call plcdownload")
|
||||||
|
|
||||||
# TODO: Daten blockweise übertragen
|
# TODO: Daten blockweise übertragen
|
||||||
|
|
||||||
file = self.packapp(mode, pictory)
|
file = self.packapp(mode, pictory)
|
||||||
if os.path.exists(file):
|
if os.path.exists(file):
|
||||||
fh = open(file, "rb")
|
fh = open(file, "rb")
|
||||||
@@ -535,53 +544,31 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
else:
|
else:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
def xml_plcupload(self, filedata, pictory=False, reset=False):
|
def xml_plcupload(self, filedata, filename):
|
||||||
proginit.logger.debug("xmlrpc call plcupload")
|
proginit.logger.debug("xmlrpc call plcupload")
|
||||||
# TODO: Daten blockweise annehmen
|
noerr = False
|
||||||
if filedata is None:
|
|
||||||
|
if filedata is None or filename is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
filename = mktemp(prefix="upl")
|
# Absoluten Pfad prüfen
|
||||||
# Daten in tmp-file schreiben
|
dirname = os.path.join(self.plcworkdir, os.path.dirname(filename))
|
||||||
fh = open(filename, "wb")
|
if self.plcworkdir not in os.path.abspath(dirname):
|
||||||
fh.write(filedata.data)
|
return False
|
||||||
fh.close()
|
|
||||||
|
|
||||||
# Packer ermitteln
|
# Ordner erzeugen
|
||||||
fh_pack = None
|
if not os.path.exists(dirname):
|
||||||
if tarfile.is_tarfile(filename):
|
os.makedirs(dirname)
|
||||||
fh_pack = tarfile.open(filename)
|
|
||||||
elif zipfile.is_zipfile(filename):
|
|
||||||
fh_pack = zipfile.ZipFile.open(filename)
|
|
||||||
|
|
||||||
if fh_pack is not None:
|
# Datei erzeugen
|
||||||
fh_pack.extractall(".")
|
try:
|
||||||
fh_pack.close()
|
fh = open(filename, "wb")
|
||||||
os.remove(filename)
|
fh.write(gzip.decompress(filedata.data))
|
||||||
|
noerr = True
|
||||||
|
finally:
|
||||||
|
fh.close()
|
||||||
|
|
||||||
if pictory and os.path.exists("./config.rsc"):
|
return noerr
|
||||||
try:
|
|
||||||
# Nur Daten kopieren damit Eigenschaften gleich bleiben
|
|
||||||
copyfile("./config.rsc", configrsc)
|
|
||||||
os.remove("./config.rsc")
|
|
||||||
except:
|
|
||||||
return -3
|
|
||||||
else:
|
|
||||||
if reset:
|
|
||||||
return os.system(picontrolreset)
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
elif pictory:
|
|
||||||
return -2
|
|
||||||
|
|
||||||
else:
|
|
||||||
# Sauber
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Kein Archiv
|
|
||||||
os.remove(filename)
|
|
||||||
return -1
|
|
||||||
|
|
||||||
def xml_plcuploadclean(self):
|
def xml_plcuploadclean(self):
|
||||||
proginit.logger.debug("xmlrpc call plcuploadclean")
|
proginit.logger.debug("xmlrpc call plcuploadclean")
|
||||||
@@ -633,6 +620,9 @@ class RevPiPyLoad(proginit.ProgInit):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
proginit.logger.debug("xmlrpc call setpictoryrsc")
|
proginit.logger.debug("xmlrpc call setpictoryrsc")
|
||||||
|
|
||||||
|
# TODO: Prüfen ob es wirklich eine piCtory Datei ist
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(configrsc, "wb") as fh:
|
with open(configrsc, "wb") as fh:
|
||||||
fh.write(filebytes.data)
|
fh.write(filebytes.data)
|
||||||
|
|||||||
Reference in New Issue
Block a user