|  31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 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 | class AyonDeadlinePlugin(DeadlinePlugin):
    """
        Standalone plugin for publishing from AYON
        Calls Ayonexecutable 'ayon_console' from first correctly found
        file based on plugin configuration. Uses 'publish' command and passes
        path to metadata json file, which contains all needed information
        for publish process.
    """
    def __init__(self):
        super().__init__()
        self.InitializeProcessCallback += self.InitializeProcess
        self.RenderExecutableCallback += self.RenderExecutable
        self.RenderArgumentCallback += self.RenderArgument
    def Cleanup(self):
        for stdoutHandler in self.StdoutHandlers:
            del stdoutHandler.HandleCallback
        del self.InitializeProcessCallback
        del self.RenderExecutableCallback
        del self.RenderArgumentCallback
    def InitializeProcess(self):
        self.LogInfo(
            "Initializing process with AYON plugin {}".format(__version__)
        )
        self.PluginType = PluginType.Simple
        self.StdoutHandling = True
        self.SingleFramesOnly = self.GetBooleanPluginInfoEntryWithDefault(
            "SingleFramesOnly", False)
        self.LogInfo("Single Frames Only: %s" % self.SingleFramesOnly)
        self.AddStdoutHandlerCallback(
            ".*Progress: (\\d+)%.*").HandleCallback += self.HandleProgress
    def RenderExecutable(self):
        job = self.GetJob()
        # set required env vars for AYON
        # cannot be in InitializeProcess as it is too soon
        config = RepositoryUtils.GetPluginConfig("Ayon")  # plugin name stays
        ayon_server_url = (
            job.GetJobEnvironmentKeyValue("AYON_SERVER_URL")
            or config.GetConfigEntryWithDefault("AyonServerUrl", "")
        )
        ayon_api_key = (
            job.GetJobEnvironmentKeyValue("AYON_API_KEY")
            or config.GetConfigEntryWithDefault("AyonApiKey", "")
        )
        ayon_bundle_name = job.GetJobEnvironmentKeyValue("AYON_BUNDLE_NAME")
        environment = {
            "AYON_SERVER_URL": ayon_server_url,
            "AYON_API_KEY": ayon_api_key,
            "AYON_BUNDLE_NAME": ayon_bundle_name,
        }
        for env, val in environment.items():
            self.SetEnvironmentVariable(env, val)
        exe_list = self.GetConfigEntry("AyonExecutable")
        # clean '\ ' for MacOS pasting
        if platform.system().lower() == "darwin":
            exe_list = exe_list.replace("\\ ", " ")
        expanded_paths = []
        for path in exe_list.split(";"):
            if path.startswith("~"):
                path = os.path.expanduser(path)
            expanded_paths.append(path)
        exe = FileUtils.SearchFileList(";".join(expanded_paths))
        if exe == "":
            self.FailRender(
                "AYON executable was not found in the semicolon separated "
                "list: \"{}\". The path to the render executable can be "
                "configured from the Plugin Configuration in the Deadline "
                "Monitor.".format(exe_list)
            )
        return exe
    def RenderArgument(self):
        arguments = str(self.GetPluginInfoEntryWithDefault("Arguments", ""))
        arguments = RepositoryUtils.CheckPathMapping(arguments)
        arguments = re.sub(r"<(?i)STARTFRAME>", str(self.GetStartFrame()),
                           arguments)
        arguments = re.sub(r"<(?i)ENDFRAME>", str(self.GetEndFrame()),
                           arguments)
        arguments = re.sub(r"<(?i)QUOTE>", "\"", arguments)
        arguments = self.ReplacePaddedFrame(arguments,
                                            "<(?i)STARTFRAME%([0-9]+)>",
                                            self.GetStartFrame())
        arguments = self.ReplacePaddedFrame(arguments,
                                            "<(?i)ENDFRAME%([0-9]+)>",
                                            self.GetEndFrame())
        count = 0
        for filename in self.GetAuxiliaryFilenames():
            localAuxFile = Path.Combine(self.GetJobsDataDirectory(), filename)
            arguments = re.sub(r"<(?i)AUXFILE" + str(count) + r">",
                               localAuxFile.replace("\\", "/"), arguments)
            count += 1
        return arguments
    def ReplacePaddedFrame(self, arguments, pattern, frame):
        frameRegex = Regex(pattern)
        while True:
            frameMatch = frameRegex.Match(arguments)
            if not frameMatch.Success:
                break
            paddingSize = int(frameMatch.Groups[1].Value)
            if paddingSize > 0:
                padding = StringUtils.ToZeroPaddedString(
                    frame, paddingSize, False)
            else:
                padding = str(frame)
            arguments = arguments.replace(
                frameMatch.Groups[0].Value, padding)
        return arguments
    def HandleProgress(self):
        progress = float(self.GetRegexMatch(1))
        self.SetProgress(progress)
 |