2014-05-16 14:20:26 +00:00
|
|
|
#!/usr/bin/env python2
|
2013-12-31 10:45:07 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2013,Thibault Saunier <thibault.saunier@collabora.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
""" Some utilies. """
|
|
|
|
|
|
|
|
import os
|
2014-01-31 11:21:21 +00:00
|
|
|
import re
|
2014-07-16 08:12:04 +00:00
|
|
|
import sys
|
2014-01-09 08:14:27 +00:00
|
|
|
import urllib
|
|
|
|
import urlparse
|
2014-01-09 14:23:38 +00:00
|
|
|
import subprocess
|
2014-01-09 08:14:27 +00:00
|
|
|
|
2014-01-31 11:21:21 +00:00
|
|
|
from operator import itemgetter
|
|
|
|
|
2014-01-09 08:14:27 +00:00
|
|
|
|
2014-02-13 14:31:58 +00:00
|
|
|
GST_SECOND = long(1000000000)
|
2014-03-26 19:09:12 +00:00
|
|
|
DEFAULT_TIMEOUT = 30
|
2014-02-06 16:24:30 +00:00
|
|
|
DEFAULT_MAIN_DIR = os.path.expanduser("~/gst-validate/")
|
2014-10-24 12:23:52 +00:00
|
|
|
DEFAULT_GST_QA_ASSETS = os.path.join(DEFAULT_MAIN_DIR, "gst-qa-assets")
|
2014-01-09 14:23:38 +00:00
|
|
|
DISCOVERER_COMMAND = "gst-discoverer-1.0"
|
2014-05-07 09:30:09 +00:00
|
|
|
# Use to set the duration from which a test is concidered as being 'long'
|
|
|
|
LONG_TEST = 40
|
2013-12-31 10:45:07 +00:00
|
|
|
|
2014-10-24 12:23:52 +00:00
|
|
|
|
2013-12-31 10:45:07 +00:00
|
|
|
class Result(object):
|
|
|
|
NOT_RUN = "Not run"
|
|
|
|
FAILED = "Failed"
|
|
|
|
TIMEOUT = "Timeout"
|
|
|
|
PASSED = "Passed"
|
2014-02-13 14:31:58 +00:00
|
|
|
KNOWN_ERROR = "Known error"
|
2013-12-31 10:45:07 +00:00
|
|
|
|
2014-01-30 15:58:58 +00:00
|
|
|
class Protocols(object):
|
|
|
|
HTTP = "http"
|
|
|
|
FILE = "file"
|
|
|
|
HLS = "hls"
|
2014-08-09 14:34:09 +00:00
|
|
|
DASH = "dash"
|
2014-01-30 15:58:58 +00:00
|
|
|
|
2014-11-20 10:55:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def needs_clock_sync(protocol):
|
|
|
|
if protocol == Protocols.HLS:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2014-01-30 15:58:58 +00:00
|
|
|
|
2013-12-31 10:45:07 +00:00
|
|
|
class Colors(object):
|
|
|
|
HEADER = '\033[95m'
|
|
|
|
OKBLUE = '\033[94m'
|
|
|
|
OKGREEN = '\033[92m'
|
|
|
|
WARNING = '\033[93m'
|
|
|
|
FAIL = '\033[91m'
|
|
|
|
ENDC = '\033[0m'
|
|
|
|
|
|
|
|
|
2014-01-10 09:27:25 +00:00
|
|
|
def desactivate_colors():
|
2013-12-31 10:45:07 +00:00
|
|
|
Colors.HEADER = ''
|
|
|
|
Colors.OKBLUE = ''
|
|
|
|
Colors.OKGREEN = ''
|
|
|
|
Colors.WARNING = ''
|
|
|
|
Colors.FAIL = ''
|
|
|
|
Colors.ENDC = ''
|
|
|
|
|
|
|
|
|
|
|
|
def mkdir(directory):
|
|
|
|
try:
|
|
|
|
os.makedirs(directory)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-01-09 14:17:53 +00:00
|
|
|
def which(name):
|
|
|
|
result = []
|
|
|
|
exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep))
|
|
|
|
path = os.environ.get('PATH', None)
|
|
|
|
if path is None:
|
|
|
|
return []
|
|
|
|
for p in os.environ.get('PATH', '').split(os.pathsep):
|
|
|
|
p = os.path.join(p, name)
|
|
|
|
if os.access(p, os.X_OK):
|
|
|
|
result.append(p)
|
|
|
|
for e in exts:
|
|
|
|
pext = p + e
|
|
|
|
if os.access(pext, os.X_OK):
|
|
|
|
result.append(pext)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2014-01-15 15:11:39 +00:00
|
|
|
def get_color_for_result(result):
|
|
|
|
if result is Result.FAILED:
|
|
|
|
color = Colors.FAIL
|
|
|
|
elif result is Result.TIMEOUT:
|
|
|
|
color = Colors.WARNING
|
|
|
|
elif result is Result.PASSED:
|
|
|
|
color = Colors.OKGREEN
|
|
|
|
else:
|
|
|
|
color = Colors.OKBLUE
|
|
|
|
|
|
|
|
return color
|
|
|
|
|
|
|
|
|
2014-01-09 14:17:53 +00:00
|
|
|
def printc(message, color="", title=False):
|
2013-12-31 10:45:07 +00:00
|
|
|
if title:
|
2014-01-09 10:14:19 +00:00
|
|
|
length = 0
|
|
|
|
for l in message.split("\n"):
|
|
|
|
if len(l) > length:
|
|
|
|
length = len(l)
|
|
|
|
if length == 0:
|
|
|
|
length = len(message)
|
|
|
|
message = length * '=' + "\n" + str(message) + "\n" + length * '='
|
|
|
|
|
2013-12-31 10:45:07 +00:00
|
|
|
if hasattr(message, "result") and color == '':
|
2014-01-15 15:11:39 +00:00
|
|
|
color = get_color_for_result(message.result)
|
2013-12-31 10:45:07 +00:00
|
|
|
|
2014-03-31 11:54:27 +00:00
|
|
|
sys.stdout.write(color + str(message) + Colors.ENDC + "\n")
|
|
|
|
sys.stdout.flush()
|
2013-12-31 10:45:07 +00:00
|
|
|
|
|
|
|
|
2014-04-28 11:08:09 +00:00
|
|
|
def launch_command(command, color=None, fails=False):
|
2013-12-31 10:45:07 +00:00
|
|
|
printc(command, Colors.OKGREEN, True)
|
2014-04-28 11:08:09 +00:00
|
|
|
res = os.system(command)
|
|
|
|
if res != 0 and fails is True:
|
|
|
|
raise subprocess.CalledProcessError(res, "%s failed" % command)
|
2014-01-09 08:14:27 +00:00
|
|
|
|
2014-01-09 14:24:52 +00:00
|
|
|
|
2014-01-09 08:14:27 +00:00
|
|
|
def path2url(path):
|
|
|
|
return urlparse.urljoin('file:', urllib.pathname2url(path))
|
|
|
|
|
|
|
|
|
2014-01-10 09:12:13 +00:00
|
|
|
def url2path(url):
|
2014-03-28 14:00:01 +00:00
|
|
|
path = urlparse.urlparse(url).path
|
|
|
|
if "win32" in sys.platform:
|
|
|
|
if path[0] == '/':
|
2014-10-24 12:23:52 +00:00
|
|
|
return path[1:] # We need to remove the first '/' on windows
|
2014-03-28 14:00:01 +00:00
|
|
|
return path
|
2014-01-10 09:12:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def isuri(string):
|
|
|
|
url = urlparse.urlparse(string)
|
2014-10-24 12:23:52 +00:00
|
|
|
if url.scheme != "" and url.scheme != "":
|
2014-01-10 09:12:13 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2014-10-24 12:23:52 +00:00
|
|
|
|
2014-04-23 09:47:10 +00:00
|
|
|
def touch(fname, times=None):
|
|
|
|
with open(fname, 'a'):
|
|
|
|
os.utime(fname, times)
|
2014-01-10 09:12:13 +00:00
|
|
|
|
2014-10-24 12:23:52 +00:00
|
|
|
|
2014-04-30 13:40:10 +00:00
|
|
|
def get_subclasses(klass, env):
|
|
|
|
subclasses = []
|
|
|
|
for symb in env.iteritems():
|
|
|
|
try:
|
|
|
|
if issubclass(symb[1], klass) and not symb[1] is klass:
|
|
|
|
subclasses.append(symb[1])
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return subclasses
|
|
|
|
|
2014-10-24 12:23:52 +00:00
|
|
|
|
2014-06-16 14:46:21 +00:00
|
|
|
def TIME_ARGS(time):
|
|
|
|
return "%u:%02u:%02u.%09u" % (time / (GST_SECOND * 60 * 60),
|
|
|
|
(time / (GST_SECOND * 60)) % 60,
|
|
|
|
(time / GST_SECOND) % 60,
|
|
|
|
time % GST_SECOND)
|
|
|
|
|
2014-10-24 12:23:52 +00:00
|
|
|
#
|
|
|
|
# Some utilities to parse gst-validate output #
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2014-01-31 11:21:21 +00:00
|
|
|
def gsttime_from_tuple(stime):
|
2014-10-24 12:23:52 +00:00
|
|
|
return long((int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2])) * GST_SECOND + int(stime[3]))
|
2014-01-31 11:21:21 +00:00
|
|
|
|
|
|
|
timeregex = re.compile(r'(?P<_0>.+):(?P<_1>.+):(?P<_2>.+)\.(?P<_3>.+)')
|
2014-10-24 12:23:52 +00:00
|
|
|
|
|
|
|
|
2014-01-30 11:42:25 +00:00
|
|
|
def parse_gsttimeargs(time):
|
2014-10-24 12:23:52 +00:00
|
|
|
stime = map(itemgetter(1), sorted(
|
|
|
|
timeregex.match(time).groupdict().items()))
|
|
|
|
return long((int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2])) * GST_SECOND + int(stime[3]))
|
|
|
|
|
2014-01-09 14:23:38 +00:00
|
|
|
|
2014-01-09 14:24:52 +00:00
|
|
|
def get_duration(media_file):
|
2014-01-14 17:05:45 +00:00
|
|
|
|
2014-01-30 11:42:25 +00:00
|
|
|
duration = 0
|
2014-06-16 14:46:21 +00:00
|
|
|
res = ''
|
2014-01-09 14:24:52 +00:00
|
|
|
try:
|
|
|
|
res = subprocess.check_output([DISCOVERER_COMMAND, media_file])
|
|
|
|
except subprocess.CalledProcessError:
|
2014-10-24 12:23:52 +00:00
|
|
|
# gst-media-check returns !0 if seeking is not possible, we do not care
|
|
|
|
# in that case.
|
2014-01-09 14:24:52 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
for l in res.split('\n'):
|
|
|
|
if "Duration: " in l:
|
|
|
|
duration = parse_gsttimeargs(l.replace("Duration: ", ""))
|
|
|
|
break
|
|
|
|
|
|
|
|
return duration
|
2014-01-09 14:23:38 +00:00
|
|
|
|
2014-02-12 10:18:14 +00:00
|
|
|
|
|
|
|
def get_scenarios():
|
|
|
|
GST_VALIDATE_COMMAND = "gst-validate-1.0"
|
|
|
|
os.system("%s --scenarios-defs-output-file %s" % (GST_VALIDATE_COMMAND,
|
|
|
|
))
|