Skip to content

Commit 76bd981

Browse files
authored
Merge pull request #4567 from enkiller/0405
[tools] Python 3 compatibility support
2 parents 2cf49c5 + 510955b commit 76bd981

File tree

12 files changed

+135
-56
lines changed

12 files changed

+135
-56
lines changed

tools/auto-ci.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
BSP_ROOT = '../bsp'
5454

55-
for bsp,cpu in bsp_to_cpu.iteritems():
55+
for bsp,cpu in bsp_to_cpu.items():
5656
project_dir = os.path.join(BSP_ROOT, bsp)
5757
if os.getenv('RTT_CPU') == cpu and os.path.isfile(os.path.join(project_dir, 'SConstruct')):
5858
if os.system('scons --directory=' + project_dir) != 0:
@@ -63,10 +63,10 @@
6363
else:
6464
results['ignore'].append(bsp)
6565

66-
for result,bsp_list in results.iteritems():
67-
print "## {0}: {1}\n".format(result, len(bsp_list))
66+
for result,bsp_list in results.items():
67+
print("## {0}: {1}\n".format(result, len(bsp_list)))
6868
for bsp in bsp_list:
69-
print "* " + bsp
69+
print("* " + bsp)
7070

7171
if fail:
7272
sys.exit(1)

tools/buildbot.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import sys
33

44
def usage():
5-
print '%s all -- build all bsp' % os.path.basename(sys.argv[0])
6-
print '%s clean -- clean all bsp' % os.path.basename(sys.argv[0])
7-
print '%s project -- update all prject files' % os.path.basename(sys.argv[0])
5+
print('%s all -- build all bsp' % os.path.basename(sys.argv[0]))
6+
print('%s clean -- clean all bsp' % os.path.basename(sys.argv[0]))
7+
print('%s project -- update all prject files' % os.path.basename(sys.argv[0]))
88

99
BSP_ROOT = os.path.join("..", "bsp")
1010

@@ -25,25 +25,25 @@ def usage():
2525
project_dir = os.path.join(BSP_ROOT, item)
2626

2727
if os.path.isfile(os.path.join(project_dir, 'template.Uv2')):
28-
print ('prepare MDK3 project file on ' + project_dir)
28+
print('prepare MDK3 project file on ' + project_dir)
2929
command = ' --target=mdk -s'
3030

3131
os.system('scons --directory=' + project_dir + command)
3232

3333
if os.path.isfile(os.path.join(project_dir, 'template.uvproj')):
34-
print ('prepare MDK4 project file on ' + project_dir)
34+
print('prepare MDK4 project file on ' + project_dir)
3535
command = ' --target=mdk4 -s'
3636

3737
os.system('scons --directory=' + project_dir + command)
3838

3939
if os.path.isfile(os.path.join(project_dir, 'template.uvprojx')):
40-
print ('prepare MDK5 project file on ' + project_dir)
40+
print('prepare MDK5 project file on ' + project_dir)
4141
command = ' --target=mdk5 -s'
4242

4343
os.system('scons --directory=' + project_dir + command)
4444

4545
if os.path.isfile(os.path.join(project_dir, 'template.ewp')):
46-
print ('prepare IAR project file on ' + project_dir)
46+
print('prepare IAR project file on ' + project_dir)
4747
command = ' --target=iar -s'
4848

4949
os.system('scons --directory=' + project_dir + command)
@@ -58,5 +58,5 @@ def usage():
5858
project_dir = os.path.join(BSP_ROOT, item)
5959
if os.path.isfile(os.path.join(project_dir, 'SConstruct')):
6060
if os.system('scons --directory=' + project_dir + command) != 0:
61-
print 'build failed!!'
61+
print('build failed!!')
6262
break

tools/building.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def spawn(self, sh, escape, cmd, args, env):
8585
try:
8686
os.remove(f)
8787
except Exception as e:
88-
print ('Error removing file: ' + e)
88+
print('Error removing file: ' + e)
8989
return -1
9090
return 0
9191

@@ -106,8 +106,8 @@ def spawn(self, sh, escape, cmd, args, env):
106106
try:
107107
proc = subprocess.Popen(cmdline, env=_e, shell=False)
108108
except Exception as e:
109-
print ('Error in calling command:' + cmdline.split(' ')[0])
110-
print ('Exception: ' + os.strerror(e.errno))
109+
print('Error in calling command:' + cmdline.split(' ')[0])
110+
print('Exception: ' + os.strerror(e.errno))
111111
if (os.strerror(e.errno) == "No such file or directory"):
112112
print ("\nPlease check Toolchains PATH setting.\n")
113113

@@ -274,7 +274,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
274274
os.environ['RTT_CC'] = rtconfig.CROSS_TOOL
275275
utils.ReloadModule(rtconfig)
276276
except KeyError:
277-
print ('Unknow target: '+ tgt_name+'. Avaible targets: ' +', '.join(tgt_dict.keys()))
277+
print('Unknow target: '+ tgt_name+'. Avaible targets: ' +', '.join(tgt_dict.keys()))
278278
sys.exit(1)
279279

280280
# auto change the 'RTT_EXEC_PATH' when 'rtconfig.EXEC_PATH' get failed
@@ -662,7 +662,7 @@ def DefineGroup(name, src, depend, **parameters):
662662
# check whether to clean up library
663663
if GetOption('cleanlib') and os.path.exists(os.path.join(group['path'], GroupLibFullName(name, Env))):
664664
if group['src'] != []:
665-
print ('Remove library:'+ GroupLibFullName(name, Env))
665+
print('Remove library:'+ GroupLibFullName(name, Env))
666666
fn = os.path.join(group['path'], GroupLibFullName(name, Env))
667667
if os.path.exists(fn):
668668
os.unlink(fn)
@@ -735,7 +735,7 @@ def BuildLibInstallAction(target, source, env):
735735
if Group['name'] == lib_name:
736736
lib_name = GroupLibFullName(Group['name'], env)
737737
dst_name = os.path.join(Group['path'], lib_name)
738-
print ('Copy '+lib_name+' => ' +dst_name)
738+
print('Copy '+lib_name+' => ' + dst_name)
739739
do_copy_file(lib_name, dst_name)
740740
break
741741

@@ -996,11 +996,11 @@ def GetVersion():
996996
prepcessor.process_contents(contents)
997997
def_ns = prepcessor.cpp_namespace
998998

999-
version = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_VERSION']))
1000-
subversion = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_SUBVERSION']))
999+
version = int([ch for ch in def_ns['RT_VERSION'] if ch in '0123456789.'])
1000+
subversion = int([ch for ch in def_ns['RT_SUBVERSION'] if ch in '0123456789.'])
10011001

10021002
if 'RT_REVISION' in def_ns:
1003-
revision = int(filter(lambda ch: ch in '0123456789.', def_ns['RT_REVISION']))
1003+
revision = int([ch for ch in def_ns['RT_REVISION'] if ch in '0123456789.'])
10041004
return '%d.%d.%d' % (version, subversion, revision)
10051005

10061006
return '0.%d.%d' % (version, subversion)

tools/clang-analyze.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def generate(env):
5353

5454
if rtconfig.EXEC_PATH:
5555
if not os.path.exists(rtconfig.EXEC_PATH):
56-
print
57-
print 'warning: rtconfig.EXEC_PATH(%s) does not exists.' % rtconfig.EXEC_PATH
58-
print
56+
print()
57+
print('warning: rtconfig.EXEC_PATH(%s) does not exists.' % rtconfig.EXEC_PATH)
58+
print()
5959
return
6060
env.AppendENVPath('PATH', rtconfig.EXEC_PATH)
6161

tools/eclipse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def HandleToolOption(tools, env, project, reset):
212212
linker_nostart_option = option
213213
elif option.get('id').find('linker.libs') != -1:
214214
linker_libs_option = option
215-
elif option.get('id').find('linker.paths') != -1 and env.has_key('LIBPATH'):
215+
elif option.get('id').find('linker.paths') != -1 and 'LIBPATH' in env:
216216
linker_paths_option = option
217217
elif option.get('id').find('linker.usenewlibnano') != -1:
218218
linker_newlib_nano_option = option
@@ -317,7 +317,7 @@ def HandleToolOption(tools, env, project, reset):
317317
option.remove(item)
318318

319319
# add new libs
320-
if env.has_key('LIBS'):
320+
if 'LIBS' in env:
321321
for lib in env['LIBS']:
322322
formatedLib = ConverToRttEclipseLibFormat(lib)
323323
SubElement(option, 'listOptionValue', {

tools/keil.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,18 @@ def MDK4AddLibToGroup(ProjectFiles, group, name, filename, project_path):
122122
if ProjectFiles.count(obj_name):
123123
name = basename + '_' + name
124124
ProjectFiles.append(obj_name)
125-
file_name.text = name.decode(fs_encoding)
125+
try:
126+
file_name.text = name.decode(fs_encoding)
127+
except:
128+
file_name.text = name
126129
file_type = SubElement(file, 'FileType')
127130
file_type.text = '%d' % _get_filetype(name)
128131
file_path = SubElement(file, 'FilePath')
129132

130-
file_path.text = path.decode(fs_encoding)
133+
try:
134+
file_path.text = path.decode(fs_encoding)
135+
except:
136+
file_path.text = path
131137

132138
return group
133139

tools/mkromfs.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import struct
77
from collections import namedtuple
8-
import StringIO
8+
import io
99

1010
import argparse
1111
parser = argparse.ArgumentParser()
@@ -43,8 +43,10 @@ def c_data(self, prefix=''):
4343

4444
if self.entry_size == 0:
4545
return ''
46-
47-
return head + ','.join(('0x%02x' % ord(i) for i in self._data)) + tail
46+
if len(self._data) > 0 and type(self._data[0]) == int:
47+
return head + ','.join(('0x%02x' % i for i in self._data)) + tail
48+
else:
49+
return head + ','.join(('0x%02x' % ord(i) for i in self._data)) + tail
4850

4951
@property
5052
def entry_size(self):
@@ -104,7 +106,8 @@ def _sort(x, y):
104106
return 1
105107
else:
106108
return -1
107-
self._children.sort(cmp=_sort)
109+
from functools import cmp_to_key
110+
self._children.sort(key=cmp_to_key(_sort))
108111

109112
# sort recursively
110113
for c in self._children:
@@ -255,7 +258,7 @@ def get_bin_data(tree, base_addr):
255258
if args.binary:
256259
data = get_bin_data(tree, int(args.addr, 16))
257260
else:
258-
data = get_c_data(tree)
261+
data = get_c_data(tree).encode()
259262

260263
output = args.output
261264
if not output:

tools/ses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ def SESProject(env) :
5959
group_tree = SDKAddGroup(project_node, group['name'], group['src'], project_path)
6060

6161
# get each group's cc flags
62-
if group.has_key('CCFLAGS') and group['CCFLAGS']:
62+
if 'CCFLAGS' in group and group['CCFLAGS']:
6363
if CCFLAGS:
6464
CCFLAGS += ' ' + group['CCFLAGS']
6565
else:
6666
CCFLAGS += group['CCFLAGS']
6767

6868
# get each group's link flags
69-
if group.has_key('LINKFLAGS') and group['LINKFLAGS']:
69+
if 'LINKFLAGS' in group and group['LINKFLAGS']:
7070
if LINKFLAGS:
7171
LINKFLAGS += ' ' + group['LINKFLAGS']
7272
else:

tools/ua.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def PrepareUA(project, RTT_ROOT, BSP_ROOT):
4545
# ua.write('import sys\n')
4646
ua.write('\n')
4747

48-
print RTT_ROOT
48+
print(RTT_ROOT)
4949

5050
CPPPATH = []
5151
CPPDEFINES = []

tools/vs.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ def VS_AddGroup(ProjectFiles, parent, name, files, libs, project_path):
4545

4646
path = _make_path_relative(project_path, path)
4747
path = os.path.join(path, name)
48-
48+
try:
49+
path = path.decode(fs_encoding)
50+
except:
51+
path = path
4952
File = SubElement(Filter, 'File')
50-
File.set('RelativePath', path.decode(fs_encoding))
53+
File.set('RelativePath', path)
5154

5255
for lib in libs:
5356
name = os.path.basename(lib)
@@ -57,7 +60,11 @@ def VS_AddGroup(ProjectFiles, parent, name, files, libs, project_path):
5760
path = os.path.join(path, name)
5861

5962
File = SubElement(Filter, 'File')
60-
File.set('RelativePath', path.decode(fs_encoding))
63+
try:
64+
path = path.decode(fs_encoding)
65+
except:
66+
path = path
67+
File.set('RelativePath', path)
6168

6269
def VS_AddHeadFilesGroup(program, elem, project_path):
6370
utils.source_ext = []
@@ -70,7 +77,11 @@ def VS_AddHeadFilesGroup(program, elem, project_path):
7077
for f in utils.source_list:
7178
path = _make_path_relative(project_path, f)
7279
File = SubElement(elem, 'File')
73-
File.set('RelativePath', path.decode(fs_encoding))
80+
try:
81+
path = path.decode(fs_encoding)
82+
except:
83+
path = path
84+
File.set('RelativePath', path)
7485

7586
def VSProject(target, script, program):
7687
project_path = os.path.dirname(os.path.abspath(target))
@@ -158,12 +169,17 @@ def VSProject(target, script, program):
158169
for path in lib_path:
159170
inc = _make_path_relative(project_path, os.path.normpath(path))
160171
paths.add(inc) #.replace('\\', '/')
161-
172+
162173
paths = [i for i in paths]
163174
paths.sort()
164175
lib_paths = ';'.join(paths)
165176
elem.set('AdditionalLibraryDirectories', lib_paths)
166177

167178
xml_indent(root)
168-
out.write(etree.tostring(root, encoding='utf-8'))
179+
text = etree.tostring(root, encoding='utf-8')
180+
try:
181+
text = text.decode(encoding="utf-8")
182+
except:
183+
text = text
184+
out.write(text)
169185
out.close()

0 commit comments

Comments
 (0)