Skip to content

Instantly share code, notes, and snippets.

@garretraziel
Created September 12, 2010 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garretraziel/576423 to your computer and use it in GitHub Desktop.
Save garretraziel/576423 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys, string
def analyze_log(path):
logfile = open(path,"r")
cont = logfile.readlines()
logfile.close()
cont = parse_log(cont)
toinstall = {}
while cont:
endpos = cont.index("End")
cut = cont[1:endpos]
toinstall[cont[0]] = cut
del cont[:endpos+1]
toinstall = dict([(key, toinstall[key]) for key in toinstall if toinstall[key]])
return toinstall
def parse_log(cont):
cont_new = []
for x in cont:
if x[:5] == "Start":
cont_new.append(string.strip(x))
elif x[:3] == "End":
cont_new.append("End")
elif x[:7] == "Install":
progs = string.split(x," ")
for prog in progs:
if prog != "Install:" and prog[0] != '(' and prog[0] not in '0123456789':
cont_new.append(prog)
elif x[:7] == "Upgrade":
pass
elif x[:6] == "Remove" or x[:5] == "Purge":
progs = string.split(x," ")
for prog in progs:
while cont_new.count(prog):
cont_new.remove(prog)
return cont_new
def create_script(path,progs):
script = open(path,"w")
script.write("#! /bin/bash\n\n")
script.write("# autocreated installation file with movescript.py\n")
script.write("# comment files you don't want to install with sharp\n\n")
for key in progs:
script.write("# "+key+"\n")
script.write("apt-get install "+string.join(progs[key],' ')+"\n")
script.close()
def main(args):
log = "/var/log/apt/history.log"
if len(args) == 1:
log = args[1]
out = args[2]
else:
out = args[1]
progs = analyze_log(log)
create_script(out,progs)
#print "Script created!"
if __name__ == "__main__":
if len(sys.argv) == 1:
print "run as 'python movescript.py [infile] outfile'"
else: main(sys.argv)
@garretraziel
Copy link
Author

Use this script on debian based systems for backuping installed programs for e. g. system reinstalling etc. This script analyzes logfile of apt and creates output script that installs same programs that are in log.

@v6ak
Copy link

v6ak commented Sep 13, 2010

So it is similar to pacman -Qqe on Arch Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment