Best Python code snippet using slash
utils.py
Source:utils.py  
1"""2.. moduleauthor:: Sandeep Nanda <mail: sandeep.nanda@guavus.com> <skype: snanda85>3This module implements the utility functions used by the Potluck framework.4Most of the functions are used internally by the framework, although they5can be used anywhere6Example::7    from potluck import utils8    9    ls_output = utils.run_cmd("ls /tmp")10"""11from __future__ import with_statement   # For jython compatibility12import os13import re14import subprocess15import sys16import platform as py_platform17import ConfigParser18import xml.etree.cElementTree as eTree19from potluck.logging import logger20from settings import ROOT_DIR, SUITES_DIR, SCRIPTS_DIR, MODULES_DIR, TESTBEDS_DIR, USER_TESTBEDS_DIR, USER_SUITES_DIR21from potluck import env22testcase_ids = {}23class NodeDict(dict):24    ## Tweaks Tweaks Tweaks ##25    # This is a dict that wraps a node object. It does the following tasks:26    # 1. If `handle` key is accessed and node is not connected, connect it27    # 2. Provides access to the members of the Node object (e.g. testbed["INSTA1"].ip)28    #    Be sure when to use this, as this will connect to the node before returning the value29    def __getitem__(self, key, *args, **kwargs):30        try:31            return super(NodeDict, self).__getitem__(key, *args, **kwargs)32        except KeyError:33            if key == "handle":34                from potluck.nodes import connect35                connect(self["alias"])36                return self["handle"]37    def __getattr__(self, name):38        if name in self:39            # Expose items of this dict as attributes40            return self[name]41        elif hasattr(self["handle"], name):42            # Provides access to the members of node object43            return getattr(self["handle"], name)44        else:45            raise AttributeError("Parameter '%s' is not present in node '%s'" % (name, self["alias"]))46def sendEmail(to, subject, body="", body_html=""):47    """Sends email to a list of people.48    The email is sent from potluck@guavus.com49    :argument to: List of recipient IDs. The trailing `@guavus.com` is optional.50    :argument subject: Subject of the email51    :argument body: Text Body of the email52    :argument body_html: HTML Body of the email53    """54    import smtplib55    from email.mime.text import MIMEText56    from email.mime.multipart import MIMEMultipart57    def make_guavus(mail_id):58        if not mail_id.endswith("guavus.com"):59            return "%s@guavus.com" % mail_id60        else:61            return mail_id62    to = map(make_guavus, to)63    logger.info("Sending email to %s" % to)64    msg = MIMEMultipart('alternative')65    if body:66        text_msg = MIMEText(body, "plain")67        msg.attach(text_msg)68    if body_html:69        html_msg = MIMEText(body_html, "html")70        msg.attach(html_msg)71    sender = "Potluck <potluck@guavus.com>"72    msg['Subject'] = subject73    msg['From'] = sender74    msg['To'] = ", ".join(to)75    s = smtplib.SMTP('localhost')76    s.sendmail(sender, to, msg.as_string())77    s.quit()78class processed_stream(object):79    """Processes a stream and replaces multiple newlines with single.80    .. note::81        82        Used by Potluck to process sys.stdout. Don't use unless you know what you are doing83    """84    def __init__(self, stream):85        self.stream = stream86    def flush(self):87        self.stream.flush()88    def write(self, data):89        str = data.rstrip()90        str = str.replace("\r\n", "\n")91        str = str.replace("\r", "\n")92        self.stream.write("%s\n" % str)93def generate_testcase_id(script):94    """Auto-Generates testcase id based on the script"""95    script_prefix, ext = os.path.splitext(script)96    tc_id = re.sub("/", "-", script_prefix).title().strip("-")97    if tc_id in testcase_ids:98        testcase_ids[tc_id] += 199        return "%s-%s" % (tc_id, testcase_ids[tc_id])100    else:101        testcase_ids[tc_id] = 1102        return tc_id103def parse_suite_text(suite_file):104    suite = {105        "testcases" : [],106        "config_file" : None,107        "mail_to" : None,108        "ui_url" : None,109        "name" : ""110    }111    # Read the Suite and Form a list of scripts to execute112    if not os.path.isfile(suite_file):113        suite_file = os.path.join(SUITES_DIR, suite_file)114    suite["name"] = os.path.split(suite_file)[-1]115    if not os.path.exists(suite_file):116        logger.error("Suite file does not exist at %r" % suite_file)117        return None118    # Parse the Test suite file119    with open(suite_file, "r") as suite_file_handle:120        for line in suite_file_handle:121            line = line.strip()122            # Skip empty lines and lines starting with #123            if not line or line[0] == "#":124                continue125            # Check if there is a variable defined in the test suite126            match = re.match("(?P<name>\w+)\s*=\s*(?P<value>.+)", line)127            if match:128                name = match.group("name").lower()129                value = match.group("value")130                if name in ("mailto", "mail_to"):131                    suite["mail_to"] = value132                elif name in ("configfile", "config_file"):133                    suite["config_file"] = value134                # Continue to the next line of the file135                continue136            tokenized_line = line.split()137            if len(tokenized_line) == 2:138                script = tokenized_line[0]139                rule = tokenized_line[1]140            else:141                script = line142                rule = None143            # Auto-generate a testcase id144            tc_id = generate_testcase_id(script)145            script_file = os.path.join(SCRIPTS_DIR, script)146            if os.path.exists(script_file):147                suite["testcases"].append({"script" : script_file, "rule" : rule, "id" : tc_id, "description" : None})148            else:149                logger.error("Script does not exist %r" % script_file)150                return None151    return suite152def parse_suite_xml(suite_name, relative_paths=False):153    """Reads the xml suite file and Form a list of scripts to execute154    .. note::155        156        Used by Potluck to parse the test suite file157    """158    from settings import SUITES_DIR, SCRIPTS_DIR159    suite = {160        "sections" : [161            #{162            #    "name" : "", 163            #    "testcases" : []164            #}165        ],166        "config_file" : None,167        "mail_to" : None,168        "ui_url" : None,169        "name" : ""170    }171    # Read the Suite and Form a list of scripts to execute172    if os.path.isfile(suite_name):173        suite_file = suite_name174    else:175        suite_file = os.path.join(SUITES_DIR, suite_name)176    # If not found in SUITES_DIR177    if not os.path.isfile(suite_file):178        logger.warning("Suite file does not exist at %r" % suite_file)179        suite_file = os.path.join(USER_SUITES_DIR, suite_name)180    # Form the suitename from the path and filename181    suite["name"] = os.path.split(suite_file)[-1]182    if not os.path.exists(suite_file):183        logger.error("Suite file does not exist at %r" % suite_file)184        return None185    tree = eTree.parse(suite_file)186    suite_root = tree.getroot()187    global_section = {188        "name" : "Global",189        "testcases" : []190    }191    # Parse the Test suite file192    for current_tag in suite_root.getchildren():193        if current_tag.tag.lower() == "section":194            # Parse the section xml195            section = parse_section(current_tag, relative_paths)196            if section is None:197                return None198            else:199                suite["sections"].append(section)200        elif current_tag.tag.lower() == "testcase":201            # For backward compatibility, If there is a testcase without a section,202            # Then use a predefined global section203            #logger.warn("There is a testcase without section. This will be executed at the end")204            testcase = parse_testcase(current_tag, relative_paths)205            if testcase != None:206                global_section["testcases"].append(testcase)207            else:208                logger.error("Unable to Parse testcase")209                return None210        elif current_tag.tag.lower() == "suite":211            # If a nested suite is defined, parse it and get its scripts212            inner_suite_file = current_tag.text.strip()213            inner_suite = parse_suite_xml(inner_suite_file)214            # Extend the sections of the parent suite215            suite["sections"].extend(inner_suite["sections"])216        elif current_tag.tag.lower() in ("mailto", "mail_to"):217            suite["mail_to"] = current_tag.text.strip()218        elif current_tag.tag.lower() in ("configfile", "config_file"):219            suite["config_file"] = current_tag.text.strip()220        elif current_tag.tag.lower() == "config":221            # Read the config parameters associated with the suite222            for param in current_tag:223                env.config.set(param.tag, "".join([param.text] + [eTree.tostring(e) for e in param.getchildren()]).strip(), "SUITE")  # Set the parameters in suite context224        elif current_tag.tag.lower() == "ui_url":225            suite["ui_url"] = current_tag.text.strip()226    # If there is something in the global section, add it to the suite227    if global_section["testcases"]:228        suite["sections"].append(global_section)229    return suite230def parse_section(root, relative_paths=False):231    section_name = root.get("name")232    if not section_name:233        logger.error("`name` not defined for section")234        return None235    # Parse the section xml236    testcases = []237    for current_tag in root:238        if current_tag.tag.lower() == "testcase":239            testcase = parse_testcase(current_tag, relative_paths)240            if testcase != None:241                testcases.append(testcase)242            else:243                logger.error("Unable to Parse section")244                return None245        elif current_tag.tag.lower() == "suite":246            # If a nested suite is defined, parse it and get its scripts247            inner_suite_file = current_tag.text.strip()248            inner_suite = parse_suite_xml(inner_suite_file)249            # Extend the scripts of the current section250            for section in inner_suite["sections"]:251                testcases.extend(252                    section.get("testcases", [])253                )254        elif current_tag.tag.lower() == "config":255            # Read the config parameters associated with the suite256            for param in current_tag:257                # Set the parameters in section context258                env.config.set(param.tag, "".join([param.text] + [eTree.tostring(e) for e in param.getchildren()]).strip(), "SECTION_%s" % section_name)259        elif current_tag.tag.lower() == "section":260            logger.error("Nested Sections are not supported in testsuite xml")261            raise ValueError("Nested Sections are not supported in testsuite xml")262    return {263        "name" : section_name,264        "testcases" : testcases,265    }266def parse_testcase(root, relative_paths=False):267    script_tag = root.find("script")268    script = None269    rule = None270    description = root.get("description")271    # Script tag is mandatory272    if script_tag is None:273        logger.error("`script` tag is not defined for test: '%s [%s]'" % (root.tag, root.attrib))274        return None275    else:276        script = script_tag.text.strip()277    # If explicit TC-Id is not mentioned in the test suite,278    # form out a TC id from the script path by replacing all the slashes with hyphens279    tc_id = generate_testcase_id(script)280    for current_tag in root:281        tagname = current_tag.tag.lower()   # Tagname282        text = current_tag.text.strip() # Text associated with the tag283        if tagname in ("onfailure", "on_failure"):284            rule = text285        elif tagname == "config":286            # Read all the config variables associated with the testcase287            for param in current_tag:288                env.config.set(param.tag, "".join([param.text] + [eTree.tostring(e) for e in param.getchildren()]).strip(), tc_id)  # Set the parameters in testcase context289        elif tagname == "description":290            # Description can be an attribute as well as a tag also291            description = text292    if script.startswith("/"):293        script_file = script294    else:295        script_file = os.path.abspath(os.path.join(SCRIPTS_DIR, script))296    if not os.path.exists(script_file):297        logger.error("Script does not exist %r" % script_file)298        raise ValueError("Script does not exist %r" % script_file)299    if relative_paths is True:300        script_file = os.path.relpath(script_file, SCRIPTS_DIR)301    return {"script" : script_file, "rule" : rule, "id" : tc_id, "description" : description}302        303def run_cmd(cmd, async=False, shell=None):304    """Executes a command on the local machine305    :argument cmd: The command to be executed306    :returns: Output of the executed command including stdout and stderr307    """308    if shell is None:309        p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)310    else:311        p = subprocess.Popen(cmd, executable=shell, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)312    if async is True:313        return p314    else:315        stdout, stderr = p.communicate()316        return stdout.strip()317def platform():318    """Returns the platform on which Potluck is running319    :returns: Either one of OSX, WIN, LINUX320    """321    platform_str = py_platform.platform().lower()322    if "mac" in platform_str or "darwin" in platform_str:323        return "OSX"324    elif "windows" in platform_str or "win32" in platform_str:325        return "WIN"326    else:327        return "LINUX"328def parse_testbed(tb_name):329    if os.path.isfile(tb_name):330        tb_file = tb_name   # If absolute path is passed331    else:332        # If absolute path is not passed, find in TESTBEDS_DIR333        tb_file = os.path.join(TESTBEDS_DIR, tb_name)334    # If not found in TESTBEDS_DIR335    if not os.path.isfile(tb_file):336        logger.warning("Testbed file does not exist at %r" % tb_file)337        tb_file = os.path.join(USER_TESTBEDS_DIR, tb_name)338    # Parse the testbed file339    if not os.path.exists(tb_file):340        logger.error("Testbed file does not exist at %r" % tb_file)341        return {}342    testbed = {}343    default_testbed_values = {344        "connection" : "ssh",345        "port" : "22",346        "serviceport" : 22222,347    }348    tb = ConfigParser.RawConfigParser(defaults=default_testbed_values)349    tb.read(tb_file)350    # Python version 2.7: The default dict_type of ConfigParser is collections.OrderedDict.351    # So, the sections are ordered. But this will not be true for versions < 2.7352    for section in tb.sections():353        node = NodeDict()354        for key, value in tb.items(section):355            node[key] = value356        node_alias = section357        node["alias"] = node_alias358        node["type"] = [t.strip() for t in node["type"].upper().split(",")]    #List of types359        testbed[node_alias] = node360    return testbed361def has_ui_testcases(suite):362    """Returns True if there is atleast one sikuli testcases present in the suite363    :argument suite: parsed suite (as returned by :func:`parse_suite_xml`)364    """365    for section in suite["sections"]:366        for testcase in section["testcases"]:367            if "sikuli" in testcase["script"]:368                return True369    return False370def csv_diff(csv1_file, csv2_file, delimiter=",", error_percent_plus=1, error_percent_minus=1, key_columns=[1]):371    """Calculates the diff between any two CSVs, based on their values372    """373    diff_str = ""374    # Load the first file in a dict375    csv1_dict = {}376    ##### INSTA CSV ######377    with open(csv1_file, "rU") as csv1_fd:378        for line in csv1_fd:379            line = line.strip()380            fields = line.split(delimiter)381            if "itrate" in line or "rowth" in line  or "imestamp" in line or  "Total" in line or "Traffic" in line or "(bps)" in line or  not line:382                continue383            elif line.startswith("All") or line.startswith("Total"):384                key = "TOTAL_ROW"385            else:386                key = tuple([fields[kc-1].upper() for kc in key_columns])387            values = [str(val) for idx, val in enumerate(fields, start=1) if idx not in key_columns]388            #if key in csv1_dict:389            #    logger.error("Key '%s' is present multiple times in the csv file. It will be overwritten." % key)390            csv1_dict[key] = (values, line)391    # Read the second file and start comparing with the first one392    ##### UI CSV #####393    with open(csv2_file, "rU") as csv2_fd:394        for line in csv2_fd:395            line = line.strip()396            fields = line.split(delimiter)397            if "itrate" in line or "rowth" in line or "imestamp" in line or  "Total" in line or "Traffic" in line or "(bps)" in line or not line:398                continue399            elif line.startswith("All") or line.startswith("Total"):400                key = "TOTAL_ROW"401            else:402                key = tuple([fields[kc-1].upper() for kc in key_columns])403            csv2_values = []404 405            for idx, val in enumerate(fields, start=1):406                if idx not in key_columns :407                    if 'NA' not in val :408                        csv2_values.append(float(val)) 409                    else: 410                        csv2_values.append(str("NA"))411            412            #csv2_values = [ float(val) for idx, val in enumerate(fields, start=1) if idx not in key_columns ]413        414            try:415                csv1_values, csv1_line = csv1_dict[key]416                del csv1_dict[key]  # If the values are found, remove this key. The remaining values will be displayed later417            except KeyError:418                diff_str += "+%s\n" % line    # Key is present in csv2, but not in csv1419                continue420            for csv1_val, csv2_val in zip(csv1_values, csv2_values):421		if csv1_val == 'NA' and csv2_val == 'NA':422                                continue423                elif  abs(float(csv1_val) - float(csv2_val)) > 10000 :424                    if float(csv1_val) != 0 :425                        difference = (float(csv1_val) - float(csv2_val)) * 100 / float(csv1_val)426                    elif float(csv2_val) != 0 :427                        difference = (float(csv1_val) - float(csv2_val)) * 100 / float(csv2_val)428                    else:429                        difference = 0430                else:431                    difference = 0432                if (difference > 0 and difference > error_percent_plus) or \433                    (difference < 0 and abs(difference) > error_percent_minus):434                    # Difference in the values is greater than allowed difference435                    diff_str += "<%s\n" % csv1_line436                    diff_str += ">%s\n" % line437                    break438    # Display the lines present in CSV1 but not present in CSV2439    for k, v in csv1_dict.iteritems():440       	diff_str += "-%s\n" % v[1]441    return diff_str442def replace_variables_in_template(template, variables, output_file=None):443    """Reads an input template file and replace given variables with their values444    :param template: Path to input template file. The file can be present in ROOT_DIR, SUITES_DIR or SCRIPTS_DIR445    :param variables: A dict with all variables to replace and their values446    :param output_file: *[Optional]* Path to output file447    """448    from string import Template449    # Order of preference to lookup finding the template file.450    for d in (ROOT_DIR, SUITES_DIR, SCRIPTS_DIR):451        template_file = os.path.join(d, template)452        if os.path.isfile(template_file):453            logger.debug("Using configure template file '%s'." % template_file)454            break455        else:456            logger.warning("Template file does not exist at '%s'." % template_file)457    else:458        logger.error("Configure template '%s' does not exist" % template)459        return False460    # Read the file as a string template461    try:462        template = Template(open(template_file, "rU").read())463    except:464        logger.exception("Unable to parse the template file")465        return False466    # Substitute the variables in the template467    try:468        config = template.substitute(variables)469    except KeyError as e:470        logger.error("%s not found in config variables" % e)471        return False472    except:473        logger.exception("Invalid config template file")474        return False475    if output_file is not None:476        parent_dir = os.path.dirname(output_file)477        try:478            if not os.path.exists(parent_dir):479                os.makedirs(parent_dir)480        except:481            logger.exception("Error while creating parent directories")482        # Write the config to a temporary location on local disk483        with open(output_file, "w") as fh:484            fh.write(config)...generate.py
Source:generate.py  
1#!/usr/bin/env python2#3# Copyright (c) Vicent Marti. All rights reserved.4#5# This file is part of clar, distributed under the ISC license.6# For full terms see the included COPYING file.7#8from __future__ import with_statement9from string import Template10import re, fnmatch, os, sys, codecs, pickle, io11class Module(object):12    class Template(object):13        def __init__(self, module):14            self.module = module15        def _render_callback(self, cb):16            if not cb:17                return '    { NULL, NULL }'18            return '    { "%s", &%s }' % (cb['short_name'], cb['symbol'])19    class DeclarationTemplate(Template):20        def render(self):21            out = "\n".join("extern %s;" % cb['declaration'] for cb in self.module.callbacks) + "\n"22            for initializer in self.module.initializers:23                out += "extern %s;\n" % initializer['declaration']24            if self.module.cleanup:25                out += "extern %s;\n" % self.module.cleanup['declaration']26            return out27    class CallbacksTemplate(Template):28        def render(self):29            out = "static const struct clar_func _clar_cb_%s[] = {\n" % self.module.name30            out += ",\n".join(self._render_callback(cb) for cb in self.module.callbacks)31            out += "\n};\n"32            return out33    class InfoTemplate(Template):34        def render(self):35            templates = []36            initializers = self.module.initializers37            if len(initializers) == 0:38                initializers = [ None ]39            for initializer in initializers:40                name = self.module.clean_name()41                if initializer and initializer['short_name'].startswith('initialize_'):42                    variant = initializer['short_name'][len('initialize_'):]43                    name += " (%s)" % variant.replace('_', ' ')44                template = Template(45            r"""46    {47        "${clean_name}",48    ${initialize},49    ${cleanup},50        ${cb_ptr}, ${cb_count}, ${enabled}51    }"""52                ).substitute(53                    clean_name = name,54                    initialize = self._render_callback(initializer),55                    cleanup = self._render_callback(self.module.cleanup),56                    cb_ptr = "_clar_cb_%s" % self.module.name,57                    cb_count = len(self.module.callbacks),58                    enabled = int(self.module.enabled)59                )60                templates.append(template)61            return ','.join(templates)62    def __init__(self, name):63        self.name = name64        self.mtime = 065        self.enabled = True66        self.modified = False67    def clean_name(self):68        return self.name.replace("_", "::")69    def _skip_comments(self, text):70        SKIP_COMMENTS_REGEX = re.compile(71            r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',72            re.DOTALL | re.MULTILINE)73        def _replacer(match):74            s = match.group(0)75            return "" if s.startswith('/') else s76        return re.sub(SKIP_COMMENTS_REGEX, _replacer, text)77    def parse(self, contents):78        TEST_FUNC_REGEX = r"^(void\s+(test_%s__(\w+))\s*\(\s*void\s*\))\s*\{"79        contents = self._skip_comments(contents)80        regex = re.compile(TEST_FUNC_REGEX % self.name, re.MULTILINE)81        self.callbacks = []82        self.initializers = []83        self.cleanup = None84        for (declaration, symbol, short_name) in regex.findall(contents):85            data = {86                "short_name" : short_name,87                "declaration" : declaration,88                "symbol" : symbol89            }90            if short_name.startswith('initialize'):91                self.initializers.append(data)92            elif short_name == 'cleanup':93                self.cleanup = data94            else:95                self.callbacks.append(data)96        return self.callbacks != []97    def refresh(self, path):98        self.modified = False99        try:100            st = os.stat(path)101            # Not modified102            if st.st_mtime == self.mtime:103                return True104            self.modified = True105            self.mtime = st.st_mtime106            with codecs.open(path, encoding='utf-8') as fp:107                raw_content = fp.read()108        except IOError:109            return False110        return self.parse(raw_content)111class TestSuite(object):112    def __init__(self, path, output):113        self.path = path114        self.output = output115    def maybe_generate(self, path):116        if not os.path.isfile(path):117            return True118        if any(module.modified for module in self.modules.values()):119            return True120        return False121    def find_modules(self):122        modules = []123        for root, _, files in os.walk(self.path):124            module_root = root[len(self.path):]125            module_root = [c for c in module_root.split(os.sep) if c]126            tests_in_module = fnmatch.filter(files, "*.c")127            for test_file in tests_in_module:128                full_path = os.path.join(root, test_file)129                module_name = "_".join(module_root + [test_file[:-2]]).replace("-", "_")130                modules.append((full_path, module_name))131        return modules132    def load_cache(self):133        path = os.path.join(self.output, '.clarcache')134        cache = {}135        try:136            fp = open(path, 'rb')137            cache = pickle.load(fp)138            fp.close()139        except (IOError, ValueError):140            pass141        return cache142    def save_cache(self):143        path = os.path.join(self.output, '.clarcache')144        with open(path, 'wb') as cache:145            pickle.dump(self.modules, cache)146    def load(self, force = False):147        module_data = self.find_modules()148        self.modules = {} if force else self.load_cache()149        for path, name in module_data:150            if name not in self.modules:151                self.modules[name] = Module(name)152            if not self.modules[name].refresh(path):153                del self.modules[name]154    def disable(self, excluded):155        for exclude in excluded:156            for module in self.modules.values():157                name = module.clean_name()158                if name.startswith(exclude):159                    module.enabled = False160                    module.modified = True161    def suite_count(self):162        return sum(max(1, len(m.initializers)) for m in self.modules.values())163    def callback_count(self):164        return sum(len(module.callbacks) for module in self.modules.values())165    def write(self):166        wrote_suite = self.write_suite()167        wrote_header = self.write_header()168        if wrote_suite or wrote_header:169            self.save_cache()170            return True171        return False172    def write_output(self, fn, data):173        if not self.maybe_generate(fn):174            return False175        current = None176        try:177            with open(fn, 'r') as input:178                current = input.read()179        except OSError:180            pass181        except IOError:182            pass183        if current == data:184            return False185        with open(fn, 'w') as output:186            output.write(data)187        return True188    def write_suite(self):189        suite_fn = os.path.join(self.output, 'clar.suite')190        with io.StringIO() as suite_file:191            modules = sorted(self.modules.values(), key=lambda module: module.name)192            for module in modules:193                t = Module.DeclarationTemplate(module)194                suite_file.write(t.render())195            for module in modules:196                t = Module.CallbacksTemplate(module)197                suite_file.write(t.render())198            suites = "static struct clar_suite _clar_suites[] = {" + ','.join(199                Module.InfoTemplate(module).render() for module in modules200            ) + "\n};\n"201            suite_file.write(suites)202            suite_file.write(u"static const size_t _clar_suite_count = %d;\n" % self.suite_count())203            suite_file.write(u"static const size_t _clar_callback_count = %d;\n" % self.callback_count())204            return self.write_output(suite_fn, suite_file.getvalue())205        return False206    def write_header(self):207        header_fn = os.path.join(self.output, 'clar_suite.h')208        with io.StringIO() as header_file:209            header_file.write(u"#ifndef _____clar_suite_h_____\n")210            header_file.write(u"#define _____clar_suite_h_____\n")211            modules = sorted(self.modules.values(), key=lambda module: module.name)212            for module in modules:213                t = Module.DeclarationTemplate(module)214                header_file.write(t.render())215            header_file.write(u"#endif\n")216            return self.write_output(header_fn, header_file.getvalue())217        return False218if __name__ == '__main__':219    from optparse import OptionParser220    parser = OptionParser()221    parser.add_option('-f', '--force', action="store_true", dest='force', default=False)222    parser.add_option('-x', '--exclude', dest='excluded', action='append', default=[])223    parser.add_option('-o', '--output', dest='output')224    options, args = parser.parse_args()225    if len(args) > 1:226        print("More than one path given")227        sys.exit(1)228    path = args.pop() if args else '.'229    output = options.output or path230    suite = TestSuite(path, output)231    suite.load(options.force)232    suite.disable(options.excluded)233    if suite.write():...testcase_manager.py
Source:testcase_manager.py  
...97                _, suffix_name = os.path.splitext(file_name)98                if suffix_name in FILTER_SUFFIX_NAME_LIST:99                    continue100101                if not self._get_valid_suite_file(test_case_out_path,102                                                  suite_file,103                                                  options.subsystem,104                                                  options.testmodule,105                                                  options.testsuit):106                    continue107108                if suffix_name == ".dex":109                    suite_file_dictionary["DEX"].append(suite_file)110                elif suffix_name == ".hap":111                    suite_file_dictionary["HAP"].append(suite_file)112                elif suffix_name == ".py":113                    if not self._check_python_test_file(suite_file):114                        continue115                    suite_file_dictionary["PYT"].append(suite_file)116                elif suffix_name == "":117                    suite_file_dictionary["CXX"].append(suite_file)118                elif suffix_name == ".bin":119                    suite_file_dictionary["BIN"].append(suite_file)120121        return suite_file_dictionary122123    @classmethod124    def _get_valid_suite_file(cls,125                              test_case_out_path,126                              suit_file,127                              test_subsystem,128                              test_module,129                              test_suit):130        is_valid_status = False131        if suit_file.startswith(test_case_out_path):132            if test_suit == "":133                suite_file_sub_path = suit_file.replace(134                    test_case_out_path, "").strip(os.sep)135                if test_subsystem != "":136                    if test_module != "":137                        if suite_file_sub_path.startswith(test_subsystem +138                                                          os.sep +
...86.py
Source:86.py  
1"""Methods for dealing with suite modules2Suites are modules located inside the /suites/ directory3"""4import os5from golem.core import utils, file_manager6def _format_list_items(list_items):7    """Generate an indented string out of a list of items."""8    list_string = ''9    if list_items:10        for item in list_items:11            list_string = list_string + "    '" + item + "',\n"12        list_string = "[\n    {}\n]".format(list_string.strip()[:-1])13    else:14        list_string = '[]'15    return list_string16def save_suite(root_path, project, suite_name, test_cases, processes,17               browsers, environments, tags):18    """Save suite content to file."""19    suite_name, parents = utils.separate_file_from_parents(suite_name)20    suite_path = os.path.join(root_path, 'projects', project, 'suites',21                              os.sep.join(parents), '{}.py'.format(suite_name))22    with open(suite_path, 'w', encoding='utf-8') as suite_file:23        suite_file.write('\n\n')24        suite_file.write('browsers = {}\n'.format(_format_list_items(browsers)))25        suite_file.write('\n')26        suite_file.write('environments = {}\n'.format(_format_list_items(environments)))27        suite_file.write('\n')28        if tags:29            suite_file.write('tags = {}\n'.format(_format_list_items(tags)))30            suite_file.write('\n')31        if not processes:32            processes = 133        suite_file.write('processes = {}'.format(processes))34        suite_file.write('\n\n')35        suite_file.write('tests = {}\n'.format(_format_list_items(test_cases)))36def new_suite(root_path, project, parents, suite_name):37    """Create a new empty suite."""38    suite_content = ('\n'39                     'browsers = []\n\n'40                     'environments = []\n\n'41                     'processes = 1\n\n'42                     'tests = []\n')43    errors = []44    base_path = os.path.join(root_path, 'projects', project, 'suites')45    full_path = os.path.join(base_path, os.sep.join(parents))46    filepath = os.path.join(full_path, '{}.py'.format(suite_name))47    if os.path.isfile(filepath):48        errors.append('a suite with that name already exists')49    if not errors:50        # create the directory structure if it does not exist51        if not os.path.isdir(full_path):52            for parent in parents:53                base_path = os.path.join(base_path, parent)54                file_manager.create_directory(path=base_path, add_init=True)55        with open(filepath, 'w') as suite_file:56            suite_file.write(suite_content)57        print('Suite {} created for project {}'.format(suite_name, project))58    return errors59def get_suite_amount_of_processes(workspace, project, suite):60    """Get the amount of processes defined in a suite.61    Default is 1 if suite does not have processes defined"""62    amount = 163    suite_module = get_suite_module(workspace, project, suite)64    if hasattr(suite_module, 'processes'):65        amount = suite_module.processes66    return amount67def get_suite_environments(workspace, project, suite):68    """Get the environments defined in a suite."""69    environments = []70    suite_module = get_suite_module(workspace, project, suite)71    if hasattr(suite_module, 'environments'):72        environments = suite_module.environments73    return environments74def get_suite_test_cases(workspace, project, suite):75    """Return a list with all the test cases of a given suite"""76    # TODO should use glob77    tests = []78    suite_module = get_suite_module(workspace, project, suite)79    if '*' in suite_module.tests:80        path = os.path.join(workspace, 'projects', project, 'tests')81        tests = file_manager.get_files_dot_path(path, extension='.py')82    else:83        for test in suite_module.tests:84            if test[-1] == '*':85                this_dir = os.path.join(test[:-2])86                path = os.path.join(workspace, 'projects', project,87                                    'tests', this_dir)88                this_dir_tests = file_manager.get_files_dot_path(path, extension='.py')89                this_dir_tests = ['{}.{}'.format(this_dir, x) for x in this_dir_tests]90                tests = tests + this_dir_tests91            else:92                tests.append(test)93    return tests94def get_suite_browsers(workspace, project, suite):95    """Get the list of browsers defined in a suite"""96    browsers = []97    suite_module = get_suite_module(workspace, project, suite)98    if hasattr(suite_module, 'browsers'):99        browsers = suite_module.browsers100    return browsers101def get_suite_module(workspace, project, suite_name):102    """Get the module of a suite"""103    suite_name, parents = utils.separate_file_from_parents(suite_name)104    path = os.path.join(workspace, 'projects', project, 'suites',105                        os.sep.join(parents), suite_name + '.py')106    suite_module, _ = utils.import_module(path)107    return suite_module108def suite_exists(workspace, project, full_suite_name):109    """suite exists.110    full_suite_name must be a relative dot path111    """112    suite_folder = os.path.join(workspace, 'projects', project, 'suites')113    suite, parents = utils.separate_file_from_parents(full_suite_name)114    path = os.path.join(suite_folder, os.sep.join(parents), '{}.py'.format(suite))115    if os.path.isfile(path):116        return True117    path = os.path.join(suite_folder, full_suite_name)118    return os.path.isfile(path)119def generate_suite_path(root_path, project, suite_name):120    """Generate full path to a python file of a suite."""121    suite_name, parents = utils.separate_file_from_parents(suite_name)122    suite_path = os.path.join(root_path, 'projects', project, 'suites',123                              os.sep.join(parents), '{}.py'.format(suite_name))124    return suite_path125def get_tags(root_path, project, suite):126    """Get the list of tags defined in a suite"""127    tags = []128    suite_module = get_suite_module(root_path, project, suite)129    if hasattr(suite_module, 'tags'):130        tags = suite_module.tags...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!!
