# 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
        info += '\nEncoder\tSample\tfps\tdeviation'
        if self.ENABLE_QUALITY_ANALYSIS:
            info += "\tq\tminq\tqlog"
        return info

    def parse_pattern(self, pattern_string):
        name, w, h, f = pattern_string.split('=')[1].split('-')
        return name, int(w), int(h), int(f)

    def run(self):
        success = False

        if hw.is_intel() and hasattr(self, 'PLUGINS_INTEL'):
            os.environ['LIBVA_DRIVER_NAME'] = 'i965'
            self.PLUGINS.extend(self.PLUGINS_INTEL)
        elif hw.is_nvidia() and hasattr(self, 'PLUGINS_NV'):
            self.PLUGINS.extend(self.PLUGINS_NV)
        else:
            if hw.is_pi() and hasattr(self, 'PLUGINS_PI'):
                self.PLUGINS.extend(self.PLUGINS_PI)
            if hw.is_jetson() and hasattr(self, 'PLUGINS_JETSON'):
                self.PLUGINS.extend(self.PLUGINS_JETSON)

        w = 1920
        h = 1080
        colorspace = self.COLORSPACE
        info = self.get_test_banner()

        available_plugins = list()
        for plugin in self.PLUGINS:

# 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

        info += '\nEncoder\tSample\tfps\tdeviation'
        if self.ENABLE_QUALITY_ANALYSIS:
            info += "\tq\tminq\tqlog"
        return info

    def parse_pattern(self, pattern_string):
        name, w, h, f = pattern_string.split('=')[1].split('-')
        return name, int(w), int(h), int(f)

    def run(self):
        success = False

        if hw.is_intel() and hasattr(self, 'PLUGINS_INTEL'):
            os.environ['LIBVA_DRIVER_NAME'] = 'i965'
            self.PLUGINS.extend(self.PLUGINS_INTEL)
        elif hw.is_nvidia() and hasattr(self, 'PLUGINS_NV'):
            self.PLUGINS.extend(self.PLUGINS_NV)
        else:
            if hw.is_pi() and hasattr(self, 'PLUGINS_PI'):
                self.PLUGINS.extend(self.PLUGINS_PI)
            if hw.is_jetson() and hasattr(self, 'PLUGINS_JETSON'):
                self.PLUGINS.extend(self.PLUGINS_JETSON)

        w = 1920
        h = 1080
        colorspace = self.COLORSPACE
        info = self.get_test_banner()

        available_plugins = list()
        for plugin in self.PLUGINS:

            if is_plugin_present(plugin[0]):