From eeba6f447e883fe8d78ca1374763bfafc3e6508f Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Sat, 7 Apr 2018 02:00:04 +0200 Subject: [PATCH] python3 compatibility --- Makefile | 10 +++++----- bootloader/Makefile | 2 +- f3_boot/Makefile | 4 +--- stm32f303/Makefile | 8 ++++---- toolchain.mak | 1 + tools/add_version_info.py | 20 ++++++-------------- tools/bootloader.py | 12 ++---------- tools/crc32.py | 2 +- tools/create_cmd.py | 22 +++++++--------------- tools/create_config.py | 30 +++++++++++------------------- tools/create_hal_tbl.py | 1 - tools/elf.py | 26 ++++++++++++-------------- 12 files changed, 51 insertions(+), 87 deletions(-) diff --git a/Makefile b/Makefile index f20f1eb8..d3354f5f 100644 --- a/Makefile +++ b/Makefile @@ -227,7 +227,7 @@ LDFLAGS += -T$(LDSCRIPT) #============================================================================ -POSTLD = tools/add_version_info.py # -q +POSTLD = $(PYTHON) tools/add_version_info.py # -q # Compiler flags to generate dependency files # @@ -263,9 +263,9 @@ src/conf_templates.c: tbl #generate hal and command tables tbl: @echo Generating tables - @tools/create_hal_tbl.py . $(COMPS) - @tools/create_config.py conf/template/* > src/conf_templates.c - @tools/create_cmd.py $(SOURCES) > inc/commandslist.h + @$(PYTHON) tools/create_hal_tbl.py . $(COMPS) + @$(PYTHON) tools/create_config.py conf/template/* > src/conf_templates.c + @$(PYTHON) tools/create_cmd.py $(SOURCES) > inc/commandslist.h boot: $(MAKE) -f bootloader/Makefile @@ -329,7 +329,7 @@ showsize: build # Flash the device # btburn: build showsize $(TARGET).dfu - @tools/bootloader.py + @$(PYTHON) tools/bootloader.py @sleep 1 @dfu-util -d 0483:df11 -a 0 -s 0x08010000:leave -D $(TARGET).dfu diff --git a/bootloader/Makefile b/bootloader/Makefile index 59c7f57b..c071b42a 100644 --- a/bootloader/Makefile +++ b/bootloader/Makefile @@ -109,7 +109,7 @@ LDFLAGS += -T$(LDSCRIPT) #============================================================================ -POSTLD = tools/add_version_info.py # -q +POSTLD = $(PYTHON) tools/add_version_info.py # -q # Compiler flags to generate dependency files # diff --git a/f3_boot/Makefile b/f3_boot/Makefile index daee6979..803bc1d3 100644 --- a/f3_boot/Makefile +++ b/f3_boot/Makefile @@ -130,7 +130,7 @@ LDFLAGS += -T$(LDSCRIPT) #============================================================================ -POSTLD = tools/add_version_info.py # -q +POSTLD = $(PYTHON) tools/add_version_info.py # -q # Compiler flags to generate dependency files # @@ -173,8 +173,6 @@ showsize: build # Flash the device # btburn: build showsize $(TARGET).dfu - @tools/bootloader.py - @sleep 1 @dfu-util -d 0483:df11 -a 0 -s 0x8000000:leave -D $(TARGET).dfu flash: $(TARGET).bin diff --git a/stm32f303/Makefile b/stm32f303/Makefile index 9fac0fa9..96c92dfc 100644 --- a/stm32f303/Makefile +++ b/stm32f303/Makefile @@ -158,7 +158,7 @@ LDFLAGS += -T$(LDSCRIPT) #============================================================================ -POSTLD = tools/add_version_info.py # -q +POSTLD = $(PYTHON) tools/add_version_info.py # -q # Compiler flags to generate dependency files # @@ -196,9 +196,9 @@ stm32f303/src/hal_tbl.c: tbl tbl: @echo Generating tables - @tools/create_hal_tbl.py stm32f303/ $(COMPS) + @$(PYTHON) tools/create_hal_tbl.py stm32f303/ $(COMPS) #shared/comps/*.c src/comps/hw/*.c src/comps/*.c - @tools/create_cmd.py $(SOURCES) > stm32f303/inc/commandslist.h + @$(PYTHON) tools/create_cmd.py $(SOURCES) > stm32f303/inc/commandslist.h gccversion: @$(CC) --version @@ -212,7 +212,7 @@ showsize: build # Flash the device # btburn: build showsize $(TARGET).dfu - @tools/bootloader.py + @$(PYTHON) tools/bootloader.py @sleep 1 @dfu-util -d 0483:df11 -a 0 -s 0x8004000:leave -D $(TARGET).dfu diff --git a/toolchain.mak b/toolchain.mak index e0c4504e..eacf2660 100644 --- a/toolchain.mak +++ b/toolchain.mak @@ -1,4 +1,5 @@ # Define programs and commands for the toolchain +PYTHON = python TOOLCHAIN = arm-none-eabi- CC = $(TOOLCHAIN)gcc OBJCOPY = $(TOOLCHAIN)objcopy diff --git a/tools/add_version_info.py b/tools/add_version_info.py index 79880331..3c88c394 100755 --- a/tools/add_version_info.py +++ b/tools/add_version_info.py @@ -1,12 +1,4 @@ -#! /bin/sh -"true" '''\' -if command -v python2.7 > /dev/null; then - exec python2.7 "$0" "$@" -else - exec python "$0" "$@" -fi -exit $? -''' +#!/usr/bin/env python # # Add CRC checksum and version information to an ELF file # Copyright (C)2015 Thomas Kindler @@ -28,7 +20,7 @@ import os, struct import argparse, subprocess import getpass, platform -from StringIO import StringIO +from io import BytesIO from datetime import datetime from crc32 import CRC32 @@ -71,8 +63,8 @@ class VersionInfo: self.image_crc, self.image_size, self.git_version, self.git_branch, - self.build_user, self.build_host, - self.build_date, self.build_time + self.build_user.encode(), self.build_host.encode(), + self.build_date.encode(), self.build_time.encode() ) @@ -132,7 +124,7 @@ def elf_to_bin(elf, elf_data): bin_data += b'\xFF' * gap bin_data += elf_data[s.sh_offset : s.sh_offset + s.sh_size] - return str(bin_data) + return bin_data def patch_section(elf, elf_data, section, data): @@ -146,8 +138,8 @@ def patch_elf(): elf_data = bytearray(file.read()) elf = ELFObject() - elf.fromFile(StringIO(elf_data)) + elf.fromFile(BytesIO(elf_data)) for s in elf.getSections(): dprint(" %-16s: 0x%08x -> 0x%08x %8d" % (s.name, s.lma, s.sh_addr, s.sh_size) diff --git a/tools/bootloader.py b/tools/bootloader.py index f3222198..4434000a 100755 --- a/tools/bootloader.py +++ b/tools/bootloader.py @@ -1,12 +1,4 @@ -#! /bin/sh -"true" '''\' -if command -v python2.7 > /dev/null; then - exec python2.7 "$0" "$@" -else - exec python "$0" "$@" -fi -exit $? -''' +#!/usr/bin/env python import serial.tools.list_ports done = False @@ -15,7 +7,7 @@ for port in serial.tools.list_ports.comports(): print ("Reseting stmbl at " + port[2]) try: stmbl = serial.Serial(port[0]) - stmbl.write('bootloader\n') + stmbl.write('bootloader\n'.encode()) stmbl.close() done = True except OSError: diff --git a/tools/crc32.py b/tools/crc32.py index 0fc92070..26c6a247 100644 --- a/tools/crc32.py +++ b/tools/crc32.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # # Copyright 2013 StalkR # diff --git a/tools/create_cmd.py b/tools/create_cmd.py index 11671085..561b77f0 100755 --- a/tools/create_cmd.py +++ b/tools/create_cmd.py @@ -1,12 +1,4 @@ -#! /bin/sh -"true" '''\' -if command -v python2.7 > /dev/null; then - exec python2.7 "$0" "$@" -else - exec python "$0" "$@" -fi -exit $? -''' +#!/usr/bin/env python import re import sys @@ -19,14 +11,14 @@ for infile in sys.argv[1:]: if match: cmd.append((match.groups(), infile, line_number)) -print "//generated by " + sys.argv[0] + " DO NOT EDIT" +print ("//generated by " + sys.argv[0] + " DO NOT EDIT") for (name, ptr, doc), file_name, line_number in cmd: - print "extern void " + ptr + "(char *); // found in " + file_name + " line: " + str(line_number + 1) + print ("extern void " + ptr + "(char *); // found in " + file_name + " line: " + str(line_number + 1)) -print "\n" +print ("\n") -print "cmd_t cmd[] = {" +print ("cmd_t cmd[] = {") for (name, ptr, doc), file_name, line_number in cmd: - print " {\"" + name + "\", \"" + doc + "\", " + ptr + "}, // found in " + file_name + " line: " + str(line_number + 1) -print "};" + print (" {\"" + name + "\", \"" + doc + "\", " + ptr + "}, // found in " + file_name + " line: " + str(line_number + 1)) +print ("};") diff --git a/tools/create_config.py b/tools/create_config.py index a0b96b67..03506fef 100755 --- a/tools/create_config.py +++ b/tools/create_config.py @@ -1,12 +1,4 @@ -#! /bin/sh -"true" '''\' -if command -v python2.7 > /dev/null; then - exec python2.7 "$0" "$@" -else - exec python "$0" "$@" -fi -exit $? -''' +#!/usr/bin/env python import re import sys import os @@ -17,17 +9,17 @@ for infile in sys.argv[1:]: with open(infile) as f: config.append((os.path.splitext(os.path.basename(infile))[0], f.read())) -print "//generated by " + sys.argv[0] + " DO NOT EDIT\n" -print "#include \"config.h\"\n" -print "const uint32_t num_of_config_templates = " + str(len(config)) + ";\n" +print ("//generated by " + sys.argv[0] + " DO NOT EDIT\n") +print ("#include \"config.h\"\n") +print ("const uint32_t num_of_config_templates = " + str(len(config)) + ";\n") -print "config_template_t config_templates[] = {" +print ("config_template_t config_templates[] = {") for index, (file_name, content) in enumerate(config): - print "{" - print ".name = \"" + file_name + "\"," - print ".config = \"\\" + print ("{") + print (".name = \"" + file_name + "\",") + print (".config = \"\\") for line in content.splitlines(): - print line + "\\n\\" - print "\"\n},\n" -print "};" + print (line + "\\n\\") + print ("\"\n},\n") +print ("};") diff --git a/tools/create_hal_tbl.py b/tools/create_hal_tbl.py index 569010cc..2e79aab6 100755 --- a/tools/create_hal_tbl.py +++ b/tools/create_hal_tbl.py @@ -1,5 +1,4 @@ #!/usr/bin/env python - import re import sys diff --git a/tools/elf.py b/tools/elf.py index 71423295..cb5dd548 100644 --- a/tools/elf.py +++ b/tools/elf.py @@ -77,15 +77,15 @@ class ELFSection: SHT_REL = 9 SHT_SHLIB = 10 SHT_DYNSYM = 11 - SHT_LOPROC = 0x70000000L - SHT_HIPROC = 0x7fffffffL - SHT_LOUSER = 0x80000000L - SHT_HIUSER = 0xffffffffL + SHT_LOPROC = 0x70000000 + SHT_HIPROC = 0x7fffffff + SHT_LOUSER = 0x80000000 + SHT_HIUSER = 0xffffffff #section attribute flags SHF_WRITE = 0x1 SHF_ALLOC = 0x2 SHF_EXECINSTR = 0x4 - SHF_MASKPROC = 0xf0000000L + SHF_MASKPROC = 0xf0000000 def __init__(self): """creat a new empty section object""" @@ -125,8 +125,8 @@ class ELFProgramHeader: PT_NOTE = 4 PT_SHLIB = 5 PT_PHDR = 6 - PT_LOPROC = 0x70000000L - PT_HIPROC = 0x7fffffffL + PT_LOPROC = 0x70000000 + PT_HIPROC = 0x7fffffff #segment flags PF_R = 0x4 #segment is readable @@ -209,14 +209,13 @@ class ELFObject: self.e_ident[self.EI_DATA] != self.ELFDATA2LSB and\ self.e_ident[self.EI_VERSION] != 1: raise ELFException("Not a valid ELF file") - #load programm headers self.programmheaders = [] if self.e_phnum: #load program headers fileobj.seek(self.e_phoff) for sectionnum in range(self.e_phnum): - shdr = (fileobj.read(self.e_phentsize) + '\0'* struct.calcsize(ELFProgramHeader.Elf32_Phdr))[0:struct.calcsize(ELFProgramHeader.Elf32_Phdr)] + shdr = (fileobj.read(self.e_phentsize) + b'\x00' * struct.calcsize(ELFProgramHeader.Elf32_Phdr))[0:struct.calcsize(ELFProgramHeader.Elf32_Phdr)] psection = ELFProgramHeader() psection.fromString(shdr) if psection.p_offset: #skip if section has invalid offset in file @@ -228,12 +227,11 @@ class ELFObject: #~ #pad if needed #~ if phdr.p_filesz < phdr.p_memsz: #~ phdr.data = phdr.data + '\0' * (phdr.p_memsz-phdr.p_filesz) - #load sections self.sections = [] fileobj.seek(self.e_shoff) for sectionnum in range(self.e_shnum): - shdr = (fileobj.read(self.e_shentsize) + '\0'* struct.calcsize(ELFSection.Elf32_Shdr))[0:struct.calcsize(ELFSection.Elf32_Shdr)] + shdr = (fileobj.read(self.e_shentsize) + b'\x00' * struct.calcsize(ELFSection.Elf32_Shdr))[0:struct.calcsize(ELFSection.Elf32_Shdr)] elfsection = ELFSection() elfsection.fromString(shdr) self.sections.append(elfsection) @@ -244,18 +242,18 @@ class ELFObject: data = fileobj.read(section.sh_size) section.data = data if section.sh_type == ELFSection.SHT_STRTAB: - section.values = data.split('\0') + section.values = data.split(b'\x00') section.lma = self.getLMA(section) #get section names for section in self.sections: start = self.sections[self.e_shstrndx].data[section.sh_name:] - section.name = start.split('\0')[0] + section.name = start.split(b'\x00')[0] def getSection(self, name): """get section by name""" for section in self.sections: - if section.name == name: + if section.name.decode() == name: return section def getProgrammableSections(self):