From 3f35475e7bdad48e5ccad2538a29fccd096a9c4f Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 26 Mar 2018 17:38:49 +0200 Subject: [PATCH] build: preprocess messages and assets in setup.py This enforces presence of the trezor-common submodule, copies coins.json to the package directory (from where we can install it with bdist) and if possible, regenerates protobuf messages. That currently doesn't work on Windows, because it's a shell script. Also it relies on presence of `protoc` protobuf compiler. Therefore the regeneration step is optional and converted protobuf messages should still be commited to this repo. coins.json, OTOH, is gitignored in trezorlib, and must be copied from trezor-common every time. This works because sdist includes the vendor directory. --- .gitignore | 3 +++ setup.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 452facd36..4c8387bc2 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ docs/_build docs/.docs-build-environment .tox/ .cache/ +.pytest_cache/ + +trezorlib/coins.json diff --git a/setup.py b/setup.py index 4d5f99247..17e76015a 100755 --- a/setup.py +++ b/setup.py @@ -1,5 +1,10 @@ #!/usr/bin/env python3 -from setuptools import setup +import os.path +import shutil +import subprocess + +from setuptools import setup, Command +from setuptools.command.build_py import build_py install_requires = [ 'setuptools>=19.0', @@ -24,6 +29,44 @@ else: from trezorlib import __version__ as VERSION + +class PrebuildCommand(Command): + description = 'update vendored files (coins.json, protobuf messages)' + user_options = [] + + TREZOR_COMMON = os.path.join('vendor', 'trezor-common') + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + # check for existence of the submodule directory + coins_json = os.path.join(self.TREZOR_COMMON, 'coins.json') + if not os.path.exists(coins_json): + raise Exception('trezor-common submodule seems to be missing.\n' + + 'Use "git submodule update --init" to retrieve it.') + + # copy coins.json to the tree + shutil.copy(coins_json, 'trezorlib') + + # regenerate messages + try: + subprocess.check_call(os.path.join(os.getcwd(), 'tools', 'build_protobuf')) + except Exception as e: + print(e) + print("Generating protobuf failed. Maybe you don't have 'protoc', or maybe you are on Windows?") + print("Using pre-generated files.") + + +class CustomBuild(build_py): + def run(self): + self.run_command('prebuild') + super().run() + + setup( name='trezor', version=VERSION, @@ -39,6 +82,9 @@ setup( 'trezorlib.tests.device_tests', 'trezorlib.tests.unit_tests', ], + package_data={ + 'trezorlib': ['coins.json'], + }, scripts=['trezorctl'], install_requires=install_requires, python_requires='>=3.3', @@ -51,4 +97,8 @@ setup( 'Operating System :: MacOS :: MacOS X', 'Programming Language :: Python :: 3 :: Only', ], + cmdclass={ + 'prebuild': PrebuildCommand, + 'build_py': CustomBuild, + }, )