# Path: jubakit/_stdio.py
# class _StandardIO(object):
#   def get(cls):
#   def set(cls, stdin=None, stdout=None, stderr=None):
#   def print(cls, *args, **kwargs):
#   def printe(cls, *args, **kwargs):
#   def devnull(cls):
# 
# Path: jubakit/_process.py
# class JubaProcess(object):
#   @classmethod
#   def get_process(cls, cmdline, env=None, *args, **kwargs):
#     """
#     Returns subprocess.Popen instance.
#     """
#     envvars = env if env else dict(os.environ)
#     if platform.system() == 'Darwin' and 'DYLD_FALLBACK_LIBRARY_PATH' not in envvars:
#       """
#       Due to homebrew-jubatus issue #15, when using Homebrew with locations other than
#       the standard installation path (/usr/local), Jubatus processes built on OS X cannot
#       be run without DYLD_FALLBACK_LIBRARY_PATH.  However, on El Capitan or later,
#       DYLD_FALLBACK_LIBRARY_PATH are not propagated from parent process.  We workaround
#       the problem by automatically estimating DYLD_FALLBACK_LIBRARY_PATH based on PATH.
#       """
#       cmdpath = distutils.spawn.find_executable(cmdline[0])
#       if cmdpath is None:
#         raise RuntimeError('{0} command not found; confirm that PATH is properly set'.format(cmdline[0]))
#       libpath = os.sep.join(cmdpath.split(os.sep)[:-2] + ['lib'])
#       if os.path.isfile(os.sep.join([libpath, 'libjubatus_core.dylib'])):
#         # If the estimated libpath is already in the default DYLD_FALLBACK_LIBRARY_PATH,
#         # we don't have to add it.  See ``man 1 dyld`` for the list of default search paths.
#         if libpath not in [os.path.expanduser('~/lib'), '/usr/local/lib', '/lib', '/usr/lib']:
#           envvars['DYLD_FALLBACK_LIBRARY_PATH'] = libpath
#           _logger.info('setting DYLD_FALLBACK_LIBRARY_PATH to %s', libpath)
#     return subprocess.Popen(cmdline, env=envvars, *args, **kwargs)


import sys
import copy
import struct
import base64
import subprocess
import tempfile
import optparse
import msgpack
import json
from binascii import crc32
from io import BytesIO
from .compat import *
from ._stdio import print, printe, get_stdio
from ._process import JubaProcess
    m._user_raw = buf
    if validate:
      if h.user_data_size != len(buf):
        raise InvalidModelFormatError(
          'EOF detected while reading user_data: ' +
          'expected {0} bytes, got {1} bytes'.format(h.user_data_size, len(buf)))
      checksum = crc32(buf, checksum)

    if validate:
      # Convert the checksum into 32-bit unsigned integer (for Python 2/3 compatibility)
      checksum = checksum & 0xffffffff

      # Check CRC
      if checksum != h.crc32:
        raise InvalidModelFormatError(
          'CRC32 mismatch: expected {0}, got {1}'.format(checksum, h.crc32))
    return m

  def dump_binary(self, f):
    """
    Dumps the model as Jubatus binary model file to binary stream ``f``.
    """
    # Dump header
    self.header.dump(f)

    # Dump system_data
    self.system.dump(f)

    # Dump user_data
    if self._user_raw is None:

# Path: jubakit/_stdio.py
# class _StandardIO(object):
#   def get(cls):
#   def set(cls, stdin=None, stdout=None, stderr=None):
#   def print(cls, *args, **kwargs):
#   def printe(cls, *args, **kwargs):
#   def devnull(cls):
# 
# Path: jubakit/_process.py
# class JubaProcess(object):
#   @classmethod
#   def get_process(cls, cmdline, env=None, *args, **kwargs):
#     """
#     Returns subprocess.Popen instance.
#     """
#     envvars = env if env else dict(os.environ)
#     if platform.system() == 'Darwin' and 'DYLD_FALLBACK_LIBRARY_PATH' not in envvars:
#       """
#       Due to homebrew-jubatus issue #15, when using Homebrew with locations other than
#       the standard installation path (/usr/local), Jubatus processes built on OS X cannot
#       be run without DYLD_FALLBACK_LIBRARY_PATH.  However, on El Capitan or later,
#       DYLD_FALLBACK_LIBRARY_PATH are not propagated from parent process.  We workaround
#       the problem by automatically estimating DYLD_FALLBACK_LIBRARY_PATH based on PATH.
#       """
#       cmdpath = distutils.spawn.find_executable(cmdline[0])
#       if cmdpath is None:
#         raise RuntimeError('{0} command not found; confirm that PATH is properly set'.format(cmdline[0]))
#       libpath = os.sep.join(cmdpath.split(os.sep)[:-2] + ['lib'])
#       if os.path.isfile(os.sep.join([libpath, 'libjubatus_core.dylib'])):
#         # If the estimated libpath is already in the default DYLD_FALLBACK_LIBRARY_PATH,
#         # we don't have to add it.  See ``man 1 dyld`` for the list of default search paths.
#         if libpath not in [os.path.expanduser('~/lib'), '/usr/local/lib', '/lib', '/usr/lib']:
#           envvars['DYLD_FALLBACK_LIBRARY_PATH'] = libpath
#           _logger.info('setting DYLD_FALLBACK_LIBRARY_PATH to %s', libpath)
#     return subprocess.Popen(cmdline, env=envvars, *args, **kwargs)


# Path: jubakit/model.py
import sys
import copy
import struct
import base64
import subprocess
import tempfile
import optparse
import msgpack
import json
from binascii import crc32
from io import BytesIO
from .compat import *
from ._stdio import print, printe, get_stdio
from ._process import JubaProcess

    m._user_raw = buf
    if validate:
      if h.user_data_size != len(buf):
        raise InvalidModelFormatError(
          'EOF detected while reading user_data: ' +
          'expected {0} bytes, got {1} bytes'.format(h.user_data_size, len(buf)))
      checksum = crc32(buf, checksum)

    if validate:
      # Convert the checksum into 32-bit unsigned integer (for Python 2/3 compatibility)
      checksum = checksum & 0xffffffff

      # Check CRC
      if checksum != h.crc32:
        raise InvalidModelFormatError(
          'CRC32 mismatch: expected {0}, got {1}'.format(checksum, h.crc32))
    return m

  def dump_binary(self, f):
    """
    Dumps the model as Jubatus binary model file to binary stream ``f``.
    """
    # Dump header
    self.header.dump(f)

    # Dump system_data
    self.system.dump(f)

    # Dump user_data
    if self._user_raw is None:

      printe('Warning: conversion from Python object to binary model format may generate corrupt model')