Commit 557155b4 authored by Romain Courteaud's avatar Romain Courteaud

Provide file output for status

This allows to have better atomicity than shell redirection
parent b498c212
...@@ -51,6 +51,8 @@ from .ssl import ( ...@@ -51,6 +51,8 @@ from .ssl import (
) )
import datetime import datetime
from email.utils import parsedate_to_datetime from email.utils import parsedate_to_datetime
import tempfile
import os
__version__ = "0.6.0" __version__ = "0.6.0"
...@@ -645,24 +647,39 @@ class WebBot: ...@@ -645,24 +647,39 @@ class WebBot:
float(self.config["WARNING_PERIOD"]), float(self.config["WARNING_PERIOD"]),
) )
if self.config["FORMAT"] == "json": if self.config["FORMAT"] == "json":
print(json.dumps(status_dict)) status_output = json.dumps(status_dict)
else: else:
status_list = []
append_status = status_list.append
for table_key in status_dict: for table_key in status_dict:
print("# %s" % table_key) append_status("# %s" % table_key)
print("") append_status("")
table = status_dict[table_key] table = status_dict[table_key]
if table: if table:
# Print the header # Print the header
table_key_list = [x for x in table[0].keys()] table_key_list = [x for x in table[0].keys()]
table_key_list.sort() table_key_list.sort()
print(" | ".join(table_key_list)) append_status(" | ".join(table_key_list))
for line in table: for line in table:
print( append_status(
" | ".join( " | ".join(
["%s" % (line[x]) for x in table_key_list] ["%s" % (line[x]) for x in table_key_list]
) )
) )
print("") append_status("")
status_output = "\n".join(status_list)
if self.config["STDOUT"] == "":
print(status_output)
else:
# https://blog.gocept.com/2013/07/15/reliable-file-updates-with-python/#write-replace
with tempfile.NamedTemporaryFile(
"w",
dir=os.path.dirname(self.config["STDOUT"]),
delete=False,
) as temp_file:
temp_file.write(status_output)
temp_file_name = temp_file.name
os.rename(temp_file_name, self.config["STDOUT"])
def create_bot(**kw): def create_bot(**kw):
......
...@@ -48,6 +48,9 @@ from .bot import create_bot ...@@ -48,6 +48,9 @@ from .bot import create_bot
help="Reload the configuration file between each crawl.", help="Reload the configuration file between each crawl.",
show_default=True, show_default=True,
) )
@click.option(
"--stdout", help="File to store status. (default: stdout)", default=""
)
@click.option( @click.option(
"--output", "--output",
"-o", "-o",
...@@ -69,6 +72,7 @@ def runSurykatka( ...@@ -69,6 +72,7 @@ def runSurykatka(
warning, warning,
configuration, configuration,
reload, reload,
stdout,
output, output,
profile, profile,
): ):
...@@ -91,6 +95,7 @@ def runSurykatka( ...@@ -91,6 +95,7 @@ def runSurykatka(
mapping["NAMESERVER"] = nameserver mapping["NAMESERVER"] = nameserver
if reload: if reload:
mapping["RELOAD"] = str(reload) mapping["RELOAD"] = str(reload)
mapping["STDOUT"] = stdout
mapping["FORMAT"] = output mapping["FORMAT"] = output
bot = create_bot(cfgfile=configuration, mapping=mapping) bot = create_bot(cfgfile=configuration, mapping=mapping)
if profile is None: if profile is None:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment