diff --git a/command/addppa b/command/addppa new file mode 100644 index 0000000..0e6cd07 --- /dev/null +++ b/command/addppa @@ -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 ); diff --git a/command/debinstall b/command/debinstall index fca606c..a351283 100644 --- a/command/debinstall +++ b/command/debinstall @@ -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 ); diff --git a/command/install b/command/install index f64989b..28dfd4c 100755 --- a/command/install +++ b/command/install @@ -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(); \ No newline at end of file diff --git a/command/list b/command/list index defb508..d8edf89 100644 --- a/command/list +++ b/command/list @@ -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; \ No newline at end of file + print "%s\033[0m" % pkg.serialize(); \ No newline at end of file diff --git a/command/parser/Manifest.py b/command/parser/Manifest.py new file mode 100644 index 0000000..faf6644 --- /dev/null +++ b/command/parser/Manifest.py @@ -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.+)$'; + _pkg_re = r'^(?P\s+)\[(?P[ x\.])\] (?P[\w\.-]+)(?:\s*(?P\[deb\])|(?:\s*\[(?P[\w\.-]+\/[\w\.-]+)\]))?(?:\s*# (?P.+))?\s*$'; + + # (1) Constructor + # + # @file 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 Group name + # + # @return exists Whether the group exists + # + #--------------------------------------------------------# + def hasGroup(self, name): + return name in self._grp; + + + + + # (4) Creates a group if it does not exist + # + # @name Group name + # + # @return created 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 Group name + # + # @return id 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 Package name + # + # @return exists 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 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 object + # @group 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 name + # @group Group name + # + # @return moved 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"); + + + diff --git a/command/parser/Package.py b/command/parser/Package.py new file mode 100644 index 0000000..b5ec28f --- /dev/null +++ b/command/parser/Package.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python + +class Package: + + _com_pad = 40; + + # (1) Constructs a new package + # + # @name Package name + # @state Package state ('x', ' ', '.') + # @com Package comm (default: None) + # @ppa<(String, String)> Package ppa (default: None) + # @deb Path to local deb file (if deb) (default: None) + # @pad 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; + + diff --git a/parser/__init__.py b/command/parser/__init__.py similarity index 100% rename from parser/__init__.py rename to command/parser/__init__.py diff --git a/command/purge b/command/purge index c1cfcc8..636c3d7 100755 --- a/command/purge +++ b/command/purge @@ -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(); \ No newline at end of file diff --git a/command/remove b/command/remove index abec02f..de6ecfd 100755 --- a/command/remove +++ b/command/remove @@ -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(); diff --git a/main b/main index 1f93886..a04bfda 100755 --- a/main +++ b/main @@ -20,6 +20,11 @@ def showhelp(): print; print "\033[1mSYNOPSIS\033[0m" print "\tapt-plus "; + print "\tapt-plus -g "; + print "\tapt-plus --gropu "; + print; + print "\t-g, --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 +62,27 @@ def showhelp(): print "\t-u, --update"; print "\t\tUpgrades packages and kernel and cleans after"; print; + print "\t-a, --add-ppa "; + print "\t\tAdds the 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; + 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) Normal arguments + #--------------------------------------------------------# # (1) Get the command argument (first) # - command = sys.argv[1]; + command = ARGS[1]; # (1) --update, -u #--------------------------------------------------------# @@ -83,58 +101,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 "); - 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 = " ".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 = " ".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 = " ".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 "); # (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 +173,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 "); + + ossystem("/usr/bin/env python %s/command/addppa %s" % (path, ARGS[2]) ); + + # (10) If no match -> show help message #--------------------------------------------------------# else: showhelp(); diff --git a/manifest b/manifest index 73efadb..f5ab149 100755 --- a/manifest +++ b/manifest @@ -1 +1 @@ -/home/xdrm-brackets/.x-migration \ No newline at end of file +/home/xdrm-brackets/.apt-plus \ No newline at end of file diff --git a/parser/Manifest.py b/parser/Manifest.py deleted file mode 100644 index 9bf8343..0000000 --- a/parser/Manifest.py +++ /dev/null @@ -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.+)$'; - _pkg_re = r'^(?P\s+)\[[ x\.]\] (?P[\w\.-]+)(?:\s*(?P\[deb\])|(?:\s*\[(?P[\w\.-]+\/[\w\.-]+)\]))?(?:\s*# (?P.+))?\s*$'; - - # (1) Constructor - # - # @file 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; \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..8e0326d --- /dev/null +++ b/test.py @@ -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(); \ No newline at end of file