From fcf5eaab223c476be06a15f7dcfe2e3a814c2ae5 Mon Sep 17 00:00:00 2001 From: HappyZ Date: Sat, 17 Nov 2018 12:21:13 -0600 Subject: [PATCH] bug fix, add md5sum --- README.md | 6 +++++- python_api/libDPT.py | 20 +++++++++++++++++--- python_api/libInteractive.py | 26 +++++++++++++++++++------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8b30031..0096f64 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,14 @@ Greatly thank ## dpt-tools.py -NOTE: Use at your own risk. I have tested this on my MacBook. You need `pip install httpsig` if you don't have it already. It only runs on Python 3. +NOTE: Use at your own risk. I have tested this on my *MacBook*. You need `pip install httpsig pyserial` if you don't have it already. It only runs on Python 3. This intends to be an interative shell commandline tool that wraps processes like updating firmware pkg, obtaining diagnosis access, etc. +### Prerequirement + +To use the tool properly, you also need `xxd`. + ### Validating successful connections ``` diff --git a/python_api/libDPT.py b/python_api/libDPT.py index 2ff27da..bc39548 100644 --- a/python_api/libDPT.py +++ b/python_api/libDPT.py @@ -91,9 +91,23 @@ class DPT(): ''' remove a file ''' - if self.diagnosis_isfile(fp): - self.diagnosis_write("rm {}".format(fp)) - return True + if not self.diagnosis_isfile(fp): + return True + resp = self.diagnosis_write("rm {}".format(fp)) + return not (resp == "") + + def diagnosis_md5sum_file(self, fp): + ''' + get md5sum of a file + ''' + if not self.diagnosis_isfile(fp): + return "" + resp = self.diagnosis_write("md5sum {}".format(fp)) + try: + return resp[1].split()[0] + except BaseException as e: + self.err_print(str(e)) + return "" def diagnosis_isfile(self, fp): ''' diff --git a/python_api/libInteractive.py b/python_api/libInteractive.py index 06b778f..2e1be99 100644 --- a/python_api/libInteractive.py +++ b/python_api/libInteractive.py @@ -169,6 +169,9 @@ def diagnosis_pull_file( resp = input( '> {} exist, overwrite? [yes/no]: '.format(localfp)) overwrite = True if resp == 'yes' else False + # get md5 + md5 = dpt.diagnosis_md5sum_file(remotefp) + # start dpt.info_print("Pulling file {}, plz be patient...".format(localfp)) if overwrite: # read from hexdump, parse, and write to local file @@ -196,16 +199,16 @@ def diagnosis_pull_file( else: break offset += count - if offset % 100: + if offset % 100 == 0: dpt.info_print("Copying.. at block {}".format(offset)) # use xxd to convert back to binary file subprocess.call('xxd -r -p {0}.tmp > {0}'.format(localfp), shell=True) duration = int(time.time() * 1000) - startTime dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0)) if os.path.isfile(localfp): - # TODO: add md5 validation dpt.info_print("File pulled to: {}".format(localfp)) - # os.remove("{}.tmp".format(localfp)) + dpt.info_print("Please verify if it's MD5 is {}".format(md5)) + os.remove("{}.tmp".format(localfp)) return localfp except BaseException as e: dpt.err_print(str(e)) @@ -250,14 +253,18 @@ def diagnosis_push_file( return None # remote file exists, overwrite it? remotefp = "{0}/{1}".format(folder, os.path.basename(localfp)) - if dpt.diagnosis_isfile(remotefp) and overwrite is None: - resp = input('> {} exist, overwrite? [yes/no]: '.format(remotefp)) - overwrite = True if resp == 'yes' else False + if overwrite is None: + overwrite = True + if dpt.diagnosis_isfile(remotefp): + resp = input( + '> {} exist, overwrite? [yes/no]: '.format(remotefp)) + overwrite = True if resp == 'yes' else False if overwrite: # write through echo firstRun = True symbol = '>' startTime = int(time.time() * 1000) + totalChunks = 0 with open(localfp, 'rb') as f: while 1: chunk = f.read(chunkSize) @@ -273,11 +280,16 @@ def diagnosis_push_file( if firstRun: symbol = '>>' firstRun = False + totalChunks += 1 + if totalChunks % 100 == 0: + dpt.info_print( + "Copying.. at chuck {}".format(totalChunks)) duration = int(time.time() * 1000) - startTime dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0)) if dpt.diagnosis_isfile(remotefp): - # TODO: add md5 validation + md5 = dpt.diagnosis_md5sum_file(remotefp) dpt.info_print("File pushed to: {}".format(remotefp)) + dpt.info_print("It's MD5 is: {}".format(md5)) return remotefp except BaseException as e: dpt.err_print(str(e))