# 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
                                        sink = sink_pattern % (output_file_count, output_file_count)
                                    output_file_count += 1
                                    if self.ENABLE_PARALLEL_PLUGINS:
                                        plugin_string_template = parallel_plugins[i % len(parallel_plugins)]
                                    plugin_string = plugin_string_template.format(**encoding_params)
                                    encoders.append("queue name=enc_%s max-size-buffers=1 ! %s ! %s "  % (i, plugin_string, sink))
                                    encoders_short.append(plugin_string)
                                if self.ENABLE_PARALLEL_PLUGINS:
                                    plugin_string = ""
                                    for p in self.PLUGINS:
                                        pname = p[0]
                                        pname_count = " ".join(encoders).count(pname)
                                        if pname_count > 0:
                                            plugin_string += "%s x%s " %(pname, pname_count)
                                encoders_string = "encoder. ! ".join(encoders)
                                num_buffers_test = channel_count*num_buffers
                                cmd = self.CMD_PATTERN %(input_file, bufsize, caps, encoders_string)

                                # check cmd and push sample data into RAM
                                print("Heating cache for test %s/%s (%i%%) %s" % (test_count, total_tests, 100*test_count/float(total_tests), plugin_string))
                                took = run_gst_cmd(cmd)
                                if took <= 0:
                                    print_red('<<< Heat test failed: %s' %plugin_string)
                                else:
                                    print('<<< Running test (%s passes, %s channels): %s (%s)' %(self.PASS_COUNT, channel_count, plugin_string, sample))
                                    fps_results = list()
                                    quality_results = list()
                                    min_quality_results = list()
                                    for i in range(self.PASS_COUNT):
                                        # Run it twice to ensure that file was in cache

# 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

                                        sink = sink_pattern % (output_file_count, output_file_count)
                                    output_file_count += 1
                                    if self.ENABLE_PARALLEL_PLUGINS:
                                        plugin_string_template = parallel_plugins[i % len(parallel_plugins)]
                                    plugin_string = plugin_string_template.format(**encoding_params)
                                    encoders.append("queue name=enc_%s max-size-buffers=1 ! %s ! %s "  % (i, plugin_string, sink))
                                    encoders_short.append(plugin_string)
                                if self.ENABLE_PARALLEL_PLUGINS:
                                    plugin_string = ""
                                    for p in self.PLUGINS:
                                        pname = p[0]
                                        pname_count = " ".join(encoders).count(pname)
                                        if pname_count > 0:
                                            plugin_string += "%s x%s " %(pname, pname_count)
                                encoders_string = "encoder. ! ".join(encoders)
                                num_buffers_test = channel_count*num_buffers
                                cmd = self.CMD_PATTERN %(input_file, bufsize, caps, encoders_string)

                                # check cmd and push sample data into RAM
                                print("Heating cache for test %s/%s (%i%%) %s" % (test_count, total_tests, 100*test_count/float(total_tests), plugin_string))
                                took = run_gst_cmd(cmd)
                                if took <= 0:
                                    print_red('<<< Heat test failed: %s' %plugin_string)
                                else:
                                    print('<<< Running test (%s passes, %s channels): %s (%s)' %(self.PASS_COUNT, channel_count, plugin_string, sample))
                                    fps_results = list()
                                    quality_results = list()
                                    min_quality_results = list()
                                    for i in range(self.PASS_COUNT):
                                        # Run it twice to ensure that file was in cache

                                        took = run_gst_cmd(cmd)