Skip to content

addon

SiteSyncAddon

Bases: AYONAddon, ITrayAddon, IPluginPaths

Addon handling sync of representation files between sites.

Synchronization server that is syncing published files from local to any of implemented providers (like GDrive, S3 etc.) Runs in the background and checks all representations, looks for files that are marked to be in different location than 'studio' (temporary), checks if 'created_dt' field is present denoting successful sync with provider destination. Sites structure is created during publish OR by calling 'add_site' method.

State of synchronization is being persisted on the server in sitesync_files_status table.

By default it will always contain 1 record with "name" == self.presets["active_site"] per representation_id with state of all its files

Each Tray app has assigned its own self.presets["local_id"] used in sites as a name. Tray is searching only for records where name matches its self.presets["active_site"] + self.presets["remote_site"]. "active_site" could be storage in studio ('studio'), or specific "local_id" when user is working disconnected from home. If the local record has its "created_dt" filled, it is a source and process will try to upload the file to all defined remote sites.

Remote files "id" is real id that could be used in appropriate API. Local files have "id" too, for conformity, contains just file name. It is expected that multiple providers will be implemented in separate classes and registered in 'providers.py'.

Source code in client/ayon_sitesync/addon.py
  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
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
class SiteSyncAddon(AYONAddon, ITrayAddon, IPluginPaths):
    """Addon handling sync of representation files between sites.

    Synchronization server that is syncing published files from local to
    any of implemented providers (like GDrive, S3 etc.)
    Runs in the background and checks all representations, looks for files
    that are marked to be in different location than 'studio' (temporary),
    checks if 'created_dt' field is present denoting successful sync
    with provider destination.
    Sites structure is created during publish OR by calling 'add_site'
    method.

    State of synchronization is being persisted on the server
    in `sitesync_files_status` table.

    By default it will always contain 1 record with
    "name" ==  self.presets["active_site"] per representation_id with state
    of all its files

    Each Tray app has assigned its own  self.presets["local_id"]
    used in sites as a name.
    Tray is searching only for records where name matches its
    self.presets["active_site"] + self.presets["remote_site"].
    "active_site" could be storage in studio ('studio'), or specific
    "local_id" when user is working disconnected from home.
    If the local record has its "created_dt" filled, it is a source and
    process will try to upload the file to all defined remote sites.

    Remote files "id" is real id that could be used in appropriate API.
    Local files have "id" too, for conformity, contains just file name.
    It is expected that multiple providers will be implemented in separate
    classes and registered in 'providers.py'.

    """
    # limit querying DB to look for X number of representations that should
    # be sync, we try to run more loops with less records
    # actual number of files synced could be lower as providers can have
    # different limits imposed by its API
    # set 0 to no limit
    REPRESENTATION_LIMIT = 100
    DEFAULT_SITE = "studio"
    LOCAL_SITE = "local"
    LOG_PROGRESS_SEC = 5  # how often log progress to DB
    DEFAULT_PRIORITY = 50  # higher is better, allowed range 1 - 1000

    name = "sitesync"
    version = __version__

    def initialize(self, addon_settings):
        """Called during Addon Manager creation.

        Collects needed data, checks asyncio presence.
        Sets 'enabled' according to global settings for the addon.
        Shouldn't be doing any initialization, that's a job for 'tray_init'
        """

        # some parts of code need to run sequentially, not in async
        self.lock = None
        self._sync_studio_settings = None
        # settings for all enabled projects for sync
        self._sync_project_settings = None
        self.sitesync_thread = None  # asyncio requires new thread

        self._paused = False
        self._paused_projects = set()
        self._paused_representations = set()
        self._anatomies = {}

        # list of long blocking tasks
        self.long_running_tasks = deque()
        # projects that long tasks are running on
        self.projects_processed = set()

    @property
    def endpoint_prefix(self):
        return "addons/{}/{}".format(self.name, self.version)

    def get_plugin_paths(self):
        return {
            "publish": os.path.join(SYNC_ADDON_DIR, "plugins", "publish")
        }

    def get_site_icons(self):
        """Icons for sites.

        Returns:
            dict[str, str]: Path to icon by site.

        """
        resource_path = os.path.join(
            SYNC_ADDON_DIR, "providers", "resources"
        )
        icons = {}
        for file_path in os.listdir(resource_path):
            if not file_path.endswith(".png"):
                continue
            provider_name, _ = os.path.splitext(os.path.basename(file_path))
            icons[provider_name] = {
                "type": "path",
                "path": os.path.join(resource_path, file_path)
            }
        return icons

    def get_launch_hook_paths(self):
        """Implementation for applications launch hooks.

        Returns:
            str: full absolut path to directory with hooks for the addon

        """
        return os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "launch_hooks"
        )

    # --- Public API ---
    def add_site(
        self,
        project_name,
        representation_id,
        site_name=None,
        file_id=None,
        force=False,
        status=SiteSyncStatus.QUEUED
    ):
        """Adds new site to representation to be synced.

        'project_name' must have synchronization enabled (globally or
        project only)

        Used as a API endpoint from outside applications (Loader etc).

        Use 'force' to reset existing site.

        Args:
            project_name (str): Project name.
            representation_id (str): Representation id.
            site_name (str): Site name of configured site.
            file_id (str): File id.
            force (bool): Reset site if exists.
            status (SiteSyncStatus): Current status,
                default SiteSyncStatus.QUEUED

        Raises:
            SiteAlreadyPresentError: If adding already existing site and
                not 'force'
            ValueError: other errors (repre not found, misconfiguration)

        """
        if not self.get_sync_project_setting(project_name):
            raise ValueError("Project not configured")

        if not site_name:
            site_name = self.DEFAULT_SITE

        representation = get_representation_by_id(
            project_name, representation_id
        )

        files = representation.get("files", [])
        if not files:
            self.log.debug("No files for {}".format(representation_id))
            return

        if not force:
            existing = self.get_repre_sync_state(
                project_name,
                representation_id,
                site_name
            )
            if existing:
                failure = True
                if file_id:
                    file_exists = existing.get("files", {}).get(file_id)
                    if not file_exists:
                        failure = False

                if failure:
                    msg = "Site {} already present".format(site_name)
                    self.log.info(msg)
                    raise SiteAlreadyPresentError(msg)

        new_site_files = [
            {
                "size": repre_file["size"],
                "status": status,
                "timestamp": datetime.now().timestamp(),
                "id": repre_file["id"],
                "fileHash": repre_file["hash"]
            }
            for repre_file in files
        ]

        payload_dict = {"files": new_site_files}
        representation_id = representation_id.replace("-", "")

        self._set_state_sync_state(
            project_name, representation_id, site_name, payload_dict
        )

    def remove_site(
        self,
        project_name,
        representation_id,
        site_name,
        remove_local_files=False
    ):
        """Removes site for particular representation in project.

        Args:
            project_name (str): project name (must match DB)
            representation_id (str): MongoDB _id value
            site_name (str): name of configured and active site
            remove_local_files (bool): remove only files for 'local_id'
                site

        Raises:
            ValueError: Throws if any issue.

        """
        if not self.get_sync_project_setting(project_name):
            raise ValueError("Project not configured")

        sync_info = self.get_repre_sync_state(
            project_name,
            representation_id,
            site_name
        )
        if not sync_info:
            msg = "Site {} not found".format(site_name)
            self.log.warning(msg)
            return

        endpoint = "{}/{}/state/{}/{}".format(
            self.endpoint_prefix,
            project_name,
            representation_id,
            site_name
        )

        response = ayon_api.delete(endpoint)
        if response.status_code not in [200, 204]:
            raise RuntimeError("Cannot update status")

        if remove_local_files:
            self._remove_local_file(project_name, representation_id, site_name)

    def compute_resource_sync_sites(self, project_name):
        """Get available resource sync sites state for publish process.

        Returns dict with prepared state of sync sites for 'project_name'.
        It checks if Site Sync is enabled, handles alternative sites.
        Publish process stores this dictionary as a part of representation
        document in DB.

        Example:
        [
            {
                'name': '42abbc09-d62a-44a4-815c-a12cd679d2d7',
                'status': SiteSyncStatus.OK
            },
            {'name': 'studio', 'status': SiteSyncStatus.QUEUED},
            {'name': 'SFTP', 'status': SiteSyncStatus.QUEUED}
        ] -- representation is published locally, artist or Settings have set
        remote site as 'studio'. 'SFTP' is alternate site to 'studio'. Eg.
        whenever file is on 'studio', it is also on 'SFTP'.
        """

        def create_metadata(name, created=True):
            """Create sync site metadata for site with `name`"""
            if created:
                status = SiteSyncStatus.OK
            else:
                status = SiteSyncStatus.QUEUED
            return {"name": name, "status": status}

        if (
            not self.sync_studio_settings["enabled"]
            or not self.sync_project_settings[project_name]["enabled"]
        ):
            return [create_metadata(self.DEFAULT_SITE)]

        local_site = self.get_active_site(project_name)
        remote_site = self.get_remote_site(project_name)

        # Attached sites metadata by site name
        # That is the local site, remote site, the always accesible sites
        # and their alternate sites (alias of sites with different protocol)
        attached_sites = {
            local_site: create_metadata(local_site)
        }
        if remote_site and remote_site not in attached_sites:
            attached_sites[remote_site] = create_metadata(
                remote_site, created=False
            )

        attached_sites = self._add_alternative_sites(
            project_name, attached_sites)
        # add skeleton for sites where it should be always synced to
        # usually it would be a backup site which is handled by separate
        # background process
        for site_name in self._get_always_accessible_sites(project_name):
            if site_name not in attached_sites:
                attached_sites[site_name] = (
                    create_metadata(site_name, created=False))
        unique_sites = {
            site["name"]: site
            for site in attached_sites.values()
        }
        return list(unique_sites.values())

    def _get_always_accessible_sites(self, project_name):
        """Sites that synced to as a part of background process.

        Artist machine doesn't handle those, explicit Tray with that site name
        as a local id must be running.
        Example is dropbox site serving as a backup solution

        Returns:
            (list[str]): list of site names
        """
        sync_settings = self.get_sync_project_setting(project_name)
        always_accessible_sites = (
            sync_settings["config"].get("always_accessible_on", [])
        )
        return [site_name.strip() for site_name in always_accessible_sites]

    def _add_alternative_sites(self, project_name, attached_sites):
        """Add skeleton document for alternative sites

        Each new configured site in System Setting could serve as a alternative
        site, it's a kind of alias. It means that files on 'a site' are
        physically accessible also on 'a alternative' site.
        Example is sftp site serving studio files via sftp protocol, physically
        file is only in studio, sftp server has this location mounted.

        Returns:
            (dict[str, dict])
        """
        sync_project_settings = self.get_sync_project_setting(project_name)
        all_sites = sync_project_settings["sites"]

        alt_site_pairs = self._get_alt_site_pairs(all_sites)

        for site_name in all_sites.keys():
            # Get alternate sites (stripped names) for this site name
            alt_sites = {
                site.strip()
                for site in alt_site_pairs.get(site_name)
            }

            # If no alternative sites we don't need to add
            if not alt_sites:
                continue

            # Take a copy of data of the first alternate site that is already
            # defined as an attached site to match the same state.
            match_meta = next(
                (
                    attached_sites[site]
                    for site in alt_sites
                    if site in attached_sites
                ),
                None
            )
            if not match_meta:
                continue

            alt_site_meta = copy.deepcopy(match_meta)
            alt_site_meta["name"] = site_name

            # Note: We change mutable `attached_site` dict in-place
            attached_sites[site_name] = alt_site_meta

        return attached_sites

    def _get_alt_site_pairs(self, conf_sites):
        """Returns dict of site and its alternative sites.

        If `site` has alternative site, it means that alt_site has 'site' as
        alternative site

        Args:
            conf_sites (dict)

        Returns:
            dict[str, list[str]]: {'site': [alternative sites]...}

        """
        alt_site_pairs = defaultdict(set)
        for site_name, site_info in conf_sites.items():
            alt_sites = set(site_info.get("alternative_sites", []))
            alt_site_pairs[site_name].update(alt_sites)

            for alt_site in alt_sites:
                alt_site_pairs[alt_site].add(site_name)

        for site_name, alt_sites in alt_site_pairs.items():
            sites_queue = deque(alt_sites)
            while sites_queue:
                alt_site = sites_queue.popleft()

                # safety against wrong config
                # {"SFTP": {"alternative_site": "SFTP"}
                if alt_site == site_name or alt_site not in alt_site_pairs:
                    continue

                for alt_alt_site in alt_site_pairs[alt_site]:
                    if (
                        alt_alt_site != site_name
                        and alt_alt_site not in alt_sites
                    ):
                        alt_sites.add(alt_alt_site)
                        sites_queue.append(alt_alt_site)

        return alt_site_pairs

    def clear_project(self, project_name, site_name):
        """
            Clear 'project_name' of 'site_name' and its local files

            Works only on real local sites, not on 'studio'
        """

        # TODO implement
        self.log.warning("Method 'clear_project' is not implemented.")

        # query = {
        #     "type": "representation",
        #     "files.sites.name": site_name
        # }
        #
        # # TODO currently not possible to replace with get_representations
        # representations = list(
        #     self.connection.database[project_name].find(query))
        # if not representations:
        #     self.log.debug("No repre found")
        #     return
        #
        # for repre in representations:
        #     self.remove_site(project_name, repre.get("_id"), site_name, True)

    # TODO hook to some trigger - no Sync Queue anymore
    def validate_project(self, project_name, site_name, reset_missing=False):
        """Validate 'project_name' of 'site_name' and its local files

        If file present and not marked with a 'site_name' in DB, DB is
        updated with site name and file modified date.

        Args:
            project_name (str): project name
            site_name (str): active site name
            reset_missing (bool): if True reset site in DB if missing
                physically to be resynched
        """
        self.log.debug("Validation of {} for {} started".format(
            project_name, site_name
        ))
        repre_entities = list(get_representations(project_name))
        if not repre_entities:
            self.log.debug("No repre found")
            return

        sites_added = 0
        sites_reset = 0
        repre_ids = [repre["id"] for repre in repre_entities]
        repre_states = self.get_representations_sync_state(
            project_name, repre_ids, site_name, site_name)

        for repre_entity in repre_entities:
            repre_id = repre_entity["id"]
            is_on_site = False
            repre_state = repre_states.get(repre_id)
            if repre_state:
                is_on_site = repre_state[0] == SiteSyncStatus.OK
            for repre_file in repre_entity.get("files", []):
                file_path = repre_file.get("path", "")
                local_file_path = self.get_local_file_path(
                    project_name, site_name, file_path
                )

                file_exists = (
                    local_file_path and os.path.exists(local_file_path)
                )
                if not is_on_site:
                    if file_exists:
                        self.log.debug(
                            f"Adding presence on site '{site_name}' for "
                            f"'{repre_id}'"
                        )
                        self.add_site(
                            project_name,
                            repre_id,
                            site_name=site_name,
                            file_id=repre_file["id"],
                            force=True,
                            status=SiteSyncStatus.OK
                        )
                        sites_added += 1
                else:
                    if not file_exists and reset_missing:
                        self.log.debug(
                            "Resetting site {} for {}".format(
                                site_name, repre_id
                            ))
                        self.reset_site_on_representation(
                            project_name,
                            repre_id,
                            site_name=site_name,
                            file_id=repre_file["_id"]
                        )
                        sites_reset += 1

        if sites_added % 100 == 0:
            self.log.debug("Sites added {}".format(sites_added))

        self.log.debug("Validation of {} for {} ended".format(
            project_name, site_name
        ))
        self.log.info("Sites added {}, sites reset {}".format(
            sites_added, reset_missing
        ))

    # TODO hook to some trigger - no Sync Queue anymore
    def pause_representation(
        self, project_name, representation_id, site_name
    ):
        """Pause sync of representation entity on site.

        Sets 'representation_id' as paused, eg. no syncing should be
            happening on it.

        Args:
            project_name (str): Project name.
            representation_id (str): Representation id.
            site_name (str): Site name 'gdrive', 'studio' etc.

        """
        self.log.info("Pausing SiteSync for {}".format(representation_id))
        self._paused_representations.add(representation_id)
        repre_entity = get_representation_by_id(
            project_name, representation_id
        )
        self.update_db(project_name, repre_entity, site_name, pause=True)

    # TODO hook to some trigger - no Sync Queue anymore
    def unpause_representation(
        self, project_name, representation_id, site_name
    ):
        """Unpause sync of representation entity on site.

        Does not fail or warn if repre wasn't paused.

        Args:
            project_name (str): Project name.
            representation_id (str): Representation id.
            site_name (str): Site name 'gdrive', 'studio' etc.
        """
        self.log.info("Unpausing SiteSync for {}".format(representation_id))
        try:
            self._paused_representations.remove(representation_id)
        except KeyError:
            pass
        # self.paused_representations is not persistent
        repre_entity = get_representation_by_id(
            project_name, representation_id
        )
        self.update_db(project_name, repre_entity, site_name, pause=False)

    def is_representation_paused(
        self, representation_id, check_parents=False, project_name=None
    ):
        """Is representation paused.

        Args:
            representation_id (str): Representation id.
            check_parents (bool): Check if parent project or server itself
                are not paused.
            project_name (str): Project to check if paused.

            if 'check_parents', 'project_name' should be set too

        Returns:
            bool: Is representation paused now.

        """
        is_paused = representation_id in self._paused_representations
        if check_parents and project_name:
            is_paused = (
                is_paused
                or self.is_project_paused(project_name)
                or self.is_paused()
            )
        return is_paused

    # TODO hook to some trigger - no Sync Queue anymore
    def pause_project(self, project_name):
        """Pause sync of whole project.

        Args:
            project_name (str): Project name.

        """
        self.log.info("Pausing SiteSync for {}".format(project_name))
        self._paused_projects.add(project_name)

    # TODO hook to some trigger - no Sync Queue anymore
    def unpause_project(self, project_name):
        """Unpause sync of whole project.

        Does not fail or warn if project wasn't paused.

        Args:
            project_name (str): Project name.

        """
        self.log.info("Unpausing SiteSync for {}".format(project_name))
        try:
            self._paused_projects.remove(project_name)
        except KeyError:
            pass

    def is_project_paused(self, project_name, check_parents=False):
        """Is project sync paused.

        Args:
            project_name (str):
            check_parents (bool): check if server itself
                is not paused

        Returns:
            bool: Is project paused.

        """
        is_paused = project_name in self._paused_projects
        if check_parents:
            is_paused = is_paused or self.is_paused()
        return is_paused

    # TODO hook to some trigger - no Sync Queue anymore
    def pause_server(self):
        """Pause sync server.

        It won't check anything, not uploading/downloading...
        """
        self.log.info("Pausing SiteSync")
        self._paused = True

    def unpause_server(self):
        """Unpause server sync."""
        self.log.info("Unpausing SiteSync")
        self._paused = False

    def is_paused(self):
        """ Is server paused """
        return self._paused

    def get_active_site_type(self, project_name, local_settings=None):
        """Active site which is defined by artist.

        Unlike 'get_active_site' is this method also checking local settings
        where might be different active site set by user. The output is limited
        to "studio" and "local".

        This method is used by Anatomy.

        Todos:
            Check if sync server is enabled for the project.
            - To be able to do that the sync settings MUST NOT be cached for
                all projects at once. The sync settings preparation for all
                projects is reasonable only in sync server loop.
            `local_settings` is probably obsolete in AYON

        Args:
            project_name (str): Name of project where to look for active site.
            local_settings (Optional[dict[str, Any]]): Prepared local settings.

        Returns:
            Literal["studio", "local"]: Active site.
        """
        if not self.enabled:
            return "studio"

        sync_project_settings = self.get_sync_project_setting(project_name)

        if not sync_project_settings["enabled"]:
            return "studio"

        return (
            sync_project_settings["local_setting"].get("active_site")
            or sync_project_settings["config"]["active_site"]
        )

    def get_active_site(self, project_name):
        """Returns active (mine) site for project from settings.

        Output logic:
            - 'studio' if Site Sync is disabled
            - value from 'get_local_site_id' if active site is 'local'
            - any other site name from local settings
                or project settings (site could be forced from PS)

        Returns:
            str: Site name.

        """
        active_site_type = self.get_active_site_type(project_name)
        if active_site_type == self.LOCAL_SITE:
            return get_local_site_id()
        return active_site_type

    # remote site
    def get_remote_site(self, project_name):
        """Remote (theirs) site for project from settings."""
        sync_project_settings = self.get_sync_project_setting(project_name)
        remote_site = (
            sync_project_settings["local_setting"].get("remote_site")
            or sync_project_settings["config"]["remote_site"]
        )
        if remote_site == self.LOCAL_SITE:
            return get_local_site_id()

        return remote_site

    def get_site_root_overrides(
        self, project_name, site_name, local_settings=None
    ):
        """Get root overrides for project on a site.

        Implemented to be used in 'Anatomy' for other than 'studio' site.

        Args:
            project_name (str): Project for which root overrides should be
                received.
            site_name (str): Name of site for which should be received roots.
            local_settings (Optional[dict[str, Any]]): Prepare local settigns
                values.

        Returns:
            Union[dict[str, Any], None]: Root overrides for this machine.

            {"work": "c:/projects_local"}
        """

        # Validate that site name is valid
        if site_name not in ("studio", "local"):
            # Consider local site id as 'local'
            if site_name != get_local_site_id():
                raise ValueError((
                    "Root overrides are available only for"
                    " default sites not for \"{}\""
                ).format(site_name))
            site_name = "local"

        sitesync_settings = self.get_sync_project_setting(project_name)

        roots = {}
        if not sitesync_settings["enabled"]:
            return roots
        local_project_settings = sitesync_settings["local_setting"]
        if site_name == "local":
            for root_info in local_project_settings["local_roots"]:
                roots[root_info["name"]] = root_info["path"]

        return roots

    def get_local_normalized_site(self, site_name):
        """Normlize local site name.

         Return 'local' if 'site_name' is local id.

        In some places Settings or Local Settings require 'local' instead
        of real site name.

        Returns:
            str: Normalized site name.

        """
        if site_name == get_local_site_id():
            site_name = self.LOCAL_SITE

        return site_name

    def is_representation_on_site(
        self, project_name, representation_id, site_name, max_retries=None
    ):
        """Check if representation has all files available on site.

        Args:
            project_name (str)
            representation_id (str)
            site_name (str)
            max_retries (int) (optional) - provide only if method used in while
                loop to bail out

        Returns:
            bool: True if representation has all files correctly on the site.

        Raises:
              ValueError  Only If 'max_retries' provided if upload/download
                failed too many times to limit infinite loop check.

        """
        representation_status = self.get_repre_sync_state(
            project_name, representation_id, site_name)
        if not representation_status:
            return False

        if site_name == get_local_site_id():
            status = representation_status["localStatus"]
        else:
            status = representation_status["remoteStatus"]

        if max_retries:
            tries = status.get("retries", 0)
            if tries >= max_retries:
                raise ValueError("Failed too many times")

        return status["status"] == SiteSyncStatus.OK

    def _reset_timer_with_rest_api(self):
        # POST to webserver sites to add to representations
        webserver_url = os.environ.get("AYON_WEBSERVER_URL")
        if not webserver_url:
            self.log.warning("Couldn't find webserver url")
            return

        rest_api_url = "{}/sitesync/reset_timer".format(
            webserver_url
        )

        try:
            import requests
        except Exception:
            self.log.warning(
                "Couldn't add sites to representations "
                "('requests' is not available)"
            )
            return

        requests.post(rest_api_url)

    def get_enabled_projects(self):
        """Returns list of projects which have SiteSync enabled."""
        enabled_projects = []

        if self.enabled:
            for project_name in get_project_names():
                if self.is_project_enabled(project_name):
                    enabled_projects.append(project_name)

        return enabled_projects

    def is_project_enabled(self, project_name, single=False):
        """Checks if 'project_name' is enabled for syncing.
        'get_sync_project_setting' is potentially expensive operation (pulls
        settings for all projects if cached version is not available), using
        project_settings for specific project should be faster.
        Args:
            project_name (str)
            single (bool): use 'get_addon_project_settings' method
        """
        if self.enabled:
            if single:
                project_settings = get_addon_project_settings(
                    self.name, self.version, project_name
                )
            else:
                project_settings = self.get_sync_project_setting(project_name)
            if project_settings and project_settings.get("enabled"):
                return True
        return False

    def handle_alternate_site(
        self, project_name, representation_id, processed_site, file_id
    ):
        """
        For special use cases where one site vendors another.

        Current use case is sftp site vendoring (exposing) same data as
        regular site (studio). Each site is accessible for different
        audience. 'studio' for artists in a studio, 'sftp' for externals.

        Change of file status on one site actually means same change on
        'alternate' site. (eg. artists publish to 'studio', 'sftp' is using
        same location >> file is accessible on 'sftp' site right away.

        Args:
            project_name (str): Project name.
            representation_id (str): Representation id.
            processed_site (str): Real site_name of published/uploaded file
            file_id (str): File id of file handled.

        """
        sites = self._transform_sites_from_settings(self.sync_studio_settings)
        sites[self.DEFAULT_SITE] = {
            "provider": "local_drive",
            "alternative_sites": []
        }

        alternate_sites = []
        for site_name, site_info in sites.items():
            conf_alternative_sites = site_info.get("alternative_sites", [])
            if processed_site in conf_alternative_sites:
                alternate_sites.append(site_name)
                continue
            if processed_site == site_name and conf_alternative_sites:
                alternate_sites.extend(conf_alternative_sites)
                continue

        if not alternate_sites:
            return

        sync_state = self.get_repre_sync_state(
            project_name,
            representation_id,
            processed_site
        )
        # not yet available on processed_site, wont update alternate site yet
        if not sync_state:
            return
        for file_info in sync_state["files"]:
            # expose status of remote site, it is expected on the server
            file_info["status"] = file_info["remoteStatus"]["status"]

        payload_dict = {"files": sync_state["files"]}

        alternate_sites = set(alternate_sites)
        for alt_site in alternate_sites:
            self.log.debug("Adding alternate {} to {}".format(
                alt_site, representation_id))

            self._set_state_sync_state(
                project_name,
                representation_id,
                alt_site,
                payload_dict
            )

    # TODO - for Loaders
    def get_repre_info_for_versions(
        self, project_name, version_ids, active_site, remote_site
    ):
        """Returns representation for versions and sites combi

        Args:
            project_name (str): Project name
            version_ids (Iterable[str]): Version ids.
            active_site (str): 'local', 'studio' etc
            remote_site (str): dtto

        Returns:

        """
        version_ids = set(version_ids)
        endpoint = "{}/projects/{}/sitesync/state".format(
            self.endpoint_prefix, project_name
        )

        # get to upload
        kwargs = {
            "localSite": active_site,
            "remoteSite": remote_site,
            "versionIdFilter": list(version_ids)
        }

        # kwargs["representationId"] = "94dca33a-7705-11ed-8c0a-34e12d91d510"

        response = ayon_api.get(endpoint, **kwargs)
        repre_states = response.data.get("representations", [])
        repre_info_by_version_id = {
            version_id: {
                "id": version_id,
                "repre_count": 0,
                "avail_repre_local": 0,
                "avail_repre_remote": 0,
            }
            for version_id in version_ids
        }
        repre_states_by_version_id = defaultdict(list)
        for repre_state in repre_states:
            version_id = repre_state["versionId"]
            repre_states_by_version_id[version_id].append(repre_state)

        for version_id, repre_states in repre_states_by_version_id.items():
            repre_info = repre_info_by_version_id[version_id]
            repre_info["repre_count"] = len(repre_states)
            repre_info["avail_repre_local"] = sum(
                self._is_available(repre_state, "localStatus")
                for repre_state in repre_states
            )
            repre_info["avail_repre_remote"] = sum(
                self._is_available(repre_state, "remoteStatus")
                for repre_state in repre_states
            )

        return list(repre_info_by_version_id.values())
    # --- End of Public API ---

    def _is_available(self, repre, status):
        """Helper to decide if repre is download/uploaded on site.

        Returns:
            int: 1 if available, 0 if not.

        """
        return int(repre[status]["status"] == SiteSyncStatus.OK)

    def get_local_file_path(self, project_name, site_name, file_path):
        """Externalized for app.

        Args:
            project_name (str): Project name.
            site_name (str): Site name.
            file_path (str): File path from other site.

        Returns:
            str: Resolved local path.

        """
        handler = LocalDriveHandler(project_name, site_name)
        local_file_path = handler.resolve_path(file_path)

        return local_file_path

    def tray_init(self):
        """Initialization of Site Sync Server for Tray.

        Called when tray is initialized, it checks if addon should be
        enabled. If not, no initialization necessary.
        """
        self.server_init()

    def server_init(self):
        """Actual initialization of Sync Server."""
        # import only in tray or Python3, because of Python2 hosts
        if not self.enabled:
            return

        from .sitesync import SiteSyncThread

        self.lock = threading.Lock()

        self.sitesync_thread = SiteSyncThread(self)

    def tray_start(self):
        """Triggered when Tray is started.

        Checks if configuration presets are available and if there is
        any provider ('gdrive', 'S3') that is activated
        (eg. has valid credentials).
        """
        self.server_start()

    def server_start(self):
        if self.enabled:
            self.sitesync_thread.start()
        else:
            self.log.info(
                "SiteSync is not enabled. Site Sync server was not started."
            )

    def tray_exit(self):
        """Stops sync thread if running.

        Called from Addon Manager
        """
        self.server_exit()

    def server_exit(self):
        if not self.sitesync_thread:
            return

        if not self.is_running:
            return
        try:
            self.log.info("Stopping sync server server")
            self.sitesync_thread.is_running = False
            self.sitesync_thread.stop()
            self.log.info("Sync server stopped")
        except Exception:
            self.log.warning(
                "Error has happened during Killing sync server",
                exc_info=True
            )

    def tray_menu(self, parent_menu):
        pass

    @property
    def is_running(self):
        return self.sitesync_thread.is_running

    def get_anatomy(self, project_name):
        """Get already created or newly created anatomy for project

        Args:
            project_name (str): Project name.

        Return:
            Anatomy: Project anatomy object.
        """
        from ayon_core.pipeline import Anatomy

        return self._anatomies.get(project_name) or Anatomy(project_name)

    @property
    def sync_studio_settings(self):
        if self._sync_studio_settings is None:
            self._sync_studio_settings = (
                get_studio_settings().get(self.name)
            )

        return self._sync_studio_settings

    @property
    def sync_project_settings(self):
        if self._sync_project_settings is None:
            self.set_sync_project_settings()

        return self._sync_project_settings

    def set_sync_project_settings(self, exclude_locals=False):
        """
            Set sync_project_settings for all projects (caching)
            Args:
                exclude_locals (bool): ignore overrides from Local Settings
            For performance
        """
        sync_project_settings = self._prepare_sync_project_settings(
            exclude_locals)

        self._sync_project_settings = sync_project_settings

    def _prepare_sync_project_settings(self, exclude_locals):
        sync_project_settings = {}

        sites = self._transform_sites_from_settings(
            self.sync_studio_settings)

        project_names = get_project_names()
        for project_name in project_names:
            project_sites = copy.deepcopy(sites)
            project_settings = get_addon_project_settings(
                self.name, self.version, project_name)

            project_sites.update(self._get_default_site_configs(
                project_settings["enabled"], project_name, project_settings
            ))

            project_sites.update(
                self._transform_sites_from_settings(project_settings))

            project_settings["sites"] = project_sites

            sync_project_settings[project_name] = project_settings

        if not sync_project_settings:
            self.log.info("No enabled and configured projects for sync.")
        return sync_project_settings

    def get_sync_project_setting(
        self, project_name, exclude_locals=False, cached=True
    ):
        """ Handles pulling sitesync's settings for enabled 'project_name'

        Args:
            project_name (str): used in project settings
            exclude_locals (bool): ignore overrides from Local Settings
            cached (bool): use pre-cached values, or return fresh ones
                cached values needed for single loop (with all overrides)
                fresh values needed for Local settings (without overrides)

        Returns:
            dict: settings dictionary for the enabled project,
                empty if no settings or sync is disabled

        """
        # presets set already, do not call again and again
        # self.log.debug("project preset {}".format(self.presets))
        if not cached:
            return self._prepare_sync_project_settings(exclude_locals)\
                [project_name]

        if (
            not self.sync_project_settings
            or not self.sync_project_settings.get(project_name)
        ):
            self.set_sync_project_settings(exclude_locals)
        return self.sync_project_settings.get(project_name)

    def _transform_sites_from_settings(self, settings):
        """Transforms list of 'sites' from Setting to dict.

        It processes both System and Project Settings as they have same format.
        """
        sites = {}
        if not self.enabled:
            return sites

        for whole_site_info in settings.get("sites", []):
            site_name = whole_site_info["name"]
            provider_specific = copy.deepcopy(
                whole_site_info[whole_site_info["provider"]]
            )
            configured_site = {
                "enabled": True,
                "alternative_sites": whole_site_info["alternative_sites"],
                "root": provider_specific.pop("roots", None)
            }
            configured_site.update(provider_specific)

            sites[site_name] = configured_site
        return sites

    def _get_project_roots_for_site(self, project_name, site_name=None):
        """Returns projects roots and their overrides."""
        # overrides for Studio site for particular user
        # TODO temporary to get roots without overrides
        # ayon_api.get_project_roots_by_site returns only overrides.
        # Should be replaced when ayon_api implements `siteRoots` method
        if not site_name:
            site_name = get_local_site_id()
        platform_name = platform.system().lower()
        roots = ayon_api.get(
            f"projects/{project_name}/siteRoots",
            platform=platform_name
        ).data
        root_overrides = get_project_roots_for_site(project_name, site_name)
        for key, value in roots.items():
            override = root_overrides.get(key)
            if override:
                roots[key] = override

        return roots

    def _get_default_site_configs(
        self, sync_enabled=True, project_name=None, project_settings=None
    ):
        """Settings for 'studio' and user's local site

        Returns base values from setting, not overridden by Local Settings,
        eg. value used to push TO LS not to get actual value for syncing.

        Args:
            sync_enabled (Optional[bool]): Is sync enabled.
            project_name (Optional[str]): Project name.
            project_settings (Optional[dict]): Project settings.

        """
        local_site_id = get_local_site_id()
        roots = self._get_project_roots_for_site(project_name, local_site_id)
        studio_config = {
            "enabled": True,
            "provider": "local_drive",
            "root": roots
        }
        all_sites = {self.DEFAULT_SITE: studio_config}
        if sync_enabled:
            roots = project_settings["local_setting"]["local_roots"]
            local_site_dict = {
                "enabled": True,
                "provider": "local_drive",
                "root": roots
            }
            all_sites[local_site_id] = local_site_dict
            # duplicate values for normalized local name
            all_sites["local"] = local_site_dict
        return all_sites

    def get_provider_for_site(self, project_name=None, site=None):
        """Get provider name for site (unique name across all projects)."""
        sites = {
            self.DEFAULT_SITE: "local_drive",
            self.LOCAL_SITE: "local_drive",
            get_local_site_id(): "local_drive"
        }

        if site in sites.keys():
            return sites[site]

        # backward compatibility
        if project_name:
            proj_settings = self.get_sync_project_setting(project_name)
            provider = (
                proj_settings
                .get("sites", {})
                .get(site, {})
                .get("provider")
            )
            if provider:
                return provider

        sync_sett = self.sync_studio_settings
        for site_config in sync_sett.get("sites"):
            sites[site_config["name"]] = site_config["provider"]

        return sites.get(site, "N/A")

    @time_function
    def get_sync_representations(
        self, project_name, active_site, remote_site, limit=10
    ):
        """
            Get representations that should be synced, these could be
            recognised by presence of document in 'files.sites', where key is
            a provider (GDrive, S3) and value is empty document or document
            without 'created_dt' field. (Don't put null to 'created_dt'!).

            Querying of 'to-be-synched' files is offloaded to Mongod for
            better performance. Goal is to get as few representations as
            possible.
        Args:
            project_name (str):
            active_site (str): identifier of current active site (could be
                'local_0' when working from home, 'studio' when working in the
                studio (default)
            remote_site (str): identifier of remote site I want to sync to

        Returns:
            list[dict]: Representation states.

        """
        self.log.debug("Check representations for: {}-{}".format(
            active_site, remote_site
        ))

        endpoint = "{}/{}/state".format(
            self.endpoint_prefix, project_name
        )

        # get to upload
        kwargs = {
            "localSite": active_site,
            "remoteSite": remote_site,
            "localStatusFilter": [SiteSyncStatus.OK],
            "remoteStatusFilter": [SiteSyncStatus.QUEUED],
        }

        response = ayon_api.get(endpoint, **kwargs)
        if response.status_code not in [200, 204]:
            raise RuntimeError(
                "Cannot get representations for sync with code {}".format(
                    response.status_code
                )
            )

        repre_states = response.data["representations"]

        # get to download
        if len(repre_states) < limit:
            kwargs["localStatusFilter"] = [SiteSyncStatus.QUEUED]
            kwargs["remoteStatusFilter"] = [SiteSyncStatus.OK]

            response = ayon_api.get(endpoint, **kwargs)
            repre_states.extend(response.data["representations"])

        return repre_states

    def check_status(self, file_state, local_site, remote_site, config_preset):
        """Check synchronization status of a file.

        The file is on representation status is checked for single 'provider'.
            (Eg. check if 'scene.ma' of lookdev.v10 should be synced to GDrive

        Always is comparing local record, eg. site with
            'name' == self.presets[PROJECT_NAME]["config"]["active_site"]

        This leads to trigger actual upload or download, there is
            a use case 'studio' <> 'remote' where user should publish
            to 'studio', see progress in Tray GUI, but do not do
            physical upload/download
            (as multiple user would be doing that).

            Do physical U/D only when any of the sites is user's local, in that
            case only user has the data and must U/D.

        Args:
            file_state (dict): File info from site sync database.
            local_site (str): Local site of compare (usually 'studio').
            remote_site (str): Remote site (gdrive etc).
            config_preset (dict): Config about active site, retries.

        Returns:
            int: Sync status value of representation.

        """
        if get_local_site_id() not in (local_site, remote_site):
            # don't do upload/download for studio sites
            self.log.debug(
                "No local site {} - {}".format(local_site, remote_site)
            )
            return SyncStatus.DO_NOTHING

        local_status = file_state["localStatus"]["status"]
        remote_status = file_state["remoteStatus"]["status"]

        if (
            local_status != SiteSyncStatus.OK
            and remote_status == SiteSyncStatus.OK
        ):
            retries = file_state["localStatus"]["retries"]
            if retries < int(config_preset["retry_cnt"]):
                return SyncStatus.DO_DOWNLOAD

        if (
            remote_status != SiteSyncStatus.OK
            and local_status == SiteSyncStatus.OK
        ):
            retries = file_state["remoteStatus"]["retries"]
            if retries < int(config_preset["retry_cnt"]):
                return SyncStatus.DO_UPLOAD

        return SyncStatus.DO_NOTHING

    def update_db(
        self,
        project_name,
        repre_status,
        site_name,
        new_file_id=None,
        file=None,
        side=None,
        error=None,
        progress=None,
        priority=None,
        pause=None
    ):
        """Update 'provider' portion of records in DB.

        Args:
            project_name (str): Project name. Force to db connection as
                each file might come from different collection.
            repre_status (dict): Representation status from sitesync database.
            site_name (str): Site name.
            new_file_id (Optional[str]): File id of new file.
            file (dict[str, Any]): info about processed file (pulled from DB)
            side (str): 'local' | 'remote'
            error (str): exception message
            progress (float): 0-1 of progress of upload/download
            priority (int): 0-100 set priority
            pause (bool): stop synchronizing (only before starting of download,
                upload)

        Returns:
            None
        """
        files_status = []
        for file_status in repre_status["files"]:
            status_entity = copy.deepcopy(
                file_status["{}Status".format(side)]
            )
            status_entity["fileHash"] = file_status["fileHash"]
            status_entity["id"] = file_status["id"]
            if file_status["fileHash"] == file["fileHash"]:
                if new_file_id:
                    status_entity["status"] = SiteSyncStatus.OK
                    status_entity.pop("message")
                    status_entity.pop("retries")
                elif progress is not None:
                    status_entity["status"] = SiteSyncStatus.IN_PROGRESS
                    status_entity["progress"] = progress
                elif error:
                    max_retries = int(
                        self.sync_project_settings
                        [project_name]
                        ["config"]
                        ["retry_cnt"]
                    )
                    tries = status_entity.get("retries", 0)
                    tries += 1
                    status_entity["retries"] = tries
                    status_entity["message"] = error
                    if tries >= max_retries:
                        status_entity["status"] = SiteSyncStatus.FAILED
                elif pause is not None:
                    if pause:
                        status_entity["pause"] = True
                    else:
                        status_entity.remove("pause")
                files_status.append(status_entity)

        representation_id = repre_status["representationId"]

        endpoint = "{}/{}/state/{}/{}".format(
            self.endpoint_prefix,
            project_name,
            representation_id,
            site_name)

        # get to upload
        kwargs = {
            "files": files_status
        }

        if priority:
            kwargs["priority"] = priority

        response = ayon_api.post(endpoint, **kwargs)
        if response.status_code not in [200, 204]:
            raise RuntimeError("Cannot update status")

        if progress is not None or priority is not None:
            return

        status = "failed"
        error_str = "with error {}".format(error)
        if new_file_id:
            status = "succeeded with id {}".format(new_file_id)
            error_str = ""

        source_file = file.get("path", "")

        self.log.debug(
            "File for {} - {source_file} process {status} {error_str}".format(
                representation_id,
                status=status,
                source_file=source_file,
                error_str=error_str
            )
        )

    def reset_site_on_representation(
        self,
        project_name,
        representation_id,
        side=None,
        file_id=None,
        site_name=None
    ):
        """
            Reset information about synchronization for particular 'file_id'
            and provider.
            Useful for testing or forcing file to be reuploaded.

            'side' and 'site_name' are disjunctive.

            'side' is used for resetting local or remote side for
            current user for repre.

            'site_name' is used to set synchronization for particular site.
            Should be used when repre should be synced to new site.

        Args:
            project_name (str): name of project (eg. collection) in DB
            representation_id (str): Representation id.
            file_id (str): File id in representation.
            side (str): Local or remote side.
            site_name (str): for adding new site

        Raises:
            SiteAlreadyPresentError - if adding already existing site and
                not 'force'
            ValueError - other errors (repre not found, misconfiguration)
        """
        representation = get_representation_by_id(
            project_name, representation_id
        )
        if not representation:
            raise ValueError(
                "Representation {} not found in {}".format(
                    representation_id, project_name
                )
            )

        if side and site_name:
            raise ValueError(
                "Misconfiguration, only one of side and"
                " site_name arguments should be passed."
            )

        if side:
            if side == "local":
                site_name = self.get_active_site(project_name)
            else:
                site_name = self.get_remote_site(project_name)

        self.add_site(
            project_name, representation_id, site_name, file_id, force=True
        )

    def _get_progress_for_repre_new(
        self,
        project_name,
        representation,
        local_site_name,
        remote_site_name=None
    ):
        representation_id = representation["id"]
        sync_status = self.get_repre_sync_state(
            project_name,
            representation_id,
            local_site_name,
            remote_site_name
        )

        progress = {
            local_site_name: -1,
            remote_site_name: -1
        }
        if not sync_status:
            return progress

        mapping = {
            "localStatus": local_site_name,
            "remoteStatus": remote_site_name
        }
        files = {local_site_name: 0, remote_site_name: 0}
        file_states = sync_status.get("files") or []
        for file_state in file_states:
            for status in mapping.keys():
                status_info = file_state[status]
                site_name = mapping[status]
                files[site_name] += 1
                norm_progress = max(progress[site_name], 0)
                if status_info["status"] == SiteSyncStatus.OK:
                    progress[site_name] = norm_progress + 1
                elif status_info.get("progress"):
                    progress[site_name] = norm_progress + status_info[
                        "progress"]
                else:  # site exists, might be failed, do not add again
                    progress[site_name] = 0

        # for example 13 fully avail. files out of 26 >> 13/26 = 0.5
        return {
            local_site_name: (
                progress[local_site_name] / max(files[local_site_name], 1)
            ),
            remote_site_name: (
                progress[remote_site_name] / max(files[remote_site_name], 1)
            )
        }

    def _get_progress_for_repre_old(
        self,
        representation,
        local_site_name,
        remote_site_name=None
    ):
        return self._get_progress_for_repre_new(
            representation["context"]["project"]["name"],
            representation,
            local_site_name,
            remote_site_name
        )

    def get_progress_for_repre(self, *args, **kwargs):
        """Calculates average progress for representation.

        If site has created_dt >> fully available >> progress == 1

        Could be calculated in aggregate if it would be too slow

        Returns:
            (dict) with active and remote sites progress
            {'studio': 1.0, 'gdrive': -1} - gdrive site is not present
                -1 is used to highlight the site should be added
            {'studio': 1.0, 'gdrive': 0.0} - gdrive site is present, not
                uploaded yet

        """
        sig_new = inspect.signature(self._get_progress_for_repre_new)
        sig_old = inspect.signature(self._get_progress_for_repre_old)
        try:
            sig_new.bind(*args, **kwargs)
            return self._get_progress_for_repre_new(*args, **kwargs)
        except TypeError:
            pass

        try:
            sig_old.bind(*args, **kwargs)
            print(
                "Using old signature of 'get_progress_for_repre'"
                " please add project name as first argument."
            )
            return self._get_progress_for_repre_old(*args, **kwargs)
        except TypeError:
            pass

        return self._get_progress_for_repre_new(*args, **kwargs)

    def _set_state_sync_state(
        self, project_name, representation_id, site_name, payload_dict
    ):
        """Calls server endpoint to store sync info for 'representation_id'."""
        endpoint = "{}/{}/state/{}/{}".format(
            self.endpoint_prefix,
            project_name,
            representation_id,
            site_name
        )

        response = ayon_api.post(endpoint, **payload_dict)
        if response.status_code not in [200, 204]:
            raise RuntimeError("Cannot update status")

    def get_repre_sync_state(
        self,
        project_name,
        representation_id,
        local_site_name,
        remote_site_name=None,
        **kwargs
    ):
        """Use server endpoint to get synchronization info for representation.

        Warning:
            Logic of this

        Args:
            project_name (str): Project name.
            representation_id (str): Representation id.
            local_site_name (str)
            remote_site_name (str)
            all other parameters for `Get Site Sync State` endpoint if
                necessary

        """
        repre_states = self._get_repres_state(
            project_name,
            {representation_id},
            local_site_name,
            remote_site_name,
            **kwargs
        )
        if repre_states:
            repre_state = repre_states[0]
            if repre_state["localStatus"]["status"] != -1:
                return repre_state

    def get_representations_sync_state(
        self,
        project_name,
        representation_ids,
        local_site_name,
        remote_site_name=None,
        **kwargs
    ):
        """Use server endpoint to get synchronization info for representations.

        Calculates float progress based on progress of all files for repre.
        If repre is fully synchronized it returns 1, 0 for any other state.

        Args:
            project_name (str):
            representation_ids (list): even single repre should be in []
            local_site_name (str)
            remote_site_name (str)
            all other parameters for `Get Site Sync State` endpoint if
                necessary.

        Returns:
            dict[str, tuple[float, float]]: Progress by representation id.

        """
        repre_states = self._get_repres_state(
            project_name,
            representation_ids,
            local_site_name,
            remote_site_name,
            **kwargs
        )
        states = {}
        for repre_state in repre_states:
            repre_files_count = len(repre_state["files"])

            repre_local_status = repre_state["localStatus"]["status"]
            repre_local_progress = 0
            if repre_local_status == SiteSyncStatus.OK:
                repre_local_progress = 1
            elif repre_local_status == SiteSyncStatus.IN_PROGRESS:
                local_sum = sum(
                    file_info["localStatus"].get("progress", 0)
                    for file_info in repre_state["files"]
                )
                repre_local_progress = local_sum / repre_files_count

            repre_remote_status = repre_state["remoteStatus"]["status"]
            repre_remote_progress = 0
            if repre_remote_status == SiteSyncStatus.OK:
                repre_remote_progress = 1
            elif repre_remote_status == SiteSyncStatus.IN_PROGRESS:
                remote_sum = sum(
                    file_info["remoteStatus"].get("progress", 0)
                    for file_info in repre_state["files"]
                )
                repre_remote_progress = remote_sum / repre_files_count

            states[repre_state["representationId"]] = (
                repre_local_progress,
                repre_remote_progress
            )

        return states

    def _get_repres_state(
        self,
        project_name,
        representation_ids,
        local_site_name,
        remote_site_name=None,
        **kwargs
    ):
        """Use server endpoint to get sync info for representations.

        Args:
            project_name (str): Project name.
            representation_ids (Iterable[str]): Representation ids.
            local_site_name (str): Local site name.
            remote_site_name (str): Remote site name.
            kwargs: All other parameters for `Get Site Sync State` endpoint if
                necessary

        """
        if not remote_site_name:
            remote_site_name = local_site_name
        payload_dict = {
            "localSite": local_site_name,
            "remoteSite": remote_site_name,
            "representationIds": representation_ids
        }
        if kwargs:
            payload_dict.update(kwargs)

        endpoint = "{}/{}/state".format(
            self.endpoint_prefix, project_name
        )

        response = ayon_api.get(endpoint, **payload_dict)
        if response.status_code != 200:
            raise RuntimeError(
                "Cannot get sync state for representations {}".format(
                    representation_ids
                )
            )

        return response.data["representations"]

    def get_version_availability(
        self,
        project_name,
        version_ids,
        local_site_name,
        remote_site_name,
        **kwargs
    ):
        """Returns aggregated state for version ids.

        Args:
            project_name (str): Project name.
            version_ids (Iterable[str]): Version ids.
            local_site_name (str): Local site name.
            remote_site_name (str): Remote site name.
            kwargs: All other parameters for `Get Site Sync State` endpoint if
                necessary.

        Returns:
            dict[str, tuple[float, float]]: Status by version id.
                Example: {version_id: (local_status, remote_status)}

        """
        version_ids = list(version_ids)
        payload_dict = {
            "localSite": local_site_name,
            "remoteSite": remote_site_name,
            "versionIdsFilter": version_ids
        }
        payload_dict.update(kwargs)

        endpoint = "{}/{}/state".format(
            self.endpoint_prefix, project_name
        )

        response = ayon_api.get(endpoint, **payload_dict)
        if response.status_code != 200:
            raise RuntimeError(
                "Cannot get sync state for versions {}".format(
                    version_ids
                )
            )

        version_statuses = {
            version_id: (0, 0)
            for version_id in version_ids
        }

        repre_avail_by_version_id = defaultdict(list)
        for repre_avail in response.data["representations"]:
            version_id = repre_avail["versionId"]
            repre_avail_by_version_id[version_id].append(repre_avail)

        for version_id, repre_avails in repre_avail_by_version_id.items():
            avail_local = sum(
                int(
                    repre_avail["localStatus"]["status"] == SiteSyncStatus.OK
                )
                for repre_avail in repre_avails
            )
            avail_remote = sum(
                int(
                    repre_avail["remoteStatus"]["status"] == SiteSyncStatus.OK
                )
                for repre_avail in repre_avails
            )
            version_statuses[version_id] = (avail_local, avail_remote)

        return version_statuses

    def _remove_local_file(self, project_name, representation_id, site_name):
        """Removes all local files for 'site_name' of 'representation_id'

        Args:
            project_name (str): Project name.
            representation_id (str): Representation id.
            site_name (str): name of configured and active site

        """
        my_local_site = get_local_site_id()
        if my_local_site != site_name:
            self.log.warning(
                "Cannot remove non local file for {}".format(site_name)
            )
            return

        provider_name = self.get_provider_for_site(site=site_name)

        if provider_name != "local_drive":
            return

        representation = get_representation_by_id(
            project_name, representation_id
        )
        if not representation:
            self.log.debug(
                "Representation with id {} was not found".format(
                    representation_id
                )
            )
            return

        for file in representation["files"]:
            local_file_path = self.get_local_file_path(
                project_name,
                site_name,
                file.get("path")
            )
            if local_file_path is None:
                raise ValueError("Missing local file path")

            try:
                self.log.debug("Removing {}".format(local_file_path))
                os.remove(local_file_path)
            except IndexError:
                msg = "No file set for {}".format(representation_id)
                self.log.debug(msg)
                raise ValueError(msg)
            except OSError:
                msg = "File {} cannot be removed".format(file["path"])
                self.log.warning(msg)
                raise ValueError(msg)

            folder = os.path.dirname(local_file_path)
            if os.listdir(folder):  # folder is not empty
                continue

            try:
                os.rmdir(folder)
            except OSError:
                msg = "folder {} cannot be removed".format(folder)
                self.log.warning(msg)
                raise ValueError(msg)

    def reset_timer(self):
        """
            Called when waiting for next loop should be skipped.

            In case of user's involvement (reset site), start that right away.
        """

        if not self.enabled:
            return

        if self.sitesync_thread is None:
            self._reset_timer_with_rest_api()
        else:
            self.sitesync_thread.reset_timer()

    def get_loop_delay(self, project_name):
        """
            Return count of seconds before next synchronization loop starts
            after finish of previous loop.

        Returns:
            (int): in seconds
        """
        if not project_name:
            return 60

        # TODO this is used in global loop it should not be based on
        #   project settings.
        ld = self.sync_project_settings[project_name]["config"]["loop_delay"]
        return int(ld)

    def cli(self, click_group):
        main = click_wrap.group(
            self._cli_main,
            name=self.name,
            help="SiteSync addon related commands."
        )

        main.command(
            self._cli_command_syncservice,
            name="syncservice",
            help="Launch Site Sync under entered site."
        ).option(
            "-a",
            "--active_site",
            help="Name of active site",
            required=True
        )
        click_group.add_command(main.to_click_obj())

    def _cli_main(self):
        pass

    def _cli_command_syncservice(self, active_site):
        """Launch sync server under entered site.

        This should be ideally used by system service (such us systemd or upstart
        on linux and window service).
        """

        os.environ["AYON_SITE_ID"] = active_site

        def signal_handler(sig, frame):
            print("You pressed Ctrl+C. Process ended.")
            self.server_exit()
            sys.exit(0)

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.server_init()
        self.server_start()

        while True:
            time.sleep(1.0)

add_site(project_name, representation_id, site_name=None, file_id=None, force=False, status=SiteSyncStatus.QUEUED)

Adds new site to representation to be synced.

'project_name' must have synchronization enabled (globally or project only)

Used as a API endpoint from outside applications (Loader etc).

Use 'force' to reset existing site.

Parameters:

Name Type Description Default
project_name str

Project name.

required
representation_id str

Representation id.

required
site_name str

Site name of configured site.

None
file_id str

File id.

None
force bool

Reset site if exists.

False
status SiteSyncStatus

Current status, default SiteSyncStatus.QUEUED

QUEUED

Raises:

Type Description
SiteAlreadyPresentError

If adding already existing site and not 'force'

ValueError

other errors (repre not found, misconfiguration)

Source code in client/ayon_sitesync/addon.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
def add_site(
    self,
    project_name,
    representation_id,
    site_name=None,
    file_id=None,
    force=False,
    status=SiteSyncStatus.QUEUED
):
    """Adds new site to representation to be synced.

    'project_name' must have synchronization enabled (globally or
    project only)

    Used as a API endpoint from outside applications (Loader etc).

    Use 'force' to reset existing site.

    Args:
        project_name (str): Project name.
        representation_id (str): Representation id.
        site_name (str): Site name of configured site.
        file_id (str): File id.
        force (bool): Reset site if exists.
        status (SiteSyncStatus): Current status,
            default SiteSyncStatus.QUEUED

    Raises:
        SiteAlreadyPresentError: If adding already existing site and
            not 'force'
        ValueError: other errors (repre not found, misconfiguration)

    """
    if not self.get_sync_project_setting(project_name):
        raise ValueError("Project not configured")

    if not site_name:
        site_name = self.DEFAULT_SITE

    representation = get_representation_by_id(
        project_name, representation_id
    )

    files = representation.get("files", [])
    if not files:
        self.log.debug("No files for {}".format(representation_id))
        return

    if not force:
        existing = self.get_repre_sync_state(
            project_name,
            representation_id,
            site_name
        )
        if existing:
            failure = True
            if file_id:
                file_exists = existing.get("files", {}).get(file_id)
                if not file_exists:
                    failure = False

            if failure:
                msg = "Site {} already present".format(site_name)
                self.log.info(msg)
                raise SiteAlreadyPresentError(msg)

    new_site_files = [
        {
            "size": repre_file["size"],
            "status": status,
            "timestamp": datetime.now().timestamp(),
            "id": repre_file["id"],
            "fileHash": repre_file["hash"]
        }
        for repre_file in files
    ]

    payload_dict = {"files": new_site_files}
    representation_id = representation_id.replace("-", "")

    self._set_state_sync_state(
        project_name, representation_id, site_name, payload_dict
    )

check_status(file_state, local_site, remote_site, config_preset)

Check synchronization status of a file.

The file is on representation status is checked for single 'provider'. (Eg. check if 'scene.ma' of lookdev.v10 should be synced to GDrive

Always is comparing local record, eg. site with 'name' == self.presets[PROJECT_NAME]["config"]["active_site"]

This leads to trigger actual upload or download, there is a use case 'studio' <> 'remote' where user should publish to 'studio', see progress in Tray GUI, but do not do physical upload/download (as multiple user would be doing that).

Do physical U/D only when any of the sites is user's local, in that
case only user has the data and must U/D.

Parameters:

Name Type Description Default
file_state dict

File info from site sync database.

required
local_site str

Local site of compare (usually 'studio').

required
remote_site str

Remote site (gdrive etc).

required
config_preset dict

Config about active site, retries.

required

Returns:

Name Type Description
int

Sync status value of representation.

Source code in client/ayon_sitesync/addon.py
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
def check_status(self, file_state, local_site, remote_site, config_preset):
    """Check synchronization status of a file.

    The file is on representation status is checked for single 'provider'.
        (Eg. check if 'scene.ma' of lookdev.v10 should be synced to GDrive

    Always is comparing local record, eg. site with
        'name' == self.presets[PROJECT_NAME]["config"]["active_site"]

    This leads to trigger actual upload or download, there is
        a use case 'studio' <> 'remote' where user should publish
        to 'studio', see progress in Tray GUI, but do not do
        physical upload/download
        (as multiple user would be doing that).

        Do physical U/D only when any of the sites is user's local, in that
        case only user has the data and must U/D.

    Args:
        file_state (dict): File info from site sync database.
        local_site (str): Local site of compare (usually 'studio').
        remote_site (str): Remote site (gdrive etc).
        config_preset (dict): Config about active site, retries.

    Returns:
        int: Sync status value of representation.

    """
    if get_local_site_id() not in (local_site, remote_site):
        # don't do upload/download for studio sites
        self.log.debug(
            "No local site {} - {}".format(local_site, remote_site)
        )
        return SyncStatus.DO_NOTHING

    local_status = file_state["localStatus"]["status"]
    remote_status = file_state["remoteStatus"]["status"]

    if (
        local_status != SiteSyncStatus.OK
        and remote_status == SiteSyncStatus.OK
    ):
        retries = file_state["localStatus"]["retries"]
        if retries < int(config_preset["retry_cnt"]):
            return SyncStatus.DO_DOWNLOAD

    if (
        remote_status != SiteSyncStatus.OK
        and local_status == SiteSyncStatus.OK
    ):
        retries = file_state["remoteStatus"]["retries"]
        if retries < int(config_preset["retry_cnt"]):
            return SyncStatus.DO_UPLOAD

    return SyncStatus.DO_NOTHING

clear_project(project_name, site_name)

Clear 'project_name' of 'site_name' and its local files

Works only on real local sites, not on 'studio'

Source code in client/ayon_sitesync/addon.py
457
458
459
460
461
462
463
464
465
def clear_project(self, project_name, site_name):
    """
        Clear 'project_name' of 'site_name' and its local files

        Works only on real local sites, not on 'studio'
    """

    # TODO implement
    self.log.warning("Method 'clear_project' is not implemented.")

compute_resource_sync_sites(project_name)

Get available resource sync sites state for publish process.

Returns dict with prepared state of sync sites for 'project_name'. It checks if Site Sync is enabled, handles alternative sites. Publish process stores this dictionary as a part of representation document in DB.

Example: [ { 'name': '42abbc09-d62a-44a4-815c-a12cd679d2d7', 'status': SiteSyncStatus.OK }, {'name': 'studio', 'status': SiteSyncStatus.QUEUED}, {'name': 'SFTP', 'status': SiteSyncStatus.QUEUED} ] -- representation is published locally, artist or Settings have set remote site as 'studio'. 'SFTP' is alternate site to 'studio'. Eg. whenever file is on 'studio', it is also on 'SFTP'.

Source code in client/ayon_sitesync/addon.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def compute_resource_sync_sites(self, project_name):
    """Get available resource sync sites state for publish process.

    Returns dict with prepared state of sync sites for 'project_name'.
    It checks if Site Sync is enabled, handles alternative sites.
    Publish process stores this dictionary as a part of representation
    document in DB.

    Example:
    [
        {
            'name': '42abbc09-d62a-44a4-815c-a12cd679d2d7',
            'status': SiteSyncStatus.OK
        },
        {'name': 'studio', 'status': SiteSyncStatus.QUEUED},
        {'name': 'SFTP', 'status': SiteSyncStatus.QUEUED}
    ] -- representation is published locally, artist or Settings have set
    remote site as 'studio'. 'SFTP' is alternate site to 'studio'. Eg.
    whenever file is on 'studio', it is also on 'SFTP'.
    """

    def create_metadata(name, created=True):
        """Create sync site metadata for site with `name`"""
        if created:
            status = SiteSyncStatus.OK
        else:
            status = SiteSyncStatus.QUEUED
        return {"name": name, "status": status}

    if (
        not self.sync_studio_settings["enabled"]
        or not self.sync_project_settings[project_name]["enabled"]
    ):
        return [create_metadata(self.DEFAULT_SITE)]

    local_site = self.get_active_site(project_name)
    remote_site = self.get_remote_site(project_name)

    # Attached sites metadata by site name
    # That is the local site, remote site, the always accesible sites
    # and their alternate sites (alias of sites with different protocol)
    attached_sites = {
        local_site: create_metadata(local_site)
    }
    if remote_site and remote_site not in attached_sites:
        attached_sites[remote_site] = create_metadata(
            remote_site, created=False
        )

    attached_sites = self._add_alternative_sites(
        project_name, attached_sites)
    # add skeleton for sites where it should be always synced to
    # usually it would be a backup site which is handled by separate
    # background process
    for site_name in self._get_always_accessible_sites(project_name):
        if site_name not in attached_sites:
            attached_sites[site_name] = (
                create_metadata(site_name, created=False))
    unique_sites = {
        site["name"]: site
        for site in attached_sites.values()
    }
    return list(unique_sites.values())

get_active_site(project_name)

Returns active (mine) site for project from settings.

Output logic
  • 'studio' if Site Sync is disabled
  • value from 'get_local_site_id' if active site is 'local'
  • any other site name from local settings or project settings (site could be forced from PS)

Returns:

Name Type Description
str

Site name.

Source code in client/ayon_sitesync/addon.py
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
def get_active_site(self, project_name):
    """Returns active (mine) site for project from settings.

    Output logic:
        - 'studio' if Site Sync is disabled
        - value from 'get_local_site_id' if active site is 'local'
        - any other site name from local settings
            or project settings (site could be forced from PS)

    Returns:
        str: Site name.

    """
    active_site_type = self.get_active_site_type(project_name)
    if active_site_type == self.LOCAL_SITE:
        return get_local_site_id()
    return active_site_type

get_active_site_type(project_name, local_settings=None)

Active site which is defined by artist.

Unlike 'get_active_site' is this method also checking local settings where might be different active site set by user. The output is limited to "studio" and "local".

This method is used by Anatomy.

Todos

Check if sync server is enabled for the project. - To be able to do that the sync settings MUST NOT be cached for all projects at once. The sync settings preparation for all projects is reasonable only in sync server loop. local_settings is probably obsolete in AYON

Parameters:

Name Type Description Default
project_name str

Name of project where to look for active site.

required
local_settings Optional[dict[str, Any]]

Prepared local settings.

None

Returns:

Type Description

Literal["studio", "local"]: Active site.

Source code in client/ayon_sitesync/addon.py
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
def get_active_site_type(self, project_name, local_settings=None):
    """Active site which is defined by artist.

    Unlike 'get_active_site' is this method also checking local settings
    where might be different active site set by user. The output is limited
    to "studio" and "local".

    This method is used by Anatomy.

    Todos:
        Check if sync server is enabled for the project.
        - To be able to do that the sync settings MUST NOT be cached for
            all projects at once. The sync settings preparation for all
            projects is reasonable only in sync server loop.
        `local_settings` is probably obsolete in AYON

    Args:
        project_name (str): Name of project where to look for active site.
        local_settings (Optional[dict[str, Any]]): Prepared local settings.

    Returns:
        Literal["studio", "local"]: Active site.
    """
    if not self.enabled:
        return "studio"

    sync_project_settings = self.get_sync_project_setting(project_name)

    if not sync_project_settings["enabled"]:
        return "studio"

    return (
        sync_project_settings["local_setting"].get("active_site")
        or sync_project_settings["config"]["active_site"]
    )

get_anatomy(project_name)

Get already created or newly created anatomy for project

Parameters:

Name Type Description Default
project_name str

Project name.

required
Return

Anatomy: Project anatomy object.

Source code in client/ayon_sitesync/addon.py
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
def get_anatomy(self, project_name):
    """Get already created or newly created anatomy for project

    Args:
        project_name (str): Project name.

    Return:
        Anatomy: Project anatomy object.
    """
    from ayon_core.pipeline import Anatomy

    return self._anatomies.get(project_name) or Anatomy(project_name)

get_enabled_projects()

Returns list of projects which have SiteSync enabled.

Source code in client/ayon_sitesync/addon.py
882
883
884
885
886
887
888
889
890
891
def get_enabled_projects(self):
    """Returns list of projects which have SiteSync enabled."""
    enabled_projects = []

    if self.enabled:
        for project_name in get_project_names():
            if self.is_project_enabled(project_name):
                enabled_projects.append(project_name)

    return enabled_projects

get_launch_hook_paths()

Implementation for applications launch hooks.

Returns:

Name Type Description
str

full absolut path to directory with hooks for the addon

Source code in client/ayon_sitesync/addon.py
143
144
145
146
147
148
149
150
151
152
153
def get_launch_hook_paths(self):
    """Implementation for applications launch hooks.

    Returns:
        str: full absolut path to directory with hooks for the addon

    """
    return os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        "launch_hooks"
    )

get_local_file_path(project_name, site_name, file_path)

Externalized for app.

Parameters:

Name Type Description Default
project_name str

Project name.

required
site_name str

Site name.

required
file_path str

File path from other site.

required

Returns:

Name Type Description
str

Resolved local path.

Source code in client/ayon_sitesync/addon.py
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
def get_local_file_path(self, project_name, site_name, file_path):
    """Externalized for app.

    Args:
        project_name (str): Project name.
        site_name (str): Site name.
        file_path (str): File path from other site.

    Returns:
        str: Resolved local path.

    """
    handler = LocalDriveHandler(project_name, site_name)
    local_file_path = handler.resolve_path(file_path)

    return local_file_path

get_local_normalized_site(site_name)

Normlize local site name.

Return 'local' if 'site_name' is local id.

In some places Settings or Local Settings require 'local' instead of real site name.

Returns:

Name Type Description
str

Normalized site name.

Source code in client/ayon_sitesync/addon.py
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
def get_local_normalized_site(self, site_name):
    """Normlize local site name.

     Return 'local' if 'site_name' is local id.

    In some places Settings or Local Settings require 'local' instead
    of real site name.

    Returns:
        str: Normalized site name.

    """
    if site_name == get_local_site_id():
        site_name = self.LOCAL_SITE

    return site_name

get_loop_delay(project_name)

Return count of seconds before next synchronization loop starts
after finish of previous loop.

Returns:

Type Description
int

in seconds

Source code in client/ayon_sitesync/addon.py
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
def get_loop_delay(self, project_name):
    """
        Return count of seconds before next synchronization loop starts
        after finish of previous loop.

    Returns:
        (int): in seconds
    """
    if not project_name:
        return 60

    # TODO this is used in global loop it should not be based on
    #   project settings.
    ld = self.sync_project_settings[project_name]["config"]["loop_delay"]
    return int(ld)

get_progress_for_repre(*args, **kwargs)

Calculates average progress for representation.

If site has created_dt >> fully available >> progress == 1

Could be calculated in aggregate if it would be too slow

Returns:

Type Description

(dict) with active and remote sites progress

{'studio': 1.0, 'gdrive': -1} - gdrive site is not present -1 is used to highlight the site should be added

{'studio': 1.0, 'gdrive': 0.0} - gdrive site is present, not uploaded yet

Source code in client/ayon_sitesync/addon.py
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
def get_progress_for_repre(self, *args, **kwargs):
    """Calculates average progress for representation.

    If site has created_dt >> fully available >> progress == 1

    Could be calculated in aggregate if it would be too slow

    Returns:
        (dict) with active and remote sites progress
        {'studio': 1.0, 'gdrive': -1} - gdrive site is not present
            -1 is used to highlight the site should be added
        {'studio': 1.0, 'gdrive': 0.0} - gdrive site is present, not
            uploaded yet

    """
    sig_new = inspect.signature(self._get_progress_for_repre_new)
    sig_old = inspect.signature(self._get_progress_for_repre_old)
    try:
        sig_new.bind(*args, **kwargs)
        return self._get_progress_for_repre_new(*args, **kwargs)
    except TypeError:
        pass

    try:
        sig_old.bind(*args, **kwargs)
        print(
            "Using old signature of 'get_progress_for_repre'"
            " please add project name as first argument."
        )
        return self._get_progress_for_repre_old(*args, **kwargs)
    except TypeError:
        pass

    return self._get_progress_for_repre_new(*args, **kwargs)

get_provider_for_site(project_name=None, site=None)

Get provider name for site (unique name across all projects).

Source code in client/ayon_sitesync/addon.py
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
def get_provider_for_site(self, project_name=None, site=None):
    """Get provider name for site (unique name across all projects)."""
    sites = {
        self.DEFAULT_SITE: "local_drive",
        self.LOCAL_SITE: "local_drive",
        get_local_site_id(): "local_drive"
    }

    if site in sites.keys():
        return sites[site]

    # backward compatibility
    if project_name:
        proj_settings = self.get_sync_project_setting(project_name)
        provider = (
            proj_settings
            .get("sites", {})
            .get(site, {})
            .get("provider")
        )
        if provider:
            return provider

    sync_sett = self.sync_studio_settings
    for site_config in sync_sett.get("sites"):
        sites[site_config["name"]] = site_config["provider"]

    return sites.get(site, "N/A")

get_remote_site(project_name)

Remote (theirs) site for project from settings.

Source code in client/ayon_sitesync/addon.py
752
753
754
755
756
757
758
759
760
761
762
def get_remote_site(self, project_name):
    """Remote (theirs) site for project from settings."""
    sync_project_settings = self.get_sync_project_setting(project_name)
    remote_site = (
        sync_project_settings["local_setting"].get("remote_site")
        or sync_project_settings["config"]["remote_site"]
    )
    if remote_site == self.LOCAL_SITE:
        return get_local_site_id()

    return remote_site

get_repre_info_for_versions(project_name, version_ids, active_site, remote_site)

Returns representation for versions and sites combi

Parameters:

Name Type Description Default
project_name str

Project name

required
version_ids Iterable[str]

Version ids.

required
active_site str

'local', 'studio' etc

required
remote_site str

dtto

required

Returns:

Source code in client/ayon_sitesync/addon.py
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
def get_repre_info_for_versions(
    self, project_name, version_ids, active_site, remote_site
):
    """Returns representation for versions and sites combi

    Args:
        project_name (str): Project name
        version_ids (Iterable[str]): Version ids.
        active_site (str): 'local', 'studio' etc
        remote_site (str): dtto

    Returns:

    """
    version_ids = set(version_ids)
    endpoint = "{}/projects/{}/sitesync/state".format(
        self.endpoint_prefix, project_name
    )

    # get to upload
    kwargs = {
        "localSite": active_site,
        "remoteSite": remote_site,
        "versionIdFilter": list(version_ids)
    }

    # kwargs["representationId"] = "94dca33a-7705-11ed-8c0a-34e12d91d510"

    response = ayon_api.get(endpoint, **kwargs)
    repre_states = response.data.get("representations", [])
    repre_info_by_version_id = {
        version_id: {
            "id": version_id,
            "repre_count": 0,
            "avail_repre_local": 0,
            "avail_repre_remote": 0,
        }
        for version_id in version_ids
    }
    repre_states_by_version_id = defaultdict(list)
    for repre_state in repre_states:
        version_id = repre_state["versionId"]
        repre_states_by_version_id[version_id].append(repre_state)

    for version_id, repre_states in repre_states_by_version_id.items():
        repre_info = repre_info_by_version_id[version_id]
        repre_info["repre_count"] = len(repre_states)
        repre_info["avail_repre_local"] = sum(
            self._is_available(repre_state, "localStatus")
            for repre_state in repre_states
        )
        repre_info["avail_repre_remote"] = sum(
            self._is_available(repre_state, "remoteStatus")
            for repre_state in repre_states
        )

    return list(repre_info_by_version_id.values())

get_repre_sync_state(project_name, representation_id, local_site_name, remote_site_name=None, **kwargs)

Use server endpoint to get synchronization info for representation.

Warning

Logic of this

Parameters:

Name Type Description Default
project_name str

Project name.

required
representation_id str

Representation id.

required
Source code in client/ayon_sitesync/addon.py
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
def get_repre_sync_state(
    self,
    project_name,
    representation_id,
    local_site_name,
    remote_site_name=None,
    **kwargs
):
    """Use server endpoint to get synchronization info for representation.

    Warning:
        Logic of this

    Args:
        project_name (str): Project name.
        representation_id (str): Representation id.
        local_site_name (str)
        remote_site_name (str)
        all other parameters for `Get Site Sync State` endpoint if
            necessary

    """
    repre_states = self._get_repres_state(
        project_name,
        {representation_id},
        local_site_name,
        remote_site_name,
        **kwargs
    )
    if repre_states:
        repre_state = repre_states[0]
        if repre_state["localStatus"]["status"] != -1:
            return repre_state

get_representations_sync_state(project_name, representation_ids, local_site_name, remote_site_name=None, **kwargs)

Use server endpoint to get synchronization info for representations.

Calculates float progress based on progress of all files for repre. If repre is fully synchronized it returns 1, 0 for any other state.

Parameters:

Name Type Description Default
project_name str
required
representation_ids list

even single repre should be in []

required

Returns:

Type Description

dict[str, tuple[float, float]]: Progress by representation id.

Source code in client/ayon_sitesync/addon.py
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
def get_representations_sync_state(
    self,
    project_name,
    representation_ids,
    local_site_name,
    remote_site_name=None,
    **kwargs
):
    """Use server endpoint to get synchronization info for representations.

    Calculates float progress based on progress of all files for repre.
    If repre is fully synchronized it returns 1, 0 for any other state.

    Args:
        project_name (str):
        representation_ids (list): even single repre should be in []
        local_site_name (str)
        remote_site_name (str)
        all other parameters for `Get Site Sync State` endpoint if
            necessary.

    Returns:
        dict[str, tuple[float, float]]: Progress by representation id.

    """
    repre_states = self._get_repres_state(
        project_name,
        representation_ids,
        local_site_name,
        remote_site_name,
        **kwargs
    )
    states = {}
    for repre_state in repre_states:
        repre_files_count = len(repre_state["files"])

        repre_local_status = repre_state["localStatus"]["status"]
        repre_local_progress = 0
        if repre_local_status == SiteSyncStatus.OK:
            repre_local_progress = 1
        elif repre_local_status == SiteSyncStatus.IN_PROGRESS:
            local_sum = sum(
                file_info["localStatus"].get("progress", 0)
                for file_info in repre_state["files"]
            )
            repre_local_progress = local_sum / repre_files_count

        repre_remote_status = repre_state["remoteStatus"]["status"]
        repre_remote_progress = 0
        if repre_remote_status == SiteSyncStatus.OK:
            repre_remote_progress = 1
        elif repre_remote_status == SiteSyncStatus.IN_PROGRESS:
            remote_sum = sum(
                file_info["remoteStatus"].get("progress", 0)
                for file_info in repre_state["files"]
            )
            repre_remote_progress = remote_sum / repre_files_count

        states[repre_state["representationId"]] = (
            repre_local_progress,
            repre_remote_progress
        )

    return states

get_site_icons()

Icons for sites.

Returns:

Type Description

dict[str, str]: Path to icon by site.

Source code in client/ayon_sitesync/addon.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def get_site_icons(self):
    """Icons for sites.

    Returns:
        dict[str, str]: Path to icon by site.

    """
    resource_path = os.path.join(
        SYNC_ADDON_DIR, "providers", "resources"
    )
    icons = {}
    for file_path in os.listdir(resource_path):
        if not file_path.endswith(".png"):
            continue
        provider_name, _ = os.path.splitext(os.path.basename(file_path))
        icons[provider_name] = {
            "type": "path",
            "path": os.path.join(resource_path, file_path)
        }
    return icons

get_site_root_overrides(project_name, site_name, local_settings=None)

Get root overrides for project on a site.

Implemented to be used in 'Anatomy' for other than 'studio' site.

Parameters:

Name Type Description Default
project_name str

Project for which root overrides should be received.

required
site_name str

Name of site for which should be received roots.

required
local_settings Optional[dict[str, Any]]

Prepare local settigns values.

None

Returns:

Type Description

Union[dict[str, Any], None]: Root overrides for this machine.

{"work": "c:/projects_local"}

Source code in client/ayon_sitesync/addon.py
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
def get_site_root_overrides(
    self, project_name, site_name, local_settings=None
):
    """Get root overrides for project on a site.

    Implemented to be used in 'Anatomy' for other than 'studio' site.

    Args:
        project_name (str): Project for which root overrides should be
            received.
        site_name (str): Name of site for which should be received roots.
        local_settings (Optional[dict[str, Any]]): Prepare local settigns
            values.

    Returns:
        Union[dict[str, Any], None]: Root overrides for this machine.

        {"work": "c:/projects_local"}
    """

    # Validate that site name is valid
    if site_name not in ("studio", "local"):
        # Consider local site id as 'local'
        if site_name != get_local_site_id():
            raise ValueError((
                "Root overrides are available only for"
                " default sites not for \"{}\""
            ).format(site_name))
        site_name = "local"

    sitesync_settings = self.get_sync_project_setting(project_name)

    roots = {}
    if not sitesync_settings["enabled"]:
        return roots
    local_project_settings = sitesync_settings["local_setting"]
    if site_name == "local":
        for root_info in local_project_settings["local_roots"]:
            roots[root_info["name"]] = root_info["path"]

    return roots

get_sync_project_setting(project_name, exclude_locals=False, cached=True)

Handles pulling sitesync's settings for enabled 'project_name'

Parameters:

Name Type Description Default
project_name str

used in project settings

required
exclude_locals bool

ignore overrides from Local Settings

False
cached bool

use pre-cached values, or return fresh ones cached values needed for single loop (with all overrides) fresh values needed for Local settings (without overrides)

True

Returns:

Name Type Description
dict

settings dictionary for the enabled project, empty if no settings or sync is disabled

Source code in client/ayon_sitesync/addon.py
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
def get_sync_project_setting(
    self, project_name, exclude_locals=False, cached=True
):
    """ Handles pulling sitesync's settings for enabled 'project_name'

    Args:
        project_name (str): used in project settings
        exclude_locals (bool): ignore overrides from Local Settings
        cached (bool): use pre-cached values, or return fresh ones
            cached values needed for single loop (with all overrides)
            fresh values needed for Local settings (without overrides)

    Returns:
        dict: settings dictionary for the enabled project,
            empty if no settings or sync is disabled

    """
    # presets set already, do not call again and again
    # self.log.debug("project preset {}".format(self.presets))
    if not cached:
        return self._prepare_sync_project_settings(exclude_locals)\
            [project_name]

    if (
        not self.sync_project_settings
        or not self.sync_project_settings.get(project_name)
    ):
        self.set_sync_project_settings(exclude_locals)
    return self.sync_project_settings.get(project_name)

get_sync_representations(project_name, active_site, remote_site, limit=10)

Get representations that should be synced, these could be
recognised by presence of document in 'files.sites', where key is
a provider (GDrive, S3) and value is empty document or document
without 'created_dt' field. (Don't put null to 'created_dt'!).

Querying of 'to-be-synched' files is offloaded to Mongod for
better performance. Goal is to get as few representations as
possible.

Args: project_name (str): active_site (str): identifier of current active site (could be 'local_0' when working from home, 'studio' when working in the studio (default) remote_site (str): identifier of remote site I want to sync to

Returns:

Type Description

list[dict]: Representation states.

Source code in client/ayon_sitesync/addon.py
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
@time_function
def get_sync_representations(
    self, project_name, active_site, remote_site, limit=10
):
    """
        Get representations that should be synced, these could be
        recognised by presence of document in 'files.sites', where key is
        a provider (GDrive, S3) and value is empty document or document
        without 'created_dt' field. (Don't put null to 'created_dt'!).

        Querying of 'to-be-synched' files is offloaded to Mongod for
        better performance. Goal is to get as few representations as
        possible.
    Args:
        project_name (str):
        active_site (str): identifier of current active site (could be
            'local_0' when working from home, 'studio' when working in the
            studio (default)
        remote_site (str): identifier of remote site I want to sync to

    Returns:
        list[dict]: Representation states.

    """
    self.log.debug("Check representations for: {}-{}".format(
        active_site, remote_site
    ))

    endpoint = "{}/{}/state".format(
        self.endpoint_prefix, project_name
    )

    # get to upload
    kwargs = {
        "localSite": active_site,
        "remoteSite": remote_site,
        "localStatusFilter": [SiteSyncStatus.OK],
        "remoteStatusFilter": [SiteSyncStatus.QUEUED],
    }

    response = ayon_api.get(endpoint, **kwargs)
    if response.status_code not in [200, 204]:
        raise RuntimeError(
            "Cannot get representations for sync with code {}".format(
                response.status_code
            )
        )

    repre_states = response.data["representations"]

    # get to download
    if len(repre_states) < limit:
        kwargs["localStatusFilter"] = [SiteSyncStatus.QUEUED]
        kwargs["remoteStatusFilter"] = [SiteSyncStatus.OK]

        response = ayon_api.get(endpoint, **kwargs)
        repre_states.extend(response.data["representations"])

    return repre_states

get_version_availability(project_name, version_ids, local_site_name, remote_site_name, **kwargs)

Returns aggregated state for version ids.

Parameters:

Name Type Description Default
project_name str

Project name.

required
version_ids Iterable[str]

Version ids.

required
local_site_name str

Local site name.

required
remote_site_name str

Remote site name.

required
kwargs

All other parameters for Get Site Sync State endpoint if necessary.

{}

Returns:

Type Description

dict[str, tuple[float, float]]: Status by version id. Example: {version_id: (local_status, remote_status)}

Source code in client/ayon_sitesync/addon.py
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
def get_version_availability(
    self,
    project_name,
    version_ids,
    local_site_name,
    remote_site_name,
    **kwargs
):
    """Returns aggregated state for version ids.

    Args:
        project_name (str): Project name.
        version_ids (Iterable[str]): Version ids.
        local_site_name (str): Local site name.
        remote_site_name (str): Remote site name.
        kwargs: All other parameters for `Get Site Sync State` endpoint if
            necessary.

    Returns:
        dict[str, tuple[float, float]]: Status by version id.
            Example: {version_id: (local_status, remote_status)}

    """
    version_ids = list(version_ids)
    payload_dict = {
        "localSite": local_site_name,
        "remoteSite": remote_site_name,
        "versionIdsFilter": version_ids
    }
    payload_dict.update(kwargs)

    endpoint = "{}/{}/state".format(
        self.endpoint_prefix, project_name
    )

    response = ayon_api.get(endpoint, **payload_dict)
    if response.status_code != 200:
        raise RuntimeError(
            "Cannot get sync state for versions {}".format(
                version_ids
            )
        )

    version_statuses = {
        version_id: (0, 0)
        for version_id in version_ids
    }

    repre_avail_by_version_id = defaultdict(list)
    for repre_avail in response.data["representations"]:
        version_id = repre_avail["versionId"]
        repre_avail_by_version_id[version_id].append(repre_avail)

    for version_id, repre_avails in repre_avail_by_version_id.items():
        avail_local = sum(
            int(
                repre_avail["localStatus"]["status"] == SiteSyncStatus.OK
            )
            for repre_avail in repre_avails
        )
        avail_remote = sum(
            int(
                repre_avail["remoteStatus"]["status"] == SiteSyncStatus.OK
            )
            for repre_avail in repre_avails
        )
        version_statuses[version_id] = (avail_local, avail_remote)

    return version_statuses

handle_alternate_site(project_name, representation_id, processed_site, file_id)

For special use cases where one site vendors another.

Current use case is sftp site vendoring (exposing) same data as regular site (studio). Each site is accessible for different audience. 'studio' for artists in a studio, 'sftp' for externals.

Change of file status on one site actually means same change on 'alternate' site. (eg. artists publish to 'studio', 'sftp' is using same location >> file is accessible on 'sftp' site right away.

Parameters:

Name Type Description Default
project_name str

Project name.

required
representation_id str

Representation id.

required
processed_site str

Real site_name of published/uploaded file

required
file_id str

File id of file handled.

required
Source code in client/ayon_sitesync/addon.py
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
def handle_alternate_site(
    self, project_name, representation_id, processed_site, file_id
):
    """
    For special use cases where one site vendors another.

    Current use case is sftp site vendoring (exposing) same data as
    regular site (studio). Each site is accessible for different
    audience. 'studio' for artists in a studio, 'sftp' for externals.

    Change of file status on one site actually means same change on
    'alternate' site. (eg. artists publish to 'studio', 'sftp' is using
    same location >> file is accessible on 'sftp' site right away.

    Args:
        project_name (str): Project name.
        representation_id (str): Representation id.
        processed_site (str): Real site_name of published/uploaded file
        file_id (str): File id of file handled.

    """
    sites = self._transform_sites_from_settings(self.sync_studio_settings)
    sites[self.DEFAULT_SITE] = {
        "provider": "local_drive",
        "alternative_sites": []
    }

    alternate_sites = []
    for site_name, site_info in sites.items():
        conf_alternative_sites = site_info.get("alternative_sites", [])
        if processed_site in conf_alternative_sites:
            alternate_sites.append(site_name)
            continue
        if processed_site == site_name and conf_alternative_sites:
            alternate_sites.extend(conf_alternative_sites)
            continue

    if not alternate_sites:
        return

    sync_state = self.get_repre_sync_state(
        project_name,
        representation_id,
        processed_site
    )
    # not yet available on processed_site, wont update alternate site yet
    if not sync_state:
        return
    for file_info in sync_state["files"]:
        # expose status of remote site, it is expected on the server
        file_info["status"] = file_info["remoteStatus"]["status"]

    payload_dict = {"files": sync_state["files"]}

    alternate_sites = set(alternate_sites)
    for alt_site in alternate_sites:
        self.log.debug("Adding alternate {} to {}".format(
            alt_site, representation_id))

        self._set_state_sync_state(
            project_name,
            representation_id,
            alt_site,
            payload_dict
        )

initialize(addon_settings)

Called during Addon Manager creation.

Collects needed data, checks asyncio presence. Sets 'enabled' according to global settings for the addon. Shouldn't be doing any initialization, that's a job for 'tray_init'

Source code in client/ayon_sitesync/addon.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def initialize(self, addon_settings):
    """Called during Addon Manager creation.

    Collects needed data, checks asyncio presence.
    Sets 'enabled' according to global settings for the addon.
    Shouldn't be doing any initialization, that's a job for 'tray_init'
    """

    # some parts of code need to run sequentially, not in async
    self.lock = None
    self._sync_studio_settings = None
    # settings for all enabled projects for sync
    self._sync_project_settings = None
    self.sitesync_thread = None  # asyncio requires new thread

    self._paused = False
    self._paused_projects = set()
    self._paused_representations = set()
    self._anatomies = {}

    # list of long blocking tasks
    self.long_running_tasks = deque()
    # projects that long tasks are running on
    self.projects_processed = set()

is_paused()

Is server paused

Source code in client/ayon_sitesync/addon.py
693
694
695
def is_paused(self):
    """ Is server paused """
    return self._paused

is_project_enabled(project_name, single=False)

Checks if 'project_name' is enabled for syncing. 'get_sync_project_setting' is potentially expensive operation (pulls settings for all projects if cached version is not available), using project_settings for specific project should be faster. Args: project_name (str) single (bool): use 'get_addon_project_settings' method

Source code in client/ayon_sitesync/addon.py
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
def is_project_enabled(self, project_name, single=False):
    """Checks if 'project_name' is enabled for syncing.
    'get_sync_project_setting' is potentially expensive operation (pulls
    settings for all projects if cached version is not available), using
    project_settings for specific project should be faster.
    Args:
        project_name (str)
        single (bool): use 'get_addon_project_settings' method
    """
    if self.enabled:
        if single:
            project_settings = get_addon_project_settings(
                self.name, self.version, project_name
            )
        else:
            project_settings = self.get_sync_project_setting(project_name)
        if project_settings and project_settings.get("enabled"):
            return True
    return False

is_project_paused(project_name, check_parents=False)

Is project sync paused.

Parameters:

Name Type Description Default
project_name str
required
check_parents bool

check if server itself is not paused

False

Returns:

Name Type Description
bool

Is project paused.

Source code in client/ayon_sitesync/addon.py
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
def is_project_paused(self, project_name, check_parents=False):
    """Is project sync paused.

    Args:
        project_name (str):
        check_parents (bool): check if server itself
            is not paused

    Returns:
        bool: Is project paused.

    """
    is_paused = project_name in self._paused_projects
    if check_parents:
        is_paused = is_paused or self.is_paused()
    return is_paused

is_representation_on_site(project_name, representation_id, site_name, max_retries=None)

Check if representation has all files available on site.

Returns:

Name Type Description
bool

True if representation has all files correctly on the site.

Source code in client/ayon_sitesync/addon.py
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
def is_representation_on_site(
    self, project_name, representation_id, site_name, max_retries=None
):
    """Check if representation has all files available on site.

    Args:
        project_name (str)
        representation_id (str)
        site_name (str)
        max_retries (int) (optional) - provide only if method used in while
            loop to bail out

    Returns:
        bool: True if representation has all files correctly on the site.

    Raises:
          ValueError  Only If 'max_retries' provided if upload/download
            failed too many times to limit infinite loop check.

    """
    representation_status = self.get_repre_sync_state(
        project_name, representation_id, site_name)
    if not representation_status:
        return False

    if site_name == get_local_site_id():
        status = representation_status["localStatus"]
    else:
        status = representation_status["remoteStatus"]

    if max_retries:
        tries = status.get("retries", 0)
        if tries >= max_retries:
            raise ValueError("Failed too many times")

    return status["status"] == SiteSyncStatus.OK

is_representation_paused(representation_id, check_parents=False, project_name=None)

Is representation paused.

Parameters:

Name Type Description Default
representation_id str

Representation id.

required
check_parents bool

Check if parent project or server itself are not paused.

False
project_name str

Project to check if paused.

None

Returns:

Name Type Description
bool

Is representation paused now.

Source code in client/ayon_sitesync/addon.py
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
def is_representation_paused(
    self, representation_id, check_parents=False, project_name=None
):
    """Is representation paused.

    Args:
        representation_id (str): Representation id.
        check_parents (bool): Check if parent project or server itself
            are not paused.
        project_name (str): Project to check if paused.

        if 'check_parents', 'project_name' should be set too

    Returns:
        bool: Is representation paused now.

    """
    is_paused = representation_id in self._paused_representations
    if check_parents and project_name:
        is_paused = (
            is_paused
            or self.is_project_paused(project_name)
            or self.is_paused()
        )
    return is_paused

pause_project(project_name)

Pause sync of whole project.

Parameters:

Name Type Description Default
project_name str

Project name.

required
Source code in client/ayon_sitesync/addon.py
636
637
638
639
640
641
642
643
644
def pause_project(self, project_name):
    """Pause sync of whole project.

    Args:
        project_name (str): Project name.

    """
    self.log.info("Pausing SiteSync for {}".format(project_name))
    self._paused_projects.add(project_name)

pause_representation(project_name, representation_id, site_name)

Pause sync of representation entity on site.

Sets 'representation_id' as paused, eg. no syncing should be happening on it.

Parameters:

Name Type Description Default
project_name str

Project name.

required
representation_id str

Representation id.

required
site_name str

Site name 'gdrive', 'studio' etc.

required
Source code in client/ayon_sitesync/addon.py
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
def pause_representation(
    self, project_name, representation_id, site_name
):
    """Pause sync of representation entity on site.

    Sets 'representation_id' as paused, eg. no syncing should be
        happening on it.

    Args:
        project_name (str): Project name.
        representation_id (str): Representation id.
        site_name (str): Site name 'gdrive', 'studio' etc.

    """
    self.log.info("Pausing SiteSync for {}".format(representation_id))
    self._paused_representations.add(representation_id)
    repre_entity = get_representation_by_id(
        project_name, representation_id
    )
    self.update_db(project_name, repre_entity, site_name, pause=True)

pause_server()

Pause sync server.

It won't check anything, not uploading/downloading...

Source code in client/ayon_sitesync/addon.py
680
681
682
683
684
685
686
def pause_server(self):
    """Pause sync server.

    It won't check anything, not uploading/downloading...
    """
    self.log.info("Pausing SiteSync")
    self._paused = True

remove_site(project_name, representation_id, site_name, remove_local_files=False)

Removes site for particular representation in project.

Parameters:

Name Type Description Default
project_name str

project name (must match DB)

required
representation_id str

MongoDB _id value

required
site_name str

name of configured and active site

required
remove_local_files bool

remove only files for 'local_id' site

False

Raises:

Type Description
ValueError

Throws if any issue.

Source code in client/ayon_sitesync/addon.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
def remove_site(
    self,
    project_name,
    representation_id,
    site_name,
    remove_local_files=False
):
    """Removes site for particular representation in project.

    Args:
        project_name (str): project name (must match DB)
        representation_id (str): MongoDB _id value
        site_name (str): name of configured and active site
        remove_local_files (bool): remove only files for 'local_id'
            site

    Raises:
        ValueError: Throws if any issue.

    """
    if not self.get_sync_project_setting(project_name):
        raise ValueError("Project not configured")

    sync_info = self.get_repre_sync_state(
        project_name,
        representation_id,
        site_name
    )
    if not sync_info:
        msg = "Site {} not found".format(site_name)
        self.log.warning(msg)
        return

    endpoint = "{}/{}/state/{}/{}".format(
        self.endpoint_prefix,
        project_name,
        representation_id,
        site_name
    )

    response = ayon_api.delete(endpoint)
    if response.status_code not in [200, 204]:
        raise RuntimeError("Cannot update status")

    if remove_local_files:
        self._remove_local_file(project_name, representation_id, site_name)

reset_site_on_representation(project_name, representation_id, side=None, file_id=None, site_name=None)

Reset information about synchronization for particular 'file_id'
and provider.
Useful for testing or forcing file to be reuploaded.

'side' and 'site_name' are disjunctive.

'side' is used for resetting local or remote side for
current user for repre.

'site_name' is used to set synchronization for particular site.
Should be used when repre should be synced to new site.

Parameters:

Name Type Description Default
project_name str

name of project (eg. collection) in DB

required
representation_id str

Representation id.

required
file_id str

File id in representation.

None
side str

Local or remote side.

None
site_name str

for adding new site

None
Source code in client/ayon_sitesync/addon.py
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
def reset_site_on_representation(
    self,
    project_name,
    representation_id,
    side=None,
    file_id=None,
    site_name=None
):
    """
        Reset information about synchronization for particular 'file_id'
        and provider.
        Useful for testing or forcing file to be reuploaded.

        'side' and 'site_name' are disjunctive.

        'side' is used for resetting local or remote side for
        current user for repre.

        'site_name' is used to set synchronization for particular site.
        Should be used when repre should be synced to new site.

    Args:
        project_name (str): name of project (eg. collection) in DB
        representation_id (str): Representation id.
        file_id (str): File id in representation.
        side (str): Local or remote side.
        site_name (str): for adding new site

    Raises:
        SiteAlreadyPresentError - if adding already existing site and
            not 'force'
        ValueError - other errors (repre not found, misconfiguration)
    """
    representation = get_representation_by_id(
        project_name, representation_id
    )
    if not representation:
        raise ValueError(
            "Representation {} not found in {}".format(
                representation_id, project_name
            )
        )

    if side and site_name:
        raise ValueError(
            "Misconfiguration, only one of side and"
            " site_name arguments should be passed."
        )

    if side:
        if side == "local":
            site_name = self.get_active_site(project_name)
        else:
            site_name = self.get_remote_site(project_name)

    self.add_site(
        project_name, representation_id, site_name, file_id, force=True
    )

reset_timer()

Called when waiting for next loop should be skipped.

In case of user's involvement (reset site), start that right away.

Source code in client/ayon_sitesync/addon.py
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
def reset_timer(self):
    """
        Called when waiting for next loop should be skipped.

        In case of user's involvement (reset site), start that right away.
    """

    if not self.enabled:
        return

    if self.sitesync_thread is None:
        self._reset_timer_with_rest_api()
    else:
        self.sitesync_thread.reset_timer()

server_init()

Actual initialization of Sync Server.

Source code in client/ayon_sitesync/addon.py
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
def server_init(self):
    """Actual initialization of Sync Server."""
    # import only in tray or Python3, because of Python2 hosts
    if not self.enabled:
        return

    from .sitesync import SiteSyncThread

    self.lock = threading.Lock()

    self.sitesync_thread = SiteSyncThread(self)

set_sync_project_settings(exclude_locals=False)

Set sync_project_settings for all projects (caching) Args: exclude_locals (bool): ignore overrides from Local Settings For performance

Source code in client/ayon_sitesync/addon.py
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
def set_sync_project_settings(self, exclude_locals=False):
    """
        Set sync_project_settings for all projects (caching)
        Args:
            exclude_locals (bool): ignore overrides from Local Settings
        For performance
    """
    sync_project_settings = self._prepare_sync_project_settings(
        exclude_locals)

    self._sync_project_settings = sync_project_settings

tray_exit()

Stops sync thread if running.

Called from Addon Manager

Source code in client/ayon_sitesync/addon.py
1102
1103
1104
1105
1106
1107
def tray_exit(self):
    """Stops sync thread if running.

    Called from Addon Manager
    """
    self.server_exit()

tray_init()

Initialization of Site Sync Server for Tray.

Called when tray is initialized, it checks if addon should be enabled. If not, no initialization necessary.

Source code in client/ayon_sitesync/addon.py
1065
1066
1067
1068
1069
1070
1071
def tray_init(self):
    """Initialization of Site Sync Server for Tray.

    Called when tray is initialized, it checks if addon should be
    enabled. If not, no initialization necessary.
    """
    self.server_init()

tray_start()

Triggered when Tray is started.

Checks if configuration presets are available and if there is any provider ('gdrive', 'S3') that is activated (eg. has valid credentials).

Source code in client/ayon_sitesync/addon.py
1085
1086
1087
1088
1089
1090
1091
1092
def tray_start(self):
    """Triggered when Tray is started.

    Checks if configuration presets are available and if there is
    any provider ('gdrive', 'S3') that is activated
    (eg. has valid credentials).
    """
    self.server_start()

unpause_project(project_name)

Unpause sync of whole project.

Does not fail or warn if project wasn't paused.

Parameters:

Name Type Description Default
project_name str

Project name.

required
Source code in client/ayon_sitesync/addon.py
647
648
649
650
651
652
653
654
655
656
657
658
659
660
def unpause_project(self, project_name):
    """Unpause sync of whole project.

    Does not fail or warn if project wasn't paused.

    Args:
        project_name (str): Project name.

    """
    self.log.info("Unpausing SiteSync for {}".format(project_name))
    try:
        self._paused_projects.remove(project_name)
    except KeyError:
        pass

unpause_representation(project_name, representation_id, site_name)

Unpause sync of representation entity on site.

Does not fail or warn if repre wasn't paused.

Parameters:

Name Type Description Default
project_name str

Project name.

required
representation_id str

Representation id.

required
site_name str

Site name 'gdrive', 'studio' etc.

required
Source code in client/ayon_sitesync/addon.py
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
def unpause_representation(
    self, project_name, representation_id, site_name
):
    """Unpause sync of representation entity on site.

    Does not fail or warn if repre wasn't paused.

    Args:
        project_name (str): Project name.
        representation_id (str): Representation id.
        site_name (str): Site name 'gdrive', 'studio' etc.
    """
    self.log.info("Unpausing SiteSync for {}".format(representation_id))
    try:
        self._paused_representations.remove(representation_id)
    except KeyError:
        pass
    # self.paused_representations is not persistent
    repre_entity = get_representation_by_id(
        project_name, representation_id
    )
    self.update_db(project_name, repre_entity, site_name, pause=False)

unpause_server()

Unpause server sync.

Source code in client/ayon_sitesync/addon.py
688
689
690
691
def unpause_server(self):
    """Unpause server sync."""
    self.log.info("Unpausing SiteSync")
    self._paused = False

update_db(project_name, repre_status, site_name, new_file_id=None, file=None, side=None, error=None, progress=None, priority=None, pause=None)

Update 'provider' portion of records in DB.

Parameters:

Name Type Description Default
project_name str

Project name. Force to db connection as each file might come from different collection.

required
repre_status dict

Representation status from sitesync database.

required
site_name str

Site name.

required
new_file_id Optional[str]

File id of new file.

None
file dict[str, Any]

info about processed file (pulled from DB)

None
side str

'local' | 'remote'

None
error str

exception message

None
progress float

0-1 of progress of upload/download

None
priority int

0-100 set priority

None
pause bool

stop synchronizing (only before starting of download, upload)

None

Returns:

Type Description

None

Source code in client/ayon_sitesync/addon.py
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
def update_db(
    self,
    project_name,
    repre_status,
    site_name,
    new_file_id=None,
    file=None,
    side=None,
    error=None,
    progress=None,
    priority=None,
    pause=None
):
    """Update 'provider' portion of records in DB.

    Args:
        project_name (str): Project name. Force to db connection as
            each file might come from different collection.
        repre_status (dict): Representation status from sitesync database.
        site_name (str): Site name.
        new_file_id (Optional[str]): File id of new file.
        file (dict[str, Any]): info about processed file (pulled from DB)
        side (str): 'local' | 'remote'
        error (str): exception message
        progress (float): 0-1 of progress of upload/download
        priority (int): 0-100 set priority
        pause (bool): stop synchronizing (only before starting of download,
            upload)

    Returns:
        None
    """
    files_status = []
    for file_status in repre_status["files"]:
        status_entity = copy.deepcopy(
            file_status["{}Status".format(side)]
        )
        status_entity["fileHash"] = file_status["fileHash"]
        status_entity["id"] = file_status["id"]
        if file_status["fileHash"] == file["fileHash"]:
            if new_file_id:
                status_entity["status"] = SiteSyncStatus.OK
                status_entity.pop("message")
                status_entity.pop("retries")
            elif progress is not None:
                status_entity["status"] = SiteSyncStatus.IN_PROGRESS
                status_entity["progress"] = progress
            elif error:
                max_retries = int(
                    self.sync_project_settings
                    [project_name]
                    ["config"]
                    ["retry_cnt"]
                )
                tries = status_entity.get("retries", 0)
                tries += 1
                status_entity["retries"] = tries
                status_entity["message"] = error
                if tries >= max_retries:
                    status_entity["status"] = SiteSyncStatus.FAILED
            elif pause is not None:
                if pause:
                    status_entity["pause"] = True
                else:
                    status_entity.remove("pause")
            files_status.append(status_entity)

    representation_id = repre_status["representationId"]

    endpoint = "{}/{}/state/{}/{}".format(
        self.endpoint_prefix,
        project_name,
        representation_id,
        site_name)

    # get to upload
    kwargs = {
        "files": files_status
    }

    if priority:
        kwargs["priority"] = priority

    response = ayon_api.post(endpoint, **kwargs)
    if response.status_code not in [200, 204]:
        raise RuntimeError("Cannot update status")

    if progress is not None or priority is not None:
        return

    status = "failed"
    error_str = "with error {}".format(error)
    if new_file_id:
        status = "succeeded with id {}".format(new_file_id)
        error_str = ""

    source_file = file.get("path", "")

    self.log.debug(
        "File for {} - {source_file} process {status} {error_str}".format(
            representation_id,
            status=status,
            source_file=source_file,
            error_str=error_str
        )
    )

validate_project(project_name, site_name, reset_missing=False)

Validate 'project_name' of 'site_name' and its local files

If file present and not marked with a 'site_name' in DB, DB is updated with site name and file modified date.

Parameters:

Name Type Description Default
project_name str

project name

required
site_name str

active site name

required
reset_missing bool

if True reset site in DB if missing physically to be resynched

False
Source code in client/ayon_sitesync/addon.py
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
def validate_project(self, project_name, site_name, reset_missing=False):
    """Validate 'project_name' of 'site_name' and its local files

    If file present and not marked with a 'site_name' in DB, DB is
    updated with site name and file modified date.

    Args:
        project_name (str): project name
        site_name (str): active site name
        reset_missing (bool): if True reset site in DB if missing
            physically to be resynched
    """
    self.log.debug("Validation of {} for {} started".format(
        project_name, site_name
    ))
    repre_entities = list(get_representations(project_name))
    if not repre_entities:
        self.log.debug("No repre found")
        return

    sites_added = 0
    sites_reset = 0
    repre_ids = [repre["id"] for repre in repre_entities]
    repre_states = self.get_representations_sync_state(
        project_name, repre_ids, site_name, site_name)

    for repre_entity in repre_entities:
        repre_id = repre_entity["id"]
        is_on_site = False
        repre_state = repre_states.get(repre_id)
        if repre_state:
            is_on_site = repre_state[0] == SiteSyncStatus.OK
        for repre_file in repre_entity.get("files", []):
            file_path = repre_file.get("path", "")
            local_file_path = self.get_local_file_path(
                project_name, site_name, file_path
            )

            file_exists = (
                local_file_path and os.path.exists(local_file_path)
            )
            if not is_on_site:
                if file_exists:
                    self.log.debug(
                        f"Adding presence on site '{site_name}' for "
                        f"'{repre_id}'"
                    )
                    self.add_site(
                        project_name,
                        repre_id,
                        site_name=site_name,
                        file_id=repre_file["id"],
                        force=True,
                        status=SiteSyncStatus.OK
                    )
                    sites_added += 1
            else:
                if not file_exists and reset_missing:
                    self.log.debug(
                        "Resetting site {} for {}".format(
                            site_name, repre_id
                        ))
                    self.reset_site_on_representation(
                        project_name,
                        repre_id,
                        site_name=site_name,
                        file_id=repre_file["_id"]
                    )
                    sites_reset += 1

    if sites_added % 100 == 0:
        self.log.debug("Sites added {}".format(sites_added))

    self.log.debug("Validation of {} for {} ended".format(
        project_name, site_name
    ))
    self.log.info("Sites added {}, sites reset {}".format(
        sites_added, reset_missing
    ))