Print logging to stderr

This commit is contained in:
Tim Van Baak 2023-06-22 20:43:03 -07:00
parent dabc1f700b
commit 7737db1883
4 changed files with 42 additions and 29 deletions

View File

@ -4,7 +4,7 @@ from pathlib import Path
from random import getrandbits from random import getrandbits
from typing import List from typing import List
import json import json
import os import sys
import time import time
from flask import ( from flask import (
@ -205,7 +205,7 @@ def deactivate(source_name, item_id):
source = LocalSource(data_path, source_name) source = LocalSource(data_path, source_name)
item = source.get_item(item_id) item = source.get_item(item_id)
if item["active"]: if item["active"]:
print(f"Deactivating {source_name}/{item_id}") print(f"Deactivating {source_name}/{item_id}", file=sys.stderr)
item["active"] = False item["active"] = False
source.save_item(item) source.save_item(item)
return jsonify({"active": item["active"]}) return jsonify({"active": item["active"]})
@ -233,14 +233,14 @@ def mass_deactivate():
data_path: Path = current_app.config["INTAKE_DATA"] data_path: Path = current_app.config["INTAKE_DATA"]
params = request.get_json() params = request.get_json()
if "items" not in params: if "items" not in params:
print(f"Bad request params: {params}") print(f"Bad request params: {params}", file=sys.stderr)
for info in params.get("items"): for info in params.get("items"):
source = info["source"] source = info["source"]
itemid = info["itemid"] itemid = info["itemid"]
source = LocalSource(data_path, source) source = LocalSource(data_path, source)
item = source.get_item(itemid) item = source.get_item(itemid)
if item["active"]: if item["active"]:
print(f"Deactivating {info['source']}/{info['itemid']}") print(f"Deactivating {info['source']}/{info['itemid']}", file=sys.stderr)
item["active"] = False item["active"] = False
source.save_item(item) source.save_item(item)
return jsonify({}) return jsonify({})

View File

@ -38,7 +38,7 @@ def cmd_edit(cmd_args):
editor_cmd = os.environ.get("EDITOR") editor_cmd = os.environ.get("EDITOR")
if not editor_cmd: if not editor_cmd:
print("Cannot edit, no EDITOR set") print("Cannot edit, no EDITOR set", file=sys.stderr)
return 1 return 1
source_path: Path = data_path / args.source source_path: Path = data_path / args.source
@ -119,14 +119,14 @@ def cmd_update(cmd_args):
update_items(source, items) update_items(source, items)
else: else:
for item in items: for item in items:
print("Item:", item._item) print("Item:", item._item, file=sys.stderr)
except InvalidConfigException as ex: except InvalidConfigException as ex:
print("Could not fetch", args.source) print("Could not fetch", args.source, file=sys.stderr)
print(ex) print(ex, file=sys.stderr)
return 1 return 1
except SourceUpdateException as ex: except SourceUpdateException as ex:
print("Error updating source", args.source) print("Error updating source", args.source, file=sys.stderr)
print(ex) print(ex, file=sys.stderr)
return 1 return 1
return 0 return 0
@ -167,10 +167,10 @@ def cmd_action(cmd_args):
source = LocalSource(data_path, args.source) source = LocalSource(data_path, args.source)
try: try:
item = execute_action(source, args.item, args.action, 5) item = execute_action(source, args.item, args.action, 5)
print("Item:", item) print("Item:", item, file=sys.stderr)
except InvalidConfigException as ex: except InvalidConfigException as ex:
print("Could not fetch", args.source) print("Could not fetch", args.source, file=sys.stderr)
print(ex) print(ex, file=sys.stderr)
return 1 return 1
except SourceUpdateException as ex: except SourceUpdateException as ex:
print( print(
@ -180,8 +180,9 @@ def cmd_action(cmd_args):
args.item, args.item,
"action", "action",
args.action, args.action,
file=sys.stderr,
) )
print(ex) print(ex, file=sys.stderr)
return 1 return 1
return 0 return 0
@ -208,7 +209,7 @@ def cmd_feed(cmd_args):
data_path: Path = Path(args.data) if args.data else intake_data_dir() data_path: Path = Path(args.data) if args.data else intake_data_dir()
if not data_path.exists() and data_path.is_dir(): if not data_path.exists() and data_path.is_dir():
print("Not a directory:", data_path) print("Not a directory:", data_path, file=sys.stderr)
return 1 return 1
if not args.sources: if not args.sources:
@ -273,7 +274,7 @@ def cmd_passwd(cmd_args):
command_exists = subprocess.run(["command", "-v" "htpasswd"], shell=True) command_exists = subprocess.run(["command", "-v" "htpasswd"], shell=True)
if command_exists.returncode: if command_exists.returncode:
print("Could not find htpasswd, cannot update password") print("Could not find htpasswd, cannot update password", file=sys.stderr)
return 1 return 1
data_path: Path = Path(args.data) if args.data else intake_data_dir() data_path: Path = Path(args.data) if args.data else intake_data_dir()
@ -287,7 +288,7 @@ def cmd_passwd(cmd_args):
["htpasswd", "-b", "/etc/intake/htpasswd", user, password] ["htpasswd", "-b", "/etc/intake/htpasswd", user, password]
) )
if update_pwd.returncode: if update_pwd.returncode:
print("Could not update password file") print("Could not update password file", file=sys.stderr)
return 1 return 1
new_creds = {"username": user, "secret": password} new_creds = {"username": user, "secret": password}
@ -337,7 +338,7 @@ def cmd_run(cmd_args):
app.run(port=args.port, debug=args.debug) app.run(port=args.port, debug=args.debug)
return 0 return 0
except Exception as ex: except Exception as ex:
print(ex) print(ex, file=sys.stderr)
return 1 return 1

View File

@ -1,6 +1,7 @@
from pathlib import Path from pathlib import Path
import os import os
import subprocess import subprocess
import sys
from intake.source import LocalSource from intake.source import LocalSource
@ -27,18 +28,24 @@ def update_crontab_entries(data_path: Path):
Update the intake-managed section of the user's crontab. Update the intake-managed section of the user's crontab.
""" """
# If there is no crontab command available, quit early. # If there is no crontab command available, quit early.
crontab_exists = subprocess.run(["command", "-v" "crontab"], shell=True) cmd = ("command", "-v", "crontab")
print("Executing", *cmd, file=sys.stderr)
crontab_exists = subprocess.run(cmd, shell=True)
if crontab_exists.returncode: if crontab_exists.returncode:
print("Could not update crontab") print("Could not update crontab", file=sys.stderr)
return return
# Get the current crontab # Get the current crontab
cmd = ["crontab", "-e"]
print("Executing", *cmd, file=sys.stderr)
get_crontab = subprocess.run( get_crontab = subprocess.run(
["crontab", "-e"], cmd,
env={**os.environ, "EDITOR": "cat"}, env={**os.environ, "EDITOR": "cat"},
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.PIPE,
) )
for line in get_crontab.stderr.decode("utf8").splitlines():
print("[stderr]", line, file=sys.stderr)
crontab_lines = get_crontab.stdout.decode("utf-8").splitlines() crontab_lines = get_crontab.stdout.decode("utf-8").splitlines()
# Splice the intake crons into the crontab # Splice the intake crons into the crontab
@ -67,16 +74,20 @@ def update_crontab_entries(data_path: Path):
new_crontab_lines.extend(get_desired_crons(data_path)) new_crontab_lines.extend(get_desired_crons(data_path))
new_crontab_lines.append(INTAKE_CRON_END) new_crontab_lines.append(INTAKE_CRON_END)
print("Updating", len(new_crontab_lines) - 2, "crontab entries", file=sys.stderr)
# Save the updated crontab # Save the updated crontab
cmd = ["crontab", "-"]
print("Executing", *cmd, file=sys.stderr)
new_crontab: bytes = "\n".join(new_crontab_lines).encode("utf8") new_crontab: bytes = "\n".join(new_crontab_lines).encode("utf8")
save_crontab = subprocess.Popen( save_crontab = subprocess.Popen(
["crontab", "-"], cmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) )
(stdout, stderr) = save_crontab.communicate(new_crontab) (stdout, stderr) = save_crontab.communicate(new_crontab)
for line in stdout.decode("utf8").splitlines(): for line in stdout.decode("utf8").splitlines():
print("[stdout]", line) print("[stdout]", line, file=sys.stderr)
for line in stderr.decode("utf8").splitlines(): for line in stderr.decode("utf8").splitlines():
print("[stderr]", line) print("[stderr]", line, file=sys.stderr)

View File

@ -7,6 +7,7 @@ from typing import List
import json import json
import os import os
import os.path import os.path
import sys
from intake.types import InvalidConfigException, SourceUpdateException from intake.types import InvalidConfigException, SourceUpdateException
@ -196,7 +197,7 @@ def _read_stdout(process: Popen, output: list) -> None:
while True: while True:
data = process.stdout.readline() data = process.stdout.readline()
if data: if data:
print(f"[stdout] <{repr(data)}>") print(f"[stdout] <{repr(data)}>", file=sys.stderr)
output.append(data) output.append(data)
if process.poll() is not None: if process.poll() is not None:
break break
@ -210,7 +211,7 @@ def _read_stderr(process: Popen) -> None:
while True: while True:
data = process.stderr.readline() data = process.stderr.readline()
if data: if data:
print(f"[stderr] <{repr(data)}>") print(f"[stderr] <{repr(data)}>", file=sys.stderr)
if process.poll() is not None: if process.poll() is not None:
break break
@ -330,7 +331,7 @@ def update_items(source: LocalSource, fetched_items: List[Item]):
""" """
# Get a list of item ids that already existed for this source. # Get a list of item ids that already existed for this source.
prior_ids = source.get_item_ids() prior_ids = source.get_item_ids()
print(f"Found {len(prior_ids)} prior items") print(f"Found {len(prior_ids)} prior items", file=sys.stderr)
# Determine which items are new and which are updates. # Determine which items are new and which are updates.
new_items: List[Item] = [] new_items: List[Item] = []
@ -364,4 +365,4 @@ def update_items(source: LocalSource, fetched_items: List[Item]):
source.delete_item(item_id) source.delete_item(item_id)
del_count += 1 del_count += 1
print(len(new_items), "new,", del_count, "deleted") print(len(new_items), "new,", del_count, "deleted", file=sys.stderr)