feat: first commit
This commit is contained in:
1 parent
8222407518
commit
2b0f9973fa
18 files changed
+1432
-1
No files matched your search
@@ -0,0 +1 @@
|
||||
# Perforce Addon
|
||||
@@ -0,0 +1,5 @@
|
||||
from .addon import PerforceAddon
|
||||
|
||||
__all__ = (
|
||||
"PerforceAddon",
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
import os
|
||||
from openpype.modules import OpenPypeModule, ILaunchHookPaths
|
||||
|
||||
from .version import __version__
|
||||
|
||||
PERFORCE_MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
class PerforceAddon(OpenPypeModule, ILaunchHookPaths):
|
||||
"""Perfrorce Addon
|
||||
"""
|
||||
name = "perforce"
|
||||
version = __version__
|
||||
label = "Perforce"
|
||||
|
||||
def initialize(self, module_settings):
|
||||
self.enabled = True
|
||||
|
||||
def get_launch_hook_paths(self, app):
|
||||
"""Implementation for applications launch hooks.
|
||||
|
||||
Returns:
|
||||
(str): full absolut path to directory with hooks for the module
|
||||
"""
|
||||
return os.path.join(PERFORCE_MODULE_DIR, "hooks")
|
||||
@@ -0,0 +1,96 @@
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from openpype.lib.applications import (
|
||||
PreLaunchHook, LaunchTypes, ApplicationLaunchFailed
|
||||
)
|
||||
|
||||
class PerforceSync(PreLaunchHook):
|
||||
"""Run Perforce Sync for the given project config.
|
||||
"""
|
||||
order = -1
|
||||
launch_types = {LaunchTypes.local}
|
||||
|
||||
def execute(self, *args, **kwargs):
|
||||
|
||||
perforce_settings = self.data["project_settings"]["perforce"]
|
||||
|
||||
perforce_hosts = {
|
||||
host["name"]: host["bypass"]
|
||||
for host in perforce_settings["hosts"]
|
||||
}
|
||||
|
||||
if self.host_name not in perforce_hosts:
|
||||
return
|
||||
|
||||
host_bypass = perforce_hosts[self.host_name]
|
||||
|
||||
p4_port = (
|
||||
perforce_settings.get("p4port")
|
||||
or self.data["env"].get("P4PORT")
|
||||
)
|
||||
p4_client = (
|
||||
perforce_settings.get("p4client")
|
||||
or self.data["env"].get("P4CLIENT")
|
||||
or "{project_name}_{COMPUTERNAME}_{USERNAME}"
|
||||
)
|
||||
p4_user = (
|
||||
perforce_settings.get("p4user")
|
||||
or self.data["env"].get("P4USER")
|
||||
)
|
||||
|
||||
if p4_client.count("{") == p4_client.count("}") > 0:
|
||||
p4_client = p4_client.format(
|
||||
project_name=self.data["project_name"],
|
||||
**self.data["env"]
|
||||
)
|
||||
|
||||
if perforce_settings.get("p4client_lower"):
|
||||
p4_client = p4_client.lower()
|
||||
|
||||
self.log.info(f"P4PORT={p4_port or None}")
|
||||
self.log.info(f"P4CLIENT={p4_client or None}")
|
||||
self.log.info(f"P4USER={p4_user or None}")
|
||||
|
||||
if not p4_port or not p4_client:
|
||||
self.log.warning("incomplete p4 configuration: skip sync !")
|
||||
if not host_bypass:
|
||||
raise ApplicationLaunchFailed(
|
||||
"Couldn't run the application! Perforce config error!"
|
||||
)
|
||||
|
||||
subprocess.run(["p4", "set", f"P4PORT={p4_port}"], check=True)
|
||||
subprocess.run(["p4", "set", f"P4CLIENT={p4_client}"], check=True)
|
||||
if p4_user:
|
||||
subprocess.run(["p4", "set", f"P4USER={p4_user}"], check=True)
|
||||
|
||||
# start sync
|
||||
self.log.info("[SYNC PERFORCE PROJECT]")
|
||||
with subprocess.Popen(
|
||||
['p4', '-I', 'sync', '-s', '-q'],
|
||||
shell=True,
|
||||
text=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
bufsize=0,
|
||||
errors='replace',
|
||||
) as process:
|
||||
capture_log = ""
|
||||
last_percent = None
|
||||
while process.poll() is None:
|
||||
capture_log += process.stdout.read(1)
|
||||
percent_match = re.search(r"\d{1,3}%|finishing", capture_log)
|
||||
if percent_match:
|
||||
current_percent = percent_match.group(0)
|
||||
if last_percent != current_percent:
|
||||
self.log.info(f"sync {current_percent}")
|
||||
last_percent = current_percent
|
||||
capture_log = ""
|
||||
if process.returncode != 0:
|
||||
self.log.info(capture_log + process.stdout.read())
|
||||
self.log.warning("sync error !")
|
||||
if not host_bypass:
|
||||
raise ApplicationLaunchFailed(
|
||||
"Couldn't run the application! Perforce sync failed!"
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Package declaring addon version."""
|
||||
__version__ = "0.1.0"
|
||||
Reference in new issue
Block a user