From c77e94ba7a7b22c06014e46edd967fbbbba0b234 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:41:37 -0800 Subject: [PATCH 1/7] PEP8. Remove noop code --- code/memanalyzer.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/code/memanalyzer.py b/code/memanalyzer.py index 2b886dd3..2d849cc1 100644 --- a/code/memanalyzer.py +++ b/code/memanalyzer.py @@ -19,15 +19,16 @@ import argparse import os import re import shlex +import subprocess import sys from collections import OrderedDict + from sortedcontainers import SortedDict -import subprocess -if (sys.version_info > (3, 0)): - from subprocess import getstatusoutput as getstatusoutput +if sys.version_info > (3, 0): + from subprocess import getstatusoutput else: - from commands import getstatusoutput as getstatusoutput + from commands import getstatusoutput # ------------------------------------------------------------------------------- @@ -105,11 +106,13 @@ def run(env_, modules_): command = "ESPURNA_BOARD=\"WEMOS_D1_MINI_RELAYSHIELD\" ESPURNA_FLAGS=\"%s\" platformio run --silent --environment %s 2>/dev/null" % (flags, env_) subprocess.check_call(command, shell=True) + def calc_free(module): free = 80 * 1024 - module['data'] - module['rodata'] - module['bss'] free = free + (16 - free % 16) module['free'] = free + def modules_get(): modules_ = SortedDict() for line in open("espurna/config/arduino.h"): @@ -120,7 +123,8 @@ def modules_get(): del modules_['NETBIOS'] return modules_ -try: + +if __name__ == '__main__': # Parse command line options parser = argparse.ArgumentParser(description=description) @@ -279,7 +283,5 @@ try: total['size'], )) -except: - raise print("\n") From 97b5ef9c8ca73de3cc51d97977438dd949890346 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:43:13 -0800 Subject: [PATCH 2/7] Make string formatting consistent --- code/memanalyzer.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/code/memanalyzer.py b/code/memanalyzer.py index 2d849cc1..ddd6a788 100644 --- a/code/memanalyzer.py +++ b/code/memanalyzer.py @@ -56,7 +56,7 @@ def file_size(file): def analyse_memory(elf_file): - command = "%s -t '%s'" % (objdump_binary, elf_file) + command = "{} -t '{}'".format(objdump_binary, elf_file) response = subprocess.check_output(shlex.split(command)) if isinstance(response, bytes): response = response.decode('utf-8') @@ -67,8 +67,8 @@ def analyse_memory(elf_file): ret = {} for (id_, _) in list(sections.items()): - section_start_token = " _%s_start" % id_ - section_end_token = " _%s_end" % id_ + section_start_token = " _{}_start".format(id_) + section_end_token = " _{}_end".format(id_) section_start = -1 section_end = -1 for line in lines: @@ -93,9 +93,9 @@ def analyse_memory(elf_file): # print("{0: >10}|{1: >30}|{2:12X}|{3:12X}|{4:8}".format(id_, descr, section_start, section_end, section_length)) # i += 1 - # print("Total Used RAM : %d" % usedRAM) - # print("Free RAM : %d" % (TOTAL_DRAM - usedRAM)) - # print("Free IRam : %d" % usedIRAM) + # print("Total Used RAM : {:d}".format(usedRAM)) + # print("Free RAM : {:d}".format(TOTAL_DRAM - usedRAM)) + # print("Free IRam : {:d}".format(usedIRAM)) return ret @@ -103,7 +103,7 @@ def run(env_, modules_): flags = "" for item in modules_.items(): flags += "-D%s_SUPPORT=%d " % item - command = "ESPURNA_BOARD=\"WEMOS_D1_MINI_RELAYSHIELD\" ESPURNA_FLAGS=\"%s\" platformio run --silent --environment %s 2>/dev/null" % (flags, env_) + command = "ESPURNA_BOARD=\"WEMOS_D1_MINI_RELAYSHIELD\" ESPURNA_FLAGS=\"{}\" platformio run --silent --environment {} 2>/dev/null".format(flags, env_) subprocess.check_call(command, shell=True) @@ -164,7 +164,7 @@ if __name__ == '__main__': # Check test modules exist for module in test_modules: if module not in available_modules: - print("Module %s not found" % module) + print("Module {} not found".format(module)) sys.exit(2) # Define base configuration @@ -177,9 +177,9 @@ if __name__ == '__main__': # Show init message if len(test_modules) > 0: - print("Analyzing module(s) %s on top of %s configuration\n" % (", ".join(test_modules), "CORE" if args.core > 0 else "DEFAULT")) + print("Analyzing module(s) {} on top of {} configuration\n".format(", ".join(test_modules), "CORE" if args.core > 0 else "DEFAULT")) else: - print("Analyzing %s configuration\n" % ("CORE" if args.core > 0 else "DEFAULT")) + print("Analyzing {} configuration\n".format("CORE" if args.core > 0 else "DEFAULT")) output_format = "{:<20}|{:<15}|{:<15}|{:<15}|{:<15}|{:<15}|{:<15}|{:<15}" print(output_format.format( @@ -215,8 +215,8 @@ if __name__ == '__main__': # Build the core without modules to get base memory usage run(env, modules) - base = analyse_memory(".pioenvs/%s/firmware.elf" % env) - base['size'] = file_size(".pioenvs/%s/firmware.bin" % env) + base = analyse_memory(".pioenvs/{}/firmware.elf".format(env)) + base['size'] = file_size(".pioenvs/{}/firmware.bin".format(env)) calc_free(base) print(output_format.format( "CORE" if args.core == 1 else "DEFAULT", @@ -235,8 +235,8 @@ if __name__ == '__main__': modules[module] = 1 run(env, modules) - results[module] = analyse_memory(".pioenvs/%s/firmware.elf" % env) - results[module]['size'] = file_size(".pioenvs/%s/firmware.bin" % env) + results[module] = analyse_memory(".pioenvs/{}/firmware.elf".format(env)) + results[module]['size'] = file_size(".pioenvs/{}/firmware.bin".format(env)) calc_free(results[module]) modules[module] = 0 @@ -257,8 +257,8 @@ if __name__ == '__main__': for module in test_modules: modules[module] = 1 run(env, modules) - total = analyse_memory(".pioenvs/%s/firmware.elf" % env) - total['size'] = file_size(".pioenvs/%s/firmware.bin" % env) + total = analyse_memory(".pioenvs/{}/firmware.elf".format(env)) + total['size'] = file_size(".pioenvs/{}/firmware.bin".format(env)) calc_free(total) if len(test_modules) > 1: From e89ec8c0329007a52f537db7d91d0edc30263f10 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:44:26 -0800 Subject: [PATCH 3/7] PEP8 --- code/ota.py | 71 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/code/ota.py b/code/ota.py index d8744997..87adf0dc 100755 --- a/code/ota.py +++ b/code/ota.py @@ -8,14 +8,14 @@ # ------------------------------------------------------------------------------- from __future__ import print_function -import shutil import argparse +import os import re +import shutil import socket import subprocess import sys import time -import os from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf @@ -33,6 +33,7 @@ description = "ESPurna OTA Manager v0.3" devices = [] discover_last = 0 + # ------------------------------------------------------------------------------- def on_service_state_change(zeroconf, service_type, name, state_change): @@ -61,7 +62,7 @@ def on_service_state_change(zeroconf, service_type, name, state_change): } for key, item in info.properties.items(): - device[key.decode('UTF-8')] = item.decode('UTF-8'); + device[key.decode('UTF-8')] = item.decode('UTF-8') # rename fields (needed for sorting by name) device['app'] = device['app_name'] @@ -75,35 +76,35 @@ def list_devices(): """ Shows the list of discovered devices """ - output_format="{:>3} {:<14} {:<15} {:<17} {:<12} {:<12} {:<25} {:<8} {:<8} {:<10}" + output_format = "{:>3} {:<14} {:<15} {:<17} {:<12} {:<12} {:<25} {:<8} {:<8} {:<10}" print(output_format.format( - "#", - "HOSTNAME", - "IP", - "MAC", - "APP", - "VERSION", - "DEVICE", - "MEM_SIZE", - "SDK_SIZE", - "FREE_SPACE" + "#", + "HOSTNAME", + "IP", + "MAC", + "APP", + "VERSION", + "DEVICE", + "MEM_SIZE", + "SDK_SIZE", + "FREE_SPACE" )) print("-" * 139) index = 0 for device in devices: - index = index + 1 + index += 1 print(output_format.format( - index, - device.get('hostname', ''), - device.get('ip', ''), - device.get('mac', ''), - device.get('app_name', ''), - device.get('app_version', ''), - device.get('target_board', ''), - device.get('mem_size', 0), - device.get('sdk_size', 0), - device.get('free_space', 0), + index, + device.get('hostname', ''), + device.get('ip', ''), + device.get('mac', ''), + device.get('app_name', ''), + device.get('app_version', ''), + device.get('target_board', ''), + device.get('mem_size', 0), + device.get('sdk_size', 0), + device.get('free_space', 0), )) print() @@ -120,11 +121,13 @@ def get_boards(): boards.append(m.group(1)) return sorted(boards) + def get_device_size(device): if device.get('mem_size', 0) == device.get('sdk_size', 0): return int(device.get('mem_size', 0)) / 1024 return 0 + def get_empty_board(): """ Returns the empty structure of a board to flash @@ -132,12 +135,13 @@ def get_empty_board(): board = {'board': '', 'ip': '', 'size': 0, 'auth': '', 'flags': ''} return board + def get_board_by_index(index): """ Returns the required data to flash a given board """ board = {} - if 1 <= index and index <= len(devices): + if 1 <= index <= len(devices): device = devices[index - 1] board['hostname'] = device.get('hostname') board['board'] = device.get('target_board', '') @@ -145,6 +149,7 @@ def get_board_by_index(index): board['size'] = get_device_size(device) return board + def get_board_by_mac(mac): """ Returns the required data to flash a given board @@ -161,6 +166,7 @@ def get_board_by_mac(mac): return board return None + def get_board_by_hostname(hostname): """ Returns the required data to flash a given board @@ -178,6 +184,7 @@ def get_board_by_hostname(hostname): return board return None + def input_board(): """ Grabs info from the user about what device to flash @@ -192,7 +199,7 @@ def input_board(): print("Board number must be between 1 and %s\n" % str(len(devices))) return None - board = get_board_by_index(index); + board = get_board_by_index(index) # Choose board type if none before if len(board.get('board', '')) == 0: @@ -202,7 +209,7 @@ def input_board(): boards = get_boards() for name in boards: print("%3d\t%s" % (count, name)) - count = count + 1 + count += 1 print() try: index = int(input("Choose the board type you want to flash: ")) @@ -227,9 +234,11 @@ def input_board(): return board + def boardname(board): return board.get('hostname', board['ip']) + def store(device, env): source = ".pioenvs/%s/firmware.elf" % env destination = ".pioenvs/elfs/%s.elf" % boardname(device).lower() @@ -240,6 +249,7 @@ def store(device, env): shutil.move(source, destination) + def run(device, env): print("Building and flashing image over-the-air...") environ = os.environ.copy() @@ -253,6 +263,7 @@ def run(device, env): store(device, env) + # ------------------------------------------------------------------------------- if __name__ == '__main__': @@ -277,8 +288,8 @@ if __name__ == '__main__': browser = ServiceBrowser(zeroconf, "_arduino._tcp.local.", handlers=[on_service_state_change]) discover_last = time.time() while time.time() < discover_last + DISCOVER_TIMEOUT: - None - #zeroconf.close() + pass + # zeroconf.close() if len(devices) == 0: print("Nothing found!\n") From 4f1524c1ffed6685fefd2a6fc286c2f8d2c23e06 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:44:53 -0800 Subject: [PATCH 4/7] typos --- code/ota.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/ota.py b/code/ota.py index 87adf0dc..f47ab6e6 100755 --- a/code/ota.py +++ b/code/ota.py @@ -283,7 +283,7 @@ if __name__ == '__main__': print(description) print() - # Look for sevices + # Look for services zeroconf = Zeroconf() browser = ServiceBrowser(zeroconf, "_arduino._tcp.local.", handlers=[on_service_state_change]) discover_last = time.time() @@ -333,7 +333,7 @@ if __name__ == '__main__': queue = sorted(queue, key=lambda device: device.get('board', '')) - # Flash eash board + # Flash each board for board in queue: # Flash core version? From b9cebcf0b06a1046c250e00758ca7223bd703ea5 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:45:39 -0800 Subject: [PATCH 5/7] Make string formatting consistent --- code/ota.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/code/ota.py b/code/ota.py index f47ab6e6..a2a5fed2 100755 --- a/code/ota.py +++ b/code/ota.py @@ -196,7 +196,7 @@ def input_board(): except ValueError: index = 0 if index < 0 or len(devices) < index: - print("Board number must be between 1 and %s\n" % str(len(devices))) + print("Board number must be between 1 and {}\n".format(str(len(devices)))) return None board = get_board_by_index(index) @@ -208,7 +208,7 @@ def input_board(): count = 1 boards = get_boards() for name in boards: - print("%3d\t%s" % (count, name)) + print("{:3d}\t{}".format(count, name)) count += 1 print() try: @@ -216,7 +216,7 @@ def input_board(): except ValueError: index = 0 if index < 1 or len(boards) < index: - print("Board number must be between 1 and %s\n" % str(len(boards))) + print("Board number must be between 1 and {}\n".format(str(len(boards)))) return None board['board'] = boards[index - 1] @@ -240,8 +240,8 @@ def boardname(board): def store(device, env): - source = ".pioenvs/%s/firmware.elf" % env - destination = ".pioenvs/elfs/%s.elf" % boardname(device).lower() + source = ".pioenvs/{}/firmware.elf".format(env) + destination = ".pioenvs/elfs/{}.elf".format(boardname(device).lower()) dst_dir = os.path.dirname(destination) if not os.path.exists(dst_dir): @@ -298,7 +298,7 @@ if __name__ == '__main__': # Sort list field = args.sort.lower() if field not in devices[0]: - print("Unknown field '%s'\n" % field) + print("Unknown field '{}'\n".format(field)) sys.exit(1) devices = sorted(devices, key=lambda device: device.get(field, '')) @@ -340,16 +340,16 @@ if __name__ == '__main__': if args.core > 0: board['flags'] = "-DESPURNA_CORE " + board['flags'] - env = "esp8266-%dm-ota" % board['size'] + env = "esp8266-{:d}m-ota".format(board['size']) # Summary print() - print("HOST = %s" % boardname(board)) - print("IP = %s" % board['ip']) - print("BOARD = %s" % board['board']) - print("AUTH = %s" % board['auth']) - print("FLAGS = %s" % board['flags']) - print("ENV = %s" % env) + print("HOST = {}".format(boardname(board))) + print("IP = {}".format(board['ip'])) + print("BOARD = {}".format(board['board'])) + print("AUTH = {}".format(board['auth'])) + print("FLAGS = {}".format(board['flags'])) + print("ENV = {}".format(env)) response = True if args.yes == 0: From 7eaceb20604157d72aa2fac8a5f3ecca3da1a385 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:47:24 -0800 Subject: [PATCH 6/7] PEP8 --- pre-commit | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pre-commit b/pre-commit index 083d44e7..3bf202d7 100755 --- a/pre-commit +++ b/pre-commit @@ -15,17 +15,18 @@ Copy this file to .git/hooks/ """ import os -import sys -import string import re - +import string +import sys from subprocess import call, check_output + try: from urllib.parse import urlparse except ImportError: from urlparse import urlparse from fileinput import FileInput + # https://github.com/python/cpython/commit/6cb7b659#diff-78790b53ff259619377058acd4f74672 if sys.version_info[0] < 3: class FileInputCtx(FileInput): @@ -35,6 +36,7 @@ if sys.version_info[0] < 3: def __exit__(self, type, value, traceback): self.close() + FileInput = FileInputCtx @@ -91,15 +93,15 @@ def espurna_get_version(base, version_h="code/espurna/config/version.h"): TEMPLATES = { "![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" - "(https://travis-ci.org/{USER}/{REPO})\n", - "![version]": "[![version](https://img.shields.io/badge/version-{VERSION:escape_hyphen}-brightgreen.svg)](CHANGELOG.md)\n", + "(https://travis-ci.org/{USER}/{REPO})\n", + "![version]": "[![version](https://img.shields.io/badge/version-{VERSION:escape_hyphen}-brightgreen.svg)]" + "(CHANGELOG.md)\n", "![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH:escape_hyphen}-orange.svg)]" - "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n", + "(https://github.com/{USER}/{REPO}/tree/{BRANCH}/)\n", } README = "README.md" - if __name__ == "__main__": base = os.getcwd() @@ -117,6 +119,7 @@ if __name__ == "__main__": for k, tmpl in TEMPLATES.items() ] + def fmt_line(line): for match, tmpl in templates: if match in line: @@ -124,6 +127,7 @@ if __name__ == "__main__": return line + path = os.path.join(base, README) with FileInput(path, inplace=True) as readme: From 4515114b892038bca013b76a4f80649d854260ae Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Tue, 5 Feb 2019 15:52:27 -0800 Subject: [PATCH 7/7] Fix last % formatter. This was auto unpacking a tuple --- code/memanalyzer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/memanalyzer.py b/code/memanalyzer.py index ddd6a788..02405a65 100644 --- a/code/memanalyzer.py +++ b/code/memanalyzer.py @@ -101,8 +101,8 @@ def analyse_memory(elf_file): def run(env_, modules_): flags = "" - for item in modules_.items(): - flags += "-D%s_SUPPORT=%d " % item + for k, v in modules_.items(): + flags += "-D{}_SUPPORT={:d} ".format(k, v) command = "ESPURNA_BOARD=\"WEMOS_D1_MINI_RELAYSHIELD\" ESPURNA_FLAGS=\"{}\" platformio run --silent --environment {} 2>/dev/null".format(flags, env_) subprocess.check_call(command, shell=True)