Bio Student, programmer, Systems Controls Consultant
Things I enjoy:
This is the first result when you google “Generic Stock Photo.”
My sample script is a changelog updater. I wrote it because doing homework is boring. The script will generate or update a changelog based on a list of programs obtained through powershell. This only works for windows computers.
from datetime import date
import subprocess, sys
import argparse
import os
import re
#Stackoverflow code to change from utf16 to utf8. WMIC generates utf16 for installed programs list,
#which doesnt work for what I'm trying to do.
def correctSubtitleEncoding(filename, newFilename, encoding_from='UTF-16', encoding_to='UTF-8'):
with open(filename, 'r', encoding=encoding_from) as fr:
with open(newFilename, 'w', encoding=encoding_to) as fw:
for line in fr:
-1]+'\r')
fw.write(line[:
def updateOld(old, oldLines, new, newLines):
#close and reopen in write mode to replace file contents.
old.close()= open("oldchangelog.txt", "w")
old for i in range(len(newLines)):
old.write(newLines[i])
#Begin Program by parsing args. Should only be help.
= argparse.ArgumentParser(description = "Updates Changelog. Open the script and change the path to the powershell file to match your computer's requirements \n You will need to do the same in the .ps1 file.")
cmdarg = cmdarg.parse_args()
args
#get the local directory for use in this program.
= os.getcwd()
dir_path += r"\p.ps1"
dir_path = repr(dir_path)
dir_path
#For log
= os.getcwd()
pslogPath += r"\PSlog.txt"
pslogPath = repr(pslogPath)
pslogPath
#create a powershell script in the current directory
= open("p.ps1", "w")
powershellScript #populate that file with the command to get the list of programs
"Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion | Format-Table -AutoSize > " + pslogPath)
powershellScript.write(#finished with the file, and we can't keep it open now or it will be in use when we try to execute it.
powershellScript.close()
#Get path to the above script
= os.getcwd()
psScriptpath += r'\p.ps1'
psScriptpath
#First generate a new up to date list of programs.
#"C:\\Users\\elkel\\Desktop\\UtilScript\\p.ps1"
= subprocess.Popen(["powershell.exe",
p
psScriptpath],=sys.stdout)
stdout
p.communicate()
#Since we should be running this after the installed programs list generator,
#and that should generate a file with the date, this will access that file.
#Then the others are easy enough to get.
= open("oldchangelog.txt", "r+")
old "PSlog.txt", "new.txt")
correctSubtitleEncoding(= open("new.txt", "r+")
new = open("changelog.txt", "a")
log
#Get the list of lines for comparison
= old.readlines()
oldLines = new.readlines()
newLines
#For first time runs, we should fill the changelog with all of the contents from the newLines list.
if len(oldLines) == 0:
"Begin Tracking " + str(date.today()))
log.write(for i in range(len(newLines)):
log.write(newLines[i])
updateOld(old, oldLines, new, newLines)
exit()
#The magic area. Goes through and checks every line, stripping all space characters and comparing them.
#If spaces are not removed, the comparison fails.
"\n\nUPDATES AS OF " + str(date.today()) + ":")
log.write(
= False
anyChanges
for i in range(len(newLines)):
if newLines[i].replace(' ', '') != oldLines[i].replace(' ', ''):
= True
anyChanges #Check to see if this is an entirely new program or just an updated version of an old one.
#If it is new, put it in the changelog as such and skip to the next line
for j in range(3):
if newLines[i][j] != oldLines[i][j]:
print("Here I am")
"\nNEW PROGRAM: " + newLines[i])
log.write(del(newLines[i])
break
print("NOT SAME, updating")
print(newLines[i])
print(oldLines[i])
#write to changelog
"\n" + newLines[i])
log.write(#Change the line that represents an update
= newLines[i]
oldLines[i]
if anyChanges == False:
"\n No Changes")
log.write(
updateOld(old, oldLines, new, newLines)