apt-plus/parser/Manifest.py

108 lines
2.5 KiB
Python

#!/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;