Exemple #1
0
def expanduser(s):
    if s == "~" or s.startswith("~/"):
        h = uos.getenv("HOME")
        return h + s[1:]
    if s[0] == "~":
        # Sorry folks, follow conventions
        return "/home/" + s[1:]
    return s
Exemple #2
0
def get_install_path():
    global install_path
    if install_path is None:
        if hasattr(os, "getenv"):
            install_path = os.getenv("MICROPYPATH")
        if install_path is None:
            # sys.path[0] is current module's path
            install_path = sys.path[1]
    install_path = install_path.split(":", 1)[0]
    install_path = expandhome(install_path)
    return install_path
Exemple #3
0
    VERSION_MAJOR,
    VERSION_MINOR,
    VERSION_PATCH,
    consteq,
    halt,
    memcpy,
)
from typing import TYPE_CHECKING

DISABLE_ANIMATION = 0

if __debug__:
    if EMULATOR:
        import uos

        DISABLE_ANIMATION = int(uos.getenv("TREZOR_DISABLE_ANIMATION") or "0")
        LOG_MEMORY = int(uos.getenv("TREZOR_LOG_MEMORY") or "0")
    else:
        LOG_MEMORY = 0

if TYPE_CHECKING:
    from typing import (
        Any,
        Iterator,
        Protocol,
        TypeVar,
        Sequence,
    )

    from trezor.protobuf import MessageType
Exemple #4
0
import gc
import sys
from trezorutils import (  # noqa: F401
    BITCOIN_ONLY, EMULATOR, GITREV, MODEL, VERSION_MAJOR, VERSION_MINOR,
    VERSION_PATCH, consteq, halt, memcpy, set_mode_unprivileged,
)

if __debug__:
    if EMULATOR:
        import uos

        TEST = int(uos.getenv("TREZOR_TEST") or "0")
        DISABLE_FADE = int(uos.getenv("TREZOR_DISABLE_FADE") or "0")
        SAVE_SCREEN = int(uos.getenv("TREZOR_SAVE_SCREEN") or "0")
        LOG_MEMORY = int(uos.getenv("TREZOR_LOG_MEMORY") or "0")
    else:
        TEST = 0
        DISABLE_FADE = 0
        SAVE_SCREEN = 0
        LOG_MEMORY = 0

if False:
    from typing import Iterable, Iterator, Protocol, List, TypeVar


def unimport_begin() -> Iterable[str]:
    return set(sys.modules)


def unimport_end(mods: Iterable[str]) -> None:
    for mod in sys.modules:
def expandhome(s):
    if "~/" in s:
        h = os.getenv("HOME")
        s = s.replace("~/", h + "/")
    return s
Exemple #6
0
from micropython import const

from storage.device import get_device_id
from trezor import io, utils

UDP_PORT = 0
WIRE_PORT_OFFSET = const(0)
DEBUGLINK_PORT_OFFSET = const(1)
WEBAUTHN_PORT_OFFSET = const(2)
VCP_PORT_OFFSET = const(3)

if utils.EMULATOR:
    import uos

    UDP_PORT = int(uos.getenv("TREZOR_UDP_PORT") or "21324")

_iface_iter = iter(range(5))

ENABLE_IFACE_DEBUG = __debug__
# change to False to enable VCP, see below
ENABLE_IFACE_WEBAUTHN = not utils.BITCOIN_ONLY

# We only have 10 available USB endpoints on real HW, of which 2 are taken up by the USB descriptor.
# iface_wire, iface_debug and iface_webauthn also consume 2 each, iface_vcp uses 3.
# That is a grand total of 11. That means that we can't enable everything at the same time.
# By default, iface_vcp is only enabled on bitcoin_only firmware, where iface_webauthn
# is disabled. Implementation-wise, we check if any of the previous ifaces is disabled
# in order to enable VCP.
ENABLE_IFACE_VCP = __debug__ and (utils.EMULATOR or not ENABLE_IFACE_DEBUG
                                  or not ENABLE_IFACE_WEBAUTHN)
Exemple #7
0
import sys

from uio import open
from uos import getenv
import micropython

# We need to insert "" to sys.path so that the frozen build can import main from the
# frozen modules, and regular build can import it from current directory.
sys.path.insert(0, "")

PATH_PREFIX = (getenv("TREZOR_SRC") or ".") + "/"


class Coverage:
    def __init__(self):
        self.__files = {}

    def line_tick(self, filename, lineno):
        if not filename in self.__files:
            self.__files[filename] = set()
        self.__files[filename].add(lineno)

    def lines_execution(self):
        lines_execution = {"lines": {}}
        lines = lines_execution["lines"]
        this_file = globals()["__file__"]
        for filename in self.__files:
            if not filename == this_file:
                lines[PATH_PREFIX + filename] = list(self.__files[filename])

        return lines_execution
Exemple #8
0
def expandhome(s):
    if "~/" in s:
        h = os.getenv("HOME")
        s = s.replace("~/", h + "/")
    return s
Exemple #9
0
def main():
    global debug
    install_path = None

    if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
        help()

    if sys.argv[1] != "install":
        fatal("Only 'install' command supported")

    to_install = []

    i = 2
    while i < len(sys.argv) and sys.argv[i][0] == "-":
        opt = sys.argv[i]
        i += 1
        if opt == "-h" or opt == "--help":
            help()
        elif opt == "-p":
            install_path = sys.argv[i]
            i += 1
        elif opt == "-r":
            list_file = sys.argv[i]
            i += 1
            with open(list_file) as f:
                while True:
                    l = f.readline()
                    if not l:
                        break
                    to_install.append(l.rstrip())
        elif opt == "--debug":
            debug = True
        else:
            fatal("Unknown/unsupported option: " + opt)

    if install_path is None:
        install_path = os.getenv("MICROPYPATH") or DEFAULT_MICROPYPATH

    install_path = install_path.split(":", 1)[0]

    install_path = expandhome(install_path)

    if install_path[-1] != "/":
        install_path += "/"

    print("Installing to: " + install_path)

    to_install.extend(sys.argv[i:])
    if not to_install:
        help()

    # sets would be perfect here, but don't depend on them
    installed = []
    try:
        while to_install:
            if debug:
                print("Queue:", to_install)
            pkg_spec = to_install.pop(0)
            if pkg_spec in installed:
                continue
            meta = install_pkg(pkg_spec, install_path)
            installed.append(pkg_spec)
            if debug:
                print(meta)
            deps = meta.get("deps", "").rstrip()
            if deps:
                deps = deps.decode("utf-8").split("\n")
                to_install.extend(deps)
    except NotFoundError:
        print("Error: cannot find '%s' package (or server error), packages may be partially installed" \
            % pkg_spec, file=sys.stderr)

    if not debug:
        cleanup()
Exemple #10
0
import gc
import sys
from trezorutils import (  # noqa: F401
    EMULATOR, GITREV, MODEL, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH,
    consteq, halt, memcpy, set_mode_unprivileged,
)

if __debug__:
    if EMULATOR:
        import uos

        TEST = int(uos.getenv("TREZOR_TEST") or "0")
        SAVE_SCREEN = int(uos.getenv("TREZOR_SAVE_SCREEN") or "0")
    else:
        TEST = 0
        SAVE_SCREEN = 0


def unimport_begin():
    return set(sys.modules)


def unimport_end(mods):
    for mod in sys.modules:
        if mod not in mods:
            # remove reference from sys.modules
            del sys.modules[mod]
            # remove reference from the parent module
            i = mod.rfind(".")
            if i < 0:
                continue
Exemple #11
0
    import urandom as _random
    import uselect as _select
    import usocket as _socket
    import ustruct as _struct
    import utime as _time
else:
    _gc = None
    import os as _os
    import random as _random
    import select as _select
    import socket as _socket
    import struct as _struct
    import time as _time

try:
    _DEBUG = _os.getenv("ARGON_DEBUG") is not None
except AttributeError:
    _DEBUG = False


class Buffer:
    def __init__(self):
        self._octets = bytearray(256)
        self._view = memoryview(self._octets)

    def skip(self, offset, size):
        return offset + size, offset

    def read(self, offset, size):
        end = offset + size
        return end, self._view[offset:end]
import uos

from pybricks.experimental import run_parallel
from pybricks.tools import wait

if uos.getenv("PYBRICKS_BUILD_ENV") == "docker-armel":
    # qemu-user-static has issues with threads
    print("SKIP")
    raise SystemExit


def task1():
    wait(1000)
    return "OK1"


def task2():
    wait(500)
    return "OK2"


def task3():
    # Unhandled Exception should interrupt all other tasks
    raise Exception("oops")


def task4():
    # Unhandled BaseException does not interrupt other tasks
    raise SystemExit

Exemple #13
0
def main():
    global debug
    install_path = None

    if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
        help()

    if sys.argv[1] != "install":
        fatal("Only 'install' command supported")

    to_install = []

    i = 2
    while i < len(sys.argv) and sys.argv[i][0] == "-":
        opt = sys.argv[i]
        i += 1
        if opt == "-h" or opt == "--help":
            help()
        elif opt == "-p":
            install_path = sys.argv[i]
            i += 1
        elif opt == "-r":
            list_file = sys.argv[i]
            i += 1
            with open(list_file) as f:
                while True:
                    l = f.readline()
                    if not l:
                        break
                    to_install.append(l.rstrip())
        elif opt == "--debug":
            debug = True
        else:
            fatal("Unknown/unsupported option: " + opt)

    if install_path is None:
        install_path = os.getenv("MICROPYPATH") or DEFAULT_MICROPYPATH

    install_path = install_path.split(":", 1)[0]

    install_path = expandhome(install_path)

    if install_path[-1] != "/":
        install_path += "/"

    print("Installing to: " + install_path)

    to_install.extend(sys.argv[i:])
    if not to_install:
        help()

    # sets would be perfect here, but don't depend on them
    installed = []
    try:
        while to_install:
            if debug:
                print("Queue:", to_install)
            pkg_spec = to_install.pop(0)
            if pkg_spec in installed:
                continue
            meta = install_pkg(pkg_spec, install_path)
            installed.append(pkg_spec)
            if debug:
                print(meta)
            deps = meta.get("deps", "").rstrip()
            if deps:
                deps = deps.decode("utf-8").split("\n")
                to_install.extend(deps)
    except NotFoundError:
        print("Error: cannot find '%s' package (or server error), packages may be partially installed" \
            % pkg_spec, file=sys.stderr)

    if not debug:
        cleanup()
Exemple #14
0
import sys

import uos
from uio import open

sys.path.insert(0, uos.getenv("TREZOR_SRC"))
del uos


class Coverage:
    def __init__(self):
        self.__files = {}

    def line_tick(self, filename, lineno):
        if not filename in self.__files:
            self.__files[filename] = set()
        self.__files[filename].add(lineno)

    def lines_execution(self):
        lines_execution = {"lines": {}}
        lines = lines_execution["lines"]
        this_file = globals()["__file__"]
        for filename in self.__files:
            if not filename == this_file:
                lines[filename] = list(self.__files[filename])

        return lines_execution


class _Prof:
    trace_count = 0
Exemple #15
0
import sys

from uio import open
from uos import getenv

# We need to insert "" to sys.path so that the frozen build can import main from the
# frozen modules, and regular build can import it from current directory.
sys.path.insert(0, "")

PATH_PREFIX = (getenv("TREZOR_SRC") or ".") + "/"


class Coverage:
    def __init__(self):
        self.__files = {}

    def line_tick(self, filename, lineno):
        if not filename in self.__files:
            self.__files[filename] = set()
        self.__files[filename].add(lineno)

    def lines_execution(self):
        lines_execution = {"lines": {}}
        lines = lines_execution["lines"]
        this_file = globals()["__file__"]
        for filename in self.__files:
            if not filename == this_file:
                lines[PATH_PREFIX + filename] = list(self.__files[filename])

        return lines_execution