Best Python code snippet using lisa_python
install.py
Source:install.py  
1#!/usr/bin/python32from torngconf.theme import *3from time import sleep4from os import geteuid, system, path, name5from subprocess import getoutput6def banner():7    print(the_banner)8    print(language.description)9def check_windows_check_root():10    if name == "nt":11        print(English.sorry_windows)12        exit()13    14    if geteuid() != 0:15        print(language.root_please)16        exit()17def check_lang():18    try:19        if path.isfile('torngconf/langconf.txt') == True:20            with open('torngconf/langconf.txt') as file_lang:21                language = eval(file_lang.readline())22                file_lang.close()23                print(language.applying_language, end='', flush=True)24                print(language.done)25                return language26        else:27            language = choose_lang()28            return language29        30    except KeyboardInterrupt:31        print()32        exit()33    except (NameError, SyntaxError, AttributeError):34        language = choose_lang()35        return language36    except FileNotFoundError:37        print("TorghostNG is lacking its needed files. Reinstall TorghostNG from Github pls")38        exit()39def choose_lang(language=English):40    try:41        with open('torngconf/langconf.txt', mode="w") as file_lang:            42            print(language.language_list)43            choice = int(input(language.choose_your_lang))44            45            if choice == 1:46                print(English.applying_language)47                file_lang.write("English")48                language = English49    50            51            elif choice == 2:52                print(Vietnamese.applying_language)53                file_lang.write("Vietnamese")54                language = Vietnamese55            else:56                print()57                print(language.invalid_choice)58                choose_lang()59            file_lang.close()60            return language61    except KeyboardInterrupt:62        print()63        exit()64def uninstall():65    try:66        choice = str(input(language.wanna_uninstall))67        68        if choice[0].upper() == "Y":69            print(language.uninstalling)70            system('rm -rf /usr/bin/torngconf')71            system('rm /usr/bin/torghostng')72            print(language.uninstalled)73            74        else:75            print(language.torghostng_tip.format('torghostng') + color.END)76            77        print(language.video_tutorials)78        exit()79            80    except KeyboardInterrupt:81        print()82        exit()83language = check_lang()84check_windows_check_root()85banner()86if path.isfile('/usr/bin/torghostng') == True:87    print(language.already_installed.format('TorghostNG'))88    uninstall()89if path.isfile('/usr/bin/pacman') == True:90    INSTALL_PACKAGES = "pacman -S "91        92elif path.isfile('/usr/bin/apt') == True:93    INSTALL_PACKAGES = "apt install "94    95elif path.isfile('/usr/bin/dnf') == True:96    INSTALL_PACKAGES = "dnf install "97        98elif path.isfile('/usr/bin/yum') == True:99    INSTALL_PACKAGES = "yum install "100        101elif path.isfile('/usr/bin/zypper') == True:102    INSTALL_PACKAGES = "zypper install "103        104elif path.isfile('/usr/bin/xbps-install') == True:105    INSTALL_PACKAGES = "xbps-install -S "106        107elif path.isfile('/usr/bin/upgradepkg') == True:108    INSTALL_PACKAGES = "upgradepkg --install-new "109else:110    print(language.sorry_some_os)111    exit()112def install_package(package):113    try:114        if path.isfile('/usr/bin/'+package) == True:115            print(language.already_installed.format(package))116            117        else:118            print(language.installing.format(package))119            120            if path.isfile('/usr/bin/upgradepkg') == True:121                print(language.downloading.format(package))122                if package == 'tor':123                    system('wget https://slack.conraid.net/repository/slackware64-current/tor/tor-0.4.2.7-x86_64-1cf.txz')124                    system(INSTALL_PACKAGES + 'tor-0.4.2.7-x86_64-1cf.txz')125                    126                elif package == 'macchanger':127                    system('wget https://slack.conraid.net/repository/slackware64-current/macchanger/macchanger-1.7.0-x86_64-5cf.txz')128                    system(INSTALL_PACKAGES + 'macchanger-1.7.0-x86_64-5cf.txz')129                    130                elif package == 'pip3':131                    system('wget https://packages.slackonly.com/pub/packages/14.1-x86_64/python/python3/python3-3.5.1-x86_64-1_slack.txz')132                    system(INSTALL_PACKAGES + 'python3-3.5.1-x86_64-1_slack.txz')133                    134                elif package == 'privoxy':135                    system('wget https://packages.slackonly.com/pub/packages/14.2-x86_64/network/privoxy/privoxy-3.0.28-x86_64-1_slonly.txz')136                    system(INSTALL_PACKAGES + 'privoxy-3.0.28-x86_64-1_slonly.txz')137                    138                elif package == 'netstat':139                    system('wget http://slackware.cs.utah.edu/pub/slackware/slackware64-current/slackware64/n/net-tools-20181103_0eebece-x86_64-1.txz')140                    system(INSTALL_PACKAGES + 'net-tools-20181103_0eebece-x86_64-1.txz')141                142            else:143                if package == 'pip3':144                    package = 'python3-pip'145                    if path.isfile('/usr/bin/pacman') == True: package = 'python-pip'146                        147                elif package == 'netstat': package = 'net-tools'148                system(INSTALL_PACKAGES + package)149            print(icon.success + language.done)150                151            print(language.installed.format(package))152            153            if path.isfile('/usr/bin/upgradepkg') == True:154                print(language.torghostng_tip.format('python3 torghostng.py'))155                print(language.video_tutorials)156                exit()157            158    except KeyboardInterrupt:159        print()160        exit()161def pyinstaller():162    try:163        system('pip2 uninstall pyinstaller')164        if path.isfile('/usr/bin/pyinstaller') == True:165            print(language.already_installed.format('PyInstaller'))166            167        else:168            print(language.installing.format('PyInstaller'))169            system('pip3 install pyinstaller')170            171            print(icon.success + language.done)172            print(language.installed.format('PyInstaller'))173            174        print(language.installing.format('TorghostNG'))175        system('pyinstaller --onefile torghostng.py')176        system('cp -r dist/torghostng /usr/bin && cp -r torngconf /usr/bin')177        system('chmod +x /usr/bin/torghostng')178        179        print(icon.success + language.done)180        print(language.torghostng_tip.format('torghostng') + color.END)181        182    except KeyboardInterrupt:183        print()184        exit()185packages = ['tor','macchanger','privoxy','netstat','pip3']186for package in packages:187    install_package(package)188pyinstaller()189print(language.video_tutorials)...get_consul_url.py
Source:get_consul_url.py  
1import subprocess2from ansible.module_utils.basic import AnsibleModule3def get_consul_url(install_packages, consul_address, consul_secured):4    """5    Calculates the contextual address to use for this machine to reach a consul agent or server.6    Arguments:7    install_packages -- the list of packages that will be installed on this machine8    consul_address -- the fully qualified domain name of a consul server in the deployment9    consul_secured -- boolean representation of whether or not consul is being secured10    Returns:11    The url that this particular machine can use to reference a consul agent or server.12    """13    scheme = "http"14    port = 850015    if consul_secured == True:16        scheme = "https"17        port = 850118    host = consul_address19    if "sas-consul" in install_packages or "sas-localconsul" in install_packages:20        host = "localhost"21    return scheme + "://" + host + ":" + repr(port)22def init_module():23    """24    Initializes Ansible module handing25    """26    module = AnsibleModule(argument_spec={27        "install_packages": {"required": True, "type": "list"},28        "consul_address": {"required": True, "type": "str"},29        "consul_secured": {"required": True, "type": "bool"}30    })31    return module32def main():33    """34    Entry function for Ansible35    """36    module = init_module()37    install_packages = module.params['install_packages']38    consul_address = module.params['consul_address']39    consul_secured = module.params['consul_secured']40    url = get_consul_url(install_packages, consul_address, consul_secured)41    success_msg = "Successfully gathered additional host facts."42    changed = False43    module.exit_json(changed=changed, msg=success_msg, url=url)44if __name__ == '__main__':...test_apt.py
Source:test_apt.py  
...22            'REINSTALL': 'pkg1'23        }24        # set apt binary to echo25        apt.APT_BIN = 'echo'26    def test_install_packages(self):27        output = install_packages(self.input['INSTALL_PACKAGES'])28        self.assertEqual(self.expected['INSTALL_PACKAGES'], output, msg=f'install_packages output is not {self.expected.get("INSTALL_PACKAGES")}: {output}')29    def test_install_upgrade(self):30        output = install_upgrade(self.input['INSTALL_UPGRADE'])31        self.assertEqual(self.expected['INSTALL_UPGRADE'], output, msg=f'install_packages output is not {self.expected.get("INSTALL_UPGRADE")}: {output}')32    def test_reinstall(self):33        output = reinstall(self.input['REINSTALL'])34        self.assertEqual(self.expected['REINSTALL'], output, msg=f'install_packages output is not {self.expected.get("REINSTALL")}: {output}')35    def test_update(self):36        output = update()37        self.assertEqual(self.expected['UPDATE'], output, msg=f'install_packages output is not {self.expected.get("UPDATE")}: {output}')38    def tearDown(self):...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!!
