From e5826429368f79e4786ebc7d30cc4804a8b30f75 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 11 Feb 2018 01:48:47 +0100 Subject: [PATCH] Cleanup, moved dumpmgr class. --- tclcheck.py | 4 +-- tclcheck_allfull.py | 3 +- tclcheck_allota.py | 3 +- tclcheck_findprd.py | 4 +-- tclcheck_findprd2.py | 4 +-- tclcheck_findver.py | 4 +-- tclcheck_gapfill.py | 4 +-- tcldown.py | 4 +-- tcllib/__init__.py | 34 ++------------------- tcllib/dumpmgr.py | 40 ++++++++++++------------- tcllib/requests/__init__.py | 1 - tcllib/requests/dumpmgr.py | 58 ------------------------------------ tcllib/requests/tclresult.py | 2 +- 13 files changed, 39 insertions(+), 126 deletions(-) delete mode 100644 tcllib/requests/dumpmgr.py diff --git a/tclcheck.py b/tclcheck.py index b5ddb97..5405049 100755 --- a/tclcheck.py +++ b/tclcheck.py @@ -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 = """ diff --git a/tclcheck_allfull.py b/tclcheck_allfull.py index 43fe33c..1b76ae0 100644 --- a/tclcheck_allfull.py +++ b/tclcheck_allfull.py @@ -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() diff --git a/tclcheck_allota.py b/tclcheck_allota.py index 47c328f..044726a 100644 --- a/tclcheck_allota.py +++ b/tclcheck_allota.py @@ -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() diff --git a/tclcheck_findprd.py b/tclcheck_findprd.py index 6d454b6..3d8a168 100755 --- a/tclcheck_findprd.py +++ b/tclcheck_findprd.py @@ -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 = """ diff --git a/tclcheck_findprd2.py b/tclcheck_findprd2.py index e2b3c88..a166ef9 100644 --- a/tclcheck_findprd2.py +++ b/tclcheck_findprd2.py @@ -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 diff --git a/tclcheck_findver.py b/tclcheck_findver.py index 5be799f..808e9ca 100755 --- a/tclcheck_findver.py +++ b/tclcheck_findver.py @@ -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 = """ diff --git a/tclcheck_gapfill.py b/tclcheck_gapfill.py index 6fe7e93..9e11604 100644 --- a/tclcheck_gapfill.py +++ b/tclcheck_gapfill.py @@ -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) diff --git a/tcldown.py b/tcldown.py index b164dde..73fb3dd 100644 --- a/tcldown.py +++ b/tcldown.py @@ -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 = """ diff --git a/tcllib/__init__.py b/tcllib/__init__.py index 96f84b6..0f0c8ac 100644 --- a/tcllib/__init__.py +++ b/tcllib/__init__.py @@ -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 diff --git a/tcllib/dumpmgr.py b/tcllib/dumpmgr.py index a24897e..e6bf993 100644 --- a/tcllib/dumpmgr.py +++ b/tcllib/dumpmgr.py @@ -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)) diff --git a/tcllib/requests/__init__.py b/tcllib/requests/__init__.py index 7d4dc86..7872868 100644 --- a/tcllib/requests/__init__.py +++ b/tcllib/requests/__init__.py @@ -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 diff --git a/tcllib/requests/dumpmgr.py b/tcllib/requests/dumpmgr.py deleted file mode 100644 index 57796c8..0000000 --- a/tcllib/requests/dumpmgr.py +++ /dev/null @@ -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 diff --git a/tcllib/requests/tclresult.py b/tcllib/requests/tclresult.py index 335a44d..3c39f41 100644 --- a/tcllib/requests/tclresult.py +++ b/tcllib/requests/tclresult.py @@ -4,7 +4,7 @@ import xml.dom.minidom from defusedxml import ElementTree -from . import dumpmgr +from .. import dumpmgr class TclResult: