#!c:\Program Files\Python39\python.exe
# -*- coding: UTF-8 -*-
import os, sys, json
from os.path import join, exists, getsize, splitext
from datetime import datetime
from urllib.parse import quote
# ***** include the biocase.lib directory in the python sys path for importing *****
exec(open(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'lib', 'biocase', 'adjustpath.py'))).read())
from biocase.wrapper.protocol.inventory.inventory_protocol import ProtocolClass as InventoryProtocolClass
from biocase.archive.general import read_inventory, list_archives_for_schema, drop_archives, ns_dataset_aware, sizeof_fmt, fmt
from biocase.configtool.general import dsaObj, form, dsa, schema, action, log
import biocase.configuration
cfg = biocase.configuration.Cfg()
archivePath = join(cfg.archiveWorkLocator, dsa) + os.sep
dldPath = join(cfg.archiveDownloadLocator, dsa) + os.sep
proc_dwca = archivePath + "DwCA.proc"
log_dwca = archivePath + "DwCA.log"
if schema is not None:
config = archivePath + schema + ".config.xml"
proc_xml = archivePath + schema + ".proc"
log = archivePath + schema + ".log"
faf = dldPath + schema + '.zip'
def pimp_export_fname(f):
if len(f) == 19:
return '%s-%s-%s %s:%s:%s' % (f[0:4], f[4:6], f[6:8], f[9:11], f[11:13], f[13:15])
else:
return f
print('Content-Type: text/html\n')
# Set encoding to UTF8 and encoding error handling to replace
# print(sys.stdout.encoding, hasattr(sys.stdout, 'buffer'), sys.stdout.errors)
sys.stdout.reconfigure(encoding='utf8', errors='replace')
# Print 30 last lines of log files
if action == "showlog":
# First, print log
if exists(log):
size = getsize(log)
f = open(log, encoding='utf8')
# Only read 4KB at the end
f.seek(max(size - 4096, 0))
lines = f.readlines()
f.close()
# Restrict output to 25 lines
if len(lines) > 30:
sys.stdout.write("(continued)
")
for line in lines[-30:]:
line = line[:-1]
if line != '' and line[:4] != "INFO":
line = '%s' % ('#FF0000' if line.startswith('ERROR') else '#FF6600', line)
# line += '
'
# Use sys.out here in order to allow printing without newline or space
print(line)
# Add line that indicates finishing
if not exists(proc_xml):
if exists(proc_dwca):
print("XML generation ended, conversion will be started...")
else:
print("... processing ended.")
else:
print(("Log file not found: " + log))
if action == "archiveexists":
if list_archives_for_schema(dsa, schema):
print("true")
else:
print("false")
if action == "list":
url_rel = cfg.archiveDownloadLocator[len(cfg.wwwLocator) + 1:].replace('\\', '/')
baseURL = cfg.getWebappURL() + url_rel + '/' + dsa + '/'
# Downloads
dld = [f for f in os.listdir(dldPath) if f.endswith('.zip')]
# Then, list archives
archives = []
for aname, (schema, NS, dataset, records, dropped, size, modified, _dummy) in read_inventory(dsa).items():
# URL-encode the name for the download link
# This is a workaround for a bug in Python 2.7 urllib.quote (http://mail.python.org/pipermail/python-dev/2006-July/067248.html)
if isinstance(aname, str):
aname_encoded = aname.encode('utf-8')
else:
aname_encoded = aname
# if NS is not ABCD2.06/HISPID5, remove it
# This way, the interface can detect which archives an be converted to DwC-A
srecs, sdropped = fmt(records, dropped if dropped else '')
archives += [(dataset, schema, NS if NS in ns_dataset_aware else None, aname, baseURL + quote(aname_encoded),
datetime.fromtimestamp(modified).strftime("%y-%m-%d %H:%M"), sizeof_fmt(size), srecs, sdropped)]
# Remove archives from the list of zip files (that will be the download list eventually)
dld.remove(aname)
# Proc and Log files of aborted runs
log = []
proc = None
for fname in os.listdir(archivePath):
fname_head = splitext(fname)[0]
if fname.endswith('.proc'):
proc = fname_head
elif fname.endswith('.log'):
addit = False
if getsize(join(archivePath, fname)) > 0: # Ignore empty files
if exists(join(dldPath, fname)):
# Same file exists in download folder: Only list if it's not the same file
a = os.stat(join(archivePath, fname))
d = os.stat(join(dldPath, fname))
if (a.st_size != d.st_size) or (d.st_mtime - a.st_mtime > 5):
addit = True
else:
# Doesn't exist in download folder: add to list
addit = True
# Exception: If it's the log file of a download, don't add it
if '