# slurm_script_generator.remote

*module*

Test local code on a cluster over ssh.

``slurm-remote`` syncs the current project to a cluster with ``rsync``, submits
a batch script there with ``sbatch``, follows the job and fetches results back.
All compute goes through SLURM, never the login node, and status polling has a
minimum interval so that the scheduler is not hammered.

The host and remote directory come from flags, the ``SLURM_REMOTE_HOST`` and
``SLURM_REMOTE_DIR`` environment variables, or a ``slurm-remote.json`` file in
the project directory, in that order of precedence. SSH connections are
multiplexed (``ControlMaster``) so that you authenticate once, also with 2FA.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L1-L1)

## slurm_script_generator.remote.CONFIG_FILE

*attribute* · *module attribute*

```python
CONFIG_FILE = 'slurm-remote.json'
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L24-L24)

## slurm_script_generator.remote.DEFAULT_EXCLUDES

*attribute* · *module attribute*

```python
DEFAULT_EXCLUDES = ('.git', '.venv', 'venv', '__pycache__', '*.pyc', 'node_modules', 'build', '*.egg-info')
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L28-L37)

## slurm_script_generator.remote.DEFAULT_REMOTE_DIR

*attribute* · *module attribute*

```python
DEFAULT_REMOTE_DIR = '~/slurm-remote'
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L25-L25)

## slurm_script_generator.remote.FINAL_STATES

*attribute* · *module attribute*

```python
FINAL_STATES = frozenset({'BOOT_FAIL', 'CANCELLED', 'COMPLETED', 'DEADLINE', 'FAILED', 'NODE_FAIL', 'OUT_OF_MEMORY', 'PREEMPTED', 'REVOKED', 'TIMEOUT'})
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L43-L56)

## slurm_script_generator.remote.MIN_POLL_INTERVAL

*attribute* · *module attribute*

```python
MIN_POLL_INTERVAL = 15
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L40-L40)

## slurm_script_generator.remote.RUN_SCRIPT

*attribute* · *module attribute*

```python
RUN_SCRIPT = '.slurm-remote-run.sh'
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L58-L58)

## slurm_script_generator.remote.RemoteConfig

*class* · *dataclass*

```python
class RemoteConfig
```

Where and how to run on the cluster.

**Attributes**

- `host` (`str`) — ssh destination, e.g. a ``Host`` alias from ``~/.ssh/config``.
- `remote_dir` (`str`) — Directory on the cluster that holds one folder per project.
- `excludes` (`list[str]`) — rsync exclude patterns for the sync.
- `setup` (`list[str]`) — Shell lines (``module load ...``) put before a command given to ``run``.
- `multiplex` (`bool`) — Whether to reuse one ssh connection for all calls.
- `poll_interval` (`int`) — Seconds between status checks while waiting.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L61-L84)

### slurm_script_generator.remote.RemoteConfig.excludes

*attribute* · *class attribute* · *instance attribute*

```python
excludes: list[str] = field(default_factory=lambda : list(DEFAULT_EXCLUDES))
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L77-L77)

### slurm_script_generator.remote.RemoteConfig.host

*attribute* · *instance attribute*

```python
host: str
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L75-L75)

### slurm_script_generator.remote.RemoteConfig.multiplex

*attribute* · *class attribute* · *instance attribute*

```python
multiplex: bool = True
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L79-L79)

### slurm_script_generator.remote.RemoteConfig.poll_interval

*attribute* · *class attribute* · *instance attribute*

```python
poll_interval: int = 30
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L80-L80)

### slurm_script_generator.remote.RemoteConfig.remote_dir

*attribute* · *class attribute* · *instance attribute*

```python
remote_dir: str = DEFAULT_REMOTE_DIR
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L76-L76)

### slurm_script_generator.remote.RemoteConfig.setup

*attribute* · *class attribute* · *instance attribute*

```python
setup: list[str] = field(default_factory=list)
```

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L78-L78)

## slurm_script_generator.remote.build_parser

*function*

```python
def build_parser() -> argparse.ArgumentParser
```

Build the argument parser of ``slurm-remote``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L438-L493)

## slurm_script_generator.remote.cmd_fetch

*function*

```python
def cmd_fetch(args, config) -> int
```

Handle ``slurm-remote fetch``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L412-L414)

## slurm_script_generator.remote.cmd_logs

*function*

```python
def cmd_logs(args, config) -> int
```

Handle ``slurm-remote logs``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L402-L409)

## slurm_script_generator.remote.cmd_run

*function*

```python
def cmd_run(args, config) -> int
```

Handle ``slurm-remote run``: sync, submit, wait, show the log, fetch.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L417-L435)

## slurm_script_generator.remote.cmd_status

*function*

```python
def cmd_status(args, config) -> int
```

Handle ``slurm-remote status``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L396-L399)

## slurm_script_generator.remote.cmd_submit

*function*

```python
def cmd_submit(args, config) -> int
```

Handle ``slurm-remote submit``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L389-L393)

## slurm_script_generator.remote.cmd_sync

*function*

```python
def cmd_sync(args, config) -> int
```

Handle ``slurm-remote sync``.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L384-L386)

## slurm_script_generator.remote.fetch

*function*

```python
def fetch(config: RemoteConfig, project_dir: str, paths: list[str], remote_absolute: bool = False) -> int
```

Copy files from the cluster project directory back, keeping layout.

**Parameters**

- `config` (`RemoteConfig`) — The remote configuration.
- `project_dir` (`str`) — Local project directory.
- `paths` (`list[str]`) — Paths relative to the remote project directory, or absolute remote paths when *remote_absolute* is set.
- `remote_absolute` (`bool`) (default: `False`) — Treat *paths* as absolute remote paths and copy them into the project directory by file name.

**Returns**

- (`int`) — The exit code of the last rsync.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L350-L381)

## slurm_script_generator.remote.job_log_path

*function*

```python
def job_log_path(config: RemoteConfig, project_dir: str, job_id: int) -> str
```

Return the stdout file of a job on the cluster.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L317-L323)

## slurm_script_generator.remote.job_state

*function*

```python
def job_state(config: RemoteConfig, job_id: int) -> str
```

Return the state of a job, from squeue or else sacct.

**Raises**

- `RuntimeError` — If neither command knows the job.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L300-L314)

## slurm_script_generator.remote.load_config

*function*

```python
def load_config(project_dir: str, host: str | None = None, remote_dir: str | None = None, config_path: str | None = None) -> RemoteConfig
```

Resolve the configuration from flags, environment and config file.

**Parameters**

- `project_dir` (`str`) — The local project directory, where the config file lives.
- `host` (`str | None`) (default: `None`) — ssh destination given on the command line.
- `remote_dir` (`str | None`) (default: `None`) — Remote base directory given on the command line.
- `config_path` (`str | None`) (default: `None`) — Alternative path of the JSON config file.

**Returns**

- (`RemoteConfig`) — The resolved configuration.

**Raises**

- `ValueError` — If no host is configured anywhere.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L87-L128)

## slurm_script_generator.remote.main

*function*

```python
def main(argv: list[str] | None = None) -> int
```

Entry point for the ``slurm-remote`` command-line tool.

**Parameters**

- `argv` (`list[str] | None`) (default: `None`) — Arguments to parse, defaulting to the process arguments.

**Returns**

- (`int`) — The exit code.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L496-L522)

## slurm_script_generator.remote.make_command_script

*function*

```python
def make_command_script(script_text: str, command: str, setup: list[str]) -> str
```

Build a batch script that runs *command* with the pragmas of a script.

**Parameters**

- `script_text` (`str`) — Text of the batch script whose ``#SBATCH`` lines to keep.
- `command` (`str`) — The shell command to run instead of the script's body.
- `setup` (`list[str]`) — Lines to put before the command.

**Returns**

- (`str`) — The text of the new batch script.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L253-L268)

## slurm_script_generator.remote.parse_job_id

*function*

```python
def parse_job_id(output: str) -> int
```

Extract the job id from ``sbatch --parsable`` output.

**Raises**

- `ValueError` — If the output holds no job id.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L241-L250)

## slurm_script_generator.remote.project_path

*function*

```python
def project_path(config: RemoteConfig, project_dir: str) -> str
```

Return the project's directory on the cluster.

One folder per local directory name, so separate worktrees do not
overwrite each other.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L145-L152)

## slurm_script_generator.remote.remote_quote

*function*

```python
def remote_quote(path: str) -> str
```

Quote a remote path but keep a leading ``~`` expandable.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L155-L159)

## slurm_script_generator.remote.remote_run

*function*

```python
def remote_run(config: RemoteConfig, remote_command: str) -> subprocess.CompletedProcess
```

Run a command over ssh and capture its output.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L212-L216)

## slurm_script_generator.remote.rsync_command

*function*

```python
def rsync_command(config: RemoteConfig, source: str, destination: str, excludes: list[str], delete: bool = False, files: list[str] | None = None) -> list[str]
```

Build an rsync command that uses the shared ssh options.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L175-L197)

## slurm_script_generator.remote.run

*function*

```python
def run(command: list[str], capture: bool = False) -> subprocess.CompletedProcess
```

Run a command, optionally capturing text output.

**Raises**

- `RuntimeError` — If the executable (ssh or rsync) is not installed.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L200-L209)

## slurm_script_generator.remote.ssh_command

*function*

```python
def ssh_command(config: RemoteConfig, remote_command: str, tty: bool = False) -> list[str]
```

Build the ssh command that runs *remote_command* on the cluster.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L162-L172)

## slurm_script_generator.remote.ssh_options

*function*

```python
def ssh_options(config: RemoteConfig) -> list[str]
```

Return the ssh options shared by ssh and rsync calls.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L131-L142)

## slurm_script_generator.remote.submit

*function*

```python
def submit(config: RemoteConfig, project_dir: str, script: str, delete: bool = False) -> int
```

Sync the project and submit *script* there.

**Parameters**

- `config` (`RemoteConfig`) — The remote configuration.
- `project_dir` (`str`) — Local project directory.
- `script` (`str`) — Path of the batch script, relative to *project_dir*.
- `delete` (`bool`) (default: `False`) — Remove remote files that no longer exist locally.

**Returns**

- (`int`) — The job id.

**Raises**

- `RuntimeError` — If syncing or submitting fails.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L271-L297)

## slurm_script_generator.remote.sync

*function*

```python
def sync(config: RemoteConfig, project_dir: str, delete: bool = False) -> int
```

Rsync the project to the cluster.

**Returns**

- (`int`) — The exit code of rsync.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L219-L238)

## slurm_script_generator.remote.wait

*function*

```python
def wait(config: RemoteConfig, job_id: int, sleep=time.sleep, report=None) -> str
```

Poll a job until it reaches a final state.

**Parameters**

- `config` (`RemoteConfig`) — The remote configuration, with the polling interval.
- `job_id` (`int`) — The job to follow.
- `sleep` (default: `time.sleep`) — Function used to wait, replaceable in tests.
- `report` (default: `None`) — Function used to print state changes.

**Returns**

- (`str`) — The final state.

[View source](https://github.com/max-models/slurm-script-generator/blob/devel/src/slurm_script_generator/remote.py#L326-L347)
