Skip to content

server_transfer

ServerTransferResource

Bases: ConfigurableResource

A resource for transferring files to other servers.

Attributes:

Name Type Description
host Optional[str]

Optional[str] The hostname or IP address of the remote server.

port Optional[int]

Optional[int] The port to connect to on the remote server.

user Optional[str]

Optional[str] The username to use for authentication.

password Optional[str]

Optional[str] The password to use for authentication (if not using key).

key_filename Optional[str]

Optional[str] The path to the private key file for key-based authentication.

target_dir Optional[str]

Optional[str] The default target directory on the remote server for file transfers.

public_dir Optional[str]

Optional[str] The 3DBAG public directory on the remote server.

Source code in packages/common/src/bag3d/common/resources/server_transfer.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class ServerTransferResource(ConfigurableResource):
    """
    A resource for transferring files to other servers.

    Attributes:
        host: Optional[str]
            The hostname or IP address of the remote server.
        port: Optional[int]
            The port to connect to on the remote server.
        user: Optional[str]
            The username to use for authentication.
        password: Optional[str]
            The password to use for authentication (if not using key).
        key_filename: Optional[str]
            The path to the private key file for key-based authentication.
        target_dir: Optional[str]
            The default target directory on the remote server for file transfers.
        public_dir: Optional[str]
            The 3DBAG public directory on the remote server.
    """

    host: Optional[str] = None
    port: Optional[int] = None
    user: Optional[str] = None
    password: Optional[str] = None
    key_filename: Optional[str] = None
    target_dir: Optional[str] = None
    public_dir: Optional[str] = None

    @property
    def connection(self) -> Connection:
        connect_kwargs = {}
        if self.key_filename:
            connect_kwargs["key_filename"] = self.key_filename
        elif self.password:
            connect_kwargs["password"] = self.password
        return Connection(
            host=self.host,
            port=self.port,
            user=self.user,
            connect_kwargs=connect_kwargs,
        )

    def transfer_file(self, local_path, remote_path) -> bool:
        """Transfer a file to remote server."""
        with self.connection as conn:
            # Upload the file
            conn.put(local_path, remote_path)

            # Verify the file was uploaded
            result = conn.run(f"test -f {remote_path}", warn=True, hide=True)
            return result.ok

    def file_exists(self, remote_path) -> bool:
        """Check if file exists on remote server."""
        with self.connection as conn:
            result = conn.run(f"test -f {remote_path}", warn=True, hide=True)
            return result.ok

file_exists(remote_path)

Check if file exists on remote server.

Source code in packages/common/src/bag3d/common/resources/server_transfer.py
61
62
63
64
65
def file_exists(self, remote_path) -> bool:
    """Check if file exists on remote server."""
    with self.connection as conn:
        result = conn.run(f"test -f {remote_path}", warn=True, hide=True)
        return result.ok

transfer_file(local_path, remote_path)

Transfer a file to remote server.

Source code in packages/common/src/bag3d/common/resources/server_transfer.py
51
52
53
54
55
56
57
58
59
def transfer_file(self, local_path, remote_path) -> bool:
    """Transfer a file to remote server."""
    with self.connection as conn:
        # Upload the file
        conn.put(local_path, remote_path)

        # Verify the file was uploaded
        result = conn.run(f"test -f {remote_path}", warn=True, hide=True)
        return result.ok