From 6c06a7a651c513266a3a1ea30f7f36fe15140656 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 6 Oct 2017 11:54:43 +0200 Subject: [PATCH] Manifest parser done --- .gitignore | 4 ++ parser/Manifest.py | 108 +++++++++++++++++++++++++++++++++++++++++++++ parser/__init__.py | 0 3 files changed, 112 insertions(+) create mode 100644 .gitignore create mode 100644 parser/Manifest.py create mode 100644 parser/__init__.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04dc0fb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +stdout +manifest +.vscode +*.pyc \ No newline at end of file diff --git a/parser/Manifest.py b/parser/Manifest.py new file mode 100644 index 0000000..9bf8343 --- /dev/null +++ b/parser/Manifest.py @@ -0,0 +1,108 @@ +#!/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/parser/__init__.py b/parser/__init__.py new file mode 100644 index 0000000..e69de29