Cleanup, moved dumpmgr class.
This commit is contained in:
parent
e6e0e83039
commit
e582642936
@ -11,9 +11,9 @@ import sys
|
||||
|
||||
from tcllib import argparser
|
||||
from tcllib.devices import Device
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, DownloadRequest, \
|
||||
ChecksumRequest, EncryptHeaderRequest, ServerSelector, \
|
||||
write_info_if_dumps_found
|
||||
ChecksumRequest, EncryptHeaderRequest, ServerSelector
|
||||
|
||||
|
||||
dpdesc = """
|
||||
|
@ -9,7 +9,8 @@ import sys
|
||||
|
||||
from tcllib import ansi, argparser, devlist
|
||||
from tcllib.devices import DesktopDevice
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector, write_info_if_dumps_found
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector
|
||||
|
||||
|
||||
dev = DesktopDevice()
|
||||
|
@ -9,7 +9,8 @@ import sys
|
||||
|
||||
from tcllib import ansi, argparser, devlist
|
||||
from tcllib.devices import MobileDevice
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector, write_info_if_dumps_found
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector
|
||||
|
||||
|
||||
dev = MobileDevice()
|
||||
|
@ -10,8 +10,8 @@ import sys
|
||||
|
||||
from tcllib import ansi, argparser, devlist
|
||||
from tcllib.devices import DesktopDevice
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector, \
|
||||
write_info_if_dumps_found
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector
|
||||
|
||||
|
||||
dpdesc = """
|
||||
|
@ -9,8 +9,8 @@ import sys
|
||||
|
||||
from tcllib import ansi, argparser, devlist
|
||||
from tcllib.devices import DesktopDevice
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector, \
|
||||
write_info_if_dumps_found
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector
|
||||
|
||||
|
||||
# Variants to scan for
|
||||
|
@ -9,8 +9,8 @@ import sys
|
||||
|
||||
from tcllib import ansi, argparser, devlist
|
||||
from tcllib.devices import MobileDevice
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector, \
|
||||
write_info_if_dumps_found
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector
|
||||
|
||||
|
||||
dpdesc = """
|
||||
|
@ -10,8 +10,8 @@ import requests
|
||||
|
||||
from tcllib import ansi
|
||||
from tcllib.devices import MobileDevice
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector, \
|
||||
write_info_if_dumps_found
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, ServerVoteSelector
|
||||
|
||||
|
||||
# 1. Fetch list of missing OTAs (e.g. from ancient versions to current)
|
||||
|
@ -10,9 +10,9 @@ import sys
|
||||
|
||||
from tcllib import argparser
|
||||
from tcllib.devices import DesktopDevice
|
||||
from tcllib.dumpmgr import write_info_if_dumps_found
|
||||
from tcllib.requests import RequestRunner, CheckRequest, DownloadRequest, \
|
||||
ChecksumRequest, EncryptHeaderRequest, ServerSelector, \
|
||||
write_info_if_dumps_found
|
||||
ChecksumRequest, EncryptHeaderRequest, ServerSelector
|
||||
|
||||
|
||||
dpdesc = """
|
||||
|
@ -1,35 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# pylint: disable=C0111,C0326,C0103
|
||||
|
||||
"""Library for TCL API work and related functions."""
|
||||
|
||||
import requests
|
||||
|
||||
from . import (dumpmgr, servervote, tclcheck, tclchecksum, tclencheader,
|
||||
tclrequest)
|
||||
|
||||
|
||||
class FotaCheck(
|
||||
tclcheck.TclCheckMixin,
|
||||
tclrequest.TclRequestMixin,
|
||||
tclchecksum.TclChecksumMixin,
|
||||
tclencheader.TclEncHeaderMixin,
|
||||
servervote.ServerVoteMixin,
|
||||
dumpmgr.DumpMgrMixin
|
||||
):
|
||||
"""Main API handler class."""
|
||||
|
||||
def __init__(self):
|
||||
"""Handle mixins and populate variables."""
|
||||
super().__init__()
|
||||
self.reset_session()
|
||||
|
||||
def reset_session(self, device=None):
|
||||
"""Reset everything to default."""
|
||||
self.g2master = self.get_master_server()
|
||||
self.sess = requests.Session()
|
||||
if device:
|
||||
self.sess.headers.update({"User-Agent": device.ua})
|
||||
return self.sess
|
||||
from .ansi import *
|
||||
from .argparser import *
|
||||
from .dumpmgr import write_info_if_dumps_found
|
||||
|
@ -15,23 +15,32 @@ from math import floor
|
||||
from . import ansi
|
||||
|
||||
|
||||
class DumpMgrMixin:
|
||||
"""A mixin component for XML dump management."""
|
||||
def get_timestamp_random():
|
||||
"""Generate timestamp + random part to avoid collisions."""
|
||||
millis = floor(time.time() * 1000)
|
||||
tail = "{:06d}".format(random.randint(0, 999999))
|
||||
return "{}_{}".format(str(millis), tail)
|
||||
|
||||
def write_info_if_dumps_found():
|
||||
"""Notify user to upload dumps if present."""
|
||||
# To disable this info, uncomment the following line.
|
||||
# return
|
||||
files = glob.glob(os.path.normpath("logs/*.xml"))
|
||||
if files:
|
||||
print()
|
||||
print("{}There are {} logs collected in the logs/ directory.{} Please consider uploading".format(ansi.YELLOW, len(files), ansi.RESET))
|
||||
print("them to https://tclota.birth-online.de/ by running {}./upload_logs.py{}.".format(ansi.CYAN, ansi.RESET))
|
||||
|
||||
class DumpMgr:
|
||||
"""A class for XML dump management."""
|
||||
|
||||
def __init__(self):
|
||||
"""Populate dump file name."""
|
||||
self.last_dump_filename = None
|
||||
|
||||
@staticmethod
|
||||
def get_timestamp_random():
|
||||
"""Generate timestamp + random part to avoid collisions."""
|
||||
millis = floor(time.time() * 1000)
|
||||
tail = "{:06d}".format(random.randint(0, 999999))
|
||||
return "{}_{}".format(str(millis), tail)
|
||||
|
||||
def write_dump(self, data):
|
||||
"""Write dump to file."""
|
||||
outfile = os.path.normpath("logs/{}.xml".format(self.get_timestamp_random()))
|
||||
outfile = os.path.normpath("logs/{}.xml".format(get_timestamp_random()))
|
||||
if not os.path.exists(os.path.dirname(outfile)):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(outfile))
|
||||
@ -47,14 +56,3 @@ class DumpMgrMixin:
|
||||
if self.last_dump_filename:
|
||||
os.unlink(self.last_dump_filename)
|
||||
self.last_dump_filename = None
|
||||
|
||||
@staticmethod
|
||||
def write_info_if_dumps_found():
|
||||
"""Notify user to upload dumps if present."""
|
||||
# To disable this info, uncomment the following line.
|
||||
# return
|
||||
files = glob.glob(os.path.normpath("logs/*.xml"))
|
||||
if files:
|
||||
print()
|
||||
print("{}There are {} logs collected in the logs/ directory.{} Please consider uploading".format(ansi.YELLOW, len(files), ansi.RESET))
|
||||
print("them to https://tclota.birth-online.de/ by running {}./upload_logs.py{}.".format(ansi.CYAN, ansi.RESET))
|
||||
|
@ -6,4 +6,3 @@ from .checksumrequest import ChecksumRequest
|
||||
from .encryptheaderrequest import EncryptHeaderRequest
|
||||
from .runner import *
|
||||
from .serverselector import *
|
||||
from .dumpmgr import write_info_if_dumps_found
|
||||
|
@ -1,58 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# pylint: disable=C0111,C0326,C0103
|
||||
|
||||
"""Tools to manage dumps of API requests."""
|
||||
|
||||
import errno
|
||||
import glob
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
from math import floor
|
||||
|
||||
from .. import ansi
|
||||
|
||||
|
||||
def get_timestamp_random():
|
||||
"""Generate timestamp + random part to avoid collisions."""
|
||||
millis = floor(time.time() * 1000)
|
||||
tail = "{:06d}".format(random.randint(0, 999999))
|
||||
return "{}_{}".format(str(millis), tail)
|
||||
|
||||
def write_info_if_dumps_found():
|
||||
"""Notify user to upload dumps if present."""
|
||||
# To disable this info, uncomment the following line.
|
||||
# return
|
||||
files = glob.glob(os.path.normpath("logs/*.xml"))
|
||||
if files:
|
||||
print()
|
||||
print("{}There are {} logs collected in the logs/ directory.{} Please consider uploading".format(ansi.YELLOW, len(files), ansi.RESET))
|
||||
print("them to https://tclota.birth-online.de/ by running {}./upload_logs.py{}.".format(ansi.CYAN, ansi.RESET))
|
||||
|
||||
class DumpMgr:
|
||||
"""A class for XML dump management."""
|
||||
|
||||
def __init__(self):
|
||||
"""Populate dump file name."""
|
||||
self.last_dump_filename = None
|
||||
|
||||
def write_dump(self, data):
|
||||
"""Write dump to file."""
|
||||
outfile = os.path.normpath("logs/{}.xml".format(get_timestamp_random()))
|
||||
if not os.path.exists(os.path.dirname(outfile)):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(outfile))
|
||||
except OSError as err:
|
||||
if err.errno != errno.EEXIST:
|
||||
raise
|
||||
with open(outfile, "w", encoding="utf-8") as fhandle:
|
||||
fhandle.write(data)
|
||||
self.last_dump_filename = outfile
|
||||
|
||||
def delete_last_dump(self):
|
||||
"""Delete last dump."""
|
||||
if self.last_dump_filename:
|
||||
os.unlink(self.last_dump_filename)
|
||||
self.last_dump_filename = None
|
@ -4,7 +4,7 @@ import xml.dom.minidom
|
||||
|
||||
from defusedxml import ElementTree
|
||||
|
||||
from . import dumpmgr
|
||||
from .. import dumpmgr
|
||||
|
||||
|
||||
class TclResult:
|
||||
|
Reference in New Issue
Block a user