1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-09-19 22:33:25 +01:00
tcl_ota_check/upload_logs.py

55 lines
1.5 KiB
Python
Raw Normal View History

2017-11-02 01:05:26 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=C0111,C0326,C0103
2018-02-03 21:25:26 +00:00
"""Upload contents of logs folder to remote database."""
2017-11-03 22:26:51 +00:00
# curl -v -H "Content-Type: text/plain" --data @test.xml http://example.org/tcl_update_db/
import glob
2017-11-02 01:34:31 +00:00
import os
2018-06-12 03:40:49 +01:00
import sys
2018-02-03 20:24:36 +00:00
2017-11-03 22:26:51 +00:00
import requests
2017-11-02 01:34:31 +00:00
2018-06-12 03:40:49 +01:00
from tcllib import argparser
dpdesc = """
Uploads contents of logs folder to remote database.
"""
dp = argparser.DefaultParser(__file__, dpdesc)
args = dp.parse_args(sys.argv[1:])
del args
2018-02-03 20:24:36 +00:00
2017-11-03 22:26:51 +00:00
# This is the URL to an installation of https://github.com/mbirth/tcl_update_db
UPLOAD_URL = "https://tclota.birth-online.de/"
2017-11-02 01:44:46 +00:00
LOGS_GLOB = os.path.normpath("logs/*.xml")
2017-11-03 22:26:51 +00:00
2018-02-03 20:24:36 +00:00
headers = {"Content-Type": "text/xml"}
2017-11-03 22:26:51 +00:00
2017-11-13 21:36:01 +00:00
file_list = glob.glob(LOGS_GLOB)
print("Found {} logs to upload.".format(len(file_list)))
for fn in file_list:
print("Uploading {}".format(fn), end="", flush=True)
2017-11-03 22:26:51 +00:00
with open(fn, "rb") as f:
r = requests.post(UPLOAD_URL, data=f, headers=headers)
if r.status_code == 200:
os.remove(fn)
2017-11-03 22:26:51 +00:00
print(" OK")
else:
add_text = ""
if r.status_code in [413, 406, 412]:
# File has been rejected by server, another try won't help
os.remove(fn)
add_text = " - Please try again later."
print(" ERROR: HTTP {}{}".format(r.status_code, add_text))
# Mark prds.json as outdated to fetch updated version
print("Mark PRD cache for update…", end="")
try:
os.utime("prds.json", times=(1, 1))
print(" OK")
except OSError as e:
print(" FAILED")