apt-plus/purge

56 lines
1.3 KiB
Plaintext
Raw Normal View History

2017-01-01 18:17:29 +00:00
#!/bin/py
import re;
from os import system as ossystem;
from os import path as ospath;
from sys import argv as sysargv;
# [1] get absolute path
path = ospath.dirname(ospath.realpath(__file__));
# [2] get packages
packages = sysargv[1:];
# [3] search for manifest file
manifest="";
with open( "%s/manifest" % path, 'r' ) as f:
manifest=f.readline();
# [4] search for installed entries in manifest
alrdy = [];
with open(manifest, 'r+') as f:
for line in f:
m = re.search(r"\s*\[x\] ([a-zA-Z0-9_-]+)", line);
if ( m != None ):
alrdy.append( m.group(1) ) ;
# [5] Purge packages
purged = [];
for pkg in packages:
print "\033[38;5;208m[purge](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg);
exitcode = ossystem("sudo apt-get purge %s" % pkg);
# if all ran successfully
if ( exitcode == 0 ):
print "\033[38;5;78msuccess\033[0m";
purged.append( pkg );
else:
print "\033[38;5;161merror\033[0m";
# [6] update manifest
for pkg in purged:
print "\033[38;5;39m[notifying](%d/%d) %s\033[0m" % (purged.index(pkg)+1, len(purged), pkg);
# if new package
if( not pkg in alrdy ):
with open(manifest, "a") as f:
f.write(" [ ] %s\n" % pkg);
else:
with open(manifest, 'r') as reader:
replaced = reader.read().replace("[x] %s" % pkg, "[ ] %s" % pkg);
with open(manifest, 'w') as writer:
writer.write( replaced );