upload_logs   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 33
dl 0
loc 55
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
# pylint: disable=C0111,C0326,C0103
5
6
"""Upload contents of logs folder to remote database."""
7
8
# curl -v -H "Content-Type: text/plain" --data @test.xml http://example.org/tcl_update_db/
9
10
import glob
11
import os
12
import sys
13
14
import requests
15
16
from tcllib import argparser
17
dpdesc = """
18
    Uploads contents of logs folder to remote database.
19
    """
20
dp = argparser.DefaultParser(__file__, dpdesc)
21
args = dp.parse_args(sys.argv[1:])
22
del args
23
24
25
# This is the URL to an installation of https://github.com/mbirth/tcl_update_db
26
UPLOAD_URL = "https://tclota.birth-online.de/"
27
LOGS_GLOB = os.path.normpath("logs/*.xml")
28
29
headers = {"Content-Type": "text/xml"}
30
31
file_list = glob.glob(LOGS_GLOB)
32
print("Found {} logs to upload.".format(len(file_list)))
33
for fn in file_list:
34
    print("Uploading {}…".format(fn), end="", flush=True)
35
    with open(fn, "rb") as f:
36
        r = requests.post(UPLOAD_URL, data=f, headers=headers)
37
    if r.status_code == 200:
38
        os.remove(fn)
39
        print(" OK")
40
    else:
41
        add_text = ""
42
        if r.status_code in [413, 406, 412]:
43
            # File has been rejected by server, another try won't help
44
            os.remove(fn)
45
            add_text = " - Please try again later."
46
        print(" ERROR: HTTP {}{}".format(r.status_code, add_text))
47
48
# Mark prds.json as outdated to fetch updated version
49
print("Mark PRD cache for update…", end="")
50
try:
51
    os.utime("prds.json", times=(1, 1))
52
    print(" OK")
53
except OSError as e:
54
    print(" FAILED")
55