Compare commits

...

3 Commits
v2 ... master

Author SHA1 Message Date
xdrm-brackets b62257c779 Added a README 2017-10-08 13:01:39 +02:00
xdrm-brackets 4f719c9d7c simplified group spec 2017-10-08 12:44:00 +02:00
xdrm-brackets 3d6c59d8e5 [BIGUPDATE] now works with groups 2017-10-06 14:29:09 +02:00
14 changed files with 808 additions and 248 deletions

94
README.md Normal file
View File

@ -0,0 +1,94 @@
# apt-plus
Linux `apt-get` wrapper which lets you store installed/removed/purged packages.
### Install
> ##### (1) Launch the install script
```bash
bash ./install_apt-plus.sh;
```
> ##### (2) Create a _manifest_ file
```bash
touch ~/.apt-plus;
```
> ##### (3) Give apt-plus the manifest file path
```bash
apt-plus -m ~/.apt-plus;
```
### Commands
#### [1] Fetches packages from remote
Strict equivalent to `apt-get update`.
> `apt-plus -f`
> `apt-plus --fetch`
#### [2] Update your system packages
It first fetches the packages from remote, then upgrades your packages, your kernel and finally removes useless data.
> `apt-plus -u`
> `apt-plus --update`
#### [3] Install one or multiple packages
It installs the given packages and store it in the _manifest_.
Package names must be separated by spaces.
> `apt-plus -i <packages>`
> `apt-plus --install <packages>`
You can specify a group for packages with the following syntax:
> `apt-plus :group_name: -i <packages>`
> `apt-plus :group_name: --install <packages>`
#### [4] Install from a .deb file
It installs the given package and store it in the _manifest_.
The path does not need to be absolute;
> `apt-plus -d <file>`
> `apt-plus --deb <file>`
You can specify a group for the package with the following syntax:
> `apt-plus :group_name: -d <file>`
> `apt-plus :group_name: --deb <file>`
#### [5] Remove one or multiple packages
It removes the given packages and store it in the _manifest_.
Package names must be separated by spaces.
In opposite of the **purge** command, it keeps the configuration files.
> `apt-plus -r <packages>`
> `apt-plus --remove <packages>`
You can specify a group for packages with the following syntax:
> `apt-plus :group_name: -r <packages>`
> `apt-plus :group_name: --remove <packages>`
#### [6] Purge one or multiple packages
It purges the given packages and store it in the _manifest_.
Package names must be separated by spaces.
In opposite of the **remove** command, it does not keep the configuration files.
> `apt-plus -p <packages>`
> `apt-plus --purge <packages>`
You can specify a group for packages with the following syntax:
> `apt-plus :group_name: -p <packages>`
> `apt-plus :group_name: --purge <packages>`
#### [7] List stored packages
It shows you the list of stored packages (managed with apt-plus).
> `apt-plus -l`
> `apt-plus --list`
You can specify a group to show only with the syntax:
> `apt-plus :group_name: -l`
> `apt-plus :group_name: --list`

81
command/addppa Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env python
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__)));
# [2] get @ppa_link argument
#========================================================#
ppa_link = sysargv[1];
# [3] search for manifest file
#========================================================#
manifest="";
with open( "%s/manifest" % path, 'r' ) as f:
manifest=f.readline();
# [4] search for already installed entries in manifest
#========================================================#
alrdy = [];
findPkg = re.compile("^\s*\[[\. x]\]\s([a-zA-Z0-9_ \[\]-]+)$", 0);
with open(manifest, 'r+') as f:
for line in f:
m = findPkg.search(line);
if ( m != None ):
alrdy.append( m.group(1) ) ;
# [5] Install the ppa_link
#========================================================#
pkgName = "";
# (1) Try to install #
print "\033[38;5;208m[deb-install] %s\033[0m" % (ppa_link);
exitcode = ossystem("sudo dpkg -i %s > %s/stdout" % (ppa_link, path));
# (2) Read local stdout copy of stdout #
findName = re.compile("^Unpacking ([a-zA-Z0-9_-]+)", re.MULTILINE)
with open( "%s/stdout" % path, 'r' ) as f:
line = f.read();
print line;
m = findName.search(line);
if ( m != None ):
pkgName = "%s [deb]" % m.group(1);
# (3) On success and notice user #
if ( exitcode == 0 and len(pkgName) > 0 ):
print "\033[38;5;78msuccess\033[0m";
# (4) On failure notice user -> abort #
else:
print "\033[38;5;161merror\033[0m";
exit(1);
# [6] Update manifest
#========================================================#
print "\033[38;5;39m[notifying] %s\033[0m" % (pkgName);
# if new package
if( not pkgName in alrdy ):
with open(manifest, "a") as f:
f.write(" [x] %s\n" % pkgName);
else:
with open(manifest, 'r') as reader:
replaced = reader.read().replace("[ ] %s" % pkgName, "[x] %s" % pkgName).replace("[.] %s" % pkgName, "[x] %s" % pkgName);
with open(manifest, 'w') as writer:
writer.write( replaced );

View File

@ -3,7 +3,9 @@
import re;
from os import system as ossystem;
from os import path as ospath;
from sys import argv as sysargv;
import sys;
from parser import Manifest, Package;
# [1] get absolute path
#========================================================#
@ -12,28 +14,23 @@ path = ospath.dirname(ospath.dirname(ospath.realpath(__file__)));
# [2] get @deb_file argument
#========================================================#
deb_file = sysargv[1];
# [2] get group option (if set)
OPTGROUP = None;
ARGS = sys.argv;
if( len(sys.argv) > 2 and ( sys.argv[1] == "-g" or sys.argv[1] == "--group" ) ):
OPTGROUP = sys.argv[2];
ARGS = ARGS[0:1] + ARGS[3:];
deb_file = ARGS[1];
# [3] search for manifest file
#========================================================#
manifest="";
with open( "%s/manifest" % path, 'r' ) as f:
manifest=f.readline();
man = Manifest.Manifest();
# [4] search for already installed entries in manifest
#========================================================#
alrdy = [];
findPkg = re.compile("^\s*\[[\. x]\]\s([a-zA-Z0-9_ \[\]-]+)$", 0);
with open(manifest, 'r+') as f:
for line in f:
m = findPkg.search(line);
if ( m != None ):
alrdy.append( m.group(1) ) ;
# [5] Install the deb_file
#========================================================#
pkgName = "";
@ -50,7 +47,7 @@ with open( "%s/stdout" % path, 'r' ) as f:
m = findName.search(line);
if ( m != None ):
pkgName = "%s [deb]" % m.group(1);
pkgName = m.group(1);
# (3) On success and notice user #
@ -69,13 +66,21 @@ else:
#========================================================#
print "\033[38;5;39m[notifying] %s\033[0m" % (pkgName);
# if new package
if( not pkgName in alrdy ):
with open(manifest, "a") as f:
f.write(" [x] %s\n" % pkgName);
else:
with open(manifest, 'r') as reader:
replaced = reader.read().replace("[ ] %s" % pkgName, "[x] %s" % pkgName).replace("[.] %s" % pkgName, "[x] %s" % pkgName);
# if existing package update it
if( man.hasPackage(pkgName) ):
pkgReg = man.getPackage(pkgName);
if( OPTGROUP != None ):
man.mvPackage(pkgName, OPTGROUP);
pkgReg._deb = True;
pkgReg.install();
# else create package
else:
pkgNew = Package.Package(pkgName, 'x', deb=True);
if( OPTGROUP != None ):
man.addPackage(pkgNew, OPTGROUP);
else:
man.addPackage(pkgNew);
man.save();
with open(manifest, 'w') as writer:
writer.write( replaced );

View File

@ -3,27 +3,27 @@
import re;
from os import system as ossystem;
from os import path as ospath;
from sys import argv as sysargv;
import sys;
from parser import Manifest, Package;
# [1] get absolute path
path = ospath.dirname(ospath.dirname(ospath.realpath(__file__)));
# [2] get packages
packages = sysargv[1:];
# [2] get group option (if set)
OPTGROUP = None;
ARGS = sys.argv;
if( len(sys.argv) > 2 and ( sys.argv[1] == "-g" or sys.argv[1] == "--group" ) ):
OPTGROUP = sys.argv[2];
ARGS = ARGS[0:1] + ARGS[3:];
# [3] get packages
packages = ARGS[1:];
# [3] search for manifest file
manifest="";
with open( "%s/manifest" % path, 'r' ) as f:
manifest=f.readline();
man = Manifest.Manifest();
# [4] search for non-installed entries in manifest
alrdy = [];
findAlrdy = re.compile("^\s*\[[\. x]\]\s([a-zA-Z0-9_ \[\]-]+).*$", 0);
with open(manifest, 'r+') as f:
for line in f:
m = findAlrdy.search(line);
if ( m != None ):
alrdy.append( m.group(1) ) ;
# [5] Install packages
installed = [];
@ -43,13 +43,19 @@ for pkg in packages:
for pkg in installed:
print "\033[38;5;39m[notifying](%d/%d) %s\033[0m" % (installed.index(pkg)+1, len(installed), pkg);
# if new package
if( not pkg in alrdy ):
with open(manifest, "a") as f:
f.write(" [x] %s\n" % pkg);
else:
with open(manifest, 'r') as reader:
replaced = reader.read().replace("[ ] %s" % pkg, "[x] %s" % pkg).replace("[.] %s" % pkg, "[x] %s" % pkg);
# if existing package update it
if( man.hasPackage(pkg) ):
pkgReg = man.getPackage(pkg);
pkgReg.install();
if( OPTGROUP != None ):
man.mvPackage(pkg, OPTGROUP);
with open(manifest, 'w') as writer:
writer.write( replaced );
# else create package
else:
pkgNew = Package.Package(pkg, 'x');
if( OPTGROUP != None ):
man.addPackage(pkgNew, OPTGROUP);
else:
man.addPackage(pkgNew);
man.save();

View File

@ -3,39 +3,51 @@
import re;
from os import system as ossystem;
from os import path as ospath;
from sys import argv as sysargv;
import sys;
from parser import Manifest, Package;
# [1] get absolute path
path = ospath.dirname(ospath.dirname(ospath.realpath(__file__)));
# [2] get group option (if set)
OPTGROUP = None;
if( len(sys.argv) > 2 and ( sys.argv[1] == "-g" or sys.argv[1] == "--group" ) ):
OPTGROUP = sys.argv[2];
# [3] search for manifest file
manifest="";
with open( "%s/manifest" % path, 'r' ) as f:
manifest=f.readline();
man = Manifest.Manifest();
# [4] search for installed entries in manifest
pkgRegex = re.compile("^\s*\[(x| |\.)\]\s+(?:([a-zA-Z0-9_-]+)( \[deb\])?).*$", 0);
with open(manifest, 'r+') as f:
for line in f:
m = pkgRegex.search(line);
# [4] If group selected
if( OPTGROUP != None ):
if ( m != None ):
if( not man.hasGroup(OPTGROUP) ):
print "Unknown group";
exit(1);
# (1) Installed #
if m.group(1) == 'x':
print "\033[38;5;78m%s\033[0m" % (m.group(2)),
print "[%s]" % OPTGROUP;
for pkg_name in man._pkg[OPTGROUP]:
pkg = man._pkg[OPTGROUP][pkg_name];
if( pkg.isInstalled() ):
print "\033[38;5;78m",;
elif( pkg.isRemoved() ):
print "\033[38;5;208m",;
elif( pkg.isPurged() ):
print "\033[38;5;161m",;
# (2) Removed (not config) #
elif m.group(1) == '.':
print "\033[38;5;208m%s\033[0m" % (m.group(2)),
print "%s\033[0m" % pkg.serialize();
# (3) purged #
elif m.group(1) == ' ':
print "\033[38;5;161m%s\033[0m" % (m.group(2)),
# [4] List all
else:
for group in man._pkg:
print "[%s]" % group;
for pkg_name in man._pkg[group]:
pkg = man._pkg[group][pkg_name];
if( pkg.isInstalled() ):
print "\033[38;5;78m",;
elif( pkg.isRemoved() ):
print "\033[38;5;208m",;
elif( pkg.isPurged() ):
print "\033[38;5;161m",;
# (4) If from deb #
if m.group(3) != None:
print "\033[0;49;34m[deb]\033[0m";
else:
print;
print "%s\033[0m" % pkg.serialize();

290
command/parser/Manifest.py Normal file
View File

@ -0,0 +1,290 @@
#!/usr/bin/env python
import re;
from os import path as ospath;
from Package import Package;
# (1) Initialisation
#--------------------------------------------------------#
# (1) Get absolute path #
ROOT = ospath.dirname( ospath.dirname(ospath.dirname(ospath.realpath(__file__))) );
# (2) Class declaration
#--------------------------------------------------------#
class Manifest:
_grp_re = r'^\[-\] (?P<name>.+)$';
_pkg_re = r'^(?P<pad>\s+)\[(?P<sta>[ x\.])\] (?P<name>[\w\.-]+)(?:\s*(?P<deb>\[deb\])|(?:\s*\[(?P<ppa>[\w\.-]+\/[\w\.-]+)\]))?(?:\s*# (?P<com>.+))?\s*$';
# (1) Constructor
#
# @file<String> Manifest file
#
#--------------------------------------------------------#
def __init__(self):
# (1) Try to read manifest path #
self._path = None;
with open( "%s/manifest" % ROOT, 'r' ) as f:
self._path = f.readline();
# (2) Manage error #
if( self._path == None ):
raise Exception('No manifest file given');
# (3) Initialize attributes #
self._grp = [];
self._pkg = {};
# (4) Parse the manifest file #
self._parse();
# (2) Parser method
#
#--------------------------------------------------------#
def _parse(self):
# (0) Init
#--------------------------------------------------------#
# (1) Create group matcher #
grpMatcher = re.compile(self._grp_re);
# (2) Create pkg matches #
pkgMatcher = re.compile(self._pkg_re);
# (3) Local variables #
grp = False;
grp_name = None;
# (2) Search for groups
#--------------------------------------------------------#
with open( "%s" % self._path, 'r' ) as f:
for l,line in enumerate(f):
# (1) If seeking for group #
if( grp == False ):
## {1} Check regex with the line ##
match = grpMatcher.search(line);
## {2} If don't match -> pass ##
if( match == None ):
continue;
## {3} Register group ##
grp = True;
grp_name = match.group('name');
self._grp.append( grp_name );
self._pkg[grp_name] = {};
# (2) If seeking for group's packages #
else:
## {1} Check regex with the line ##
match = pkgMatcher.search(line);
## {2} If don't match -> back to group seeking ##
if( match == None ):
grp = False;
continue;
## {3} Register package ##
pkg = Package(
name = match.group('name'),
sta = match.group('sta'),
pad = len(match.group('pad')),
deb = match.group('deb') != None,
ppa = match.group('ppa') if match.group('ppa') != None else None,
com = match.group('com') if match.group('com') != None else None
);
self._pkg[grp_name][match.group('name')] = pkg;
# (3) Return if a group exists
#
# @name<String> Group name
#
# @return exists<bool> Whether the group exists
#
#--------------------------------------------------------#
def hasGroup(self, name):
return name in self._grp;
# (4) Creates a group if it does not exist
#
# @name<String> Group name
#
# @return created<id> Created id
#
#--------------------------------------------------------#
def createGroup(self, name):
# (1) Error if already exists #
if( self.hasGroup(name) ):
return None;
# (2) Create group #
self._grp.append(name);
self._pkg[name] = [];
# (3) Return status #
for g,grp in enumerate(self._grp):
if( grp == name ):
return g;
return None;
# (5) Get a group's id
#
# @name<String> Group name
#
# @return id<int> Group id (None on error)
#
#--------------------------------------------------------#
def getGroup(self, name):
# (1) Error if doesn't exist #
if( not self.hasGroup(name) ):
return None;
# (2) Return id #
for g,grp in enumerate(self._grp):
if( grp == name ):
return g;
return None;
# (6) Check if the package exists
#
# @name<String> Package name
#
# @return exists<bool> Whether the package exists
#
#--------------------------------------------------------#
def hasPackage(self, name):
for grp in self._pkg:
for pkg in self._pkg[grp]:
if( pkg == name ):
return True;
return False;
# (7) Get a package by name
#
# @name<String> Package name
#
#--------------------------------------------------------#
def getPackage(self, name):
for grp in self._pkg:
for pkg in self._pkg[grp]:
if( pkg == name ):
return self._pkg[grp][pkg];
return None;
# (8) Adds a new package to a group
#
# @package<Package> Package object
# @group<String> Group name
#
# @return id<(int,int)> (id_group, id_pkg) of the created package (None on error)
#
#--------------------------------------------------------#
def addPackage(self, pkg, group="misc"):
# (1) Check pkg type #
if( not isinstance(pkg, Package) ):
return None;
# (2) Error if already exists #
if( self.hasPackage(pkg._name) ):
return None;
# (3) If missing group -> fail #
if( not self.hasGroup(group) ):
return None;
# (4) Create package #
self._pkg[group][ pkg.getName() ] = pkg;
# (4) Return status #
return True;
# (8) Move a package to another group
#
# @package<Package> Package name
# @group<String> Group name
#
# @return moved<bool> Whether it have been moved
#
#--------------------------------------------------------#
def mvPackage(self, pkg_name, group="misc"):
# (1) Error if not exists #
if( not self.hasPackage(pkg_name) ):
return False;
# (2) If missing group -> fail #
if( not self.hasGroup(group) ):
return False;
# (3) Get package #
pkgRef = self.getPackage(pkg_name);
# (4) Copy package to new group #
self._pkg[group][ pkg_name ] = pkgRef;
# (5) Remove package from old group #
for grp in self._pkg:
if( grp == group ):
continue;
for pkg in self._pkg[grp]:
if( pkg == pkg_name ):
del self._pkg[grp][pkg_name];
break;
# (4) Return status #
return True;
# (9) Save the current manifest
#
#--------------------------------------------------------#
def save(self):
with open( "%s" % self._path, 'w' ) as f:
for grp in self._pkg:
f.write("[-] %s\n" % grp );
for pkg in self._pkg[grp]:
f.write("%s\n" % self._pkg[grp][pkg].serialize());
f.write("\n");

109
command/parser/Package.py Normal file
View File

@ -0,0 +1,109 @@
#!/usr/bin/env python
class Package:
_com_pad = 40;
# (1) Constructs a new package
#
# @name<String> Package name
# @state<char> Package state ('x', ' ', '.')
# @com<String> Package comm (default: None)
# @ppa<(String, String)> Package ppa (default: None)
# @deb<Path> Path to local deb file (if deb) (default: None)
# @pad<int> Number of pad spaces (default: 2)
#
#--------------------------------------------------------#
def __init__(self, name=None, sta=None, com=None, ppa=None, deb=False, pad=2):
# (1) Arguments check
#--------------------------------------------------------#
# (1) Is name set #
if( name == None ):
raise Exception('Missing argument "name" !');
# (2) deb set but not a bool #
if( not isinstance(deb, bool) ):
raise Exception('Argument "deb" must be a boolean !');
# (3) Check pad is (+) int #
try:
pad = int(pad);
if( pad < 0 ):
raise Exception('Argument "pad" must be a positive integer !');
except ValueError:
raise Exception('Argument "pad" must be a positive integer !');
# (4) Check state #
if( sta != 'x' and sta != '.' and sta != ' ' ):
raise Exception('Argument "sta" is invalid or missing !');
# (2) Set attributes
#--------------------------------------------------------#
self._name = name;
self._sta = sta;
self._com = com;
self._ppa = "%s/%s" % (ppa[0], ppa[1]) if ppa != None else None;
self._deb = deb;
self._pad = pad;
# (2) Getters
def getName(self):
return self._name;
def getDeb(self):
return self._deb;
def getPpa(self):
return self._ppa;
def getPad(self):
return self._pad;
def getCom(self):
return self._com;
def isInstalled(self):
return self._sta == 'x';
def isRemoved(self):
return self._sta == '.';
def isPurged(self):
return self._sta == ' ';
def install(self):
self._sta = 'x';
def remove(self):
self._sta = '.';
def purge(self):
self._sta = ' ';
# (3) Serialize
#
#--------------------------------------------------------#
def serialize(self):
raw = '%s[%s] %s' % ( ' '*self._pad, self._sta, self._name);
if( self._ppa != None ):
raw += ' [%s]' % self._ppa;
elif( self._deb ):
raw += ' [deb]';
if( self._com != None ):
raw += ' ' * (self._com_pad - len(raw));
raw += '# %s' % self._com;
return raw;

View File

@ -3,31 +3,28 @@
import re;
from os import system as ossystem;
from os import path as ospath;
from sys import argv as sysargv;
import sys;
from parser import Manifest, Package;
# [1] get absolute path
path = ospath.dirname(ospath.dirname(ospath.realpath(__file__)));
# [2] get group option (if set)
OPTGROUP = None;
ARGS = sys.argv;
if( len(sys.argv) > 2 and ( sys.argv[1] == "-g" or sys.argv[1] == "--group" ) ):
OPTGROUP = sys.argv[2];
ARGS = ARGS[0:1] + ARGS[3:];
# [2] get packages
packages = sysargv[1:];
packages = ARGS[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);
with open(manifest, 'r+') as f:
for line in f:
m = findAlrdy.search(line);
if ( m != None ):
alrdy.append( m.group(1) ) ;
man = Manifest.Manifest();
# [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);
@ -44,13 +41,20 @@ for pkg in packages:
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).replace("[x] %s [deb]" % pkg, "[ ] %s [deb]" % pkg);
# if existing package update it
if( man.hasPackage(pkg) ):
pkgReg = man.getPackage(pkg);
pkgReg.purge();
if( OPTGROUP != None ):
man.mvPackage(pkg, OPTGROUP);
with open(manifest, 'w') as writer:
writer.write( replaced );
# else create package
else:
pkgNew = Package.Package(pkg, ' ');
if( OPTGROUP != None ):
man.addPackage(pkgNew, OPTGROUP);
else:
man.addPackage(pkgNew);
man.save();

View File

@ -3,31 +3,29 @@
import re;
from os import system as ossystem;
from os import path as ospath;
from sys import argv as sysargv;
import sys;
from parser import Manifest, Package;
# [1] get absolute path
path = ospath.dirname(ospath.dirname(ospath.realpath(__file__)));
# [2] get group option (if set)
OPTGROUP = None;
ARGS = sys.argv;
if( len(sys.argv) > 2 and ( sys.argv[1] == "-g" or sys.argv[1] == "--group" ) ):
OPTGROUP = sys.argv[2];
ARGS = ARGS[0:1] + ARGS[3:];
# [2] get packages
packages = sysargv[1:];
packages = ARGS[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);
with open(manifest, 'r+') as f:
for line in f:
m = findAlrdy.search(line);
if ( m != None ):
alrdy.append( m.group(1) ) ;
man = Manifest.Manifest();
# [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);
@ -44,13 +42,20 @@ for pkg in packages:
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);
# if existing package update it
if( man.hasPackage(pkg) ):
pkgReg = man.getPackage(pkg);
pkgReg.remove();
if( OPTGROUP != None ):
man.mvPackage(pkg, OPTGROUP);
with open(manifest, 'w') as writer:
writer.write( replaced );
# else create package
else:
pkgNew = Package.Package(pkg, '.');
man.addPackage(pkgNew);
if( OPTGROUP != None ):
man.addPackage(pkgNew, OPTGROUP);
else:
man.addPackage(pkgNew);
man.save();

81
main
View File

@ -7,6 +7,9 @@
from os import path as ospath;
from os import system as ossystem;
import sys;
import re;
grpMatcher = re.compile(r'^:(.+):$');
# get absolute path
@ -20,6 +23,10 @@ def showhelp():
print;
print "\033[1mSYNOPSIS\033[0m"
print "\tapt-plus <command> <packages>";
print "\tapt-plus :<group>: <command> <packages>";
print;
print "\t<group>";
print "\t\tProccess actions within a specific group (applies to: install, purge, remove, list, deb install).";
print;
print "\033[1mDESCRIPTION\033[0m"
print ""
@ -57,14 +64,28 @@ def showhelp():
print "\t-u, --update";
print "\t\tUpgrades packages and kernel and cleans after";
print;
print "\t-a, --add-ppa <ppa_link>";
print "\t\tAdds the <ppa_link> repository to dpkg then update it";
print;
# [2] Command dispatch (if at least 1 arg)
#========================================================#
if ( len(sys.argv) > 1 ):
# (1) Manage optional -g --group modifier
#--------------------------------------------------------#
OPTGROUP = None;
ARGS = sys.argv;
match = grpMatcher.search(sys.argv[1]);
if( len(sys.argv) > 2 and match != None ):
OPTGROUP = match.group(1);
ARGS = ARGS[0:1] + ARGS[2:];
# (2) Normal arguments
#--------------------------------------------------------#
# (1) Get the command argument (first) #
command = sys.argv[1];
command = ARGS[1];
# (1) --update, -u
#--------------------------------------------------------#
@ -83,58 +104,71 @@ if ( len(sys.argv) > 1 ):
elif ( command == "-d" or command == "--deb" ):
# if package/s given
if ( len(sys.argv) < 3 ):
if ( len(ARGS) < 3 ):
sys.exit("\n(!) Missing argument <deb_file>");
ossystem("/usr/bin/env python %s/command/debinstall %s" % (path, sys.argv[2]) );
if( OPTGROUP != None ):
ossystem("/usr/bin/env python %s/command/debinstall -g %s %s" % (path, OPTGROUP, ARGS[2]) );
else:
ossystem("/usr/bin/env python %s/command/debinstall %s" % (path, ARGS[2]) );
# (4) --install, -i
#--------------------------------------------------------#
elif ( command == "-i" or command == "--install" ):
# if package/s given
if ( len(sys.argv) < 3 ):
if ( len(ARGS) < 3 ):
sys.exit("\n(!) Missing argument <packages>");
packages = " ".join(sys.argv[2:]);
packages = " ".join(ARGS[2:]);
ossystem("/usr/bin/env python %s/command/install %s" % (path, packages) );
if( OPTGROUP != None ):
ossystem("/usr/bin/env python %s/command/install -g %s %s" % (path, OPTGROUP, packages) );
else:
ossystem("/usr/bin/env python %s/command/install %s" % (path, packages) );
# (5) --remove, -r
#--------------------------------------------------------#
elif ( command == "-r" or command == "--remove" ):
# if package/s given
if ( len(sys.argv) < 3 ):
if ( len(ARGS) < 3 ):
sys.exit("\n(!) Missing argument <packages>");
packages = " ".join(sys.argv[2:]);
ossystem("/usr/bin/env python %s/command/remove %s" % (path, packages) );
packages = " ".join(ARGS[2:]);
if( OPTGROUP != None ):
ossystem("/usr/bin/env python %s/command/remove -g %s %s" % (path, OPTGROUP, packages) );
else:
ossystem("/usr/bin/env python %s/command/remove %s" % (path, packages) );
# (6) --purge, -p
#--------------------------------------------------------#
elif ( command == "-p" or command == "--purge" ):
# if package/s given
if ( len(sys.argv) < 3 ):
if ( len(ARGS) < 3 ):
sys.exit("\n(!) Missing argument <packages>");
packages = " ".join(sys.argv[2:]);
ossystem("/usr/bin/env python %s/command/purge %s" % (path, packages) );
packages = " ".join(ARGS[2:]);
if( OPTGROUP != None ):
ossystem("/usr/bin/env python %s/command/purge -g %s %s" % (path, OPTGROUP, packages) );
else:
ossystem("/usr/bin/env python %s/command/purge %s" % (path, packages) );
# (7) --manifest, -m
#--------------------------------------------------------#
elif ( command == "-m" or command == "--manifest" ):
# (1) If argument missing -> abort #
if ( len(sys.argv) < 3 ):
if ( len(ARGS) < 3 ):
sys.exit("\n(!) Missing argument <manifest_file>");
# (2) If argument is not a valid file -> abort #
elif ( not ospath.isfile(sys.argv[2]) ):
elif ( not ospath.isfile(ARGS[2]) ):
sys.exit("Given path isn't a file");
# (3) Store absolute path in ./manifest file #
file = ospath.realpath(sys.argv[2]);
file = ospath.realpath(ARGS[2]);
with open( ("%s/manifest" % path), "w") as f:
f.write(file);
@ -142,9 +176,22 @@ if ( len(sys.argv) > 1 ):
#--------------------------------------------------------#
elif ( command == "-l" or command == "--list" ):
ossystem("/usr/bin/env python %s/command/list" % path );
if( OPTGROUP != None ):
ossystem("/usr/bin/env python %s/command/list -g %s" % (path, OPTGROUP) );
else:
ossystem("/usr/bin/env python %s/command/list" % path );
# (9) If no match -> show help message
# (9) --add-ppa, -a
#--------------------------------------------------------#
elif ( command == "-a" or command == "--add-ppa" ):
# if ppa is given
if ( len(ARGS) < 3 ):
sys.exit("\n(!) Missing argument <ppa_link>");
ossystem("/usr/bin/env python %s/command/addppa %s" % (path, ARGS[2]) );
# (10) If no match -> show help message
#--------------------------------------------------------#
else:
showhelp();

View File

@ -1 +1 @@
/home/xdrm-brackets/.x-migration
/home/xdrm-brackets/.apt-plus

View File

@ -1,108 +0,0 @@
#!/usr/bin/env python
import re;
from os import path as ospath;
# (1) Initialisation
#--------------------------------------------------------#
# (1) Get absolute path #
ROOT = ospath.dirname(ospath.dirname(ospath.realpath(__file__)));
# (2) Class declaration
#--------------------------------------------------------#
class Class:
_grp_re = r'^\[-\] (?P<name>.+)$';
_pkg_re = r'^(?P<pad>\s+)\[[ x\.]\] (?P<name>[\w\.-]+)(?:\s*(?P<deb>\[deb\])|(?:\s*\[(?P<ppa>[\w\.-]+\/[\w\.-]+)\]))?(?:\s*# (?P<com>.+))?\s*$';
# (1) Constructor
#
# @file<String> Manifest file
#
#--------------------------------------------------------#
def __init__(self):
# (1) Try to read manifest path #
self._path = None;
with open( "%s/manifest" % ROOT, 'r' ) as f:
self._path = f.readline();
# (2) Manage error #
if( self._path == None ):
raise Exception('No manifest file given');
# (3) Initialize attributes #
self._grp = [];
self._pkg = {};
# (2) Parser method
#
#--------------------------------------------------------#
def _parse(self):
# (0) Init
#--------------------------------------------------------#
# (1) Create group matcher #
grpMatcher = re.compile(self._grp_re);
# (2) Create pkg matches #
pkgMatcher = re.compile(self._pkg_re);
# (3) Local variables #
grp = False;
grp_name = None;
# (2) Search for groups
#--------------------------------------------------------#
with open( "%s" % self._path, 'r' ) as f:
for l,line in enumerate(f):
# (1) If seeking for group #
if( grp == False ):
## {1} Check regex with the line ##
match = grpMatcher.search(line);
## {2} If don't match -> pass ##
if( match == None ):
continue;
## {3} Register group ##
grp = True;
grp_name = match.group('name');
self._grp.append( grp_name );
self._pkg[grp_name] = [];
# (2) If seeking for group's packages #
else:
## {1} Check regex with the line ##
match = pkgMatcher.search(line);
## {2} If don't match -> back to group seeking ##
if( match == None ):
grp = False;
continue;
## {3} Register package ##
pkg = {
'pad': len(match.group('pad')),
'name': match.group('name'),
'is_deb': match.group('deb') != None,
'ppa': match.group('ppa') if match.group('ppa') != None else None,
'com': match.group('com') if match.group('com') != None else None
};
self._pkg[grp_name].append(pkg);
print self._grp;
print self._pkg;

15
test.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python
from parser import Manifest;
from parser import Package;
p = Manifest.Manifest();
pkg = Package.Package('pkgname', 'x', com="blabla sdfkj dfskj", deb=True);
# print p.hasPackage('blender');
p.addPackage(pkg, 'Basis');
pkg.remove();
# print p._pkg['Basis']['pkgname'].serialize();
p.save();