From 43331cbeaf3069abfa6ffba6f301a7fb74561363 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Tue, 28 Jul 2015 14:45:18 +0900 Subject: [PATCH] fix for python3 --- ChangeLog | 3 +++ tool/gnuk_token.py | 5 +--- tool/gpg_agent.py | 51 ++++++++++++++++++++------------------- tool/stlinkv2.py | 59 ++++++++++++++++++++++------------------------ 4 files changed, 59 insertions(+), 59 deletions(-) diff --git a/ChangeLog b/ChangeLog index 747a9c0..070805b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2015-07-28 Niibe Yutaka + * tool/stlinkv2.py: Fix for Python3. + * tool/gpg_agent.py: Fix for Python3. + * src/usb-msc.c: Update from Fraucheky. * src/usb_stm32f103.c (struct DATA_INFO): Remove offset. diff --git a/tool/gnuk_token.py b/tool/gnuk_token.py index 1560b19..f46c5c1 100644 --- a/tool/gnuk_token.py +++ b/tool/gnuk_token.py @@ -68,10 +68,7 @@ class gnuk_token(object): if interface.interfaceSubClass != CCID_SUBCLASS: raise ValueError("Wrong interface sub class") self.__devhandle = device.open() - try: - self.__devhandle.setConfiguration(configuration) - except: - pass + self.__devhandle.setConfiguration(configuration.value) self.__devhandle.claimInterface(interface) self.__devhandle.setAltInterface(0) diff --git a/tool/gpg_agent.py b/tool/gpg_agent.py index 6d58e5d..4e46ae5 100644 --- a/tool/gpg_agent.py +++ b/tool/gpg_agent.py @@ -1,7 +1,7 @@ """ gpg_agent.py - a library to connect gpg-agent -Copyright (C) 2013 Free Software Initiative of Japan +Copyright (C) 2013, 2015 Free Software Initiative of Japan Author: NIIBE Yutaka This file is a part of Gnuk, a GnuPG USB Token implementation. @@ -34,7 +34,7 @@ class gpg_agent(object): comm_port = os.path.join(home, "gnupg", "S.gpg-agent") # f = open(comm_port, "rb", 0) - infostr = f.read() + infostr = f.read().decode('UTF-8') f.close() # info = infostr.split('\n', 1) @@ -50,17 +50,17 @@ class gpg_agent(object): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(path) self.sock = s - self.buf_remained = "" + self.buf_remained = b"" self.response = None def read_line(self): - line = "" - if self.buf_remained != "": + line = b"" + if self.buf_remained != b"": chunk = self.buf_remained else: chunk = self.sock.recv(BUFLEN) while True: - pos = chunk.find('\n') + pos = chunk.find(b'\n') if pos >= 0: self.buf_remained = chunk[pos+1:] line = line + chunk[0:pos] @@ -71,37 +71,40 @@ class gpg_agent(object): def get_response(self): r = self.response - result = "" + result = b"" while True: - i = r.find('%') + i = r.find(b'%') if i < 0: result += r break - hex_str = r[i+1:i+3] + hex_str = r[i+1:i+3].decode('UTF-8') result += r[0:i] - result += chr(int(hex_str,16)) + if bytes == str: + result += chr(int(hex_str,16)) + else: + result += bytes.fromhex(hex_str) r = r[i+3:] return result def send_command(self, cmd): - self.sock.send(cmd) - self.response = "" + self.sock.send(cmd.encode('UTF-8')) + self.response = b"" while True: while True: l = self.read_line() - if l[0] != '#': + if l[0] != b'#'[0]: break - if l[0] == 'D': + if l[0] == b'D'[0]: self.response += l[2:] - elif l[0] == 'O' and l[1] == 'K': + elif l[0:2] == b'OK': return True - elif l[0] == 'E' and l[1] == 'R' and l[2] == 'R': + elif l[0:3] == b'ERR': return False else: # XXX: S, INQUIRE, END return False def close(self): - self.sock.send('BYE\n') + self.sock.send(b'BYE\n') bye = self.read_line() self.sock.close() return bye # "OK closing connection" @@ -109,17 +112,17 @@ class gpg_agent(object): # Test if __name__ == '__main__': g = gpg_agent() - print g.read_line() - print g.send_command("KEYINFO --list --data\n") - kl_str = g.get_response() + print(g.read_line().decode('UTF-8')) + print(g.send_command("KEYINFO --list --data\n")) + kl_str = g.get_response().decode('UTF-8') kl_str = kl_str[0:-1] kl = kl_str.split('\n') import re kl_o3 = [kg for kg in kl if re.search("OPENPGP\\.3", kg)] - print kl_o3 + print(kl_o3) kg = kl_o3[0].split(' ')[0] - print g.send_command("READKEY %s\n" % kg) + print(g.send_command("READKEY %s\n" % kg)) r = g.get_response() import binascii - print binascii.hexlify(r) - print g.close() + print(binascii.hexlify(r).decode('UTF-8')) + print(g.close().decode('UTF-8')) diff --git a/tool/stlinkv2.py b/tool/stlinkv2.py index 36d7038..0316bf5 100755 --- a/tool/stlinkv2.py +++ b/tool/stlinkv2.py @@ -120,20 +120,20 @@ BLOCK_WRITE_TIMEOUT=80 # Increase this when you increase BLOCK_SIZE class TimeOutError(Exception): - def __init__(self, msg): - self.msg = msg - def __str__(self): - return repr(self.msg) - def __repr__(self): - return "TimeoutError(" + self.msg + ")" + def __init__(self, msg): + self.msg = msg + def __str__(self): + return repr(self.msg) + def __repr__(self): + return "TimeoutError(" + self.msg + ")" class OperationFailure(Exception): - def __init__(self, msg): - self.msg = msg - def __str__(self): - return repr(self.msg) - def __repr__(self): - return "OperationFailure(" + self.msg + ")" + def __init__(self, msg): + self.msg = msg + def __str__(self): + return repr(self.msg) + def __repr__(self): + return "OperationFailure(" + self.msg + ")" class stlinkv2(object): @@ -148,10 +148,7 @@ class stlinkv2(object): if intf.interfaceClass != 0xff: # Vendor specific raise ValueError("Wrong interface class.", intf.interfaceClass) self.__devhandle = dev.open() - try: - self.__devhandle.setConfiguration(conf) - except: - pass + self.__devhandle.setConfiguration(conf.value) self.__devhandle.claimInterface(intf) # self.__devhandle.setAltInterface(0) # This was not good for libusb-win32 with wrong arg intf, new correct value 0 would be OK @@ -339,7 +336,7 @@ class stlinkv2(object): def option_bytes_write(self,addr,val): self.write_memory_u32(FLASH_KEYR, FLASH_KEY1) self.write_memory_u32(FLASH_KEYR, FLASH_KEY2) - self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) + self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY1) self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY2) @@ -363,13 +360,13 @@ class stlinkv2(object): def option_bytes_erase(self): self.write_memory_u32(FLASH_KEYR, FLASH_KEY1) self.write_memory_u32(FLASH_KEYR, FLASH_KEY2) - self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) + self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY1) self.write_memory_u32(FLASH_OPTKEYR, FLASH_KEY2) - self.write_memory_u32(FLASH_CR, FLASH_CR_OPTER) - self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_OPTER) + self.write_memory_u32(FLASH_CR, FLASH_CR_OPTER) + self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_OPTER) i = 0 while True: @@ -381,7 +378,7 @@ class stlinkv2(object): break self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK) - if (status & FLASH_SR_EOP) == 0: + if (status & FLASH_SR_EOP) == 0: raise OperationFailure("option bytes erase") def flash_write_internal(self, addr, data, off, size): @@ -404,7 +401,7 @@ class stlinkv2(object): def flash_write(self, addr, data): self.write_memory_u32(FLASH_KEYR, FLASH_KEY1) self.write_memory_u32(FLASH_KEYR, FLASH_KEY2) - self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) + self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) off = 0 while True: @@ -423,10 +420,10 @@ class stlinkv2(object): def flash_erase_all(self): self.write_memory_u32(FLASH_KEYR, FLASH_KEY1) self.write_memory_u32(FLASH_KEYR, FLASH_KEY2) - self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) + self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) - self.write_memory_u32(FLASH_CR, FLASH_CR_MER) - self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_MER) + self.write_memory_u32(FLASH_CR, FLASH_CR_MER) + self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_MER) i = 0 while True: @@ -440,18 +437,18 @@ class stlinkv2(object): self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK) - if (status & FLASH_SR_EOP) == 0: + if (status & FLASH_SR_EOP) == 0: raise OperationFailure("flash erase all") def flash_erase_page(self, addr): self.write_memory_u32(FLASH_KEYR, FLASH_KEY1) self.write_memory_u32(FLASH_KEYR, FLASH_KEY2) - self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) + self.write_memory_u32(FLASH_SR, FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_PGERR) - self.write_memory_u32(FLASH_CR, FLASH_CR_PER) - self.write_memory_u32(FLASH_AR, addr) - self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_PER) + self.write_memory_u32(FLASH_CR, FLASH_CR_PER) + self.write_memory_u32(FLASH_AR, addr) + self.write_memory_u32(FLASH_CR, FLASH_CR_STRT | FLASH_CR_PER) i = 0 while True: @@ -464,7 +461,7 @@ class stlinkv2(object): self.write_memory_u32(FLASH_CR, FLASH_CR_LOCK) - if (status & FLASH_SR_EOP) == 0: + if (status & FLASH_SR_EOP) == 0: raise OperationFailure("flash page erase") def start(self):