Dear folks, sometimes you might run into the task where you need to automatically install something in Windows.
Recently, I needed to do that task. So today I'm gonna share with you how to do that in powershell wrapper by Python.
I.What is powershell:
You might want to checkout in wiki
Powershell is a shell tool available in windows (since Windows 8)
It has more functionality compared to the good old command in DOS
II.Real world problem:
We would need to check if the app is installed yet, if it is installed and it is the latest version -> skip. If it is installed but the version is not latest --> uninstall then install.
If the app is not installed yet, --> proceed to install the latest version.
1.Get the current running version of the app:
The sample app we're working on would be "Cốc Cốc" browser.
def get_current_login_user():
import subprocess
import re
p1 = subprocess.Popen(["powershell.exe", "whoami"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
who_am_i = p1.communicate()[0]
user_name = re.split(r'\\r', (re.split(r'.\\\\', str(who_am_i))[1]))[0]
return user_name
def get_coccoc_version_folder_name():
import subprocess
import re
p = subprocess.Popen(["powershell.exe",
f"cd C:\\Users\\{current_user}\\AppData\\Local\\CocCoc\\Browser\\Application; ls"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
current_dir = str(p.communicate()[0])
coccoc_version = re.findall(r'\b\d+\.+\b\d+\.\b\d+\.+\d*', current_dir)[0]
return coccoc_version
2.Get the latest version info stored in yaml file:
yaml_made_utils = YamlUtils()
configuration_info = None
configuration_path = '/resources/configurations.yaml'
def get_configurations_info():
if configuration_info is None:
return yaml_made_utils.read_data_from_file(os.getcwd() + configuration_path)
else:
return configuration_info
class CocCocConfigs:
COCCOC_DEV_VERSION = get_configurations_info()['coccoc_dev_version']
@staticmethod
def update_coccoc_dev_version(coccoc_version):
data = get_configurations_info()
data['coccoc_dev_version'] = coccoc_version
return yaml_made_utils.write_data_to_file(data, os.getcwd() + configuration_path)
3.Uninstall the app
def uninstall_coccoc_silently():
import subprocess
p = subprocess.Popen(["powershell.exe",
f"cd C:\\Users\\{current_user}\\AppData\\Local\\CocCoc\\Browser\\Application"
f"\\{get_coccoc_version_folder_name()}"
f"\\Installer; .\\setup.exe --uninstall --force-uninstall"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
p.communicate()
4.Install the app
def install_coccoc_silently():
import subprocess
p = subprocess.Popen(["powershell.exe",
f"cd C:\\coccoc-dev; .\\{coccoc_installer_name} /silent /install"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
p.communicate()
5.Main function to run would be like :
def main():
if check_if_coccoc_is_installed():
coccoc_version = get_coccoc_version_folder_name()
from configs.yaml_configs import CocCocConfigs
if coccoc_version in CocCocConfigs.COCCOC_DEV_VERSION:
pass
else:
uninstall_coccoc_silently()
import time
time.sleep(5)
install_coccoc_silently()
else:
install_coccoc_silently()
if __name__ == "__main__":
main()
You can checkout the source code from here
Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :
This will help me to contributing more valued contents.
Happy coding!!!
Top comments (0)