pysys.utils.filediff

Comparing (diffing) text file contents.

trimContents

pysys.utils.filediff.trimContents(contents, expressions, exclude=True, flags=0)[source]

Reduce a list of strings based by including/excluding lines which match any of a set of regular expressions, returning the processed list.

The method reduces an input list of strings based on whether each string matches or does not match a list of regular expressions.

Parameters
  • contents – The input list of strings to trim based on matches to regular expressions

  • expressions – The input list of regular expressions

  • exclude – If true any matches to the regular expressions exclude the line, if false any matches include the line

Returns

The processed list

Return type

list

replace

pysys.utils.filediff.replace(list, replacementList, flags=0)[source]

Replace all occurrences of keyword values in a list of strings with a set value, returning the processed list.

The replacementList parameter should contain a list of tuples to use in the replacement, e.g. [(‘foo’, ‘bar’), (‘swim’, ‘swam’)].

Parameters
  • list – The input list of strings to performed the replacement on

  • replacementList – A list of tuples (key, value) where matches to key are replaced with value

Returns

The processed list

Return type

list

logContents

pysys.utils.filediff.logContents(message, list)[source]

Log a list of strings, prepending the line number to each line in the log output.

Parameters

list – The list of strings to log

filediff

pysys.utils.filediff.filediff(file1, file2, ignore=[], sort=True, replacementList=[], include=[], unifiedDiffOutput=None, encoding=None, stripWhitespace=True, flags=0)[source]

Perform a file comparison between two (preprocessed) input files, returning true if the files are equivalent.

The method reads in the files and loads the contents of each as a list of strings. The two files are said to be equal if the two lists are equal. The method allows for preprocessing of the string lists to trim down their contents prior to the comparison being performed. Preprocessing is either to remove entries from the lists which match any entry in a set of regular expressions, include only lines which match any entry in a set of regular expressions, replace certain keywords in the string values of each list with a set value (e.g. to replace time stamps etc), or to sort the lists before the comparison (e.g. where determinism may not exist). Verbose logging of the method occurs at DEBUG level showing the contents of the processed lists prior to the comparison being performed.

Parameters
  • file1 – The full path to the first file to use in the comparison

  • file2 – The full path to the second file to use in the comparison, typically a reference file

  • ignore – A list of regular expressions which remove entries in the input file contents before making the comparison

  • sort – Boolean to sort the input file contents before making the comparison

  • replacementList – A list of tuples (key, value) where matches to key are replaced with value in the input file contents before making the comparison

  • stripWhitespace – If True, every line has leading and trailing whitespace stripped before comparison, which means indentation differences and whether the file ends with a blank line do not affect the outcome. If False, only newline characters are stripped.

  • include – A list of regular expressions used to select lines from the input file contents to use in the comparison

  • unifiedDiffOutput – If specified, indicates the full path of a file to which unified diff output will be written, if the diff fails.

  • encoding – Specifies the encoding to be used for opening the file, or None for default.

Returns

success (True / False)

Return type

boolean

Raises

FileNotFoundException – Raised if either of the files do not exist