Ejemplo n.º 1
0
Archivo: linux.py Proyecto: cmhe/tbot
 def workdir(self: Self) -> "linux.Path[Self]":  # noqa: D102
     return linux.Path(self, "/tmp")
Ejemplo n.º 2
0
Archivo: shell.py Proyecto: zkrx/tbot
def copy(p1: linux.Path[H1], p2: linux.Path[H2]) -> None:
    """
    Copy a file, possibly from one host to another.

    The following transfers are currently supported:

    * ``H`` 🢥 ``H`` (transfer without changing host)
    * **lab-host** 🢥 **ssh-machine** (:py:class:`~tbot.machine.connector.SSHConnector`, using ``scp``)
    * **ssh-machine** 🢥 **lab-host** (Using ``scp``)
    * **local-host** 🢥 **paramiko-host** (:py:class:`~tbot.machine.connector.ParamikoConnector`, using ``scp``)
    * **local-host** 🢥 **ssh-machine** (:py:class:`~tbot.machine.connector.SSHConnector`, using ``scp``)
    * **paramiko-host**/**ssh-machine** 🢥 **local-host** (Using ``scp``)

    The following transfers are **not** supported:

    * **ssh-machine** 🢥 **ssh-machine** (There is no guarantee that two remote hosts can
      connect to each other.  If you need this, transfer to the lab-host first
      and then to the other remote)
    * **lab-host** 🢥 **board-machine** (Transfers over serial are not (yet) implemented.
      To 'upload' files, connect to your target via ssh or use a tftp download)

    :param linux.Path p1: Exisiting path to be copied
    :param linux.Path p2: Target where ``p1`` should be copied
    """
    if isinstance(p1.host, p2.host.__class__) or isinstance(p2.host, p1.host.__class__):
        # Both paths are on the same host
        p2_w1 = linux.Path(p1.host, p2)
        p1.host.exec0("cp", p1, p2_w1)
        return
    elif isinstance(p1.host, connector.SSHConnector) and p1.host.host is p2.host:
        # Copy from an SSH machine
        _scp_copy(
            local_path=p2,
            remote_path=p1,
            copy_to_remote=False,
            username=p1.host.username,
            hostname=p1.host.hostname,
            ignore_hostkey=p1.host.ignore_hostkey,
            port=p1.host.port,
            ssh_config=p1.host.ssh_config,
            authenticator=p1.host.authenticator,
        )
    elif isinstance(p2.host, connector.SSHConnector) and p2.host.host is p1.host:
        # Copy to an SSH machine
        _scp_copy(
            local_path=p1,
            remote_path=p2,
            copy_to_remote=True,
            username=p2.host.username,
            hostname=p2.host.hostname,
            ignore_hostkey=p2.host.ignore_hostkey,
            port=p2.host.port,
            ssh_config=p2.host.ssh_config,
            authenticator=p2.host.authenticator,
        )
    elif isinstance(p1.host, connector.SubprocessConnector) and (
        isinstance(p2.host, connector.ParamikoConnector)
        or isinstance(p2.host, connector.SSHConnector)
    ):
        # Copy from local to ssh labhost
        _scp_copy(
            local_path=p1,
            remote_path=p2,
            copy_to_remote=True,
            username=p2.host.username,
            hostname=p2.host.hostname,
            ignore_hostkey=p2.host.ignore_hostkey,
            port=p2.host.port,
            ssh_config=getattr(p2.host, "ssh_config", []),
            authenticator=p2.host.authenticator,
        )
    elif isinstance(p2.host, connector.SubprocessConnector) and (
        isinstance(p1.host, connector.ParamikoConnector)
        or isinstance(p1.host, connector.SSHConnector)
    ):
        # Copy to local from ssh labhost
        _scp_copy(
            local_path=p2,
            remote_path=p1,
            copy_to_remote=False,
            username=p1.host.username,
            hostname=p1.host.hostname,
            ignore_hostkey=p1.host.ignore_hostkey,
            port=p1.host.port,
            ssh_config=getattr(p2.host, "ssh_config", []),
            authenticator=p1.host.authenticator,
        )
    else:
        raise NotImplementedError(f"Can't copy from {p1.host} to {p2.host}!")
Ejemplo n.º 3
0
 def workdir(self) -> "linux.Path[LabHost]":  # noqa: D102
     wd = super().workdir
     return linux.Path(self, wd)
Ejemplo n.º 4
0
 def tftp_dir_board(self) -> "linux.Path[Lab1]":
     """
     returns tftp path for u-boot tftp command
     """
     return linux.Path(self, f"{tbot.selectable.Board.name}/tbot")
Ejemplo n.º 5
0
 def do_patch(self, repo: git.GitRepository) -> None:
     repo.am(
         linux.Path(
             repo.host,
             "/home/hs/abb/mainlining/aristainetos/patches/20200515"))
Ejemplo n.º 6
0
 def tftp_dir_board(self) -> "linux.Path[SmalllaptopLab]":
     """
     returns tftp path for u-boot tftp command
     """
     return linux.Path(self, f"{self.get_boardname}/tbot")
Ejemplo n.º 7
0
 def tftp_dir(self) -> "linux.path.Path[SmallLab]":
     return linux.Path(self,
                       f"/srv/tftpboot/" + self.get_boardname + "/tbot")
Ejemplo n.º 8
0
 def workdir(self) -> "linux.Path[DummyLinuxMach2]":
     return linux.Path(self, "/tmp/foo")
Ejemplo n.º 9
0
def selftest_path_stat(lab: typing.Optional[linux.LabHost] = None, ) -> None:
    with lab or tbot.acquire_lab() as lh:
        tbot.log.message("Setting up test files ...")
        symlink = lh.workdir / "symlink"
        if symlink.exists():
            lh.exec0("rm", symlink)
        lh.exec0("ln", "-s", "/proc/version", symlink)

        fifo = lh.workdir / "fifo"
        if fifo.exists():
            lh.exec0("rm", fifo)
        lh.exec0("mkfifo", fifo)

        nonexistent = lh.workdir / "nonexistent"
        if nonexistent.exists():
            lh.exec0("rm", nonexistent)

        # Block device
        block_list = (lh.exec0(
            *["find", "/dev", "-type", "b"],
            linux.Raw("2>/dev/null"),
            linux.OrElse,
            "true",
        ).strip().split("\n"))
        block_dev = None
        if block_list != []:
            block_dev = linux.Path(lh, "/dev") / block_list[0]

        # Existence checks
        tbot.log.message("Checking existence ...")
        assert not (lh.workdir / "nonexistent").exists()
        assert symlink.exists()

        # File mode checks
        tbot.log.message("Checking file modes ...")
        assert linux.Path(lh, "/dev").is_dir()
        assert linux.Path(lh, "/proc/version").is_file()
        assert symlink.is_symlink()
        if block_dev is not None:
            assert linux.Path(lh, block_dev).is_block_device()
        assert linux.Path(lh, "/dev/tty").is_char_device()
        assert fifo.is_fifo()

        # File mode nonexistence checks
        tbot.log.message("Checking file modes on nonexistent files ...")
        assert not nonexistent.is_dir()
        assert not nonexistent.is_file()
        assert not nonexistent.is_symlink()
        assert not nonexistent.is_block_device()
        assert not nonexistent.is_char_device()
        assert not nonexistent.is_fifo()
        assert not nonexistent.is_socket()

        stat_list = [
            (linux.Path(lh, "/dev"), stat.S_ISDIR),
            (linux.Path(lh, "/proc/version"), stat.S_ISREG),
            (symlink, stat.S_ISLNK),
            (linux.Path(lh, "/dev/tty"), stat.S_ISCHR),
            (fifo, stat.S_ISFIFO),
        ]

        if block_dev is not None:
            stat_list.insert(3, (linux.Path(lh, block_dev), stat.S_ISBLK))

        tbot.log.message("Checking stat results ...")
        for p, check in stat_list:
            assert check(p.stat().st_mode)
Ejemplo n.º 10
0
 def tftp_dir(self) -> "linux.Path[PolluxLab]":
     if tbot.selectable.Board.name == "taurus":
         return linux.Path(self, f"{self.tftproot}/at91_taurus/tbot/")
     else:
         return linux.Path(self,
                           f"{self.tftproot}/{self.get_boardname}/tbot")
Ejemplo n.º 11
0
 def sign_dir(self) -> "linux.Path[PolluxLab]":
     return linux.Path(self, f"/home/hs/tools/cst-2.3.3/linux64/bin")
Ejemplo n.º 12
0
 def do_patch(self, repo: git.GitRepository) -> None:
     repo.am(linux.Path(repo.host, "/work/hs/tbot2go/patches/bbb"))
Ejemplo n.º 13
0
 def workdir(self) -> "linux.Path[LabQEMU]":
     p = linux.Path(self, f"/")
     return p
Ejemplo n.º 14
0
def testcase_swupdate(lh: linux.LinuxShell) -> None:
    with tbot.acquire_local() as lo:
        swu_path = linux.Path(lo, "/opt/swupdate/update-image.swu")
        swupdate.swupdate_update_web(lo, swu_path, "192.168.1.48")
Ejemplo n.º 15
0
 def nfs_root(self) -> "linux.Path[SmalllaptopLab]":
     return linux.Path(self, f"/work/tbot2go/nfs")
Ejemplo n.º 16
0
def selftest_machine_shell(
    m: typing.Union[linux.LinuxMachine, board.UBootMachine]
) -> None:
    # Capabilities
    cap = []
    if isinstance(m, linux.LinuxMachine):
        if m.shell == linux.shell.Bash:
            cap.extend(["printf", "jobs", "control"])
        if m.shell == linux.shell.Ash:
            cap.extend(["printf", "control"])

    tbot.log.message("Testing command output ...")
    out = m.exec0("echo", "Hello World")
    assert out == "Hello World\n", repr(out)

    out = m.exec0("echo", "$?", "!#")
    assert out == "$? !#\n", repr(out)

    if "printf" in cap:
        out = m.exec0("printf", "Hello World")
        assert out == "Hello World", repr(out)

        out = m.exec0("printf", "Hello\\nWorld")
        assert out == "Hello\nWorld", repr(out)

        out = m.exec0("printf", "Hello\nWorld")
        assert out == "Hello\nWorld", repr(out)

    s = "_".join(map(lambda i: f"{i:02}", range(80)))
    out = m.exec0("echo", s)
    assert out == f"{s}\n", repr(out)

    tbot.log.message("Testing return codes ...")
    assert m.test("true")
    assert not m.test("false")

    if isinstance(m, linux.LinuxMachine):
        tbot.log.message("Testing env vars ...")
        value = "12\nfoo !? # true; exit\n"
        m.exec0("export", f"TBOT_TEST_ENV_VAR={value}")
        out = m.env("TBOT_TEST_ENV_VAR")
        assert out == value, repr(out)

        tbot.log.message("Testing redirection (and weird paths) ...")
        f = m.workdir / ".redir test.txt"
        if f.exists():
            m.exec0("rm", f)

        m.exec0("echo", "Some data\nAnd some more", stdout=f)

        out = m.exec0("cat", f)
        assert out == "Some data\nAnd some more\n", repr(out)

        tbot.log.message("Testing formatting ...")
        tmp = linux.Path(m, "/tmp/f o/bar")
        out = m.exec0("echo", linux.F("{}:{}:{}", tmp, linux.Pipe, "foo"))
        assert out == "/tmp/f o/bar:|:foo\n", repr(out)

        m.exec0("export", linux.F("NEWPATH={}:{}", tmp, linux.Env("PATH"), quote=False))
        out = m.env("NEWPATH")
        assert out != "/tmp/f o/bar:${PATH}", repr(out)

        if "jobs" in cap:
            t1 = time.monotonic()
            out = m.exec0(
                "sleep", "10", linux.Background, "echo", "Hello World"
            ).strip()
            t2 = time.monotonic()

            assert re.match(r"\[\d+\] \d+\nHello World", out), repr(out)
            assert (
                t2 - t1
            ) < 9.0, (
                f"Command took {t2 - t1}s (max 9s). Sleep was not sent to background"
            )

        if "control" in cap:
            out = m.exec0(
                "false", linux.AndThen, "echo", "FOO", linux.OrElse, "echo", "BAR"
            ).strip()
            assert out == "BAR", repr(out)

            out = m.exec0(
                "true", linux.AndThen, "echo", "FOO", linux.OrElse, "echo", "BAR"
            ).strip()
            assert out == "FOO", repr(out)

        tbot.log.message("Testing subshell ...")
        out = m.env("SUBSHELL_TEST_VAR")
        assert out == "", repr(out)

        with m.subshell():
            m.exec0("export", "SUBSHELL_TEST_VAR=123")
            out = m.env("SUBSHELL_TEST_VAR")
            assert out == "123", repr(out)

        out = m.env("SUBSHELL_TEST_VAR")
        assert out == "", repr(out)

        with m.subshell(
            "env", "SUBSHELL_TEST_VAR2=1337", "bash", "--norc", shell=linux.shell.Bash
        ):
            out = m.env("SUBSHELL_TEST_VAR2")
            assert out == "1337", repr(out)

    if isinstance(m, board.UBootMachine):
        tbot.log.message("Testing env vars ...")
        m.exec0("setenv", "TBOT_TEST", "Lorem ipsum dolor sit amet")
        out = m.exec0("printenv", "TBOT_TEST")
        assert out == "TBOT_TEST=Lorem ipsum dolor sit amet\n", repr(out)

        out = m.env("TBOT_TEST")
        assert out == "Lorem ipsum dolor sit amet", repr(out)
Ejemplo n.º 17
0
 def tftp_root_path(self) -> "linux.Path[SmalllaptopLab]":
     """
     returns root tftp path
     """
     return linux.Path(self, self.tftproot)
Ejemplo n.º 18
0
Archivo: shell.py Proyecto: saimen/tbot
def copy(p1: linux.Path[H1], p2: linux.Path[H2]) -> None:
    """
    Copy a file, possibly from one host to another.

    If one of the paths is associated with an SSHMachine,
    ``scp`` will be used to do the transfer.

    :param linux.Path p1: Exisiting path to be copied
    :param linux.Path p2: Target where ``p1`` should be copied
    """
    if isinstance(p1.host, p2.host.__class__) or isinstance(
            p2.host, p1.host.__class__):
        # Both paths are on the same host
        p2_w1 = linux.Path(p1.host, p2)
        p1.host.exec0("cp", p1, p2_w1)
        return
    elif isinstance(p1.host, linux.SSHMachine) and p1.host.labhost is p2.host:
        # Copy from an SSH machine
        _scp_copy(
            local_path=p2,
            remote_path=p1,
            copy_to_remote=False,
            username=p1.host.username,
            hostname=p1.host.hostname,
            ignore_hostkey=p1.host.ignore_hostkey,
            port=p1.host.port,
            ssh_config=p1.host.ssh_config,
            authenticator=p1.host.authenticator,
        )
    elif isinstance(p2.host, linux.SSHMachine) and p2.host.labhost is p1.host:
        # Copy to an SSH machine
        _scp_copy(
            local_path=p1,
            remote_path=p2,
            copy_to_remote=True,
            username=p2.host.username,
            hostname=p2.host.hostname,
            ignore_hostkey=p2.host.ignore_hostkey,
            port=p2.host.port,
            ssh_config=p2.host.ssh_config,
            authenticator=p2.host.authenticator,
        )
    elif isinstance(p1.host, linux.lab.LocalLabHost) and isinstance(
            p2.host, linux.lab.SSHLabHost):
        # Copy from local to ssh labhost
        _scp_copy(
            local_path=p1,
            remote_path=p2,
            copy_to_remote=True,
            username=p2.host.username,
            hostname=p2.host.hostname,
            ignore_hostkey=p2.host.ignore_hostkey,
            port=p2.host.port,
            ssh_config=[],
            authenticator=p2.host.authenticator,
        )
    elif isinstance(p2.host, linux.lab.LocalLabHost) and isinstance(
            p1.host, linux.lab.SSHLabHost):
        # Copy to local from ssh labhost
        _scp_copy(
            local_path=p2,
            remote_path=p1,
            copy_to_remote=False,
            username=p1.host.username,
            hostname=p1.host.hostname,
            ignore_hostkey=p1.host.ignore_hostkey,
            port=p1.host.port,
            ssh_config=[],
            authenticator=p1.host.authenticator,
        )
    else:
        raise NotImplementedError(f"Can't copy from {p1.host} to {p2.host}!")
Ejemplo n.º 19
0
 def yocto_result_dir(self) -> "linux.Path[SmalllaptopLab]":
     return linux.Path(
         self, f"{self.tftproot}/" + tbot.selectable.Board.name +
         "/tbot/yocto_results")
Ejemplo n.º 20
0
 def yocto_result_dir(self) -> "linux.path.Path[XpertLab]":
     return linux.Path(
         self, f"/tftpboot/{tbot.selectable.Board.name}/yocto_results")
Ejemplo n.º 21
0
 def tftp_root_path(self) -> "linux.Path[Lab1]":
     """
     returns root tftp path
     """
     return linux.Path(self, self.tftproot)
Ejemplo n.º 22
0
 def tftp_dir_board(self) -> "linux.path.Path[XpertLab]":
     return linux.Path(self, f"{self.get_boardname}/{self.username}/tbot")
Ejemplo n.º 23
0
 def toolsdir(self) -> "linux.path.Path[Lab1]":
     return linux.Path(self, "/home/hs/tbot2go/bin")
Ejemplo n.º 24
0
 def nfs_root(self) -> "linux.Path[Tbot2go2Lab]":
     return linux.Path(self, f"/work/tbot2go/nfs")