diff options
Diffstat (limited to 'minidinstall/DpkgControl.py')
| -rwxr-xr-x | minidinstall/DpkgControl.py | 124 |
1 files changed, 45 insertions, 79 deletions
diff --git a/minidinstall/DpkgControl.py b/minidinstall/DpkgControl.py index be08155..02e3567 100755 --- a/minidinstall/DpkgControl.py +++ b/minidinstall/DpkgControl.py @@ -1,7 +1,10 @@ -# DpkgControl.py +#!/usr/bin/python3 +# DpkgControl -*- mode: python; coding: utf-8 -*- # # This module implements control file parsing. # +# Copyright (c) 2001 Adam Heath <doogie@debian.org> +# # DpkgParagraph is a low-level class, that reads/parses a single paragraph # from a file object. # @@ -16,9 +19,7 @@ # To test this, pass it a filetype char, a filename, then, optionally, # the key to a paragraph to display, and if a fourth arg is given, only # show that field. -# -# Copyright 2001 Adam Heath <doogie@debian.org> -# + # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or @@ -33,20 +34,15 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -import re, string from .DpkgDatalist import * -from minidinstall.SignedFile import * +from .SignedFile import * class DpkgParagraph(DpkgOrderedDatalist): - caseSensitive = 0 trueFieldCasing = {} - def setCaseSensitive( self, value ): self.caseSensitive = value - def load( self, f ): - "Paragraph data from a file object." + """Paragraph data from a file object""" key = None - value = None while True: line = f.readline() if not line: @@ -57,85 +53,53 @@ class DpkgParagraph(DpkgOrderedDatalist): continue else: return - line = line[ :-1 ] if line[ 0 ] != ' ': - key, value = line.split( ":", 1 ) - if value: value = value[ 1: ] - if not self.caseSensitive: - newkey = key.lower() - if key not in self.trueFieldCasing: - self.trueFieldCasing[ newkey ] = key - key = newkey + ( truekey, value ) = line.split( ":", 1 ) + key = truekey.lower() + self.trueFieldCasing[ key ] = truekey + self[ key ] = value.strip() else: - if isinstance( value, list ): - value.append( line[ 1: ] ) - else: - value = [ value, line[ 1: ] ] - self[ key ] = value + self[ key ] += "\n%s" % line.strip() def _storeField( self, f, value, lead = " " ): - if isinstance( value, list ): - value = "\n".join(list(map( lambda v, lead = lead: v and ( lead + v ) or v, value ))) - else: - if value: value = lead + value - f.write( "%s\n" % ( value ) ) + value = "\n".join(list(map( lambda v, lead = lead: lead + v if v else "", value.splitlines() ))) + f.write( "%s\n" % value ) def _store( self, f ): - "Write our paragraph data to a file object" - for key in list(self.keys()): - value = self[ key ] - if key in self.trueFieldCasing: - key = self.trueFieldCasing[ key ] - f.write( "%s:" % key ) + """Write our paragraph data to a file object""" + for ( key, value ) in list(self.items()): + truekey = self.trueFieldCasing[ key ] + f.write( "%s:" % truekey ) self._storeField( f, value ) -class DpkgControl(DpkgOrderedDatalist): - - key = "package" - caseSensitive = 0 - - def setkey( self, key ): self.key = key - def setCaseSensitive( self, value ): self.caseSensitive = value - - def _load_one( self, f ): - p = DpkgParagraph( None ) - p.setCaseSensitive( self.caseSensitive ) - p.load( f ) - return p - - def load( self, f ): +class DpkgControl(DpkgParagraph): + def load( self, f, source = False ): while True: - p = self._load_one( f ) - if not p: break - self[ p[ self.key ] ] = p + para = DpkgParagraph() + para.load( f ) + if not para: + break + if "source" not in para: + self[ para[ "package" ] ] = para + elif source: + self[ "source" ] = para def _store( self, f ): - "Write our control data to a file object" - - for key in list(self.keys()): - self[ key ]._store( f ) - f.write( "\n" ) - -class DpkgSourceControl( DpkgControl ): - source = None - + """Write our control data to a file object""" + keys = list(self.keys()) + while keys: + self[ keys.pop( 0 ) ]._store( f ) + if keys: + f.write( "\n" ) + +class DpkgSourceControl(DpkgControl): def load( self, f ): - f = SignedFile(f) - self.source = self._load_one( f ) - DpkgControl.load( self, f ) - - def __repr__( self ): - return self.source.__repr__() + "\n" + DpkgControl.__repr__( self ) - - def _store( self, f ): - "Write our control data to a file object" - self.source._store( f ) - f.write( "\n" ) - DpkgControl._store( self, f ) + f = SignedFile( f ) + DpkgControl.load( self, f, source = True ) if __name__ == "__main__": import sys - types = { 'p' : DpkgParagraph, 'c' : DpkgControl, 's' : DpkgSourceControl } + types = { 'p': DpkgParagraph, 'c': DpkgControl, 's': DpkgSourceControl } type = sys.argv[ 1 ] if type not in types: print( "Unknown type `%s'!" % type ) @@ -144,11 +108,13 @@ if __name__ == "__main__": data = types[ type ]() data.load( file ) if len( sys.argv ) > 3: - para = data[ sys.argv[ 3 ] ] - if len( sys.argv ) > 4: - para._storeField( sys.stdout, para[ sys.argv[ 4 ] ], "" ) + rargs = sys.argv[ 3: ] + if type != 'p': + data = data[ rargs.pop( 0 ) ] + if rargs: + data._storeField( sys.stdout, data[ rargs[ 0 ].lower() ], "" ) else: - para._store( sys.stdout ) + data._store( sys.stdout ) else: data._store( sys.stdout ) |
