<commit_before>#!/usr/bin/env python3

#Meeprommer commandline interface
#By <NAME>
#Project Home:
#https://github.com/mkeller0815/MEEPROMMER
#http://www.ichbinzustaendig.de/dev/meeprommer-en

# Some tweaks by @pda

import serial, sys, argparse

# Parse command line arguments
parser = argparse.ArgumentParser(
        description='Meepromer Command Line Interface',
        epilog='Read source for further information')

task = parser.add_mutually_exclusive_group()
task.add_argument('-V', '--version', dest="cmd", action="store_const",
        const="version", help='Request and display device version')
task.add_argument('-w', '--write', dest="cmd", action="store_const",
        const="write", help='Write to EEPROM')
task.add_argument('-W', '--write_paged', dest="cmd", action="store_const",
        const="write_paged", help='Fast paged write to EEPROM')
task.add_argument('-r', '--read', dest="cmd", action="store_const",
        const="read", help='Read from EEPROM as ascii')
task.add_argument('-d', '--dump', dest="cmd", action="store_const",
        const="dump", help='Dump EEPROM to binary file')
task.add_argument('-v', '--verify', dest="cmd", action="store_const",
        const="verify", help='Compare EEPROM with file')
task.add_argument('-u', '--unlock', dest="cmd", action="store_const",
        const="unlock", help='Unlock EEPROM')
task.add_argument('-l', '--list', dest="cmd", action="store_const",
        const="list", help='List serial ports')

parser.add_argument('-a', '--address', action='store', default='0',
        help='Starting eeprom address (as hex), default 0')
parser.add_argument('-o', '--offset', action='store', default='0',
        help='Input file offset (as hex), default 0')
parser.add_argument('-b', '--bytes', action='store', default='8',
        type=int, help='Number of kBytes to r/w, default 8')
parser.add_argument('-p', '--page_size', action='store', default='32',
        type=int, help='Number of bytes per EEPROM page e.g.:'+
            'CAT28C*=32, AT28C*=64, X28C*=64, default 32')
parser.add_argument('-f', '--file', action='store',
        help='Name of data file')
parser.add_argument('-c', '--com', action='store', 
        default='COM5', help='Com port address')
parser.add_argument('-s', '--speed', action='store', 
        type=int, default='115200', help='Com port baud, default 115200')

def list_ports():
    from serial.tools import list_ports
    for x in list_ports.comports():
        print(x[0], x[1])

def dump_file():
    ser.flushInput()
    ser.write(bytes("r "+format(args.address,'04x')+" "+
        format(args.address+args.bytes*1024,'04x')+
        " 10\n", 'ascii'))
    eeprom = ser.read(args.bytes*1024)
    if(ser.read(1) != b'\0'):
        print("Error: no Ack")
        sys.exit(1)
    try:
        fo = open(args.file,'wb+')
    except OSError:
        print("Error: File cannot be opened, verify it is not in use")
        sys.exit(1)
    fo.write(eeprom)
    fo.close()

def verify():
    print("Verifying...")
    ser.flushInput()
    ser.write(bytes("r "+format(args.address,'04x')+" "+
        format(args.bytes*1024,'04x')+" 10\n", 'ascii'))
    try:
        fi = open(args.file,'rb')
    except FileNotFoundError:
        print("Error: ",args.file," not found, please select a valid file")
        sys.exit(1)
    except TypeError:
        print("Error: No file specified")
        sys.exit(1)
        
    fi.seek(args.offset)
    file = fi.read(args.bytes*1024)
    eeprom = ser.read(args.bytes*1024)
    if ser.read(1) != b'\0':
        print("Error: no EOF received")
        
    if file != eeprom:
        print("Not equal")
        n = 0
        for i in range(args.bytes*1024):
            if file[i] != eeprom[i]:
                n+=1
        print(n,"differences found")
        sys.exit(1)
    else:
        print("Ok")

def read_eeprom():
    ser.flushInput()
    ser.write(bytes("R "+format(args.address,'04x')+" "+
        format(args.address+args.bytes*1024,'04x')+
        " 10\n", 'ascii'))
    ser.readline()#remove blank starting line
    for i in range(round(args.bytes*1024/16)):
        print(ser.readline().decode('ascii').rstrip())

def write_eeprom(paged):
    import time
    
    fi = open(args.file,'rb')
    fi.seek(args.offset)
    now = time.time() #start our stopwatch
    for i in range(args.bytes): #write n blocks of 1024 bytes
        output = fi.read(1024)
        print("Writing from",format(args.address+i*1024,'04x'),
              "to",format(args.address+i*1024+1023,'04x'))
        if paged:
            ser.write(bytes("W "+format(args.address+i*1024,'04x')+
                " 0400 "+format(args.page_size,'02x')+"\n", 'ascii'))
        else:
            ser.write(bytes("w "+format(args.address+i*1024,'04x')+
                " 0400 00\n", 'ascii'))

        ser.flushInput()
        ser.write(output)
        if(ser.read(1) != b'%'):
            print("Error: no Ack")
            sys.exit(1)
    print("Wrote",args.bytes*1024,"bytes in","%.2f"%(time.time()-now),"seconds")
    
def unlock():
    print("Unlocking...")
    ser.flushInput()
    ser.write(bytes("u 0000 0000 00\n", 'ascii'))
    if ser.read(1) != b'%':
        print("Error: no ack")
        sys.exit(1)

def version():
    ser.write(b'V\n')
    print(ser.readline().decode('ascii').rstrip())

args = parser.parse_args()
#convert our hex strings to ints
args.address = int(args.address,16)
args.offset = int(args.offset,16)

class DebugSerial(serial.Serial):
    def write(self, data):
        #print("write: %s" % (data,))
        super().write(data)

SERIAL_TIMEOUT = 20 #seconds
try:
    ser = DebugSerial(args.com, args.speed, timeout=SERIAL_TIMEOUT)
except serial.serialutil.SerialException:
    print("Error: Serial port is not valid, please select a valid port")
    sys.exit(1)

# It seems necessary to wait >= ~1.8 seconds for the device to respond.
import time
time.sleep(2)

if args.cmd == 'version':
    version()
elif args.cmd == 'write':
    write_eeprom(False)
elif args.cmd == 'write_paged':
    write_eeprom(True)
elif args.cmd == 'read':
    read_eeprom()
elif args.cmd == 'dump':
    dump_file()
elif args.cmd == 'verify':
    verify()
elif args.cmd == 'unlock':
    unlock();
elif args.cmd == 'list':
    list_ports()
    
ser.close()
sys.exit(0)<commit_msg>added err handling to python3 cli client<commit_after>#!/usr/bin/env python3

# Version : 1.4

#Meeprommer commandline interface
#By <NAME>
#Project Home:
#https://github.com/mkeller0815/MEEPROMMER
#http://www.ichbinzustaendig.de/dev/meeprommer-en

# Some tweaks by @pda

# Uupdated by <NAME>
# https://github.com/brendanhoran
# added better error handling


import serial, sys, argparse, time


# Parse command line arguments
parser = argparse.ArgumentParser(
        description='Meepromer Command Line Interface',
        epilog='Read source for further information')

task = parser.add_mutually_exclusive_group()
task.add_argument('-V', '--version', dest="cmd", action="store_const",
        const="version", help='Request and display device version')
task.add_argument('-w', '--write', dest="cmd", action="store_const",
        const="write", help='Write to EEPROM')
task.add_argument('-W', '--write_paged', dest="cmd", action="store_const",
        const="write_paged", help='Fast paged write to EEPROM')
task.add_argument('-r', '--read', dest="cmd", action="store_const",
        const="read", help='Read from EEPROM as ascii')
task.add_argument('-d', '--dump', dest="cmd", action="store_const",
        const="dump", help='Dump EEPROM to binary file')
task.add_argument('-v', '--verify', dest="cmd", action="store_const",
        const="verify", help='Compare EEPROM with file')
task.add_argument('-u', '--unlock', dest="cmd", action="store_const",
        const="unlock", help='Unlock EEPROM')
task.add_argument('-l', '--list', dest="cmd", action="store_const",
        const="list", help='List serial ports')

parser.add_argument('-a', '--address', action='store', default='0',
        help='Starting eeprom address (as hex), default 0')
parser.add_argument('-o', '--offset', action='store', default='0',
        help='Input file offset (as hex), default 0')
parser.add_argument('-b', '--bytes', action='store', default='8',
        type=int, help='Number of kBytes to r/w, default 8')
parser.add_argument('-p', '--page_size', action='store', default='32',
        type=int, help='Number of bytes per EEPROM page e.g.:'+
            'CAT28C*=32, AT28C*=64, X28C*=64, default 32')
parser.add_argument('-f', '--file', action='store',
        help='Name of data file')
parser.add_argument('-c', '--com', action='store', 
        default='COM5', help='Com port address')
parser.add_argument('-s', '--speed', action='store', 
        type=int, default='115200', help='Com port baud, default 115200')


def list_ports():
    from serial.tools import list_ports
    print("Found the following serial devices :")
    for x in list_ports.comports():
        print(x[0], x[1])


def dump_file():
    ser.flushInput()
    ser.write(bytes("r "+format(args.address,'04x')+" "+
        format(args.address+args.bytes*1024,'04x')+
        " 10\n", 'ascii'))
    eeprom = ser.read(args.bytes*1024)
    if(ser.read(1) != b'\0'):
        print("Error: no Ack")
        sys.exit(1)
    try:
        fo = open(args.file,'wb+')
    except OSError:
        print("Error: File cannot be opened, verify it is not in use")
        sys.exit(1)
    fo.write(eeprom)
    fo.close()


def verify():
    print("Verifying...")
    ser.flushInput()
    ser.write(bytes("r "+format(args.address,'04x')+" "+
        format(args.bytes*1024,'04x')+" 10\n", 'ascii'))
    try:
        fi = open(args.file,'rb')
    except FileNotFoundError:
        print("Error: ",args.file," not found, please select a valid file")
        sys.exit(1)
    except TypeError:
        print("Error: No file specified")
        sys.exit(1)
        
    fi.seek(args.offset)
    file = fi.read(args.bytes*1024)
    eeprom = ser.read(args.bytes*1024)
    if ser.read(1) != b'\0':
        print("Error: no EOF received")
        
    if file != eeprom:
        print("Not equal")
        n = 0
        for i in range(args.bytes*1024):
            if file[i] != eeprom[i]:
                n+=1
        print(n,"differences found")
        sys.exit(1)
    else:
        print("Ok")


def read_eeprom():
    ser.flushInput()
    ser.write(bytes("R "+format(args.address,'04x')+" "+
        format(args.address+args.bytes*1024,'04x')+
        " 10\n", 'ascii'))
    ser.readline()#remove blank starting line
    for i in range(round(args.bytes*1024/16)):
        print(ser.readline().decode('ascii').rstrip())


def write_eeprom(paged):
    fi = open(args.file,'rb')
    fi.seek(args.offset)
    now = time.time() #start our stopwatch
    for i in range(args.bytes): #write n blocks of 1024 bytes
        output = fi.read(1024)
        print("Writing from",format(args.address+i*1024,'04x'),
              "to",format(args.address+i*1024+1023,'04x'))
        if paged:
            ser.write(bytes("W "+format(args.address+i*1024,'04x')+
                " 0400 "+format(args.page_size,'02x')+"\n", 'ascii'))
        else:
            ser.write(bytes("w "+format(args.address+i*1024,'04x')+
                " 0400 00\n", 'ascii'))

        ser.flushInput()
        ser.write(output)
        if(ser.read(1) != b'%'):
            print("Error: no Ack")
            sys.exit(1)
    print("Wrote",args.bytes*1024,"bytes in","%.2f"%(time.time()-now),"seconds")

 
def unlock():
    print("Unlocking...")
    ser.flushInput()
    ser.write(bytes("u 0000 0000 00\n", 'ascii'))
    if ser.read(1) != b'%':
        print("Error: no ack")
        sys.exit(1)
    else:
        print("Unlocked!")


def version():
    ser.write(b'V\n')
    print(ser.readline().decode('ascii').rstrip())


args = parser.parse_args()
#convert our hex strings to ints
args.address = int(args.address,16)
args.offset = int(args.offset,16)


if not args.cmd:
    print("Missing arguments :")
    print("Com/tty port device must be set (-c)")
    print("-h for detailed help")
    parser.print_usage()
    sys.exit(1)


class DebugSerial(serial.Serial):
    def write(self, data):
        #enable the below print statment to show debug data sent to the arduino
        #print("write: %s" % (data,))
        super().write(data)


SERIAL_TIMEOUT = 20 #seconds
try:
    ser = DebugSerial(args.com, args.speed, timeout=SERIAL_TIMEOUT)
except serial.serialutil.SerialException:
    print("Error: Serial port is not valid, please select a valid port")
    list_ports()
    sys.exit(1)


def CleanUp():
    ser.close()


# It seems necessary to wait >= ~1.8 seconds for the device to respond.
time.sleep(2)


if args.cmd == 'version':
    version()
elif args.cmd == 'write':
    write_eeprom(False)
    CleanUp()
elif args.cmd == 'write_paged':
    write_eeprom(True)
    CleanUp()
elif args.cmd == 'read':
    read_eeprom()
    CleanUp()
elif args.cmd == 'dump':
    dump_file()
    CleanUp()
elif args.cmd == 'verify':
    verify()
    CleanUp()
elif args.cmd == 'unlock':
    unlock()
    CleanUp()
elif args.cmd == 'list':
    list_ports()
    CleanUp()
else:
    parser.print_usage()
{'fraction_non_alphanumeric': 0.1774793231656423, 'fraction_numerical': 0.02799908946050535, 'mean_word_length': 3.1394472361809047, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3371681', 'n_tokens_mistral': 4692, 'n_tokens_neox': 4371, 'n_words': 1007}