Best Python code snippet using fMBT_python
regression-video.py
Source:regression-video.py  
1#!/usr/bin/env python2#/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *3# *   Mupen64plus - regression-video.py                                     *4# *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *5# *   Copyright (C) 2008 Richard Goedeken                                   *6# *                                                                         *7# *   This program is free software; you can redistribute it and/or modify  *8# *   it under the terms of the GNU General Public License as published by  *9# *   the Free Software Foundation; either version 2 of the License, or     *10# *   (at your option) any later version.                                   *11# *                                                                         *12# *   This program is distributed in the hope that it will be useful,       *13# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *14# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *15# *   GNU General Public License for more details.                          *16# *                                                                         *17# *   You should have received a copy of the GNU General Public License     *18# *   along with this program; if not, write to the                         *19# *   Free Software Foundation, Inc.,                                       *20# *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *21# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */22from optparse import OptionParser23from threading import Thread24from datetime import date25import subprocess26import commands27import shutil28import stat29import sys30import os31# set global report string32report = "Mupen64Plus Regression Test report\n----------------------------------\n"33#******************************************************************************34# main functions35#36def main(rootdir, cfgfile, nobuild):37    global report38    # set up child directory paths39    srcdir = os.path.join(rootdir, "source")40    shotdir = os.path.join(rootdir, "current")41    refdir = os.path.join(rootdir, "reference")42    archivedir = os.path.join(rootdir, "archive")43    # run the test procedure44    tester = RegTester(rootdir, srcdir, shotdir)45    rval = 046    while True:47        # Step 1: load the test config file48        if not tester.LoadConfig(cfgfile):49            rval = 150            break51        # Step 2: check out from SVN52        if not nobuild:53            if not CheckoutSource(srcdir):54                rval = 255                break56        # Step 3: run test builds57        if not nobuild:58            testlist = [ name.strip() for name in tester.generalParams["testbuilds"].split(',') ]59            makeparams = [ params.strip() for params in tester.generalParams["testbuildparams"].split(',') ]60            if len(testlist) != len(makeparams):61                report += "Config file error for test builds.  Build name list and makefile parameter list have different lengths.\n"62            testbuilds = min(len(testlist), len(makeparams))63            for i in range(testbuilds):64                buildname = testlist[i]65                buildmake = makeparams[i]66                BuildSource(srcdir, buildname, buildmake, True)67        # Step 4: build the binary for the video regression test68        if not nobuild:69            videobuild = tester.generalParams["videobuild"]70            videomake = tester.generalParams["videobuildparams"]71            if not BuildSource(srcdir, videobuild, videomake, False):72                rval = 373                break74        # Step 5: run the tests, check the results75        if not tester.RunTests():76            rval = 477            break78        if not tester.CheckResults(refdir):79            rval = 580            break81        # test procedure is finished82        break83    # Step 6: send email report and archive the results84    if not tester.SendReport():85        rval = 686    if not tester.ArchiveResults(archivedir):87        rval = 788    # all done with test process89    return rval90#******************************************************************************91# Checkout & build functions92#93def CheckoutSource(srcdir):94    global report95    # remove any current source directory96    if not deltree(srcdir):97        return False98    # call svn to checkout current Mupen64Plus source tree99    output = commands.getoutput("svn co svn://fascination.homelinux.net:7684/mupen64plus/trunk " + srcdir)100    # parse the output101    lastline = output.split("\n")[-1]102    if lastline[:20] == "Checked out revision":103        report += "SVN Checkout successful: %s\n\n" % lastline104        return True105    report += "SVN Error: %s\n\n" % lastline106    return False107def BuildSource(srcdir, buildname, buildmake, istest):108    global report109    # print build report message and clear counters110    testbuildcommand = "make -C %s %s" % (srcdir, buildmake)111    if istest:112        report += "Running test build \"%s\" with command \"%s\"\n" % (buildname, testbuildcommand)113    else:114        report += "Building Mupen64Plus \"%s\" for video test with command \"%s\"\n" % (buildname, testbuildcommand)115    warnings = 0116    errors = 0117    # run make and capture the output118    output = commands.getoutput(testbuildcommand)119    makelines = output.split("\n")120    # print warnings and errors121    for line in makelines:122        if "error:" in line:123            report += "    " + line + "\n"124            errors += 1125        if "warning:" in line:126            report += "    " + line + "\n"127            warnings += 1128    report += "%i errors. %i warnings.\n" % (errors, warnings)129    if errors > 0 and not istest:130        return False131    # check for program files132    binfiles = [ "mupen64plus" ]133    libfiles = [ "blight_input.so", "dummyaudio.so", "dummyvideo.so", "glN64.so", "glide64.so", "ricevideo.so",134                 "mupen64_hle_rsp_azimer.so", "jttl_audio.so" ]135    filelist = [ os.path.join(srcdir, filename) for filename in binfiles ]136    filelist += [ os.path.join(srcdir, "plugins", filename) for filename in libfiles ]137    for filename in filelist:138        if not os.path.exists(filename):139            report += "Build failed: '%s' not found\n" % filename140            errors += 1141    if errors > 0 and not istest:142        return False143    # clean up if this was a test144    if istest:145        os.system("make -C %s clean" % srcdir)146    # build was successful!147    return True148#******************************************************************************149# Test execution classes150#151class RegTester:152    def __init__(self, rootdir, bindir, screenshotdir):153        self.rootdir = rootdir154        self.bindir = bindir155        self.screenshotdir = screenshotdir156        self.libdir = os.path.join(bindir, "plugins")157        self.generalParams = { }158        self.gamesAndParams = { }159        self.videoplugins = [ "glN64.so", "glide64.so", "ricevideo.so" ]160        self.thisdate = str(date.today())161    def LoadConfig(self, filename):162        global report163        # read the config file164        report += "\nLoading regression test configuration.\n"165        try:166            cfgfile = open(os.path.join(self.rootdir, filename), "r")167            cfglines = cfgfile.read().split("\n")168            cfgfile.close()169        except Exception, e:170            report += "Error in RegTestConfigParser::LoadConfig(): %s" % e171            return False172        # parse the file173        GameFilename = None174        for line in cfglines:175            # strip leading and trailing whitespace176            line = line.strip()177            # test for comment178            if len(line) == 0 or line[0] == '#':179                continue180            # test for new game filename181            if line[0] == '[' and line [-1] == ']':182                GameFilename = line[1:-1]183                if GameFilename in self.gamesAndParams:184                    report += "    Warning: Config file '%s' contains duplicate game entry '%s'\n" % (filename, GameFilename)185                else:186                    self.gamesAndParams[GameFilename] = { }187                continue188            # print warning and continue if it's not a (key = value) pair189            pivot = line.find('=')190            if pivot == -1:191                report += "    Warning: Config file '%s' contains unrecognized line: '%s'\n" % (filename, line)192                continue193            # parse key, value194            key = line[:pivot].strip().lower()195            value = line[pivot+1:].strip()196            if GameFilename is None:197                paramDict = self.generalParams198            else:199                paramDict = self.gamesAndParams[GameFilename]200            if key in paramDict:201                report += "    Warning: Game '%s' contains duplicate key '%s'\n" % (str(GameFilename), key)202                continue203            paramDict[key] = value204        # check for required parameters205        if "rompath" not in self.generalParams:206            report += "    Error: rompath is not given in config file\n"207            return False208        # config is loaded209        return True210    def RunTests(self):211        global report212        rompath = self.generalParams["rompath"]213        if not os.path.exists(rompath):214            report += "    Error: ROM directory '%s' does not exist!\n" % rompath215            return False216        # Remove any current screenshot directory217        if not deltree(self.screenshotdir):218            return False219        # Data initialization and start message220        os.mkdir(self.screenshotdir)221        for plugin in self.videoplugins:222            videoname = plugin[:plugin.find('.')]223            os.mkdir(os.path.join(self.screenshotdir, videoname))224        report += "\nRunning regression tests on %i games.\n" % len(self.gamesAndParams)225        # loop over each game filename given in regtest config file226        for GameFilename in self.gamesAndParams:227            GameParams = self.gamesAndParams[GameFilename]228            # if no screenshots parameter given for this game then skip it229            if "screenshots" not in GameParams:230                report += "    Warning: no screenshots taken for game '%s'\n" % GameFilename231                continue232            # make a list of screenshots and check it233            shotlist = [ str(int(framenum.strip())) for framenum in GameParams["screenshots"].split(',') ]234            if len(shotlist) < 1 or (len(shotlist) == 1 and shotlist[0] == '0'):235                report += "    Warning: invalid screenshot list for game '%s'\n" % GameFilename236                continue237            # run a test for each video plugin238            for plugin in self.videoplugins:239                videoname = plugin[:plugin.find('.')]240                # check if this plugin should be skipped241                if "skipvideo" in GameParams:242                    skipit = False243                    skiplist = [ name.strip() for name in GameParams["skipvideo"].split(',') ]244                    for skiptag in skiplist:245                        if skiptag.lower() in plugin.lower():246                            skipit = True247                    if skipit:248                        continue249                # construct the command line250                exepath = os.path.join(self.bindir, "mupen64plus")251                exeparms = ["--nogui", "--noosd", "--noask", "--emumode", "1" ]252                exeparms += [ "--testshots",  ",".join(shotlist) ]253                exeparms += [ "--installdir", self.bindir ]254                exeparms += [ "--sshotdir", os.path.join(self.screenshotdir, videoname) ]255                myconfig = os.path.join(self.rootdir, "config")256                if os.path.exists(myconfig):257                    exeparms += [ "--configdir", myconfig ]258                exeparms += [ "--gfx", os.path.join(self.libdir, plugin) ]259                exeparms += [ "--audio", os.path.join(self.libdir, "dummyaudio.so") ]260                exeparms += [ "--input", os.path.join(self.libdir, "blight_input.so") ]261                exeparms += [ "--rsp", os.path.join(self.libdir, "mupen64_hle_rsp_azimer.so") ]262                exeparms += [ os.path.join(rompath, GameFilename) ]263                # run it, but if it takes too long print an error and kill it264                testrun = RegTestRunner(exepath, exeparms)265                testrun.start()266                testrun.join(60.0)267                if testrun.isAlive():268                    report += "    Error: Test run timed out after 60 seconds:  '%s'\n" % " ".join(exeparms)269                    os.kill(testrun.pid, 9)270                    testrun.join(10.0)271                272        # all tests have been run273        return True                274    def CheckResults(self, refdir):275        global report276        # print message277        warnings = 0278        errors = 0279        report += "\nChecking regression test results\n"280        # get lists of files in the reference folders281        refshots = { }282        if not os.path.exists(refdir):283            os.mkdir(refdir)284        for plugin in self.videoplugins:285            videoname = plugin[:plugin.find('.')]286            videodir = os.path.join(refdir, videoname)287            if not os.path.exists(videodir):288                os.mkdir(videodir)289                refshots[videoname] = [ ]290            else:291                refshots[videoname] = [ filename for filename in os.listdir(videodir) ]292        # get lists of files produced by current test runs293        newshots = { }294        for plugin in self.videoplugins:295            videoname = plugin[:plugin.find('.')]296            videodir = os.path.join(self.screenshotdir, videoname)297            if not os.path.exists(videodir):298                newshots[videoname] = [ ]299            else:300                newshots[videoname] = [ filename for filename in os.listdir(videodir) ]301        # make list of matching ref/test screenshots, and look for missing reference screenshots302        checklist = { }303        for plugin in self.videoplugins:304            videoname = plugin[:plugin.find('.')]305            checklist[videoname] = [ ]306            for filename in newshots[videoname]:307                if filename in refshots[videoname]:308                    checklist[videoname] += [ filename ]309                else:310                    report += "    Warning: reference screenshot '%s/%s' missing. Copying from current test run\n" % (videoname, filename)311                    shutil.copy(os.path.join(self.screenshotdir, videoname, filename), os.path.join(refdir, videoname))312                    warnings += 1313        # look for missing test screenshots314        for plugin in self.videoplugins:315            videoname = plugin[:plugin.find('.')]316            for filename in refshots[videoname]:317                if filename not in newshots[videoname]:318                    report += "    Error: Test screenshot '%s/%s' missing.\n" % (videoname, filename)319                    errors += 1320        # do image comparisons321        for plugin in self.videoplugins:322            videoname = plugin[:plugin.find('.')]323            for filename in checklist[videoname]:324                refimage = os.path.join(refdir, videoname, filename)325                testimage = os.path.join(self.screenshotdir, videoname, filename)326                diffimage = os.path.join(self.screenshotdir, videoname, os.path.splitext(filename)[0] + "_DIFF.png")327                cmd = ("/usr/bin/compare", "-metric", "PSNR", refimage, testimage, diffimage)328                pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout329                similarity = pipe.read().strip()330                pipe.close()331                try:332                    db = float(similarity)333                except:334                    db = 0335                if db > 60.0:336                    os.unlink(diffimage)337                else:338                    report += "    Warning: test image '%s/%s' does not match reference.  PSNR = %s\n" % (videoname, filename, similarity)339                    warnings += 1340        # give report and return341        report += "%i errors. %i warnings.\n" % (errors, warnings)342        return True343    def SendReport(self):344        global report345        # if there are no email addresses in the config file, then just we're done346        if "sendemail" not in self.generalParams:347            return True348        if len(self.generalParams["sendemail"]) < 5:349            return True350        # construct the email message header351        emailheader = "To: %s\n" % self.generalParams["sendemail"]352        emailheader += "From: Mupen64Plus-Tester@fascination.homelinux.net\n"353        emailheader += "Subject: %s Regression Test Results for Mupen64Plus\n" % self.thisdate354        emailheader += "Reply-to: do-not-reply@fascination.homelinux.net\n"355        emailheader += "Content-Type: text/plain; charset=UTF-8\n"356        emailheader += "Content-Transfer-Encoding: 8bit\n\n"357        # open a pipe to sendmail and dump our report358        try:359            pipe = subprocess.Popen(("/usr/sbin/sendmail", "-t"), stdin=subprocess.PIPE).stdin360            pipe.write(emailheader)361            pipe.write(report)362            pipe.close()363        except Exception, e:364            report += "Exception encountered when calling sendmail: '%s'\n" % e365            report += "Email header:\n%s\n" % emailheader366            return False367        return True368    def ArchiveResults(self, archivedir):369        global report370        # create archive dir if it doesn't exist371        if not os.path.exists(archivedir):372            os.mkdir(archivedir)373        # move the images into a subdirectory of 'archive' given by date374        subdir = os.path.join(archivedir, self.thisdate)375        if os.path.exists(subdir):376            if not deltree(subdir):377                return False378        shutil.move(self.screenshotdir, subdir)379        # copy the report into the archive directory380        f = open(os.path.join(archivedir, "report_%s.txt" % self.thisdate), "w")381        f.write(report)382        f.close()383        # archival is complete384        return True385class RegTestRunner(Thread):386    def __init__(self, exepath, exeparms):387        self.exepath = exepath388        self.exeparms = exeparms389        self.pid = 0390        self.returnval = None391        Thread.__init__(self)392    def run(self):393        # start the process394        testprocess = subprocess.Popen([self.exepath] + self.exeparms)395        # get the PID of the new test process396        self.pid = testprocess.pid397        # wait for the test to complete398        self.returnval = testprocess.wait()399#******************************************************************************400# Generic helper functions401#402def deltree(dirname):403    global report404    if not os.path.exists(dirname):405        return True406    try:407        for path in (os.path.join(dirname, filename) for filename in os.listdir(dirname)):408            if os.path.isdir(path):409                if not deltree(path):410                    return False411            else:412                os.unlink(path)413        os.rmdir(dirname)414    except Exception, e:415        report += "Error in deltree(): %s\n" % e416        return False417    return True418#******************************************************************************419# main function call for standard script execution420#421if __name__ == "__main__":422    # parse the command-line arguments423    parser = OptionParser()424    parser.add_option("-n", "--nobuild", dest="nobuild", default=False, action="store_true",425                      help="Assume source code is present; don't check out and build")426    parser.add_option("-t", "--testpath", dest="testpath",427                      help="Set root of testing directory to PATH", metavar="PATH")428    parser.add_option("-c", "--cfgfile", dest="cfgfile", default="daily-tests.cfg",429                      help="Use regression test config file FILE", metavar="FILE")430    (opts, args) = parser.parse_args()431    # check test path432    if opts.testpath is None:433        # change directory to the directory containing this script and set root test path to "."434        scriptdir = os.path.dirname(sys.argv[0])435        os.chdir(scriptdir)436        rootdir = "."437    else:438        rootdir = opts.testpath439    # call the main function440    rval = main(rootdir, opts.cfgfile, opts.nobuild)...startMarkdownHelper.py
Source:startMarkdownHelper.py  
1#!/usr/bin/python2import os3from subprocess import call4from os.path import expanduser5#Change these vars6imagesDir      = './doc/images'7screenShotDir  = '~/Downloads'8openPreview    = False9fileNamePrefix = 'INL'10#Define a function processing pathes11scriptDir = os.path.abspath(os.path.dirname(__file__) + '/')12def processPath(path):13    homeDir = expanduser("~")14    path = path.replace('~', homeDir)15    if(not os.path.isabs(path)):16        path.replace('\\', '/')17        path = os.path.join(scriptDir, path)18    return path;19#Process screenshot dir20screenShotDir = processPath(screenShotDir);21imagesDir = processPath(imagesDir);22#Check pathes23if not os.path.exists(screenShotDir):24    print 'ScreenshotDir "' + screenShotDir + '" does not exist'25    exit(1)26if not os.path.exists(imagesDir):27    print 'Images Dir "' + imagesDir + '" does not exist'28    exit(1)29#Process show preview30openPreviewString = ''31if openPreview:32    openPreviewString = ' ' + '--openPreview'33def par(string):34    return '"' + string + '"'35#Call markDownHelper.py36command = ['markDownHelper.py',37           '--markdownDir', par(scriptDir),38           '--imagesDir', par(imagesDir),39           '--screenShotDir',  par(screenShotDir),40           '--prefix', fileNamePrefix,41           '--copyToClipboard', openPreviewString];42commandString = " ".join(command)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
