Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
#664 - moved last 2 functions
Browse files Browse the repository at this point in the history
removed obsolete lib_old.py
  • Loading branch information
kalisp committed Nov 11, 2020
1 parent 0353a83 commit 5d07af2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 101 deletions.
14 changes: 7 additions & 7 deletions pype/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
from .applications import (
ApplicationLaunchFailed,
launch_application,
ApplicationAction
ApplicationAction,
_subprocess
)

from .plugin_tools import filter_pyblish_plugins
from .plugin_tools import filter_pyblish_plugins, source_hash

from .path_tools import (
version_up,
Expand All @@ -34,10 +35,6 @@
get_ffmpeg_tool_path
)

from .lib_old import (
_subprocess,
source_hash
)
from .ffmpeg_utils import ffprobe_streams

__all__ = [
Expand Down Expand Up @@ -67,5 +64,8 @@
"get_paths_from_environ",
"get_ffmpeg_tool_path",

"ffprobe_streams"
"ffprobe_streams",

"source_hash",
"_subprocess"
]
66 changes: 66 additions & 0 deletions pype/lib/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import platform
import logging
import subprocess

import acre

Expand Down Expand Up @@ -389,3 +390,68 @@ def _start_timer(self, session, entity, _ftrack_api):
on_error="ignore"
)
self.log.debug("Timer start triggered successfully.")


# Special naming case for subprocess since its a built-in method.
def _subprocess(*args, **kwargs):
"""Convenience method for getting output errors for subprocess.
Entered arguments and keyword arguments are passed to subprocess Popen.
Args:
*args: Variable length arument list passed to Popen.
**kwargs : Arbitary keyword arguments passed to Popen. Is possible to
pass `logging.Logger` object under "logger" if want to use
different than lib's logger.
Returns:
str: Full output of subprocess concatenated stdout and stderr.
Raises:
RuntimeError: Exception is raised if process finished with nonzero
return code.
"""

# Get environents from kwarg or use current process environments if were
# not passed.
env = kwargs.get("env") or os.environ
# Make sure environment contains only strings
filtered_env = {k: str(v) for k, v in env.items()}

# Use lib's logger if was not passed with kwargs.
logger = kwargs.pop("logger", log)

# set overrides
kwargs['stdout'] = kwargs.get('stdout', subprocess.PIPE)
kwargs['stderr'] = kwargs.get('stderr', subprocess.PIPE)
kwargs['stdin'] = kwargs.get('stdin', subprocess.PIPE)
kwargs['env'] = filtered_env

proc = subprocess.Popen(*args, **kwargs)

full_output = ""
_stdout, _stderr = proc.communicate()
if _stdout:
_stdout = _stdout.decode("utf-8")
full_output += _stdout
logger.debug(_stdout)

if _stderr:
_stderr = _stderr.decode("utf-8")
# Add additional line break if output already containt stdout
if full_output:
full_output += "\n"
full_output += _stderr
logger.warning(_stderr)

if proc.returncode != 0:
exc_msg = "Executing arguments was not successful: \"{}\"".format(args)
if _stdout:
exc_msg += "\n\nOutput:\n{}".format(_stdout)

if _stderr:
exc_msg += "Error:\n{}".format(_stderr)

raise RuntimeError(exc_msg)

return full_output
94 changes: 0 additions & 94 deletions pype/lib/lib_old.py

This file was deleted.

21 changes: 21 additions & 0 deletions pype/lib/plugin_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ def filter_pyblish_plugins(plugins):
option, value, plugin.__name__))

setattr(plugin, option, value)


def source_hash(filepath, *args):
"""Generate simple identifier for a source file.
This is used to identify whether a source file has previously been
processe into the pipeline, e.g. a texture.
The hash is based on source filepath, modification time and file size.
This is only used to identify whether a specific source file was already
published before from the same location with the same modification date.
We opt to do it this way as opposed to Avalanch C4 hash as this is much
faster and predictable enough for all our production use cases.
Args:
filepath (str): The source file path.
You can specify additional arguments in the function
to allow for specific 'processing' values to be included.
"""
# We replace dots with comma because . cannot be a key in a pymongo dict.
file_name = os.path.basename(filepath)
time = str(os.path.getmtime(filepath))
size = str(os.path.getsize(filepath))
return "|".join([file_name, time, size] + list(args)).replace(".", ",")
3 changes: 3 additions & 0 deletions pype/tests/test_lib_restructuralization.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ def test_backward_compatibility(printer):

from pype.hosts.fusion.lib import switch_item

from pype.lib import source_hash
from pype.lib import _subprocess

except ImportError as e:
raise

0 comments on commit 5d07af2

Please sign in to comment.