From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 1AB5F1381F3 for ; Thu, 5 Sep 2013 09:25:09 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 661E9E0D56; Thu, 5 Sep 2013 09:25:07 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 7F8ABE0D30 for ; Thu, 5 Sep 2013 09:25:06 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 5D7D533EA96 for ; Thu, 5 Sep 2013 09:25:05 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 8370FE5467 for ; Thu, 5 Sep 2013 09:25:02 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1378372148.2e90b548ac7177ce8af2c2ff53321db463c9f840.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/db/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/db/distmap.py X-VCS-Directories: roverlay/db/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: 2e90b548ac7177ce8af2c2ff53321db463c9f840 X-VCS-Branch: master Date: Thu, 5 Sep 2013 09:25:02 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 549b16bb-2140-449a-99a2-f2764dcbec84 X-Archives-Hash: 3f0f9260f9406bbadcbfc2894fe1b282 commit: 2e90b548ac7177ce8af2c2ff53321db463c9f840 Author: André Erdmann mailerd de> AuthorDate: Thu Sep 5 09:09:08 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Thu Sep 5 09:09:08 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=2e90b548 distmap: use roverlay.util.fileio.TextFile --- roverlay/db/distmap.py | 156 +++++++++---------------------------------------- 1 file changed, 27 insertions(+), 129 deletions(-) diff --git a/roverlay/db/distmap.py b/roverlay/db/distmap.py index d683f24..584ae8c 100644 --- a/roverlay/db/distmap.py +++ b/roverlay/db/distmap.py @@ -177,17 +177,16 @@ class DistMapInfo ( object ): # --- end of DistMapInfo --- -class _DistMapBase ( object ): +class _DistMapBase ( roverlay.util.objects.PersistentContent ): # { attr[, as_attr] } DISTMAP_BIND_ATTR = frozenset ({ 'get', 'keys', 'items', 'values', ( 'get', 'get_entry' ), }) - def __init__ ( self ): + def __init__ ( self, *args, **kwargs ): super ( _DistMapBase, self ).__init__() self.logger = logging.getLogger ( self.__class__.__name__ ) - self.dirty = False self._distmap = dict() self.stats = roverlay.stats.collector.static.distmap @@ -237,12 +236,12 @@ class _DistMapBase ( object ): def _file_added ( self, distfile ): self.stats.file_added() - self.dirty = True + self.set_dirty() # --- end of _file_added (...) --- def _file_removed ( self, distfile ): self.stats.file_removed() - self.dirty = True + self.set_dirty() # --- end of _file_removed (...) --- def _iter_persistent ( self ): @@ -578,7 +577,7 @@ class _DistMapBase ( object ): # --- end of _DistMapBase --- -class FileDistMap ( _DistMapBase ): +class FileDistMap ( roverlay.util.fileio.TextFile, _DistMapBase ): """A distmap that is read from / written to a file.""" # the default info field separator @@ -588,17 +587,6 @@ class FileDistMap ( _DistMapBase ): # file format (reserved for future usage) FILE_FORMAT = '0' - def set_compression ( self, compression ): - if not compression or compression in { 'default', 'none' }: - self.compression = None - elif compression in roverlay.util.fileio.SUPPORTED_COMPRESSION: - self.compression = compression - else: - raise ValueError ( - "unknown distmap compression {!r}".format ( compression ) - ) - # --- end of set_compression (...) --- - def __init__ ( self, distmap_file, distmap_compression=None, ignore_missing=False ): @@ -612,10 +600,9 @@ class FileDistMap ( _DistMapBase ): raises: ValueError if distmap_compression not supported. """ - super ( FileDistMap, self ).__init__ () - self.dbfile = distmap_file - self.compression = None - self.set_compression ( distmap_compression ) + super ( FileDistMap, self ).__init__ ( + filepath=distmap_file, compression=distmap_compression + ) if ignore_missing: self.try_read() @@ -623,73 +610,6 @@ class FileDistMap ( _DistMapBase ): self.read() # --- end of __init__ (...) --- - def backup_file ( self, destfile=None, move=False, ignore_missing=False ): - """Creates a backup copy of the distmap file. - - arguments: - * destfile -- backup file path - Defaults to + '.bak'. - * move -- move distmap file (instead of copying) - * ignore_missing -- return False if distmap file does not exist instead - of raising an exception - Defaults to False. - """ - dest = destfile or self.dbfile + '.bak' - try: - roverlay.util.dodir ( os.path.dirname ( dest ), mkdir_p=True ) - if move: - shutil.move ( self.dbfile, dest ) - return True - else: - shutil.copyfile ( self.dbfile, dest ) - return True - except IOError as ioerr: - if ignore_missing and ioerr.errno == errno.ENOENT: - return False - else: - raise - # --- end of backup_file (...) --- - - def backup_and_write ( self, - db_file=None, backup_file=None, - force=False, move=False, ignore_missing=True - ): - """Creates a backup copy of the distmap file and writes the modified - distmap afterwards. - - arguments: - * db_file -- distmap file path (defaults to self.dbfile) - * backup_file -- backup file path (see backup_file()) - * force -- enforce writing even if distmap not modified - * move -- move distmap (see backup_file()) - * ignore_missing -- do not fail if distmap file does not exist - Defaults to True. - """ - if force or self.dirty: - self.backup_file ( - destfile=backup_file, move=move, ignore_missing=ignore_missing - ) - return self.write ( filepath=db_file, force=True ) - else: - return True - # --- end of backup_and_write (...) --- - - def file_exists ( self ): - """Returns True if the distmap file exists, else False.""" - return os.path.isfile ( self.dbfile ) - # --- end of file_exists (...) --- - - def try_read ( self, *args, **kwargs ): - """Tries to read the distmap file.""" - try: - self.read ( *args, **kwargs ) - except IOError as ioerr: - if ioerr.errno == errno.ENOENT: - pass - else: - raise - # --- end of try_read (...) --- - def get_header ( self ): return "<{d}<{fmt}".format ( d=self.FIELD_DELIMITER, fmt=self.FILE_FORMAT @@ -727,51 +647,29 @@ class FileDistMap ( _DistMapBase ): return False # --- end of _read_header (...) --- - def read ( self, filepath=None ): - """Reads the distmap. - - arguments: - * filepath -- path to the distmap file (defaults to self.dbfile) - """ - dbfile = self.dbfile if filepath is None else filepath - first = True - - for line in roverlay.util.fileio.read_text_file ( - dbfile, preparse=True, try_harder=True - ): - if first: - first = False - if self._read_header ( line ): - continue - # else no header - # -- end if - - distfile, info = roverlay.util.headtail ( - line.split ( self.FIELD_DELIMITER ) - ) - self._distmap [distfile] = DistMapInfo ( distfile, *info ) - self._nondirty_file_added ( distfile ) - # -- end for - self.dirty = self.dirty or filepath is not None - # --- end of read (...) --- + def parse_line ( self, line ): + distfile, info = roverlay.util.headtail ( + line.split ( self.FIELD_DELIMITER ) + ) + self._distmap [distfile] = DistMapInfo ( distfile, *info ) + self._nondirty_file_added ( distfile ) + return True + # --- end of parse_line (...) --- - def write ( self, filepath=None, force=False ): - """Writes the distmap. + def parse_header_line ( self, line ): + """Tries to parse a text line as distmap file header, else parses + it as normal text line. arguments: - * filepath -- path to the distmap file (defaults to self.dbfile) - * force -- enforce writing even if distmap not modified + * line -- """ - if force or self.dirty or filepath is not None: - dbfile = self.dbfile if filepath is None else filepath - roverlay.util.fileio.write_text_file ( - dbfile, self.gen_lines(), - compression=self.compression, create_dir=True - ) - self.dirty = self.dirty and filepath is not None - return True + if len ( line ) > 2 and line[0] == line[2]: + # instance attr + self.FIELD_DELIMITER = line[1] + if len ( line ) > 3: + self.FILE_FORMAT = line[3:] else: - return False - # --- end of write (...) --- + return self.parse_line ( line ) + # --- end of parse_header_line (...) --- # --- end of FileDistMap ---