File size: 2,106 Bytes
9c6594c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
66
67
68
69
70
71
72
import pathlib
from typing import TYPE_CHECKING, Optional

from wandb import util
from wandb.errors.term import termerror, termlog

from . import wandb_setup
from .backend.backend import Backend
from .lib.runid import generate_id

if TYPE_CHECKING:
    from wandb.proto import wandb_internal_pb2


def _sync(
    path: str,
    run_id: Optional[str] = None,
    project: Optional[str] = None,
    entity: Optional[str] = None,
    mark_synced: Optional[bool] = None,
    append: Optional[bool] = None,
    skip_console: Optional[bool] = None,
) -> "wandb_internal_pb2.SyncResponse":
    wl = wandb_setup.setup()
    assert wl is not None

    stream_id = generate_id()

    settings = wl.settings.to_proto()
    p = pathlib.Path(path)

    # update sync_file setting to point to the passed path
    settings.sync_file.value = str(p.absolute())
    settings.sync_dir.value = str(p.parent.absolute())
    settings.files_dir.value = str(p.parent.absolute() / "files")
    settings.x_sync.value = True
    if run_id:
        settings.run_id.value = run_id
    if entity:
        settings.entity.value = entity
    if project:
        settings.project.value = project
    if skip_console:
        settings.console.value = "off"
    if append:
        settings.resume.value = "allow"

    service = wl.ensure_service()
    service.inform_init(settings=settings, run_id=stream_id)

    backend = Backend(settings=wl.settings, service=service)
    backend.ensure_launched()

    assert backend.interface
    backend.interface._stream_id = stream_id  # type: ignore

    handle = backend.interface.deliver_finish_sync()
    result = handle.wait_or(timeout=None)
    response = result.response.sync_response
    if response.url:
        termlog(f"Synced {p} to {util.app_url(response.url)}")
        # create a .synced file in the directory if mark_synced is true
        if mark_synced:
            with open(f"{p}.synced", "w"):
                pass
    else:
        termerror(f"Failed to sync {p}")
    if response.error and response.error.message:
        termerror(response.error.message)

    return response