Best Python code snippet using tempest_python
Deploy.py
Source:Deploy.py
...44 self.needs_all_dependencies_passing = True45 # Declare attributes that need to be extracted from the HJSon cfg.46 self._define_attrs()47 # Set class instance attributes.48 self._set_attrs()49 # Check if all attributes that are needed are set.50 self._check_attrs()51 # Do variable substitutions.52 self._subst_vars()53 # List of vars required to be exported to sub-shell, as a dict.54 self.exports = self._process_exports()55 # Construct the job's command.56 self.cmd = self._construct_cmd()57 # Create the launcher object. Launcher retains the handle to self for58 # lookup & callbacks.59 self.launcher = get_launcher(self)60 def _define_attrs(self):61 """Defines the attributes this instance needs to have.62 These attributes are extracted from the Mode object / HJson config with63 which this instance is created. There are two types of attributes -64 one contributes to the generation of the command directly; the other65 provides supplementary information pertaining to the job, such as66 patterns that determine whether it passed or failed. These are67 represented as dicts, whose values indicate in boolean whether the68 extraction was successful.69 """70 # These attributes are explicitly used to construct the job command.71 self.mandatory_cmd_attrs = {}72 # These attributes may indirectly contribute to the construction of the73 # command (through substitution vars) or other things such as pass /74 # fail patterns.75 self.mandatory_misc_attrs = {76 "name": False,77 "build_mode": False,78 "flow_makefile": False,79 "exports": False,80 "dry_run": False81 }82 # Function to parse a dict and extract the mandatory cmd and misc attrs.83 def _extract_attrs(self, ddict):84 """Extracts the attributes from the supplied dict.85 'ddict' is typically either the Mode object or the entire config86 object's dict. It is used to retrieve the instance attributes defined87 in 'mandatory_cmd_attrs' and 'mandatory_misc_attrs'.88 """89 ddict_keys = ddict.keys()90 for key in self.mandatory_cmd_attrs.keys():91 if self.mandatory_cmd_attrs[key] is False:92 if key in ddict_keys:93 setattr(self, key, ddict[key])94 self.mandatory_cmd_attrs[key] = True95 for key in self.mandatory_misc_attrs.keys():96 if self.mandatory_misc_attrs[key] is False:97 if key in ddict_keys:98 setattr(self, key, ddict[key])99 self.mandatory_misc_attrs[key] = True100 def _set_attrs(self):101 """Sets additional attributes.102 Invokes '_extract_attrs()' to read in all the necessary instance103 attributes. Based on those, some additional instance attributes may104 be derived. Those are set by this method.105 """106 self._extract_attrs(self.sim_cfg.__dict__)107 # Output directory where the artifacts go (used by the launcher).108 self.odir = getattr(self, self.target + "_dir")109 # Qualified name disambiguates the instance name with other instances110 # of the same class (example: 'uart_smoke' reseeded multiple times111 # needs to be disambiguated using the index -> '0.uart_smoke'.112 self.qual_name = self.name113 # Full name disambiguates across multiple cfg being run (example:114 # 'aes:default', 'uart:default' builds.115 self.full_name = self.sim_cfg.name + ":" + self.qual_name116 # Job name is used to group the job by cfg and target. The scratch path117 # directory name is assumed to be uniquified, in case there are more118 # than one sim_cfgs with the same name.119 self.job_name = "{}_{}".format(120 Path(self.sim_cfg.scratch_path).name, self.target)121 # Input directories (other than self) this job depends on.122 self.input_dirs = []123 # Directories touched by this job. These directories are marked124 # becuase they are used by dependent jobs as input.125 self.output_dirs = [self.odir]126 # Pass and fail patterns.127 self.pass_patterns = []128 self.fail_patterns = []129 def _check_attrs(self):130 """Checks if all required class attributes are set.131 Invoked in __init__() after all attributes are extracted and set.132 """133 for attr in self.mandatory_cmd_attrs.keys():134 if self.mandatory_cmd_attrs[attr] is False:135 raise AttributeError("Attribute {!r} not found for "136 "{!r}.".format(attr, self.name))137 for attr in self.mandatory_misc_attrs.keys():138 if self.mandatory_misc_attrs[attr] is False:139 raise AttributeError("Attribute {!r} not found for "140 "{!r}.".format(attr, self.name))141 def _subst_vars(self, ignored_subst_vars=[]):142 """Recursively search and replace substitution variables.143 First pass: search within self dict. We ignore errors since some144 substitions may be available in the second pass. Second pass: search145 the entire sim_cfg object."""146 self.__dict__ = find_and_substitute_wildcards(self.__dict__,147 self.__dict__,148 ignored_subst_vars, True)149 self.__dict__ = find_and_substitute_wildcards(self.__dict__,150 self.sim_cfg.__dict__,151 ignored_subst_vars,152 False)153 def _process_exports(self):154 """Convert 'exports' as a list of dicts in the HJson to a dict.155 Exports is a list of key-value pairs that are to be exported to the156 subprocess' environment so that the tools can lookup those options.157 DVSim limits how the data is presented in the HJson (the value of a158 HJson member cannot be an object). This method converts a list of dicts159 into a dict variable, which makes it easy to merge the list of exports160 with the subprocess' env where the ASIC tool is invoked.161 """162 return {k: str(v) for item in self.exports for k, v in item.items()}163 def _construct_cmd(self):164 """Construct the command that will eventually be launched."""165 cmd = "make -f {} {}".format(self.flow_makefile, self.target)166 if self.dry_run is True:167 cmd += " -n"168 for attr in sorted(self.mandatory_cmd_attrs.keys()):169 value = getattr(self, attr)170 if type(value) is list:171 # Join attributes that are list of commands with '&&' to chain172 # them together when executed as a Make target's recipe.173 separator = " && " if attr in self.cmds_list_vars else " "174 value = separator.join(item.strip() for item in value)175 if type(value) is bool:176 value = int(value)177 if type(value) is str:178 value = value.strip()179 cmd += " {}={}".format(attr, shlex.quote(value))180 return cmd181 def is_equivalent_job(self, item):182 """Checks if job that would be dispatched with 'item' is equivalent to183 'self'.184 Determines if 'item' and 'self' would behave exactly the same way when185 deployed. If so, then there is no point in keeping both. The caller can186 choose to discard 'item' and pick 'self' instead. To do so, we check187 the final resolved 'cmd' & the exports. The 'name' field will be unique188 to 'item' and 'self', so we take that out of the comparison.189 """190 if type(self) != type(item):191 return False192 # Check if the cmd field is identical.193 item_cmd = item.cmd.replace(item.name, self.name)194 if self.cmd != item_cmd:195 return False196 # Check if exports have identical set of keys.197 if self.exports.keys() != item.exports.keys():198 return False199 # Check if exports have identical values.200 for key, val in self.exports.items():201 item_val = item.exports[key]202 if type(item_val) is str:203 item_val = item_val.replace(item.name, self.name)204 if val != item_val:205 return False206 log.log(VERBOSE, "Deploy job \"%s\" is equivalent to \"%s\"",207 item.name, self.name)208 return True209 def pre_launch(self):210 """Callback to perform additional pre-launch activities.211 This is invoked by launcher::_pre_launch().212 """213 pass214 def post_finish(self, status):215 """Callback to perform additional post-finish activities.216 This is invoked by launcher::_post_finish().217 """218 pass219 def get_log_path(self):220 """Returns the log file path."""221 return "{}/{}.log".format(self.odir, self.target)222class CompileSim(Deploy):223 """Abstraction for building the simulation executable."""224 target = "build"225 cmds_list_vars = ["pre_build_cmds", "post_build_cmds"]226 weight = 5227 def __init__(self, build_mode, sim_cfg):228 self.build_mode_obj = build_mode229 super().__init__(sim_cfg)230 def _define_attrs(self):231 super()._define_attrs()232 self.mandatory_cmd_attrs.update({233 # tool srcs234 "proj_root": False,235 # Flist gen236 "sv_flist_gen_cmd": False,237 "sv_flist_gen_dir": False,238 "sv_flist_gen_opts": False,239 # Build240 "build_dir": False,241 "pre_build_cmds": False,242 "build_cmd": False,243 "build_opts": False,244 "post_build_cmds": False,245 })246 self.mandatory_misc_attrs.update({247 "cov_db_dir": False,248 "build_pass_patterns": False,249 "build_fail_patterns": False250 })251 def _set_attrs(self):252 super()._extract_attrs(self.build_mode_obj.__dict__)253 super()._set_attrs()254 # 'build_mode' is used as a substitution variable in the HJson.255 self.build_mode = self.name256 self.job_name += f"_{self.build_mode}"257 if self.sim_cfg.cov:258 self.output_dirs += [self.cov_db_dir]259 self.pass_patterns = self.build_pass_patterns260 self.fail_patterns = self.build_fail_patterns261 def pre_launch(self):262 # Delete old coverage database directories before building again. We263 # need to do this because the build directory is not 'renewed'.264 rm_path(self.cov_db_dir)265class CompileOneShot(Deploy):266 """Abstraction for building the design (used by non-DV flows)."""267 target = "build"268 def __init__(self, build_mode, sim_cfg):269 self.build_mode_obj = build_mode270 super().__init__(sim_cfg)271 def _define_attrs(self):272 super()._define_attrs()273 self.mandatory_cmd_attrs.update({274 # tool srcs275 "proj_root": False,276 # Flist gen277 "sv_flist_gen_cmd": False,278 "sv_flist_gen_dir": False,279 "sv_flist_gen_opts": False,280 # Build281 "build_dir": False,282 "pre_build_cmds": False,283 "build_cmd": False,284 "build_opts": False,285 "post_build_cmds": False,286 "build_log": False,287 # Report processing288 "report_cmd": False,289 "report_opts": False290 })291 self.mandatory_misc_attrs.update({"build_fail_patterns": False})292 def _set_attrs(self):293 super()._extract_attrs(self.build_mode_obj.__dict__)294 super()._set_attrs()295 # 'build_mode' is used as a substitution variable in the HJson.296 self.build_mode = self.name297 self.job_name += f"_{self.build_mode}"298 self.fail_patterns = self.build_fail_patterns299class RunTest(Deploy):300 """Abstraction for running tests. This is one per seed for each test."""301 # Initial seed values when running tests (if available).302 target = "run"303 seeds = []304 fixed_seed = None305 cmds_list_vars = ["pre_run_cmds", "post_run_cmds"]306 def __init__(self, index, test, build_job, sim_cfg):307 self.test_obj = test308 self.index = index309 self.seed = RunTest.get_seed()310 super().__init__(sim_cfg)311 if build_job is not None:312 self.dependencies.append(build_job)313 # We did something wrong if build_mode is not the same as the build_job314 # arg's name.315 assert self.build_mode == build_job.name316 self.launcher.renew_odir = True317 def _define_attrs(self):318 super()._define_attrs()319 self.mandatory_cmd_attrs.update({320 # tool srcs321 "proj_root": False,322 "uvm_test": False,323 "uvm_test_seq": False,324 "sw_images": False,325 "sw_build_device": False,326 "sw_build_dir": False,327 "run_dir": False,328 "pre_run_cmds": False,329 "run_cmd": False,330 "run_opts": False,331 "post_run_cmds": False,332 })333 self.mandatory_misc_attrs.update({334 "run_dir_name": False,335 "cov_db_dir": False,336 "cov_db_test_dir": False,337 "run_pass_patterns": False,338 "run_fail_patterns": False339 })340 def _set_attrs(self):341 super()._extract_attrs(self.test_obj.__dict__)342 super()._set_attrs()343 # 'test' is used as a substitution variable in the HJson.344 self.test = self.name345 self.build_mode = self.test_obj.build_mode.name346 self.qual_name = self.run_dir_name + "." + str(self.seed)347 self.full_name = self.sim_cfg.name + ":" + self.qual_name348 self.job_name += f"_{self.build_mode}"349 if self.sim_cfg.cov:350 self.output_dirs += [self.cov_db_test_dir]351 # In GUI mode, the log file is not updated; hence, nothing to check.352 if not self.sim_cfg.gui:353 self.pass_patterns = self.run_pass_patterns354 self.fail_patterns = self.run_fail_patterns355 def post_finish(self, status):356 if status != 'P':357 # Delete the coverage data if available.358 rm_path(self.cov_db_test_dir)359 @staticmethod360 def get_seed():361 # If --seeds option is passed, then those custom seeds are consumed362 # first. If --fixed-seed <val> is also passed, the subsequent tests363 # (once the custom seeds are consumed) will be run with the fixed seed.364 if not RunTest.seeds:365 if RunTest.fixed_seed:366 return RunTest.fixed_seed367 for i in range(1000):368 seed = random.getrandbits(32)369 RunTest.seeds.append(seed)370 return RunTest.seeds.pop(0)371class CovUnr(Deploy):372 """Abstraction for coverage UNR flow."""373 target = "cov_unr"374 def __init__(self, sim_cfg):375 super().__init__(sim_cfg)376 def _define_attrs(self):377 super()._define_attrs()378 self.mandatory_cmd_attrs.update({379 # tool srcs380 "proj_root": False,381 # Need to generate filelist based on build mode382 "sv_flist_gen_cmd": False,383 "sv_flist_gen_dir": False,384 "sv_flist_gen_opts": False,385 "build_dir": False,386 "cov_unr_build_cmd": False,387 "cov_unr_build_opts": False,388 "cov_unr_run_cmd": False,389 "cov_unr_run_opts": False390 })391 self.mandatory_misc_attrs.update({392 "cov_unr_dir": False,393 "cov_merge_db_dir": False,394 "build_fail_patterns": False395 })396 def _set_attrs(self):397 super()._set_attrs()398 self.qual_name = self.target399 self.full_name = self.sim_cfg.name + ":" + self.qual_name400 self.input_dirs += [self.cov_merge_db_dir]401 # Reuse the build_fail_patterns set in the HJson.402 self.fail_patterns = self.build_fail_patterns403class CovMerge(Deploy):404 """Abstraction for merging coverage databases."""405 target = "cov_merge"406 weight = 10407 def __init__(self, run_items, sim_cfg):408 # Construct the cov_db_dirs right away from the run_items. This is a409 # special variable used in the HJson.410 self.cov_db_dirs = []411 for run in run_items:412 if run.cov_db_dir not in self.cov_db_dirs:413 self.cov_db_dirs.append(run.cov_db_dir)414 # Early lookup the cov_merge_db_dir, which is a mandatory misc415 # attribute anyway. We need it to compute additional cov db dirs.416 self.cov_merge_db_dir = subst_wildcards("{cov_merge_db_dir}",417 sim_cfg.__dict__)418 # Prune previous merged cov directories, keeping past 7 dbs.419 prev_cov_db_dirs = clean_odirs(odir=self.cov_merge_db_dir, max_odirs=7)420 # If the --cov-merge-previous command line switch is passed, then421 # merge coverage with the previous runs.422 if sim_cfg.cov_merge_previous:423 self.cov_db_dirs += [str(item) for item in prev_cov_db_dirs]424 super().__init__(sim_cfg)425 self.dependencies += run_items426 # Run coverage merge even if one test passes.427 self.needs_all_dependencies_passing = False428 # Append cov_db_dirs to the list of exports.429 self.exports["cov_db_dirs"] = shlex.quote(" ".join(self.cov_db_dirs))430 def _define_attrs(self):431 super()._define_attrs()432 self.mandatory_cmd_attrs.update({433 "cov_merge_cmd": False,434 "cov_merge_opts": False435 })436 self.mandatory_misc_attrs.update({437 "cov_merge_dir": False,438 "cov_merge_db_dir": False439 })440 def _set_attrs(self):441 super()._set_attrs()442 self.qual_name = self.target443 self.full_name = self.sim_cfg.name + ":" + self.qual_name444 # For merging coverage db, the precise output dir is set in the HJson.445 self.odir = self.cov_merge_db_dir446 self.input_dirs += self.cov_db_dirs447 self.output_dirs = [self.odir]448class CovReport(Deploy):449 """Abstraction for coverage report generation. """450 target = "cov_report"451 weight = 10452 def __init__(self, merge_job, sim_cfg):453 super().__init__(sim_cfg)454 self.dependencies.append(merge_job)455 def _define_attrs(self):456 super()._define_attrs()457 self.mandatory_cmd_attrs.update({458 "cov_report_cmd": False,459 "cov_report_opts": False460 })461 self.mandatory_misc_attrs.update({462 "cov_report_dir": False,463 "cov_merge_db_dir": False,464 "cov_report_txt": False465 })466 def _set_attrs(self):467 super()._set_attrs()468 self.qual_name = self.target469 self.full_name = self.sim_cfg.name + ":" + self.qual_name470 # Keep track of coverage results, once the job is finished.471 self.cov_total = ""472 self.cov_results = ""473 def post_finish(self, status):474 """Extract the coverage results summary for the dashboard.475 If that fails for some reason, report the job as a failure.476 """477 if self.dry_run or status != 'P':478 return479 results, self.cov_total, ex_msg = get_cov_summary_table(480 self.cov_report_txt, self.sim_cfg.tool)481 if ex_msg:482 self.launcher.fail_msg += ex_msg483 log.error(ex_msg)484 return485 # Succeeded in obtaining the coverage data.486 colalign = (("center", ) * len(results[0]))487 self.cov_results = tabulate(results,488 headers="firstrow",489 tablefmt="pipe",490 colalign=colalign)491 # Delete the cov report - not needed.492 rm_path(self.get_log_path())493class CovAnalyze(Deploy):494 """Abstraction for running the coverage analysis tool."""495 target = "cov_analyze"496 def __init__(self, sim_cfg):497 # Enforce GUI mode for coverage analysis.498 sim_cfg.gui = True499 super().__init__(sim_cfg)500 def _define_attrs(self):501 super()._define_attrs()502 self.mandatory_cmd_attrs.update({503 # tool srcs504 "proj_root": False,505 "cov_analyze_cmd": False,506 "cov_analyze_opts": False507 })508 self.mandatory_misc_attrs.update({509 "cov_analyze_dir": False,510 "cov_merge_db_dir": False511 })512 def _set_attrs(self):513 super()._set_attrs()514 self.qual_name = self.target515 self.full_name = self.sim_cfg.name + ":" + self.qual_name...
rename.py
Source:rename.py
...17 >>> foo.__name__18 'NEW_NAME'19 >>>20 '''...
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!!