Source code for ApplicationVersioningSupport.VersionHistoryFileParser

# Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
# **InsertLicense** code
#----------------------------------------------------------------------------------
#! VersionHistoryFileParser: Helps to read and parse Version History files
#/*!
# \file    VersionHistoryFileParser.py
# \author  Jan-Martin Kuhnigk
# \date    2014-08
#*/
#----------------------------------------------------------------------------------

# associated_tests: ApplicationVersioningSupport.tests.VersionHistoryFileParserTest

import os
import sys

from .VersionInfo import VersionInfo
from . import VersionHistoryParser


[docs]class VersionHistoryFileParserError( Exception ): pass
[docs]class VersionInfoFileNotFoundException( VersionHistoryFileParserError ): pass
[docs]def ReadVersionHistoryStringFromFile( versionHistoryFileName ): """ Reads a version info file and returns its contents as a string :param versionHistoryFileName: Path to the version history text file :raises: VersionInfoFileNotFoundException if file is not found :raises: IOError if reading fails for some reason :return: The contents of the file """ if not os.path.isfile( versionHistoryFileName ): raise VersionInfoFileNotFoundException( "Version history file not found at {}!".format( versionHistoryFileName ) ) else: with open( versionHistoryFileName, "r", encoding="UTF-8", errors="surrogateescape" ) as versionInfoFile: versionHistoryString = versionInfoFile.read( ) return versionHistoryString
[docs]def GetCurrentVersionInfoFromFile( inputFilename ): """ Extracts the current version info from a version history file and returns it :param inputFilename: Input version history file, expected to have a "Current Version:" tag. :return: Extracted VersionInfo object """ try: versionHistory = __extractVersionHistoryFromFile( inputFilename ) except ( VersionInfoFileNotFoundException, VersionHistoryParser.VersionInfoCurrentVersionNotFoundException ) as e: print( "GetCurrentVersionInfoFromFile() error: {}".format( e ), file=sys.stderr ) versionInfo = VersionInfo( ) else: versionInfo = versionHistory.getCurrentVersionInfo( ) return versionInfo
[docs]def PrintCurrentVersionStringToStdout( inputFilename ): """ Extracts a version string from a version history file and prints it to stdout. This is handy in .pro files where the version number should be embedded in binares. Note that this method catches all exceptions and returns its success instead :param inputFilename: Filename of the version history to parse for the current version number :return: True on success """ success = False try: versionHistory = __extractVersionHistoryFromFile( inputFilename ) versionInfo = versionHistory.getCurrentVersionInfo() print ( versionInfo.getVersionString() ) success = True except ( VersionInfoFileNotFoundException, VersionHistoryParser.VersionInfoCurrentVersionNotFoundException ) as e: print( "Printing current version string failed with read error: {}".format( e ), file=sys.stderr ) return success
[docs]def ExtractCurrentVersionStringToFile( inputFilename, outputFilename ): """ Extracts a version string from a version history file and writes it to another file for further processing. This is handy in installer definitions where the version number shall be read from a file. Note that this method catches all exceptions and returns its success instead :param inputFilename: Filename of the version history to parse for the current version number :param outputFilename: Filename for the extracted version string :return: True on success """ success = False try: versionHistory = __extractVersionHistoryFromFile( inputFilename ) versionInfo = versionHistory.getCurrentVersionInfo() __writeTextToFile( versionInfo.getVersionString(), outputFilename ) print ( "Successfully wrote version string '{}' to file {}".format( versionInfo.getVersionString(), outputFilename )) success = True except ( VersionInfoFileNotFoundException, VersionHistoryParser.VersionInfoCurrentVersionNotFoundException ) as e: print( "Extracting current version string to file failed with read error: {}".format( e ), file=sys.stderr ) except IOError as e: print( "Extracting current version string to file failed with write error ({}): {}".format( e.errno, e.strerror ), file=sys.stderr ) return success
[docs]def ExtractCurrentVersionStringToFiles( versionHistoryFileName ): """ Extracts a version string from a version history file and writes it to another files for further processing. Creates three files containing complete version string, major and minor part where the file names are derived from input version history file name using dedicated suffixes. :param versionHistoryFileName: Filename of the version history to parse for the current version number :raises: VersionHistoryFileParserError on any error """ try: __validateInputFilename( versionHistoryFileName ) versionInfo = __extractCurrentVersionInfoFromFile( versionHistoryFileName ) __writeVersionInfoToFiles( versionHistoryFileName, versionInfo ) except VersionInfoFileNotFoundException as e: print ( __createMessageFromErrorWhenExtractingCurrentVersionStringToFiles( e ), file=sys.stderr ) raise e except Exception as e: msg = __createMessageFromErrorWhenExtractingCurrentVersionStringToFiles( e ) print ( msg, file=sys.stderr ) raise VersionHistoryFileParserError( msg )
def __createMessageFromErrorWhenExtractingCurrentVersionStringToFiles( e ): return 'Extracting current version to files failed with error ({}): {}'.format( type( e ), str( e )) def __validateInputFilename( name ): if ( name is None ): raise VersionInfoFileNotFoundException( 'Invalid input file name: None' ) elif ( not os.path.isfile( name )): raise VersionInfoFileNotFoundException( 'Invalid input file name: Does not point to a file' ) def __extractCurrentVersionInfoFromFile( filename ): history = __extractVersionHistoryFromFile( filename ) return history.getCurrentVersionInfo() def __extractVersionHistoryFromFile( filename ): historyString = ReadVersionHistoryStringFromFile( filename ) return VersionHistoryParser.GetVersionHistoryFromString( historyString ) def __writeVersionInfoToFiles( inputFilename, versionInfo ): dirPath = os.path.dirname( inputFilename ) filenamePrefix = '{}'.format( os.path.basename( inputFilename )) texts = ( versionInfo.getVersionString(), versionInfo.getMajorVersionString(), versionInfo.getMinorVersionString()) filenameSuffixes = ( '_version.tmp', '_version_major.tmp', '_version_minor.tmp' ) for text, suffix in zip( texts, filenameSuffixes ): __writeTextToFile( text, os.path.join( dirPath, '{}{}'.format( filenamePrefix, suffix ))) def __writeTextToFile( text, filename ): try: with open( filename, 'w' ) as f: f.write( text ) except IOError as e: print( 'Failed to write file with error ({}): {}'.format( e.errno, e.strerror ), file=sys.stderr ) raise e