python3 compatibility

This commit is contained in:
Rene Hopf
2018-04-07 02:00:04 +02:00
parent dd37e720d5
commit eeba6f447e
12 changed files with 51 additions and 87 deletions

View File

@@ -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 # Compiler flags to generate dependency files
# #
@@ -263,9 +263,9 @@ src/conf_templates.c: tbl
#generate hal and command tables #generate hal and command tables
tbl: tbl:
@echo Generating tables @echo Generating tables
@tools/create_hal_tbl.py . $(COMPS) @$(PYTHON) tools/create_hal_tbl.py . $(COMPS)
@tools/create_config.py conf/template/* > src/conf_templates.c @$(PYTHON) tools/create_config.py conf/template/* > src/conf_templates.c
@tools/create_cmd.py $(SOURCES) > inc/commandslist.h @$(PYTHON) tools/create_cmd.py $(SOURCES) > inc/commandslist.h
boot: boot:
$(MAKE) -f bootloader/Makefile $(MAKE) -f bootloader/Makefile
@@ -329,7 +329,7 @@ showsize: build
# Flash the device # Flash the device
# #
btburn: build showsize $(TARGET).dfu btburn: build showsize $(TARGET).dfu
@tools/bootloader.py @$(PYTHON) tools/bootloader.py
@sleep 1 @sleep 1
@dfu-util -d 0483:df11 -a 0 -s 0x08010000:leave -D $(TARGET).dfu @dfu-util -d 0483:df11 -a 0 -s 0x08010000:leave -D $(TARGET).dfu

View File

@@ -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 # Compiler flags to generate dependency files
# #

View File

@@ -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 # Compiler flags to generate dependency files
# #
@@ -173,8 +173,6 @@ showsize: build
# Flash the device # Flash the device
# #
btburn: build showsize $(TARGET).dfu btburn: build showsize $(TARGET).dfu
@tools/bootloader.py
@sleep 1
@dfu-util -d 0483:df11 -a 0 -s 0x8000000:leave -D $(TARGET).dfu @dfu-util -d 0483:df11 -a 0 -s 0x8000000:leave -D $(TARGET).dfu
flash: $(TARGET).bin flash: $(TARGET).bin

View File

@@ -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 # Compiler flags to generate dependency files
# #
@@ -196,9 +196,9 @@ stm32f303/src/hal_tbl.c: tbl
tbl: tbl:
@echo Generating tables @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 #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: gccversion:
@$(CC) --version @$(CC) --version
@@ -212,7 +212,7 @@ showsize: build
# Flash the device # Flash the device
# #
btburn: build showsize $(TARGET).dfu btburn: build showsize $(TARGET).dfu
@tools/bootloader.py @$(PYTHON) tools/bootloader.py
@sleep 1 @sleep 1
@dfu-util -d 0483:df11 -a 0 -s 0x8004000:leave -D $(TARGET).dfu @dfu-util -d 0483:df11 -a 0 -s 0x8004000:leave -D $(TARGET).dfu

View File

@@ -1,4 +1,5 @@
# Define programs and commands for the toolchain # Define programs and commands for the toolchain
PYTHON = python
TOOLCHAIN = arm-none-eabi- TOOLCHAIN = arm-none-eabi-
CC = $(TOOLCHAIN)gcc CC = $(TOOLCHAIN)gcc
OBJCOPY = $(TOOLCHAIN)objcopy OBJCOPY = $(TOOLCHAIN)objcopy

View File

@@ -1,12 +1,4 @@
#! /bin/sh #!/usr/bin/env python
"true" '''\'
if command -v python2.7 > /dev/null; then
exec python2.7 "$0" "$@"
else
exec python "$0" "$@"
fi
exit $?
'''
# #
# Add CRC checksum and version information to an ELF file # Add CRC checksum and version information to an ELF file
# Copyright (C)2015 Thomas Kindler <mail@t-kindler.de> # Copyright (C)2015 Thomas Kindler <mail@t-kindler.de>
@@ -28,7 +20,7 @@ import os, struct
import argparse, subprocess import argparse, subprocess
import getpass, platform import getpass, platform
from StringIO import StringIO from io import BytesIO
from datetime import datetime from datetime import datetime
from crc32 import CRC32 from crc32 import CRC32
@@ -71,8 +63,8 @@ class VersionInfo:
self.image_crc, self.image_size, self.image_crc, self.image_size,
self.git_version, self.git_version,
self.git_branch, self.git_branch,
self.build_user, self.build_host, self.build_user.encode(), self.build_host.encode(),
self.build_date, self.build_time 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 += b'\xFF' * gap
bin_data += elf_data[s.sh_offset : s.sh_offset + s.sh_size] 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): def patch_section(elf, elf_data, section, data):
@@ -146,8 +138,8 @@ def patch_elf():
elf_data = bytearray(file.read()) elf_data = bytearray(file.read())
elf = ELFObject() elf = ELFObject()
elf.fromFile(StringIO(elf_data))
elf.fromFile(BytesIO(elf_data))
for s in elf.getSections(): for s in elf.getSections():
dprint(" %-16s: 0x%08x -> 0x%08x %8d" % dprint(" %-16s: 0x%08x -> 0x%08x %8d" %
(s.name, s.lma, s.sh_addr, s.sh_size) (s.name, s.lma, s.sh_addr, s.sh_size)

View File

@@ -1,12 +1,4 @@
#! /bin/sh #!/usr/bin/env python
"true" '''\'
if command -v python2.7 > /dev/null; then
exec python2.7 "$0" "$@"
else
exec python "$0" "$@"
fi
exit $?
'''
import serial.tools.list_ports import serial.tools.list_ports
done = False done = False
@@ -15,7 +7,7 @@ for port in serial.tools.list_ports.comports():
print ("Reseting stmbl at " + port[2]) print ("Reseting stmbl at " + port[2])
try: try:
stmbl = serial.Serial(port[0]) stmbl = serial.Serial(port[0])
stmbl.write('bootloader\n') stmbl.write('bootloader\n'.encode())
stmbl.close() stmbl.close()
done = True done = True
except OSError: except OSError:

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/env python
# #
# Copyright 2013 StalkR <github-misc@stalkr.net> # Copyright 2013 StalkR <github-misc@stalkr.net>
# #

View File

@@ -1,12 +1,4 @@
#! /bin/sh #!/usr/bin/env python
"true" '''\'
if command -v python2.7 > /dev/null; then
exec python2.7 "$0" "$@"
else
exec python "$0" "$@"
fi
exit $?
'''
import re import re
import sys import sys
@@ -19,14 +11,14 @@ for infile in sys.argv[1:]:
if match: if match:
cmd.append((match.groups(), infile, line_number)) 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: 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: for (name, ptr, doc), file_name, line_number in cmd:
print " {\"" + name + "\", \"" + doc + "\", " + ptr + "}, // found in " + file_name + " line: " + str(line_number + 1) print (" {\"" + name + "\", \"" + doc + "\", " + ptr + "}, // found in " + file_name + " line: " + str(line_number + 1))
print "};" print ("};")

View File

@@ -1,12 +1,4 @@
#! /bin/sh #!/usr/bin/env python
"true" '''\'
if command -v python2.7 > /dev/null; then
exec python2.7 "$0" "$@"
else
exec python "$0" "$@"
fi
exit $?
'''
import re import re
import sys import sys
import os import os
@@ -17,17 +9,17 @@ for infile in sys.argv[1:]:
with open(infile) as f: with open(infile) as f:
config.append((os.path.splitext(os.path.basename(infile))[0], f.read())) config.append((os.path.splitext(os.path.basename(infile))[0], f.read()))
print "//generated by " + sys.argv[0] + " DO NOT EDIT\n" print ("//generated by " + sys.argv[0] + " DO NOT EDIT\n")
print "#include \"config.h\"\n" print ("#include \"config.h\"\n")
print "const uint32_t num_of_config_templates = " + str(len(config)) + ";\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): for index, (file_name, content) in enumerate(config):
print "{" print ("{")
print ".name = \"" + file_name + "\"," print (".name = \"" + file_name + "\",")
print ".config = \"\\" print (".config = \"\\")
for line in content.splitlines(): for line in content.splitlines():
print line + "\\n\\" print (line + "\\n\\")
print "\"\n},\n" print ("\"\n},\n")
print "};" print ("};")

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python
import re import re
import sys import sys

View File

@@ -77,15 +77,15 @@ class ELFSection:
SHT_REL = 9 SHT_REL = 9
SHT_SHLIB = 10 SHT_SHLIB = 10
SHT_DYNSYM = 11 SHT_DYNSYM = 11
SHT_LOPROC = 0x70000000L SHT_LOPROC = 0x70000000
SHT_HIPROC = 0x7fffffffL SHT_HIPROC = 0x7fffffff
SHT_LOUSER = 0x80000000L SHT_LOUSER = 0x80000000
SHT_HIUSER = 0xffffffffL SHT_HIUSER = 0xffffffff
#section attribute flags #section attribute flags
SHF_WRITE = 0x1 SHF_WRITE = 0x1
SHF_ALLOC = 0x2 SHF_ALLOC = 0x2
SHF_EXECINSTR = 0x4 SHF_EXECINSTR = 0x4
SHF_MASKPROC = 0xf0000000L SHF_MASKPROC = 0xf0000000
def __init__(self): def __init__(self):
"""creat a new empty section object""" """creat a new empty section object"""
@@ -125,8 +125,8 @@ class ELFProgramHeader:
PT_NOTE = 4 PT_NOTE = 4
PT_SHLIB = 5 PT_SHLIB = 5
PT_PHDR = 6 PT_PHDR = 6
PT_LOPROC = 0x70000000L PT_LOPROC = 0x70000000
PT_HIPROC = 0x7fffffffL PT_HIPROC = 0x7fffffff
#segment flags #segment flags
PF_R = 0x4 #segment is readable 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_DATA] != self.ELFDATA2LSB and\
self.e_ident[self.EI_VERSION] != 1: self.e_ident[self.EI_VERSION] != 1:
raise ELFException("Not a valid ELF file") raise ELFException("Not a valid ELF file")
#load programm headers #load programm headers
self.programmheaders = [] self.programmheaders = []
if self.e_phnum: if self.e_phnum:
#load program headers #load program headers
fileobj.seek(self.e_phoff) fileobj.seek(self.e_phoff)
for sectionnum in range(self.e_phnum): 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 = ELFProgramHeader()
psection.fromString(shdr) psection.fromString(shdr)
if psection.p_offset: #skip if section has invalid offset in file if psection.p_offset: #skip if section has invalid offset in file
@@ -228,12 +227,11 @@ class ELFObject:
#~ #pad if needed #~ #pad if needed
#~ if phdr.p_filesz < phdr.p_memsz: #~ if phdr.p_filesz < phdr.p_memsz:
#~ phdr.data = phdr.data + '\0' * (phdr.p_memsz-phdr.p_filesz) #~ phdr.data = phdr.data + '\0' * (phdr.p_memsz-phdr.p_filesz)
#load sections #load sections
self.sections = [] self.sections = []
fileobj.seek(self.e_shoff) fileobj.seek(self.e_shoff)
for sectionnum in range(self.e_shnum): 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 = ELFSection()
elfsection.fromString(shdr) elfsection.fromString(shdr)
self.sections.append(elfsection) self.sections.append(elfsection)
@@ -244,18 +242,18 @@ class ELFObject:
data = fileobj.read(section.sh_size) data = fileobj.read(section.sh_size)
section.data = data section.data = data
if section.sh_type == ELFSection.SHT_STRTAB: if section.sh_type == ELFSection.SHT_STRTAB:
section.values = data.split('\0') section.values = data.split(b'\x00')
section.lma = self.getLMA(section) section.lma = self.getLMA(section)
#get section names #get section names
for section in self.sections: for section in self.sections:
start = self.sections[self.e_shstrndx].data[section.sh_name:] 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): def getSection(self, name):
"""get section by name""" """get section by name"""
for section in self.sections: for section in self.sections:
if section.name == name: if section.name.decode() == name:
return section return section
def getProgrammableSections(self): def getProgrammableSections(self):