Fix memfs.py to work with Python 2

This commit is contained in:
Baudouin Raoult 2017-07-23 08:08:54 +01:00
parent bdb3321519
commit 4e2cd7e4a7
1 changed files with 9 additions and 4 deletions

View File

@ -13,6 +13,13 @@ print(dirs)
FILES = {} FILES = {}
NAMES = [] NAMES = []
try:
str("",'ascii')
ascii = lambda x: str(x, 'ascii')
except:
ascii = lambda x: str(x)
# The last argument is the generated C file # The last argument is the generated C file
g = open(sys.argv[-1], "w") g = open(sys.argv[-1], "w")
@ -30,7 +37,7 @@ for directory in dirs:
continue continue
fname = full[full.find("/%s/" % (dname,)):] fname = full[full.find("/%s/" % (dname,)):]
#print("MEMFS add", fname) print("MEMFS add", fname)
name = re.sub(r'\W', '_', fname) name = re.sub(r'\W', '_', fname)
assert name not in FILES assert name not in FILES
@ -49,9 +56,7 @@ for directory in dirs:
# Read two characters at a time and convert to C hex # Read two characters at a time and convert to C hex
# e.g. 23 -> 0x23 # e.g. 23 -> 0x23
for n in range(0, len(fcont), 2): for n in range(0, len(fcont), 2):
twoChars = fcont[n:n+2] twoChars = ascii(fcont[n:n+2])
if sys.version_info.major > 2:
twoChars = str(twoChars,'ascii')
print("0x%s," % (twoChars,), end="", file=g) print("0x%s," % (twoChars,), end="", file=g)
i += 1 i += 1
if (i % 20) == 0: if (i % 20) == 0: