bug fix, add md5sum

This commit is contained in:
HappyZ 2018-11-17 12:21:13 -06:00
parent 6cd50706ea
commit fcf5eaab22
3 changed files with 41 additions and 11 deletions

View File

@ -17,10 +17,14 @@ Greatly thank
## dpt-tools.py ## 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. 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 ### Validating successful connections
``` ```

View File

@ -91,9 +91,23 @@ class DPT():
''' '''
remove a file remove a file
''' '''
if self.diagnosis_isfile(fp): if not self.diagnosis_isfile(fp):
self.diagnosis_write("rm {}".format(fp)) return True
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): def diagnosis_isfile(self, fp):
''' '''

View File

@ -169,6 +169,9 @@ def diagnosis_pull_file(
resp = input( resp = input(
'> {} exist, overwrite? [yes/no]: '.format(localfp)) '> {} exist, overwrite? [yes/no]: '.format(localfp))
overwrite = True if resp == 'yes' else False 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)) dpt.info_print("Pulling file {}, plz be patient...".format(localfp))
if overwrite: if overwrite:
# read from hexdump, parse, and write to local file # read from hexdump, parse, and write to local file
@ -196,16 +199,16 @@ def diagnosis_pull_file(
else: else:
break break
offset += count offset += count
if offset % 100: if offset % 100 == 0:
dpt.info_print("Copying.. at block {}".format(offset)) dpt.info_print("Copying.. at block {}".format(offset))
# use xxd to convert back to binary file # use xxd to convert back to binary file
subprocess.call('xxd -r -p {0}.tmp > {0}'.format(localfp), shell=True) subprocess.call('xxd -r -p {0}.tmp > {0}'.format(localfp), shell=True)
duration = int(time.time() * 1000) - startTime duration = int(time.time() * 1000) - startTime
dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0)) dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0))
if os.path.isfile(localfp): if os.path.isfile(localfp):
# TODO: add md5 validation
dpt.info_print("File pulled to: {}".format(localfp)) 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 return localfp
except BaseException as e: except BaseException as e:
dpt.err_print(str(e)) dpt.err_print(str(e))
@ -250,14 +253,18 @@ def diagnosis_push_file(
return None return None
# remote file exists, overwrite it? # remote file exists, overwrite it?
remotefp = "{0}/{1}".format(folder, os.path.basename(localfp)) remotefp = "{0}/{1}".format(folder, os.path.basename(localfp))
if dpt.diagnosis_isfile(remotefp) and overwrite is None: if overwrite is None:
resp = input('> {} exist, overwrite? [yes/no]: '.format(remotefp)) overwrite = True
overwrite = True if resp == 'yes' else False if dpt.diagnosis_isfile(remotefp):
resp = input(
'> {} exist, overwrite? [yes/no]: '.format(remotefp))
overwrite = True if resp == 'yes' else False
if overwrite: if overwrite:
# write through echo # write through echo
firstRun = True firstRun = True
symbol = '>' symbol = '>'
startTime = int(time.time() * 1000) startTime = int(time.time() * 1000)
totalChunks = 0
with open(localfp, 'rb') as f: with open(localfp, 'rb') as f:
while 1: while 1:
chunk = f.read(chunkSize) chunk = f.read(chunkSize)
@ -273,11 +280,16 @@ def diagnosis_push_file(
if firstRun: if firstRun:
symbol = '>>' symbol = '>>'
firstRun = False firstRun = False
totalChunks += 1
if totalChunks % 100 == 0:
dpt.info_print(
"Copying.. at chuck {}".format(totalChunks))
duration = int(time.time() * 1000) - startTime duration = int(time.time() * 1000) - startTime
dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0)) dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0))
if dpt.diagnosis_isfile(remotefp): 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("File pushed to: {}".format(remotefp))
dpt.info_print("It's MD5 is: {}".format(md5))
return remotefp return remotefp
except BaseException as e: except BaseException as e:
dpt.err_print(str(e)) dpt.err_print(str(e))