# Path: utils/files.py
# def write_timestamped_results(data):
#     fname = get_timestamped_fname()
#     with open(fname, 'w') as f:
#         f.write(data)
#     print('Wrote results to %s' % fname)
# 
# def write_results(data, path):
#     with open(path, 'w') as f:
#         f.write(data)
#     print('Wrote %s' % path)
# 
# Path: utils/process.py
# def run_gst_cmd(cmd):
#     try:
#         rc, stdout, stderr = run_cmd(cmd, shell=False)
#         took = parse_gst_execution_time(stdout)
#     except Exception as e:
#         print(e)
#         took = 0
#     return took
# 
# 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/colors.py
# def print_red(text):
#     print("%s%s%s" %(Colors.RED, text, Colors.DEFAULT))
# 
# Path: utils/gstreamer.py
# def is_plugin_present(plugin_name):
#     result = process.check_cmd('gst-inspect-1.0 %s &> /dev/null' %plugin_name.split(' ')[0], shell=True, complain=False)[0]
#     if not result:
#         print('Plugin %s not found' % plugin_name)
#     return result
# 
# def get_gst_version():
#     return process.get_stdout('gst-launch-1.0 --gst-version').strip().split(' ')[-1]


import os
import time
import utils.video as video
import utils.hardware as hw
from statistics import mean
from utils.files import write_timestamped_results, write_results 
from utils.process import run_gst_cmd, run_cmd
from utils.colors import print_red 
from utils.gstreamer import is_plugin_present, get_gst_version
    from utils.run import run_test
                available_plugins.append(plugin[1])

        if self.ENABLE_PARALLEL_PLUGINS:
            parallel_plugins = list(available_plugins)
            # only keep a single item in the plugin list because we will parallelize them
            available_plugins = ['parallel'] 

        if self.SCAN_SAMPLES:
            self.SAMPLES.extend(video.scan_samples_folder(self.SAMPLES_FOLDER))
        total_tests = len(self.SAMPLES) * self.CHANNELS * len(available_plugins) * len(self.BITRATES_K)
        print('About to run %s tests with these samples:\n\t%s' %(total_tests, "\n\t".join(self.SAMPLES)))
        test_count = 0

        for sample in self.SAMPLES:
            num_buffers = None
            if 'pattern' in sample:
                framerate = 30
                sample_name, w, h, framerate = self.parse_pattern(sample)
                num_buffers, bufsize, caps = video.generate_buffers_from_pattern(colorspace, w, h, self.RAW_BUF_FILE, pattern=sample_name, framerate=framerate, max_buffers=self.MAX_BUFFERS)
                input_file = self.RAW_BUF_FILE
            elif 'sample' in sample:
                sample_name, w, h, framerate = video.parse_sample_string(sample)
                input_file_path = os.path.join('samples', sample_name)
                num_buffers, bufsize, caps = video.generate_buffers_from_file(input_file_path, colorspace, w, h, self.RAW_BUF_FILE, framerate=framerate, max_buffers=self.MAX_BUFFERS)
                input_file = self.RAW_BUF_FILE

            output_file_count = 0
            if num_buffers:
                for plugin_string_template in available_plugins:
                    abort = False

# Path: utils/files.py
# def write_timestamped_results(data):
#     fname = get_timestamped_fname()
#     with open(fname, 'w') as f:
#         f.write(data)
#     print('Wrote results to %s' % fname)
# 
# def write_results(data, path):
#     with open(path, 'w') as f:
#         f.write(data)
#     print('Wrote %s' % path)
# 
# Path: utils/process.py
# def run_gst_cmd(cmd):
#     try:
#         rc, stdout, stderr = run_cmd(cmd, shell=False)
#         took = parse_gst_execution_time(stdout)
#     except Exception as e:
#         print(e)
#         took = 0
#     return took
# 
# 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/colors.py
# def print_red(text):
#     print("%s%s%s" %(Colors.RED, text, Colors.DEFAULT))
# 
# Path: utils/gstreamer.py
# def is_plugin_present(plugin_name):
#     result = process.check_cmd('gst-inspect-1.0 %s &> /dev/null' %plugin_name.split(' ')[0], shell=True, complain=False)[0]
#     if not result:
#         print('Plugin %s not found' % plugin_name)
#     return result
# 
# def get_gst_version():
#     return process.get_stdout('gst-launch-1.0 --gst-version').strip().split(' ')[-1]


# Path: common/encoding.py
import os
import time
import utils.video as video
import utils.hardware as hw
from statistics import mean
from utils.files import write_timestamped_results, write_results 
from utils.process import run_gst_cmd, run_cmd
from utils.colors import print_red 
from utils.gstreamer import is_plugin_present, get_gst_version
    from utils.run import run_test

                available_plugins.append(plugin[1])

        if self.ENABLE_PARALLEL_PLUGINS:
            parallel_plugins = list(available_plugins)
            # only keep a single item in the plugin list because we will parallelize them
            available_plugins = ['parallel'] 

        if self.SCAN_SAMPLES:
            self.SAMPLES.extend(video.scan_samples_folder(self.SAMPLES_FOLDER))
        total_tests = len(self.SAMPLES) * self.CHANNELS * len(available_plugins) * len(self.BITRATES_K)
        print('About to run %s tests with these samples:\n\t%s' %(total_tests, "\n\t".join(self.SAMPLES)))
        test_count = 0

        for sample in self.SAMPLES:
            num_buffers = None
            if 'pattern' in sample:
                framerate = 30
                sample_name, w, h, framerate = self.parse_pattern(sample)
                num_buffers, bufsize, caps = video.generate_buffers_from_pattern(colorspace, w, h, self.RAW_BUF_FILE, pattern=sample_name, framerate=framerate, max_buffers=self.MAX_BUFFERS)
                input_file = self.RAW_BUF_FILE
            elif 'sample' in sample:
                sample_name, w, h, framerate = video.parse_sample_string(sample)
                input_file_path = os.path.join('samples', sample_name)
                num_buffers, bufsize, caps = video.generate_buffers_from_file(input_file_path, colorspace, w, h, self.RAW_BUF_FILE, framerate=framerate, max_buffers=self.MAX_BUFFERS)
                input_file = self.RAW_BUF_FILE

            output_file_count = 0
            if num_buffers:
                for plugin_string_template in available_plugins:
                    abort = False

                    for channel_count in range(1, self.CHANNELS + 1):