Skip to content

integrate

IntegrateAsset

Bases: InstancePlugin

Register publish in the database and transfer files to destinations.

Steps

1) Register the product and version 2) Transfer the representation files to the destination 3) Register the representation

Requires

instance.data['representations'] - must be a list and each member must be a dictionary with following data: 'files': list of filenames for sequence, string for single file. Only the filename is allowed, without the folder path. 'stagingDir': "path/to/folder/with/files" 'name': representation name (usually the same as extension) 'ext': file extension optional data "frameStart" "frameEnd" 'fps' "data": additional metadata for each representation.

Source code in client/ayon_core/plugins/publish/integrate.py
  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
class IntegrateAsset(pyblish.api.InstancePlugin):
    """Register publish in the database and transfer files to destinations.

    Steps:
        1) Register the product and version
        2) Transfer the representation files to the destination
        3) Register the representation

    Requires:
        instance.data['representations'] - must be a list and each member
        must be a dictionary with following data:
            'files': list of filenames for sequence, string for single file.
                     Only the filename is allowed, without the folder path.
            'stagingDir': "path/to/folder/with/files"
            'name': representation name (usually the same as extension)
            'ext': file extension
        optional data
            "frameStart"
            "frameEnd"
            'fps'
            "data": additional metadata for each representation.
    """

    label = "Integrate Asset"
    order = pyblish.api.IntegratorOrder

    default_template_name = "publish"

    # Representation context keys that should always be written to
    # the database even if not used by the destination template
    db_representation_context_keys = [
        "project",
        "hierarchy",
        "folder",
        "task",
        "product",
        "version",
        "representation",
        "username",
        "user",
        "output",
        # OpenPype keys - should be removed
        "asset",  # folder[name]
        "subset",  # product[name]
        "family",  # product[type]
    ]

    def process(self, instance):
        # Instance should be integrated on a farm
        if instance.data.get("farm"):
            self.log.debug(
                "Instance is marked to be processed on farm. Skipping")
            return

        # Instance is marked to not get integrated
        if not instance.data.get("integrate", True):
            self.log.debug("Instance is marked to skip integrating. Skipping")
            return

        filtered_repres = self.filter_representations(instance)
        # Skip instance if there are not representations to integrate
        #   all representations should not be integrated
        if not filtered_repres:
            self.log.warning((
                "Skipping, there are no representations"
                " to integrate for instance {}"
            ).format(instance.data["productType"]))
            return

        file_transactions = FileTransaction(log=self.log,
                                            # Enforce unique transfers
                                            allow_queue_replacements=False)
        try:
            self.register(instance, file_transactions, filtered_repres)
        except DuplicateDestinationError as exc:
            # Raise DuplicateDestinationError as KnownPublishError
            # and rollback the transactions
            file_transactions.rollback()
            raise KnownPublishError(exc).with_traceback(sys.exc_info()[2])

        except Exception as exc:
            # clean destination
            # todo: preferably we'd also rollback *any* changes to the database
            file_transactions.rollback()
            self.log.critical("Error when registering", exc_info=True)
            raise exc

        # Finalizing can't rollback safely so no use for moving it to
        # the try, except.
        file_transactions.finalize()

    def filter_representations(self, instance):
        # Prepare repsentations that should be integrated
        repres = instance.data.get("representations")
        # Raise error if instance don't have any representations
        if not repres:
            raise KnownPublishError(
                "Instance {} has no representations to integrate".format(
                    instance.data["productType"]
                )
            )

        # Validate type of stored representations
        if not isinstance(repres, (list, tuple)):
            raise TypeError(
                "Instance 'files' must be a list, got: {0} {1}".format(
                    str(type(repres)), str(repres)
                )
            )

        # Filter representations
        filtered_repres = []
        for repre in repres:
            if "delete" in repre.get("tags", []):
                continue
            filtered_repres.append(repre)

        return filtered_repres

    def register(self, instance, file_transactions, filtered_repres):
        project_name = instance.context.data["projectName"]

        instance_stagingdir = instance.data.get("stagingDir")
        if not instance_stagingdir:
            self.log.debug((
                "{0} is missing reference to staging directory."
                " Will try to get it from representation."
            ).format(instance))

        else:
            self.log.debug(
                "Establishing staging directory "
                "@ {0}".format(instance_stagingdir)
            )

        template_name = self.get_template_name(instance)

        op_session = OperationsSession()
        product_entity = self.prepare_product(
            instance, op_session, project_name
        )
        version_entity = self.prepare_version(
            instance, op_session, product_entity, project_name
        )
        instance.data["versionEntity"] = version_entity

        anatomy = instance.context.data["anatomy"]

        # Get existing representations (if any)
        existing_repres_by_name = {
            repre_entity["name"].lower(): repre_entity
            for repre_entity in get_representations(
                project_name,
                version_ids=[version_entity["id"]]
            )
        }

        # Prepare all representations
        prepared_representations = []
        for repre in filtered_repres:
            # todo: reduce/simplify what is returned from this function
            prepared = self.prepare_representation(
                repre,
                template_name,
                existing_repres_by_name,
                version_entity,
                instance_stagingdir,
                instance)

            for src, dst in prepared["transfers"]:
                # todo: add support for hardlink transfers
                file_transactions.add(src, dst)

            prepared_representations.append(prepared)

        # Each instance can also have pre-defined transfers not explicitly
        # part of a representation - like texture resources used by a
        # .ma representation. Those destination paths are pre-defined, etc.
        # todo: should we move or simplify this logic?
        resource_destinations = set()

        file_copy_modes = [
            ("transfers", FileTransaction.MODE_COPY),
            ("hardlinks", FileTransaction.MODE_HARDLINK)
        ]
        for files_type, copy_mode in file_copy_modes:
            for src, dst in instance.data.get(files_type, []):
                self._validate_path_in_project_roots(anatomy, dst)

                file_transactions.add(src, dst, mode=copy_mode)
                resource_destinations.add(os.path.abspath(dst))

        # Bulk write to the database
        # We write the product and version to the database before the File
        # Transaction to reduce the chances of another publish trying to
        # publish to the same version number since that chance can greatly
        # increase if the file transaction takes a long time.
        op_session.commit()

        self.log.info((
            "Product '{}' version {} written to database.."
        ).format(product_entity["name"], version_entity["version"]))

        # Process all file transfers of all integrations now
        self.log.debug("Integrating source files to destination ...")
        file_transactions.process()
        self.log.debug(
            "Backed up existing files: {}".format(file_transactions.backups))
        self.log.debug(
            "Transferred files: {}".format(file_transactions.transferred))
        self.log.debug("Retrieving Representation Site Sync information ...")

        # Compute the resource file infos once (files belonging to the
        # version instance instead of an individual representation) so
        # we can reuse those file infos per representation
        resource_file_infos = self.get_files_info(
            resource_destinations, anatomy
        )

        # Finalize the representations now the published files are integrated
        # Get 'files' info for representations and its attached resources
        new_repre_names_low = set()
        for prepared in prepared_representations:
            repre_entity = prepared["representation"]
            repre_update_data = prepared["repre_update_data"]
            transfers = prepared["transfers"]
            destinations = [dst for src, dst in transfers]
            repre_files = self.get_files_info(
                destinations, anatomy
            )
            # Add the version resource file infos to each representation
            repre_files += resource_file_infos
            repre_entity["files"] = repre_files

            # Set up representation for writing to the database. Since
            # we *might* be overwriting an existing entry if the version
            # already existed we'll use ReplaceOnce with `upsert=True`
            if repre_update_data is None:
                op_session.create_entity(
                    project_name, "representation", repre_entity
                )
            else:
                # Add files to update data
                repre_update_data["files"] = repre_files
                op_session.update_entity(
                    project_name,
                    "representation",
                    repre_entity["id"],
                    repre_update_data
                )

            new_repre_names_low.add(repre_entity["name"].lower())

        # Delete any existing representations that didn't get any new data
        # if the instance is not set to append mode
        if not instance.data.get("append", False):
            for name, existing_repres in existing_repres_by_name.items():
                if name not in new_repre_names_low:
                    # We add the exact representation name because `name` is
                    # lowercase for name matching only and not in the database
                    op_session.delete_entity(
                        project_name, "representation", existing_repres["id"]
                    )

        self.log.debug("{}".format(op_session.to_data()))
        op_session.commit()

        # Backwards compatibility used in hero integration.
        # todo: can we avoid the need to store this?
        instance.data["published_representations"] = {
            p["representation"]["id"]: p
            for p in prepared_representations
        }

        self.log.info(
            "Registered {} representations: {}".format(
                len(prepared_representations),
                ", ".join(p["representation"]["name"]
                          for p in prepared_representations)
            )
        )

    def prepare_product(self, instance, op_session, project_name):
        folder_entity = instance.data["folderEntity"]
        product_name = instance.data["productName"]
        product_type = instance.data["productType"]
        self.log.debug("Product: {}".format(product_name))

        # Get existing product if it exists
        existing_product_entity = get_product_by_name(
            project_name, product_name, folder_entity["id"]
        )

        # Define product data
        data = {
            "families": get_instance_families(instance)
        }
        attributes = {}

        product_group = instance.data.get("productGroup")
        if product_group:
            attributes["productGroup"] = product_group
        elif existing_product_entity:
            # Preserve previous product group if new version does not set it
            product_group = existing_product_entity.get("attrib", {}).get(
                "productGroup"
            )
            if product_group is not None:
                attributes["productGroup"] = product_group

        product_id = None
        if existing_product_entity:
            product_id = existing_product_entity["id"]
        product_entity = new_product_entity(
            product_name,
            product_type,
            folder_entity["id"],
            data=data,
            attribs=attributes,
            entity_id=product_id
        )

        if existing_product_entity is None:
            # Create a new product
            self.log.info(
                "Product '%s' not found, creating ..." % product_name
            )
            op_session.create_entity(
                project_name, "product", product_entity
            )

        else:
            # Update existing product data with new data and set in database.
            # We also change the found product in-place so we don't need to
            # re-query the product afterwards
            update_data = prepare_changes(
                existing_product_entity, product_entity
            )
            op_session.update_entity(
                project_name,
                "product",
                product_entity["id"],
                update_data
            )

        self.log.debug("Prepared product: {}".format(product_name))
        return product_entity

    def prepare_version(
        self, instance, op_session, product_entity, project_name
    ):
        version_number = instance.data["version"]
        task_id = None
        task_entity = instance.data.get("taskEntity")
        if task_entity:
            task_id = task_entity["id"]

        existing_version = get_version_by_name(
            project_name,
            version_number,
            product_entity["id"]
        )
        version_id = None
        if existing_version:
            version_id = existing_version["id"]

        all_version_data = self.create_version_data(instance)
        version_data = {}
        version_attributes = {}
        attr_defs = self._get_attributes_for_type(instance.context, "version")
        for key, value in all_version_data.items():
            if key in attr_defs:
                version_attributes[key] = value
            else:
                version_data[key] = value

        version_entity = new_version_entity(
            version_number,
            product_entity["id"],
            task_id=task_id,
            status=instance.data.get("status"),
            data=version_data,
            attribs=version_attributes,
            entity_id=version_id,
        )

        if existing_version:
            self.log.debug("Updating existing version ...")
            update_data = prepare_changes(existing_version, version_entity)
            op_session.update_entity(
                project_name,
                "version",
                version_entity["id"],
                update_data
            )
        else:
            self.log.debug("Creating new version ...")
            op_session.create_entity(
                project_name, "version", version_entity
            )

        self.log.debug(
            "Prepared version: v{0:03d}".format(version_entity["version"])
        )

        return version_entity

    def _validate_repre_files(self, files, is_sequence_representation):
        """Validate representation files before transfer preparation.

        Check if files contain only filenames instead of full paths and check
        if sequence don't contain more than one sequence or has remainders.

        Args:
            files (Union[str, List[str]]): Files from representation.
            is_sequence_representation (bool): Files are for sequence.

        Raises:
            KnownPublishError: If validations don't pass.
        """

        if not files:
            return

        if not is_sequence_representation:
            files = [files]

        for fname in files:
            if os.path.isabs(fname):
                raise KnownPublishError(
                    f"Representation file names contains full paths: {fname}"
                )

        if not is_sequence_representation:
            return

        src_collections, remainders = clique.assemble(files)
        if len(files) < 2 or len(src_collections) != 1 or remainders:
            raise KnownPublishError((
                "Files of representation does not contain proper"
                " sequence files.\nCollected collections: {}"
                "\nCollected remainders: {}"
            ).format(
                ", ".join([str(col) for col in src_collections]),
                ", ".join([str(rem) for rem in remainders])
            ))

    def prepare_representation(
        self,
        repre,
        template_name,
        existing_repres_by_name,
        version_entity,
        instance_stagingdir,
        instance
    ):
        # pre-flight validations
        if repre["ext"].startswith("."):
            raise KnownPublishError((
                "Extension must not start with a dot '.': {}"
            ).format(repre["ext"]))

        if repre.get("transfers"):
            raise KnownPublishError((
                "Representation is not allowed to have transfers"
                "data before integration. They are computed in "
                "the integrator. Got: {}"
            ).format(repre["transfers"]))

        # create template data for Anatomy
        template_data = copy.deepcopy(instance.data["anatomyData"])

        # required representation keys
        files = repre["files"]
        template_data["representation"] = repre["name"]
        template_data["ext"] = repre["ext"]

        # allow overwriting existing version
        template_data["version"] = version_entity["version"]

        # add template data for colorspaceData
        if repre.get("colorspaceData"):
            colorspace = repre["colorspaceData"]["colorspace"]
            # replace spaces with underscores
            # pipeline.colorspace.parse_colorspace_from_filepath
            # is checking it with underscores too
            colorspace = colorspace.replace(" ", "_")
            template_data["colorspace"] = colorspace

        stagingdir = repre.get("stagingDir")
        if not stagingdir:
            # Fall back to instance staging dir if not explicitly
            # set for representation in the instance
            self.log.debug((
                "Representation uses instance staging dir: {}"
            ).format(instance_stagingdir))
            stagingdir = instance_stagingdir

        if not stagingdir:
            raise KnownPublishError(
                "No staging directory set for representation: {}".format(repre)
            )

        # optionals
        # retrieve additional anatomy data from representation if exists
        for key, anatomy_key in {
            # Representation Key: Anatomy data key
            "resolutionWidth": "resolution_width",
            "resolutionHeight": "resolution_height",
            "fps": "fps",
            "outputName": "output",
            "originalBasename": "originalBasename"
        }.items():
            # Allow to take value from representation
            # if not found also consider instance.data
            value = repre.get(key)
            if value is None:
                value = instance.data.get(key)

            if value is not None:
                template_data[anatomy_key] = value

        self.log.debug("Anatomy template name: {}".format(template_name))
        anatomy = instance.context.data["anatomy"]
        publish_template = anatomy.get_template_item("publish", template_name)
        path_template_obj = publish_template["path"]
        template = path_template_obj.template.replace("\\", "/")

        is_udim = bool(repre.get("udim"))

        # handle publish in place
        if "{originalDirname}" in template:
            # store as originalDirname only original value without project root
            # if instance collected originalDirname is present, it should be
            # used for all represe
            # from temp to final
            original_directory = (
                instance.data.get("originalDirname") or instance_stagingdir)

            _rootless = self.get_rootless_path(anatomy, original_directory)
            if _rootless == original_directory:
                raise KnownPublishError((
                        "Destination path '{}' ".format(original_directory) +
                        "must be in project dir"
                ))
            relative_path_start = _rootless.rfind('}') + 2
            without_root = _rootless[relative_path_start:]
            template_data["originalDirname"] = without_root

        is_sequence_representation = isinstance(files, (list, tuple))
        self._validate_repre_files(files, is_sequence_representation)

        # Output variables of conditions below:
        # - transfers (List[Tuple[str, str]]): src -> dst filepaths to copy
        # - repre_context (Dict[str, Any]): context data used to fill template
        # - template_data (Dict[str, Any]): source data used to fill template
        #   - to add required data to 'repre_context' not used for
        #       formatting

        # Treat template with 'orignalBasename' in special way
        if "{originalBasename}" in template:
            # Remove 'frame' from template data
            template_data.pop("frame", None)

            # Find out first frame string value
            first_index_padded = None
            if not is_udim and is_sequence_representation:
                col = clique.assemble(files)[0][0]
                sorted_frames = tuple(sorted(col.indexes))
                # First frame used for end value
                first_frame = sorted_frames[0]
                # Get last frame for padding
                last_frame = sorted_frames[-1]
                # Use padding from collection of length of last frame as string
                padding = max(col.padding, len(str(last_frame)))
                first_index_padded = get_frame_padded(
                    frame=first_frame,
                    padding=padding
                )

            # Convert files to list for single file as remaining part is only
            #   transfers creation (iteration over files)
            if not is_sequence_representation:
                files = [files]

            repre_context = None
            transfers = []
            for src_file_name in files:
                template_data["originalBasename"], _ = os.path.splitext(
                    src_file_name)

                dst = path_template_obj.format_strict(template_data)
                src = os.path.join(stagingdir, src_file_name)
                transfers.append((src, dst))
                if repre_context is None:
                    repre_context = dst.used_values

            if not is_udim and first_index_padded is not None:
                repre_context["frame"] = first_index_padded

        elif is_sequence_representation:
            # Collection of files (sequence)
            src_collections, remainders = clique.assemble(files)

            src_collection = src_collections[0]
            destination_indexes = list(src_collection.indexes)
            # Use last frame for minimum padding
            #   - that should cover both 'udim' and 'frame' minimum padding
            destination_padding = len(str(destination_indexes[-1]))
            if not is_udim:
                # Change padding for frames if template has defined higher
                #   padding.
                template_padding = anatomy.templates_obj.frame_padding
                if template_padding > destination_padding:
                    destination_padding = template_padding

                # If the representation has `frameStart` set it renumbers the
                # frame indices of the published collection. It will start from
                # that `frameStart` index instead. Thus if that frame start
                # differs from the collection we want to shift the destination
                # frame indices from the source collection.
                # In case source are published in place we need to
                # skip renumbering
                repre_frame_start = repre.get("frameStart")
                explicit_frames = instance.data.get("hasExplicitFrames", False)
                if not explicit_frames and repre_frame_start is not None:
                    index_frame_start = int(repre_frame_start)
                    # Shift destination sequence to the start frame
                    destination_indexes = [
                        index_frame_start + idx
                        for idx in range(len(destination_indexes))
                    ]

            # To construct the destination template with anatomy we require
            # a Frame or UDIM tile set for the template data. We use the first
            # index of the destination for that because that could've shifted
            # from the source indexes, etc.
            first_index_padded = get_frame_padded(
                frame=destination_indexes[0],
                padding=destination_padding
            )

            # Construct destination collection from template
            repre_context = None
            dst_filepaths = []
            for index in destination_indexes:
                if is_udim:
                    template_data["udim"] = index
                else:
                    template_data["frame"] = index
                template_filled = path_template_obj.format_strict(
                    template_data
                )
                dst_filepaths.append(template_filled)
                if repre_context is None:
                    self.log.debug(
                        "Template filled: {}".format(str(template_filled))
                    )
                    repre_context = template_filled.used_values

            # Make sure context contains frame
            # NOTE: Frame would not be available only if template does not
            #   contain '{frame}' in template -> Do we want support it?
            if not is_udim:
                repre_context["frame"] = first_index_padded

            # store renderlayer in context if it exists
            # to be later used for example by delivery templates
            if instance.data.get("renderlayer"):
                repre_context["renderlayer"] = instance.data["renderlayer"]

            # Update the destination indexes and padding
            dst_collection = clique.assemble(dst_filepaths)[0][0]
            dst_collection.padding = destination_padding
            if len(src_collection.indexes) != len(dst_collection.indexes):
                raise KnownPublishError((
                    "This is a bug. Source sequence frames length"
                    " does not match integration frames length"
                ))

            # Multiple file transfers
            transfers = []
            for src_file_name, dst in zip(src_collection, dst_collection):
                src = os.path.join(stagingdir, src_file_name)
                transfers.append((src, dst))

        else:
            # Single file
            # Manage anatomy template data
            template_data.pop("frame", None)
            if is_udim:
                template_data["udim"] = repre["udim"][0]
            # Construct destination filepath from template
            template_filled = path_template_obj.format_strict(template_data)
            repre_context = template_filled.used_values
            dst = os.path.normpath(template_filled)

            # Single file transfer
            src = os.path.join(stagingdir, files)
            transfers = [(src, dst)]

        # todo: Are we sure the assumption each representation
        #       ends up in the same folder is valid?
        if not instance.data.get("publishDir"):
            template_obj = publish_template["directory"]
            template_filled = template_obj.format_strict(template_data)
            instance.data["publishDir"] = template_filled

        for key in self.db_representation_context_keys:
            # Also add these values to the context even if not used by the
            # destination template
            value = template_data.get(key)
            if value is not None:
                repre_context[key] = value

        # Use previous representation's id if there is a name match
        existing = existing_repres_by_name.get(repre["name"].lower())
        repre_id = None
        if existing:
            repre_id = existing["id"]

        # Store first transferred destination as published path data
        # - used primarily for reviews that are integrated to custom modules
        # TODO we should probably store all integrated files
        #   related to the representation?
        published_path = transfers[0][1]
        repre["published_path"] = published_path

        # todo: `repre` is not the actual `representation` entity
        #       we should simplify/clarify difference between data above
        #       and the actual representation entity for the database
        attr_defs = self._get_attributes_for_type(
            instance.context, "representation"
        )
        attributes = {"path": published_path, "template": template}
        data = {"context": repre_context}
        for key, value in repre.get("data", {}).items():
            if key in attr_defs:
                attributes[key] = value
            else:
                data[key] = value

        # add colorspace data if any exists on representation
        if repre.get("colorspaceData"):
            data["colorspaceData"] = repre["colorspaceData"]

        repre_doc = new_representation_entity(
            repre["name"],
            version_entity["id"],
            # files are filled afterwards
            [],
            data=data,
            attribs=attributes,
            entity_id=repre_id
        )
        update_data = None
        if repre_id is not None:
            update_data = prepare_changes(existing, repre_doc)

        return {
            "representation": repre_doc,
            "repre_update_data": update_data,
            "anatomy_data": template_data,
            "transfers": transfers,
            # todo: avoid the need for 'published_files' used by Integrate Hero
            # backwards compatibility
            "published_files": [transfer[1] for transfer in transfers]
        }

    def create_version_data(self, instance):
        """Create the data dictionary for the version

        Args:
            instance: the current instance being published

        Returns:
            dict: the required information for version["data"]
        """

        context = instance.context

        # create relative source path for DB
        if "source" in instance.data:
            source = instance.data["source"]
        else:
            source = context.data["currentFile"]
            anatomy = instance.context.data["anatomy"]
            source = self.get_rootless_path(anatomy, source)
        self.log.debug("Source: {}".format(source))

        version_data = {
            "families": get_instance_families(instance),
            "time": context.data["time"],
            "author": context.data["user"],
            "source": source,
            "comment": instance.data["comment"],
            "machine": context.data.get("machine"),
            "fps": instance.data.get("fps", context.data.get("fps"))
        }

        # todo: preferably we wouldn't need this "if dict" etc. logic and
        #       instead be able to rely what the input value is if it's set.
        intent_value = context.data.get("intent")
        if intent_value and isinstance(intent_value, dict):
            intent_value = intent_value.get("value")

        if intent_value:
            version_data["intent"] = intent_value

        # Include optional data if present in
        optionals = [
            "frameStart", "frameEnd", "step",
            "handleEnd", "handleStart", "sourceHashes"
        ]
        for key in optionals:
            if key in instance.data:
                version_data[key] = instance.data[key]

        # Include instance.data[versionData] directly
        version_data_instance = instance.data.get("versionData")
        if version_data_instance:
            version_data.update(version_data_instance)

        return version_data

    def get_template_name(self, instance):
        """Return anatomy template name to use for integration"""

        # Anatomy data is pre-filled by Collectors
        context = instance.context
        project_name = context.data["projectName"]

        # Task can be optional in anatomy data
        host_name = context.data["hostName"]
        anatomy_data = instance.data["anatomyData"]
        product_type = instance.data["productType"]
        task_info = anatomy_data.get("task") or {}

        return get_publish_template_name(
            project_name,
            host_name,
            product_type,
            task_name=task_info.get("name"),
            task_type=task_info.get("type"),
            project_settings=context.data["project_settings"],
            logger=self.log
        )

    def get_rootless_path(self, anatomy, path):
        """Returns, if possible, path without absolute portion from root
            (eg. 'c:\' or '/opt/..')

         This information is platform dependent and shouldn't be captured.
         Example:
             'c:/projects/MyProject1/Assets/publish...' >
             '{root}/MyProject1/Assets...'

        Args:
            anatomy (Anatomy): Project anatomy.
            path (str): Absolute path.

        Returns:
            str: Path where root path is replaced by formatting string.

        """
        success, rootless_path = anatomy.find_root_template_from_path(path)
        if success:
            path = rootless_path
        else:
            self.log.warning((
                "Could not find root path for remapping \"{}\"."
                " This may cause issues on farm."
            ).format(path))
        return path

    def get_files_info(self, filepaths, anatomy):
        """Prepare 'files' info portion for representations.

        Arguments:
            filepaths (Iterable[str]): List of transferred file paths.
            anatomy (Anatomy): Project anatomy.

        Returns:
            list[dict[str, Any]]: Representation 'files' information.

        """
        file_infos = []
        for filepath in filepaths:
            file_info = self.prepare_file_info(filepath, anatomy)
            file_infos.append(file_info)
        return file_infos

    def prepare_file_info(self, path, anatomy):
        """ Prepare information for one file (asset or resource)

        Arguments:
            path (str): Destination url of published file.
            anatomy (Anatomy): Project anatomy part from instance.

        Returns:
            dict[str, Any]: Representation file info dictionary.

        """
        return {
            "id": create_entity_id(),
            "name": os.path.basename(path),
            "path": self.get_rootless_path(anatomy, path),
            "size": os.path.getsize(path),
            "hash": source_hash(path),
            "hash_type": "op3",
        }

    def _validate_path_in_project_roots(self, anatomy, file_path):
        """Checks if 'file_path' starts with any of the roots.

        Used to check that published path belongs to project, eg. we are not
        trying to publish to local only folder.
        Args:
            anatomy (Anatomy): Project anatomy.
            file_path (str): Filepath.

        Raises:
            KnownPublishError: When failed to find root for the path.
        """
        path = self.get_rootless_path(anatomy, file_path)
        if not path:
            raise KnownPublishError((
                "Destination path '{}' ".format(file_path) +
                "must be in project dir"
            ))

    def _get_attributes_for_type(self, context, entity_type):
        return self._get_attributes_by_type(context)[entity_type]

    def _get_attributes_by_type(self, context):
        attributes = context.data.get("ayonAttributes")
        if attributes is None:
            attributes = {}
            for key in (
                "project",
                "folder",
                "product",
                "version",
                "representation",
            ):
                attributes[key] = get_attributes_for_type(key)
            context.data["ayonAttributes"] = attributes
        return attributes

create_version_data(instance)

Create the data dictionary for the version

Parameters:

Name Type Description Default
instance

the current instance being published

required

Returns:

Name Type Description
dict

the required information for version["data"]

Source code in client/ayon_core/plugins/publish/integrate.py
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
def create_version_data(self, instance):
    """Create the data dictionary for the version

    Args:
        instance: the current instance being published

    Returns:
        dict: the required information for version["data"]
    """

    context = instance.context

    # create relative source path for DB
    if "source" in instance.data:
        source = instance.data["source"]
    else:
        source = context.data["currentFile"]
        anatomy = instance.context.data["anatomy"]
        source = self.get_rootless_path(anatomy, source)
    self.log.debug("Source: {}".format(source))

    version_data = {
        "families": get_instance_families(instance),
        "time": context.data["time"],
        "author": context.data["user"],
        "source": source,
        "comment": instance.data["comment"],
        "machine": context.data.get("machine"),
        "fps": instance.data.get("fps", context.data.get("fps"))
    }

    # todo: preferably we wouldn't need this "if dict" etc. logic and
    #       instead be able to rely what the input value is if it's set.
    intent_value = context.data.get("intent")
    if intent_value and isinstance(intent_value, dict):
        intent_value = intent_value.get("value")

    if intent_value:
        version_data["intent"] = intent_value

    # Include optional data if present in
    optionals = [
        "frameStart", "frameEnd", "step",
        "handleEnd", "handleStart", "sourceHashes"
    ]
    for key in optionals:
        if key in instance.data:
            version_data[key] = instance.data[key]

    # Include instance.data[versionData] directly
    version_data_instance = instance.data.get("versionData")
    if version_data_instance:
        version_data.update(version_data_instance)

    return version_data

get_files_info(filepaths, anatomy)

Prepare 'files' info portion for representations.

Parameters:

Name Type Description Default
filepaths Iterable[str]

List of transferred file paths.

required
anatomy Anatomy

Project anatomy.

required

Returns:

Type Description

list[dict[str, Any]]: Representation 'files' information.

Source code in client/ayon_core/plugins/publish/integrate.py
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
def get_files_info(self, filepaths, anatomy):
    """Prepare 'files' info portion for representations.

    Arguments:
        filepaths (Iterable[str]): List of transferred file paths.
        anatomy (Anatomy): Project anatomy.

    Returns:
        list[dict[str, Any]]: Representation 'files' information.

    """
    file_infos = []
    for filepath in filepaths:
        file_info = self.prepare_file_info(filepath, anatomy)
        file_infos.append(file_info)
    return file_infos

get_rootless_path(anatomy, path)

Returns, if possible, path without absolute portion from root (eg. 'c:' or '/opt/..')

This information is platform dependent and shouldn't be captured. Example: 'c:/projects/MyProject1/Assets/publish...' > '{root}/MyProject1/Assets...'

Parameters:

Name Type Description Default
anatomy Anatomy

Project anatomy.

required
path str

Absolute path.

required

Returns:

Name Type Description
str

Path where root path is replaced by formatting string.

Source code in client/ayon_core/plugins/publish/integrate.py
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
def get_rootless_path(self, anatomy, path):
    """Returns, if possible, path without absolute portion from root
        (eg. 'c:\' or '/opt/..')

     This information is platform dependent and shouldn't be captured.
     Example:
         'c:/projects/MyProject1/Assets/publish...' >
         '{root}/MyProject1/Assets...'

    Args:
        anatomy (Anatomy): Project anatomy.
        path (str): Absolute path.

    Returns:
        str: Path where root path is replaced by formatting string.

    """
    success, rootless_path = anatomy.find_root_template_from_path(path)
    if success:
        path = rootless_path
    else:
        self.log.warning((
            "Could not find root path for remapping \"{}\"."
            " This may cause issues on farm."
        ).format(path))
    return path

get_template_name(instance)

Return anatomy template name to use for integration

Source code in client/ayon_core/plugins/publish/integrate.py
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
def get_template_name(self, instance):
    """Return anatomy template name to use for integration"""

    # Anatomy data is pre-filled by Collectors
    context = instance.context
    project_name = context.data["projectName"]

    # Task can be optional in anatomy data
    host_name = context.data["hostName"]
    anatomy_data = instance.data["anatomyData"]
    product_type = instance.data["productType"]
    task_info = anatomy_data.get("task") or {}

    return get_publish_template_name(
        project_name,
        host_name,
        product_type,
        task_name=task_info.get("name"),
        task_type=task_info.get("type"),
        project_settings=context.data["project_settings"],
        logger=self.log
    )

prepare_file_info(path, anatomy)

Prepare information for one file (asset or resource)

Parameters:

Name Type Description Default
path str

Destination url of published file.

required
anatomy Anatomy

Project anatomy part from instance.

required

Returns:

Type Description

dict[str, Any]: Representation file info dictionary.

Source code in client/ayon_core/plugins/publish/integrate.py
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
def prepare_file_info(self, path, anatomy):
    """ Prepare information for one file (asset or resource)

    Arguments:
        path (str): Destination url of published file.
        anatomy (Anatomy): Project anatomy part from instance.

    Returns:
        dict[str, Any]: Representation file info dictionary.

    """
    return {
        "id": create_entity_id(),
        "name": os.path.basename(path),
        "path": self.get_rootless_path(anatomy, path),
        "size": os.path.getsize(path),
        "hash": source_hash(path),
        "hash_type": "op3",
    }

get_frame_padded(frame, padding)

Return frame number as string with padding amount of padded zeros

Source code in client/ayon_core/plugins/publish/integrate.py
80
81
82
def get_frame_padded(frame, padding):
    """Return frame number as string with `padding` amount of padded zeros"""
    return "{frame:0{padding}d}".format(padding=padding, frame=frame)

get_instance_families(instance)

Get all families of the instance

Source code in client/ayon_core/plugins/publish/integrate.py
65
66
67
68
69
70
71
72
73
74
75
76
77
def get_instance_families(instance):
    """Get all families of the instance"""
    # todo: move this to lib?
    family = instance.data.get("family")
    families = []
    if family:
        families.append(family)

    for _family in (instance.data.get("families") or []):
        if _family not in families:
            families.append(_family)

    return families

prepare_changes(old_entity, new_entity)

Prepare changes for entity update.

Parameters:

Name Type Description Default
old_entity

Existing entity.

required
new_entity

New entity.

required

Returns:

Type Description

dict[str, Any]: Changes that have new entity.

Source code in client/ayon_core/plugins/publish/integrate.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def prepare_changes(old_entity, new_entity):
    """Prepare changes for entity update.

    Args:
        old_entity: Existing entity.
        new_entity: New entity.

    Returns:
        dict[str, Any]: Changes that have new entity.

    """
    changes = {}
    for key in set(new_entity.keys()):
        if key == "attrib":
            continue

        if key in new_entity and new_entity[key] != old_entity.get(key):
            changes[key] = new_entity[key]
            continue

    attrib_changes = {}
    if "attrib" in new_entity:
        for key, value in new_entity["attrib"].items():
            if value != old_entity["attrib"].get(key):
                attrib_changes[key] = value
    if attrib_changes:
        changes["attrib"] = attrib_changes
    return changes