Best Python code snippet using lisa_python
runbook.py
Source:runbook.py
1#!/usr/bin/env python22#3# Copyright (C) Microsoft Corporation, All rights reserved.4"""Runbook module."""5import os6import configuration7import gpg8import iohelper9import jrdsclient10import linuxutil11from workerexception import *12definition_kind_int_to_str = {0: "Unknown",13 1: "Script",14 2: "Workflow",15 3: "Graph",16 4: "Configuration",17 5: "PowerShell",18 6: "PowerShellWorkflow",19 7: "GraphPowerShell",20 8: "GraphPowerShellWorkflow",21 9: "Python2",22 10: "Python3",23 11: "Bash",24 13: "PowerShell7"}25class Runbook:26 def __init__(self, runbook_data):27 """:type runbook_data: jrdsclient.RunbookData"""28 self.runbook_data = runbook_data29 self.definition_kind = runbook_data.runbook_definition_kind30 self.definition_kind_str = definition_kind_int_to_str[runbook_data.runbook_definition_kind]31 self.runbook_file_path = ""32 # should be overwritten by derived class33 self.file_extension = ""34 def initialize(self):35 """Initializes the runbook."""36 self.write_to_disk()37 if configuration.get_enforce_runbook_signature_validation():38 self.validate_signature()39 def write_to_disk(self):40 """Writes the runbook's definition to disk."""41 file_name = self.runbook_data.name + self.runbook_data.runbook_version_id + self.file_extension42 self.runbook_file_path = os.path.join(configuration.get_working_directory_path(), file_name)43 runbook_definition = str(self.runbook_data.definition.encode("utf-8"))44 if linuxutil.is_posix_host() is True:45 # replace dos end of line to unix end of line46 runbook_definition = runbook_definition.replace("\r\n", "\n")47 iohelper.write_to_file(self.runbook_file_path, data=runbook_definition, mode="w+b")48 def validate_signature(self):49 """Validates that the runbook signature is valid.50 Raises:51 InvalidRunbookSignature if the signature is invalid52 """53 decrypted_runbook_path = self.runbook_file_path + ".d"54 is_valid = gpg.verify_signature(self.runbook_file_path, decrypted_runbook_path)55 if is_valid:56 os.remove(self.runbook_file_path)57 os.rename(decrypted_runbook_path, self.runbook_file_path)58 else:59 raise InvalidRunbookSignature()60class Python2Runbook(Runbook):61 def __init__(self, runbook_data):62 Runbook.__init__(self, runbook_data)63 self.file_extension = ".py"64 self.initialize()65class Python3Runbook(Runbook):66 def __init__(self, runbook_data):67 Runbook.__init__(self, runbook_data)68 self.file_extension = ".py"69 self.initialize()70class BashRunbook(Runbook):71 def __init__(self, runbook_data):72 Runbook.__init__(self, runbook_data)73 self.file_extension = ".sh"74 self.initialize()75class PowerShellRunbook(Runbook):76 def __init__(self, runbook_data):77 Runbook.__init__(self, runbook_data)78 self.file_extension = ".ps1"79 self.initialize()80class PowerShell7Runbook(Runbook):81 def __init__(self, runbook_data):82 Runbook.__init__(self, runbook_data)83 self.file_extension = ".ps1"...
runtimefactory.py
Source:runtimefactory.py
1#!/usr/bin/env python32# ====================================3# Copyright (c) Microsoft Corporation. All rights reserved.4# ====================================5"""Runtime factory module."""6from runtime import *7from workerexception import *8import configuration3 as configuration9# This has to map with JRDS : RunbookDefinitionKind enum10PowerShell = 511PYTHON2 = 912PYTHON3 = 1013BASH = 1114PowerShell7 = 1315def create_runtime(job_data, runbook_data):16 """Create a new instance of the appropriate language Runtime.17 :param job_data : the job data18 :param runbook_data : the runbook_data19 :type job_data : jrdsclient.JobData20 :type runbook_data : jrdsclient.RunbookData21 Returns:22 An instance of Python2Runtime if the runbook_definition_kind parameter is "Python2".23 An instance of Python3Runtime if the runbook_definition_kind parameter is "Python3".24 An instance of BashRuntime if the runbook_definition_kind parameter is "Bash".25 An instance of PowerShell7 if the runbook_definition_kind parameter is "PowerShell7".26 Throws:27 UnsupportedRunbookType : If the OS or the worker doesn't support the specified runbook_definition_kind.28 """29 """30 Special case handling for update management.31 Currently update management runbook comes runbook kind as python232 So it won't work on the python3 only machine so fixing that case.33 """34 if runbook_data.name == "PatchMicrosoftOMSLinuxComputer" and configuration.get_worker_type() == "auto-registered":35 import sys36 if sys.version_info[0] >= 3:37 runbook_data.runbook_definition_kind = PYTHON338 39 runbook_definition_kind = runbook_data.runbook_definition_kind40 if runbook_definition_kind == PowerShell:41 runbook = PowerShellRunbook(runbook_data)42 runtime = PowerShellRuntime(job_data, runbook)43 elif runbook_definition_kind == PYTHON2:44 runbook = Python2Runbook(runbook_data)45 runtime = Python2Runtime(job_data, runbook)46 elif runbook_definition_kind == PYTHON3:47 runbook = Python3Runbook(runbook_data)48 runtime = Python3Runtime(job_data, runbook)49 elif runbook_definition_kind == BASH:50 runbook = BashRunbook(runbook_data)51 runtime = BashRuntime(job_data, runbook)52 elif runbook_definition_kind == PowerShell7:53 runbook = PowerShell7Runbook(runbook_data)54 runtime = PowerShell7Runtime(job_data, runbook)55 else:56 raise WorkerUnsupportedRunbookType()57 if runtime.is_runtime_supported() is False:58 raise OSUnsupportedRunbookType()...
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!!