# Path: utils/mem.py
# def get_free_space_bytes(path="/tmp"):
#     cmd = "/bin/df %s --output=avail | tail -n 1" %path
#     return int(get_stdout(cmd, shell=True))*1024
# 
# Path: utils/files.py
# def check_temp_path(path='/tmp'):
#     path = os.path.dirname(path)
#     if not check_path_is_tmpfs(path):
#         print('Path %s is not a tmpfs' % path)
#         return False
#     if not check_path_is_writable(path):
#         print('Path %s is not writable' % path)
#         return False
#     return True
# 
# Path: utils/process.py
# def run_cmd(cmd, shell=False):
#     env = dict(os.environ)
#     env["LANG"] = "C"
#     if shell:
#         args = cmd
#     else:
#         args = shlex.split(cmd)
#     p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=shell)
#     stdout, stderr = p.communicate()
#     rc = p.returncode
#     if rc != 0:
#         if '|' in cmd:
#             raise Exception('Command %s failed, maybe shell=True is required' %cmd)
#         else:
#             raise Exception('Command %s failed, error: %s' % (cmd, stderr))
#     elif contains_errors(stderr):
#         raise Exception('Command %s had an error' %cmd)
#     return rc, stdout, stderr


import os
from utils.mem import get_free_space_bytes
from utils.files import check_temp_path
from utils.process import run_cmd
    'RGB': 24,
    'RGBA': 32,
    'NV12': 12,
}

TMP_BUF_FILE = 'tmp.raw'

def get_buffer_size_bytes(colorspace, width, height):
    bpp = COLORSPACES_BPP[colorspace]
    return int(round(width*height*bpp/8))

RAW_BUF_FILE = '/tmp/buf.raw'
TEMP_BUF_FILE = 'tmp.raw'

def get_num_buffers_for_path(colorspace, width, height, raw_buf_file, max_buffers=1000):
    bufsize = get_buffer_size_bytes(colorspace, width, height)
    memsize = get_free_space_bytes(os.path.dirname(os.path.abspath(raw_buf_file)))
    return min(int(0.45*memsize/bufsize), max_buffers)

def generate_buffers_from_file(location, colorspace, width, height, raw_buf_file=RAW_BUF_FILE, framerate=30, num_buffers=None, max_buffers=1000):
    if not os.path.exists(location):
        print("Sample %s not found, skipping" % location)
        return None, None, None
    if not check_temp_path(raw_buf_file):
        print("Cannot use temporary file %s, skipping" % raw_buf_file)
        return None, None, None

    print('Generating intermediate raw sample from %s' % location)
    bufsize = get_buffer_size_bytes(colorspace, width, height)
    tmp_location = os.path.join(os.path.dirname(location), TMP_BUF_FILE)

# Path: utils/mem.py
# def get_free_space_bytes(path="/tmp"):
#     cmd = "/bin/df %s --output=avail | tail -n 1" %path
#     return int(get_stdout(cmd, shell=True))*1024
# 
# Path: utils/files.py
# def check_temp_path(path='/tmp'):
#     path = os.path.dirname(path)
#     if not check_path_is_tmpfs(path):
#         print('Path %s is not a tmpfs' % path)
#         return False
#     if not check_path_is_writable(path):
#         print('Path %s is not writable' % path)
#         return False
#     return True
# 
# Path: utils/process.py
# def run_cmd(cmd, shell=False):
#     env = dict(os.environ)
#     env["LANG"] = "C"
#     if shell:
#         args = cmd
#     else:
#         args = shlex.split(cmd)
#     p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=shell)
#     stdout, stderr = p.communicate()
#     rc = p.returncode
#     if rc != 0:
#         if '|' in cmd:
#             raise Exception('Command %s failed, maybe shell=True is required' %cmd)
#         else:
#             raise Exception('Command %s failed, error: %s' % (cmd, stderr))
#     elif contains_errors(stderr):
#         raise Exception('Command %s had an error' %cmd)
#     return rc, stdout, stderr


# Path: utils/video.py
import os
from utils.mem import get_free_space_bytes
from utils.files import check_temp_path
from utils.process import run_cmd

    'RGB': 24,
    'RGBA': 32,
    'NV12': 12,
}

TMP_BUF_FILE = 'tmp.raw'

def get_buffer_size_bytes(colorspace, width, height):
    bpp = COLORSPACES_BPP[colorspace]
    return int(round(width*height*bpp/8))

RAW_BUF_FILE = '/tmp/buf.raw'
TEMP_BUF_FILE = 'tmp.raw'

def get_num_buffers_for_path(colorspace, width, height, raw_buf_file, max_buffers=1000):
    bufsize = get_buffer_size_bytes(colorspace, width, height)
    memsize = get_free_space_bytes(os.path.dirname(os.path.abspath(raw_buf_file)))
    return min(int(0.45*memsize/bufsize), max_buffers)

def generate_buffers_from_file(location, colorspace, width, height, raw_buf_file=RAW_BUF_FILE, framerate=30, num_buffers=None, max_buffers=1000):
    if not os.path.exists(location):
        print("Sample %s not found, skipping" % location)
        return None, None, None
    if not check_temp_path(raw_buf_file):
        print("Cannot use temporary file %s, skipping" % raw_buf_file)
        return None, None, None

    print('Generating intermediate raw sample from %s' % location)
    bufsize = get_buffer_size_bytes(colorspace, width, height)
    tmp_location = os.path.join(os.path.dirname(location), TMP_BUF_FILE)

    memsize = get_free_space_bytes(os.path.dirname(os.path.abspath(tmp_location)))