apt-plus/command/remove

57 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env python
2017-01-01 18:17:29 +00:00
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.dirname(ospath.realpath(__file__)));
2017-01-01 18:17:29 +00:00
# [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 = [];
findAlrdy = re.compile("^\s*\[x\]\s(?:([a-zA-Z0-9_-]+)(?: \[deb\])?).*$", 0);
2017-01-01 18:17:29 +00:00
with open(manifest, 'r+') as f:
for line in f:
m = findAlrdy.search(line);
2017-01-01 18:17:29 +00:00
if ( m != None ):
alrdy.append( m.group(1) ) ;
# [5] Remove packages
removed = [];
for pkg in packages:
print "\033[38;5;208m[remove](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg);
exitcode = ossystem("sudo apt-get remove %s" % pkg);
2017-01-01 18:17:29 +00:00
# if all ran successfully
if ( exitcode == 0 ):
print "\033[38;5;78msuccess\033[0m";
removed.append( pkg );
else:
print "\033[38;5;161merror\033[0m";
# [6] update manifest
for pkg in removed:
print "\033[38;5;39m[notifying](%d/%d) %s\033[0m" % (removed.index(pkg)+1, len(removed), 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).replace("[x] %s [deb]" % pkg, "[.] %s [deb]" % pkg);
2017-01-01 18:17:29 +00:00
with open(manifest, 'w') as writer:
writer.write( replaced );