aboutsummaryrefslogtreecommitdiff
path: root/src/device_trezor/trezor/tools
diff options
context:
space:
mode:
authorDusan Klinec <dusan.klinec@gmail.com>2018-11-05 00:38:58 +0100
committerDusan Klinec <dusan.klinec@gmail.com>2018-11-25 11:57:19 +0100
commit2ffe53d9e668c1993fc6f1cbcd7d74c895a5fbb7 (patch)
tree6d6c2fb1a32e5a489c7892756b61ac9fc764cf69 /src/device_trezor/trezor/tools
parentMerge pull request #4781 (diff)
downloadmonero-2ffe53d9e668c1993fc6f1cbcd7d74c895a5fbb7.tar.xz
device/trezor: webusb transport added, cmake fixes
- webusb transport based on libusb added. Provides direct access to Trezor via USB, no need for Trezor bridge. - trezor protocol message handler improved, no recursion used. Ready for upcoming integration tests. - libusb (for docker) bumped from v1.0.9 to v1.0.22, newer version required for webusb transport, for device enumeration. - cmake improvements and fixes. Cmake Trezor checks are moved to a dedicated CheckTrezor.cmake file. In case of a problem Trezor is excluded from build. - ifdefs made consistent to Ledger. - UDP Transport enumeration disabled by default in release mode
Diffstat (limited to 'src/device_trezor/trezor/tools')
-rw-r--r--src/device_trezor/trezor/tools/README.md25
-rw-r--r--src/device_trezor/trezor/tools/pb2cpp.py61
2 files changed, 54 insertions, 32 deletions
diff --git a/src/device_trezor/trezor/tools/README.md b/src/device_trezor/trezor/tools/README.md
index 91a8fb3f0..b176017ac 100644
--- a/src/device_trezor/trezor/tools/README.md
+++ b/src/device_trezor/trezor/tools/README.md
@@ -2,33 +2,28 @@
## Messages rebuild
-Install `protoc` for your distribution.
+Install `protoc` for your distribution. Requirements:
- `protobuf-compiler`
- `libprotobuf-dev`
-- `libprotoc-dev`
-- `python-protobuf`
+- `python`
-Python 3 is required. If you don't have python 3 quite an easy way is
-to use [pyenv].
-It is also advised to create own python virtual environment so dependencies
-are installed in this project-related virtual environment.
+Soft requirement: Python 3, can be easily installed with [pyenv].
-```bash
-python -m venv /
-```
+### Python 2
-Make sure your python has `protobuf` package installed
+Workaround if there is no Python3 available:
```bash
-pip install protobuf
+pip install backports.tempfile
```
-Regenerate messages:
+### Regenerate messages
-```
-./venv/bin/python3 src/device_trezor/trezor/tools/build_protob.py
+```bash
+cd src/device_trezor/trezor
+python tools/build_protob.py
```
The messages regeneration is done also automatically via cmake.
diff --git a/src/device_trezor/trezor/tools/pb2cpp.py b/src/device_trezor/trezor/tools/pb2cpp.py
index eaa8a90ed..4d7cc775f 100644
--- a/src/device_trezor/trezor/tools/pb2cpp.py
+++ b/src/device_trezor/trezor/tools/pb2cpp.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python
# Converts Google's protobuf python definitions of TREZOR wire messages
# to plain-python objects as used in TREZOR Core and python-trezor
@@ -8,11 +8,19 @@ import os
import re
import shutil
import subprocess
-import sys
import glob
-import tempfile
import hashlib
+try:
+ from tempfile import TemporaryDirectory
+except:
+ # Py2 backward compatibility, optionally installed by user
+ # pip install backports.tempfile
+ try:
+ from backports.tempfile import TemporaryDirectory
+ except:
+ raise EnvironmentError('TemporaryDirectory could not be imported. Try: pip install backports.tempfile')
+
AUTO_HEADER = "# Automatically generated by pb2cpp\n"
@@ -23,6 +31,9 @@ UNDEF_STATEMENT = """
#endif
"""
+PROTOC = None
+PROTOC_INCLUDE = None
+
def which(pgm):
path = os.getenv('PATH')
@@ -32,15 +43,6 @@ def which(pgm):
return p
-PROTOC = which("protoc")
-if not PROTOC:
- print("protoc command not found")
- sys.exit(1)
-
-PROTOC_PREFIX = os.path.dirname(os.path.dirname(PROTOC))
-PROTOC_INCLUDE = os.path.join(PROTOC_PREFIX, "include")
-
-
def namespace_file(fpath, package):
"""Adds / replaces package name. Simple regex parsing, may use https://github.com/ph4r05/plyprotobuf later"""
with open(fpath) as fh:
@@ -82,9 +84,10 @@ def protoc(files, out_dir, additional_includes=(), package=None, force=False):
include_dirs = set()
include_dirs.add(PROTOC_INCLUDE)
- include_dirs.update(additional_includes)
+ if additional_includes:
+ include_dirs.update(additional_includes)
- with tempfile.TemporaryDirectory() as tmpdir_protob, tempfile.TemporaryDirectory() as tmpdir_out:
+ with TemporaryDirectory() as tmpdir_protob, TemporaryDirectory() as tmpdir_out:
include_dirs.add(tmpdir_protob)
new_files = []
@@ -125,9 +128,9 @@ def update_message_files(tmpdir_out, out_dir, force=False):
dest_file = os.path.join(out_dir, bname)
if not force and os.path.exists(dest_file):
data = open(fname, 'rb').read()
- data_hash = hashlib.sha3_256(data).digest()
+ data_hash = hashlib.sha256(data).digest()
data_dest = open(dest_file, 'rb').read()
- data_dest_hash = hashlib.sha3_256(data_dest).digest()
+ data_dest_hash = hashlib.sha256(data_dest).digest()
if data_hash == data_dest_hash:
continue
@@ -163,7 +166,8 @@ def strip_leader(s, prefix):
return s
-if __name__ == "__main__":
+def main():
+ global PROTOC, PROTOC_INCLUDE
logging.basicConfig(level=logging.DEBUG)
parser = argparse.ArgumentParser()
@@ -179,8 +183,31 @@ if __name__ == "__main__":
protoc_includes = args.protoc_include or (os.environ.get("PROTOC_INCLUDE"),)
+ PROTOBUF_INCLUDE_DIRS = os.getenv("PROTOBUF_INCLUDE_DIRS", None)
+ PROTOBUF_PROTOC_EXECUTABLE = os.getenv("PROTOBUF_PROTOC_EXECUTABLE", None)
+
+ if PROTOBUF_PROTOC_EXECUTABLE and not os.path.exists(PROTOBUF_PROTOC_EXECUTABLE):
+ raise ValueError("PROTOBUF_PROTOC_EXECUTABLE set but not found: %s" % PROTOBUF_PROTOC_EXECUTABLE)
+
+ elif PROTOBUF_PROTOC_EXECUTABLE:
+ PROTOC = PROTOBUF_PROTOC_EXECUTABLE
+
+ else:
+ if os.name == "nt":
+ PROTOC = which("protoc.exe")
+ else:
+ PROTOC = which("protoc")
+
+ if not PROTOC:
+ raise ValueError("protoc command not found. Set PROTOBUF_PROTOC_EXECUTABLE env var to the protoc binary and optionally PROTOBUF_INCLUDE_DIRS")
+
+ PROTOC_PREFIX = os.path.dirname(os.path.dirname(PROTOC))
+ PROTOC_INCLUDE = PROTOBUF_INCLUDE_DIRS if PROTOBUF_INCLUDE_DIRS else os.path.join(PROTOC_PREFIX, "include")
+
protoc(
args.proto, args.out_dir, protoc_includes, package=args.namespace, force=args.force
)
+if __name__ == "__main__":
+ main()