Untitled
#!/usr/bin/python # -*- coding: utf-8 -*- import os import sys import subprocess import argparse if __name__=="__main__": parser = argparse.ArgumentParser(description='Parse the given file and concatenate its contents to the PATH variable. Must run as admin.') parser.add_argument("file", type = lambda x:os.path.abspath(x), help = "file where each line shall be one entry in the path.") args = parser.parse_args() file = args.file separator = ";" resultList = set() pathRef = os.environ["PATH"] os.environ["Helper"] = "%" with open(file) as f: for line in f: entry = line.split("=") if len(entry) == 2: shortname = entry[0] pathes = entry[1].strip() pathes = os.path.abspath(pathes) indexPath = pathRef.find(pathes) indexShortname = pathRef.find("%"+shortname+"%") if indexPath < 0 and indexShortname < 0: setxCommand = "setx -m %s \"%s\"" % (shortname,pathes) subprocess.check_output(setxCommand, shell=True, universal_newlines=True) print(setxCommand) resultList.add("%Helper%"+shortname+"%Helper%") else: print("Entry is already in path: "+line) if len(resultList) > 0: resultString = separator.join(resultList) pathVariable = "%PATH%" setxCommand = "setx -m PATH \"%s;%s\"" % (pathVariable,resultString) print(setxCommand) subprocess.check_output(setxCommand, shell=True) else: print("Nothing to add to path!") sys.exit(0) import winreg as wrg import subprocess def set_reg_env(name, value): REG_PATH=r"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" try: #wrg.CreateKey(wrg.HKEY_LOCAL_MACHINE, REG_PATH) registry_key = wrg.OpenKey(wrg.HKEY_LOCAL_MACHINE, REG_PATH, 0, wrg.KEY_SET_VALUE) wrg.SetValueEx(registry_key, name, 0, wrg.REG_EXPAND_SZ, value) wrg.CloseKey(registry_key) print("True") except Exception as e: print(str(e)) return False def update_reg_path_value(path_to_add): env_key="Path" current_path = get_current_path() val_string = create_new_path_value(current_path, path_to_add) wrg.SetValueEx(env_key,"Path",0,wrg.REG_SZ,val_string) def create_new_path_value(current_path, path_to_add): return current_path+";"+path_to_add def get_current_path(): location = wrg.HKEY_LOCAL_MACHINE # Storing path in soft soft = wrg.OpenKeyEx(location,r"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment") # reading values in value_1 and value_2 path_value = wrg.QueryValueEx(soft,"Path") # Closing folder if soft: wrg.CloseKey(soft) #print(value_1) return path_value #------------------------------------------------ # get all the env vars keys and their values : def get_all_env_keys(): hKey = wrg.OpenKey(wrg.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment") try: count = 0 while 1: name, value, type = wrg.EnumValue(hKey, count) print(name, value, type) count = count + 1 except WindowsError as err: print(err) wrg.CloseKey(hKey) pass #---------------- #set_reg_env("aaa","koi") #---------------- #perform a WM_SETTINGCHANGE broadcast which eliminates the need to reboot the system. def call_at_end(): setxCommand = "SETX /M USERNAME %USERNAME%" print(setxCommand) subprocess.check_output(setxCommand, shell=True) #----------------------------- rough set path in regex. okay. but what abt other vars, setting them.. new env vars. firstly checking id they exist in the regex. also getting all env vars from regex. # HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment Path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Git\cmd;C:\Program Files\PuTTY\ Name, Type, Data REG_EXPAND_SZ SETX /M USERNAME %USERNAME% # now, create key how and update key (path) how. (need to test this) # reqd: permissions: set the user to have permissions(full control) over environment key. # **********take a backup of the registry editor and know how to restore it. ** before modifying the path. #
Leave a Comment