# HG changeset patch # User Zach Welch zach@mandolincreekfarm.com # Fake Node ID 0bb87d9c5b84de44d96edb84f20cc47fa65a6a21
Fix style issues in tools (#2355)
diff --git a/tools/bitdiff.py b/tools/bitdiff.py index a13c63b..d06bdb2 100644 --- a/tools/bitdiff.py +++ b/tools/bitdiff.py @@ -1,81 +1,89 @@ #!/usr/bin/env python # -## Copyright 2013 Jens Jensen AF5MI kd4tjx@yahoo.com +# Copyright 2013 Jens Jensen AF5MI kd4tjx@yahoo.com + +import sys +import os +import argparse +import time
-import sys, os, argparse, time
def printDiff(pos, byte1, byte2, args): - - bits1 = '{0:08b}'.format(byte1) - bits2 = '{0:08b}'.format(byte2) - print "@%04Xh" % pos - print "1:%02Xh, %sb" % (byte1, bits1) - print "2:%02Xh, %sb" % (byte2, bits2) - if args.csv: - writeDiffCSV(pos, byte1, byte2, args) + bits1 = '{0:08b}'.format(byte1) + bits2 = '{0:08b}'.format(byte2) + print "@%04Xh" % pos + print "1:%02Xh, %sb" % (byte1, bits1) + print "2:%02Xh, %sb" % (byte2, bits2) + if args.csv: + writeDiffCSV(pos, byte1, byte2, args) +
def writeDiffCSV(pos, byte1, byte2, args): bits1 = '{0:08b}'.format(byte1) bits2 = '{0:08b}'.format(byte2) csvline = '%s, %s, %04X, %02X, %s, %02X, %s, %s, %s' % \ - (args.file1, args.file2, pos, byte1, bits1, byte2, bits2, args.setting, args.value) + (args.file1, args.file2, pos, byte1, bits1, + byte2, bits2, args.setting, args.value) if not os.path.isfile(args.csv): fh = open(args.csv, "w") - header = "filename1, filename2, byte_offset, byte1, bits1, byte2, bits2, item_msg, value_msg" + header = "filename1, filename2, byte_offset, byte1, " \ + "bits1, byte2, bits2, item_msg, value_msg" fh.write(header + os.linesep) else: fh = open(args.csv, "a") fh.write(csvline + os.linesep) fh.close() - +
def compareFiles(args): - f1 = open(args.file1, "rb") - f1.seek(args.offset) - f2 = open(args.file2, "rb") - f2.seek(args.offset) - - while True: - pos = f1.tell() - args.offset - c1 = f1.read(1) - c2 = f2.read(1) - if not (c1 and c2): - break - b1 = ord(c1) - b2 = ord(c2) - if b1 != b2: - printDiff(pos, b1, b2, args) - - pos = f1.tell() - args.offset - print "bytes read: %02d" % pos - f1.close() - f2.close() + f1 = open(args.file1, "rb") + f1.seek(args.offset) + f2 = open(args.file2, "rb") + f2.seek(args.offset) + + while True: + pos = f1.tell() - args.offset + c1 = f1.read(1) + c2 = f2.read(1) + if not (c1 and c2): + break + b1 = ord(c1) + b2 = ord(c2) + if b1 != b2: + printDiff(pos, b1, b2, args) + + pos = f1.tell() - args.offset + print "bytes read: %02d" % pos + f1.close() + f2.close() +
def compareFilesDat(args): - f1 = open(args.file1, "r") - f1contents = f1.read() - f1.close() - f2 = open(args.file2, "r") - f2contents = f2.read() - f2.close() - - f1strlist = f1contents.split() - f1intlist = map(int, f1strlist) - f2strlist = f2contents.split() - f2intlist = map(int, f2strlist) - f1bytes = bytearray(f1intlist) - f2bytes = bytearray(f2intlist) - - length = len(f1intlist) - for i in range(length): - b1 = f1bytes[i] - b2 = f2bytes[i] - pos = i - if b1 != b2: - printDiff(pos, b1, b2, args) - - pos = length - print "bytes read: %02d" % pos + f1 = open(args.file1, "r") + f1contents = f1.read() + f1.close() + f2 = open(args.file2, "r") + f2contents = f2.read() + f2.close() + + f1strlist = f1contents.split() + f1intlist = map(int, f1strlist) + f2strlist = f2contents.split() + f2intlist = map(int, f2strlist) + f1bytes = bytearray(f1intlist) + f2bytes = bytearray(f2intlist) + + length = len(f1intlist) + for i in range(length): + b1 = f1bytes[i] + b2 = f2bytes[i] + pos = i + if b1 != b2: + printDiff(pos, b1, b2, args) + + pos = length + print "bytes read: %02d" % pos +
def convertFileToBin(args): f1 = open(args.file1, "r") @@ -88,34 +96,53 @@ def convertFileToBin(args): f2.write(f1bytes) f2.close
+ def convertFileToDat(args): f1 = open(args.file1, "rb") f1contents = f1.read() f1.close() f2 = open(args.file2, "w") - for i in range(0, len(f1contents)): + for i in range(0, len(f1contents)): f2.write(" %d " % (ord(f1contents[i]), )) if i % 16 == 15: f2.write("\r\n") f2.close
-# -## main + +# main
ap = argparse.ArgumentParser(description="byte-/bit- comparison of two files") ap.add_argument("file1", help="first (reference) file to parse") ap.add_argument("file2", help="second file to parse") + mutexgrp1 = ap.add_mutually_exclusive_group() -mutexgrp1.add_argument("-o", "--offset", help="offset (hex) to start comparison", default=0) -mutexgrp1.add_argument("-d", "--dat", help="process input files from .DAT/.ADJ format (from 'jujumao' oem programming software for chinese radios)", action="store_true") -mutexgrp1.add_argument("--convert2bin", help="convert file1 from .dat/.adj to binary image file2", action="store_true") -mutexgrp1.add_argument("--convert2dat", help="convert file1 from bin to .dat/.adj file2", action="store_true") -ap.add_argument("-w", "--watch", help="'watch' changes. runs in a loop", action="store_true") +mutexgrp1.add_argument("-o", "--offset", default=0, + help="offset (hex) to start comparison") +mutexgrp1.add_argument("-d", "--dat", action="store_true", + help="process input files from .DAT/.ADJ format " + "(from 'jujumao' oem programming software " + "for chinese radios)") +mutexgrp1.add_argument("--convert2bin", action="store_true", + help="convert file1 from .dat/.adj to " + "binary image file2") +mutexgrp1.add_argument("--convert2dat", action="store_true", + help="convert file1 from bin to .dat/.adj file2") + +ap.add_argument("-w", "--watch", action="store_true", + help="'watch' changes. runs in a loop") + csvgrp = ap.add_argument_group("csv output") -csvgrp.add_argument("-c", "--csv", help="file to append csv results. format: \ - filename1, filename2, byte_offset, byte1, bits1, byte2, bits2, item_msg, value_msg") -csvgrp.add_argument("-s", "--setting", help="user-meaningful field indicating setting/item modified, e.g. 'beep' or 'txtone'") -csvgrp.add_argument("-v", "--value", help="user-meaningful field indicating values changed, e.g. 'true->false' or '110.9->100.0'") +csvgrp.add_argument("-c", "--csv", + help="file to append csv results. format: filename1, " + "filename2, byte_offset, byte1, bits1, byte2, " + "bits2, item_msg, value_msg") +csvgrp.add_argument("-s", "--setting", + help="user-meaningful field indicating setting/item " + "modified, e.g. 'beep' or 'txtone'") +csvgrp.add_argument("-v", "--value", + help="user-meaningful field indicating values " + "changed, e.g. 'true->false' or '110.9->100.0'") + args = ap.parse_args() if args.offset: args.offset = int(args.offset, 16) @@ -130,10 +157,10 @@ while True: elif (args.convert2bin): convertFileToBin(args) elif (args.convert2dat): - convertFileToDat(args) + convertFileToDat(args) else: - compareFiles(args) + compareFiles(args) if not args.watch: - break + break print "------" time.sleep(delay) diff --git a/tools/cpep8.blacklist b/tools/cpep8.blacklist index 2824a96..5d7ad07 100644 --- a/tools/cpep8.blacklist +++ b/tools/cpep8.blacklist @@ -70,5 +70,3 @@ ./chirp/wouxun.py ./chirp/wouxun_common.py ./chirp/yaesu_clone.py -./tools/bitdiff.py -./tools/img2thd72.py diff --git a/tools/img2thd72.py b/tools/img2thd72.py index a68a15c..407bdf7 100644 --- a/tools/img2thd72.py +++ b/tools/img2thd72.py @@ -18,43 +18,46 @@ # along with this software. If not, see http://www.gnu.org/licenses/.
-import sys, getopt +import sys +import getopt from PIL import Image as im
+ def die(msg): print msg sys.exit(1)
+ def thd72bitmap(fname, invert): img = im.open(ifname) - if img.size != (120,48): + if img.size != (120, 48): die("Image has wrong dimensions: must be 120x48") - + colors = img.getcolors() if len(colors) != 2: die("Image must be 1 bits per pixel (black and white)") - - if ('-i','') in opts: - c,black = colors[0] - c,white = colors[1] - else: - c,white = colors[0] - c,black = colors[1]
+ if ('-i', '') in opts: + c, black = colors[0] + c, white = colors[1] + else: + c, white = colors[0] + c, black = colors[1]
- colors = { black: 1, white: 0 } + colors = {black: 1, white: 0} data = img.getdata() buf = '' for y in range(6): for x in range(120): b = 0 for i in range(8): - b |= colors[data[x + 120*(y*8+i)]] << i + b |= colors[data[x + 120 * (y * 8 + i)]] << i buf += chr(b) return buf
+ def display_thd72(buf): - dots = { 0: '*', 1: ' '} + dots = {0: '*', 1: ' '} lines = [] for y in range(48): line = '' @@ -65,8 +68,10 @@ def display_thd72(buf): for l in lines: print l
+ def usage(): - print "\nUsage: %s <-s|-g> [-i] [-d] <image-file> <thd72-nvram-file>" % sys.argv[0] + print "\nUsage: %s <-s|-g> [-i] [-d] " \ + "<image-file> <thd72-nvram-file>" % sys.argv[0] print "\nThis program will modify whatever nvram file provided or will" print "create a new one if the file does not exist. After using this to" print "modify the image, you can use that file to upload all or part of" @@ -112,7 +117,7 @@ if __name__ == "__main__": of.seek(65536) of.close()
- if ('-d','') in opts: + if ('-d', '') in opts: display_thd72(buf)
blocks = [0, ] @@ -120,4 +125,3 @@ if __name__ == "__main__": blocks.append(1+imgpos/256) blocks.append(2+imgpos/256) print "Modified block list:", blocks -