Skip to content

Commit 26823aa

Browse files
Refactoring the codes
0 parents  commit 26823aa

File tree

2 files changed

+315
-0
lines changed

2 files changed

+315
-0
lines changed

.gitignore

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Created by https://www.gitignore.io/api/python
2+
# Edit at https://www.gitignore.io/?templates=python
3+
4+
### Python ###
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
pip-wheel-metadata/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# PyInstaller
35+
# Usually these files are written by a python script from a template
36+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
37+
*.manifest
38+
*.spec
39+
40+
# Installer logs
41+
pip-log.txt
42+
pip-delete-this-directory.txt
43+
44+
# Unit test / coverage reports
45+
htmlcov/
46+
.tox/
47+
.nox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
.hypothesis/
55+
.pytest_cache/
56+
57+
# Translations
58+
*.mo
59+
*.pot
60+
61+
# Django stuff:
62+
*.log
63+
local_settings.py
64+
db.sqlite3
65+
66+
# Flask stuff:
67+
instance/
68+
.webassets-cache
69+
70+
# Scrapy stuff:
71+
.scrapy
72+
73+
# Sphinx documentation
74+
docs/_build/
75+
76+
# PyBuilder
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# pyenv
87+
.python-version
88+
89+
# celery beat schedule file
90+
celerybeat-schedule
91+
92+
# SageMath parsed files
93+
*.sage.py
94+
95+
# Environments
96+
.env
97+
.venv
98+
env/
99+
venv/
100+
ENV/
101+
env.bak/
102+
venv.bak/
103+
.vscode
104+
105+
# Spyder project settings
106+
.spyderproject
107+
.spyproject
108+
109+
# Rope project settings
110+
.ropeproject
111+
112+
# mkdocs documentation
113+
/site
114+
115+
# mypy
116+
.mypy_cache/
117+
.dmypy.json
118+
dmypy.json
119+
120+
# Pyre type checker
121+
.pyre/
122+
123+
### Python Patch ###
124+
.venv/
125+
126+
### TestinggGrounds
127+
testing_logics.py
128+
define_logic_and _to_Imple.md
129+
130+
# End of https://www.gitignore.io/api/python

ultimate_PowerShell_config.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)