|
| 1 | +import ctypes |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from shutil import which |
| 6 | +from threading import Timer |
| 7 | +import traceback |
| 8 | + |
| 9 | + |
| 10 | +def show_exception_and_exit(exc_type, exc_value, tb): |
| 11 | + |
| 12 | + traceback.print_exception(exc_type, exc_value, tb) |
| 13 | + sys.exit(-1) |
| 14 | + |
| 15 | +# Check if the current execution is run on elevated mode |
| 16 | + |
| 17 | + |
| 18 | +def is_admin(): |
| 19 | + try: |
| 20 | + return ctypes.windll.shell32.IsUserAnAdmin() |
| 21 | + except: |
| 22 | + return False |
| 23 | + |
| 24 | + |
| 25 | +# Define subprocess.Popen to execute PowerShell commands |
| 26 | +def ps_arg(rarg): |
| 27 | + sub = subprocess.Popen |
| 28 | + ps = 'powershell.exe' |
| 29 | + |
| 30 | + cmd = sub([ps, rarg], stdout=subprocess.PIPE) |
| 31 | + |
| 32 | + return cmd |
| 33 | + |
| 34 | + |
| 35 | +# A function to Set-ExecutionPolicy on windows system |
| 36 | +def set_ps_exec_policy(): |
| 37 | + print("Setting Execution Policy...") |
| 38 | + setting_policy = ps_arg( |
| 39 | + r'Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force') |
| 40 | + print("Execution Policy Setup Successfully! \n") |
| 41 | + |
| 42 | + return setting_policy |
| 43 | + |
| 44 | + |
| 45 | +def show_output(arg): |
| 46 | + while True: |
| 47 | + output = arg.stdout.readline(1) |
| 48 | + if output == b'' and arg.poll() is not None: |
| 49 | + break |
| 50 | + if output: |
| 51 | + sys.stdout.write(output.decode('utf-8')) |
| 52 | + sys.stdout.flush() |
| 53 | + |
| 54 | + |
| 55 | +# A function to check if the tool/program 'to-be' installed is already available on the system |
| 56 | +def is_tool(prog): |
| 57 | + names_list = prog.split() |
| 58 | + available_prog = [] |
| 59 | + |
| 60 | + for i in names_list: |
| 61 | + if which(i) is not None: |
| 62 | + available_prog.append(i) |
| 63 | + |
| 64 | + return available_prog, names_list |
| 65 | + |
| 66 | + |
| 67 | +# A function to install chocolatey if it doesn't exist on the system already |
| 68 | +def install_choco(): |
| 69 | + print("Checking if Chocolatey exists else installing...") |
| 70 | + available, _ = is_tool("choco") |
| 71 | + |
| 72 | + if "choco" not in available: |
| 73 | + print("Installing Chocolatey...") |
| 74 | + ps_arg(r'iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex') |
| 75 | + else: |
| 76 | + print("Chocolatey already exist! \n") |
| 77 | + |
| 78 | + |
| 79 | +# A function to install a tool/program that the specified on 'is_tool()' |
| 80 | +def install_tool(prog): |
| 81 | + available, tool = is_tool(prog) |
| 82 | + for i in tool: |
| 83 | + # print(i) |
| 84 | + if i not in available: |
| 85 | + print("Installing {}...".format(i)) |
| 86 | + run = ps_arg(r'choco install -y {}'.format(i)) |
| 87 | + show_output(run) |
| 88 | + else: |
| 89 | + print("{} already exist! \n".format(i)) |
| 90 | + |
| 91 | + |
| 92 | +def input_ans(arg): |
| 93 | + yes = {'yes', 'y', 'ye', ''} |
| 94 | + no = {'no', 'n'} |
| 95 | + |
| 96 | + choice = input(arg).lower() |
| 97 | + if choice in yes: |
| 98 | + return True |
| 99 | + elif choice in no: |
| 100 | + return False |
| 101 | + else: |
| 102 | + sys.stdout.write("Please respond with 'yes' or 'no'") |
| 103 | + |
| 104 | + |
| 105 | +def git_setup(): |
| 106 | + |
| 107 | + print("Checking if git exists else installing...") |
| 108 | + install_tool("git") |
| 109 | + |
| 110 | + print("Setting git environment...") |
| 111 | + ps_arg( |
| 112 | + r'if (-not (Test-Path $env:Path:"C:\Program Files\Git\usr\bin")) {[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\Git\usr\bin", "Machine")}') |
| 113 | + print("git Env setup successfully! \n") |
| 114 | + |
| 115 | + print("Generating Ssh Key... \n") |
| 116 | + email = input("Enter your email > ") |
| 117 | + key_name = input("Specify key name > ") |
| 118 | + generating_ssh_key = ps_arg( |
| 119 | + r'ssh-keygen -t rsa -b 4096 -C "{}" -f $env:USERPROFILE\.ssh\{}'.format(email, key_name)) |
| 120 | + show_output(generating_ssh_key) |
| 121 | + print("The generated key is stored at $env:USERPROFILE\\.ssh \n") |
| 122 | + |
| 123 | + cp_ans = input_ans( |
| 124 | + "Do you want to copy the generated key to the clipboard? y/N > ") |
| 125 | + |
| 126 | + if cp_ans == True: |
| 127 | + ps_arg(r'Get-Content $env:USERPROFILE\.ssh\{}.pub | clip'.format(key_name)) |
| 128 | + show_output( |
| 129 | + ps_arg(r'ssh-keygen -y -f $env:USERPROFILE\.ssh\{}'.format(key_name))) |
| 130 | + |
| 131 | + print("""\nThe above key is successfully copied to clipboard! \n |
| 132 | + You can now Add the public key to your GitHub A/C\n |
| 133 | + See https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/ for more info\n""") |
| 134 | + else: |
| 135 | + print("Key not copied! \n") |
| 136 | + |
| 137 | + print("Adding Ssh-Agent...") |
| 138 | + show_output( |
| 139 | + ps_arg(r'Start-SshAgent;Add-SshKey $env:USERPROFILE\.ssh\{}'.format(key_name))) |
| 140 | + print("Ssh-Agent successfully added!\n") |
| 141 | + |
| 142 | + print("Test authentication to GitHub...") |
| 143 | + show_output(ps_arg(r'ssh -T git@github.com')) |
| 144 | + |
| 145 | + |
| 146 | +def config_global_git(): |
| 147 | + config_ans = input_ans( |
| 148 | + "Do you want to Configure global Git settings? y/N > ") |
| 149 | + email, username = input( |
| 150 | + "Enter Your Github email address and username separated with space respectively > ").split() |
| 151 | + |
| 152 | + if config_ans == True: |
| 153 | + ps_arg(r'git config --global user.email {};git config --global user.name {};git config --global push.default simple;git config --global core.ignorecase false;git config --global core.autocrlf true'.format(email, username)) |
| 154 | + else: |
| 155 | + print("Global Git settings not configured") |
| 156 | + |
| 157 | +# A function to run specific commands with elevated privileges |
| 158 | + |
| 159 | + |
| 160 | +def run_as_admin(): |
| 161 | + |
| 162 | + if is_admin(): |
| 163 | + set_ps_exec_policy() |
| 164 | + install_choco() |
| 165 | + git_setup() |
| 166 | + config_global_git() |
| 167 | + install_ans = input_ans( |
| 168 | + "Do you want to install any other program/programs with choco? y/N > ") |
| 169 | + if install_ans == True: |
| 170 | + install_tool(input("\nProgram name or names > ")) |
| 171 | + else: |
| 172 | + print("Well, this is the end of the program execution") |
| 173 | + |
| 174 | + input("Press a key to exit") |
| 175 | + else: |
| 176 | + # Re-run the program with admin rights |
| 177 | + ctypes.windll.shell32.ShellExecuteW( |
| 178 | + None, "runas", sys.executable, __file__, None, 1) |
| 179 | + |
| 180 | + return |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + run_as_admin() |
| 185 | + sys.excepthook = show_exception_and_exit |
0 commit comments