diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 04179b5..1cffd4d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,7 +24,11 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + + - name: Lint + uses: cclauss/GitHub-Action-for-pylint@master + with: + args: "pylint src/vcs/" - name: Install dependencies run: sudo apt-get -y install appstream appstream-util desktop-file-utils gettext diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..296041c --- /dev/null +++ b/.pylintrc @@ -0,0 +1,584 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) +extension-pkg-whitelist= + +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold to be exceeded before program exits with error. +fail-under=10.0 + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS + +# Add files or directories matching the regex patterns to the ignore-list. The +# regex matches against paths. +ignore-paths= + +# Files or directories matching the regex patterns are skipped. The regex +# matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Min Python version to use for version dependend checks. Will default to the +# version used to run pylint. +py-version=3.9 + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead, + + # temporary silenced messages + invalid-name, + missing-module-docstring, + missing-class-docstring, + missing-function-docstring, + import-error, + no-self-use, + too-many-branches, + too-many-locals, + too-many-statements, + too-many-nested-blocks, + too-few-public-methods, + unused-argument, + fixme, + unnecessary-dict-index-lookup, + duplicate-code + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'error', 'warning', 'refactor', and 'convention' +# which contain the number of messages in each category, as well as 'statement' +# which is the total number of statements analyzed. This score is used by the +# global evaluation report (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. +#class-const-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=100 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[LOGGING] + +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + +# Regular expression of note tags to take in consideration. +#notes-rgx= + + +[SIMILARITIES] + +# Comments are removed from the similarity computation +ignore-comments=yes + +# Docstrings are removed from the similarity computation +ignore-docstrings=yes + +# Imports are removed from the similarity computation +ignore-imports=no + +# Signatures are removed from the similarity computation +ignore-signatures=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: en_AG (hunspell), en_AU +# (hunspell), en_BS (hunspell), en_BW (hunspell), en_BZ (hunspell), en_CA +# (hunspell), en_DK (hunspell), en_GB (hunspell), en_GH (hunspell), en_HK +# (hunspell), en_IE (hunspell), en_IN (hunspell), en_JM (hunspell), en_MW +# (hunspell), en_NA (hunspell), en_NG (hunspell), en_NZ (hunspell), en_PH +# (hunspell), en_SG (hunspell), en_TT (hunspell), en_US (hunspell), en_ZA +# (hunspell), en_ZM (hunspell), en_ZW (hunspell), fr_BE (hunspell), fr_CA +# (hunspell), fr_CH (hunspell), fr_FR (hunspell), fr_LU (hunspell), fr_MC +# (hunspell). +spelling-dict= + +# List of comma separated words that should be considered directives if they +# appear and the beginning of a comment and should not be checked. +spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy: + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains the private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to the private dictionary (see the +# --spelling-private-dict-file option) instead of raising a message. +spelling-store-unknown-words=no + + +[STRING] + +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no + +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + +# List of decorators that change the signature of a decorated function. +signature-mutators= + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins=_ + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of names allowed to shadow builtins +allowed-redefined-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[CLASSES] + +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp, + __post_init__ + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# List of qualified class names to ignore when counting class parents (see +# R0901) +ignored-parents= + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement (see R0916). +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[IMPORTS] + +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e5b7b7..6b157f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Added a linter (pylint) and fixed some errors +- Added a lint step in the CI ### Changed diff --git a/src/diffuse.in b/src/diffuse.in index dab1359..973f974 100755 --- a/src/diffuse.in +++ b/src/diffuse.in @@ -21,13 +21,12 @@ import sys import gettext -import locale -pkgdatadir = '@pkgdatadir@' -localedir = '@localedir@' +PKGDATADIR = '@pkgdatadir@' +LOCALEDIR = '@localedir@' -sys.path.insert(1, pkgdatadir) -gettext.install('diffuse', localedir) +sys.path.insert(1, PKGDATADIR) +gettext.install('diffuse', LOCALEDIR) if __name__ == '__main__': from diffuse import main diff --git a/src/main.py b/src/main.py index b291b19..278eb0b 100644 --- a/src/main.py +++ b/src/main.py @@ -55,10 +55,6 @@ from diffuse import utils from diffuse import constants from diffuse.vcs.vcs_registry import VcsRegistry -if not hasattr(__builtins__, 'WindowsError'): - # define 'WindowsError' so 'except' statements will work on all platforms - WindowsError = IOError - # avoid some dictionary lookups when string.whitespace is used in loops # this is sorted based upon frequency to speed up code for stripping whitespace whitespace = ' \t\n\r\x0b\x0c' @@ -131,38 +127,6 @@ class SyntaxParser: start = end return state_name, blocks -# split string into lines based upon DOS, Mac, and Unix line endings -def splitlines(s): - # split on new line characters - temp, i, n = [], 0, len(s) - while i < n: - j = s.find('\n', i) - if j < 0: - temp.append(s[i:]) - break - j += 1 - temp.append(s[i:j]) - i = j - # split on carriage return characters - ss = [] - for s in temp: - i, n = 0, len(s) - while i < n: - j = s.find('\r', i) - if j < 0: - ss.append(s[i:]) - break - j += 1 - if j < n and s[j] == '\n': - j += 1 - ss.append(s[i:j]) - i = j - return ss - -# also recognise old Mac OS line endings -def readlines(fd): - return strip_eols(splitlines(fd.read())) - def readconfiglines(fd): return fd.read().replace('\r', '').split('\n') @@ -1141,33 +1105,12 @@ def getFormat(ss): flags |= UNIX_FORMAT return flags -# returns the number of characters in the string excluding any line ending -# characters -def len_minus_line_ending(s): - if s is None: - return 0 - n = len(s) - if s.endswith('\r\n'): - n -= 2 - elif s.endswith('\r') or s.endswith('\n'): - n -= 1 - return n - -# returns the string without the line ending characters -def strip_eol(s): - if s is not None: - return s[:len_minus_line_ending(s)] - -# returns the list of strings without line ending characters -def strip_eols(ss): - return [ strip_eol(s) for s in ss ] - # convenience method to change the line ending of a string def convert_to_format(s, format): if s is not None and format != 0: old_format = getFormat([ s ]) if old_format != 0 and (old_format & format) == 0: - s = strip_eol(s) + s = utils.strip_eol(s) # prefer the host line ending style if (format & DOS_FORMAT) and os.linesep == '\r\n': s += os.linesep @@ -1913,7 +1856,7 @@ class FileDiffViewer(Gtk.Grid): # This is an inline loop over self.characterWidth() for performance reasons. def stringWidth(self, s): if not self.prefs.getBool('display_show_whitespace'): - s = strip_eol(s) + s = utils.strip_eol(s) col = 0 for c in s: try: @@ -1965,7 +1908,7 @@ class FileDiffViewer(Gtk.Grid): def expand(self, s): visible = self.prefs.getBool('display_show_whitespace') if not visible: - s = strip_eol(s) + s = utils.strip_eol(s) tab_width = self.prefs.getInt('display_tab_width') col = 0 result = [] @@ -2563,7 +2506,7 @@ class FileDiffViewer(Gtk.Grid): # hashes for non-null lines should start with '+' to distinguish # them from blank lines if pref('align_ignore_endofline'): - text = strip_eol(text) + text = utils.strip_eol(text) if pref('align_ignore_blanklines') and isBlank(text): # consider all lines containing only white space as the same return '' @@ -2792,8 +2735,8 @@ class FileDiffViewer(Gtk.Grid): if text is None: text = '' # split the replacement text into lines - ss = splitlines(text) - if len(ss) == 0 or len(ss[-1]) != len_minus_line_ending(ss[-1]): + ss = utils.splitlines(text) + if len(ss) == 0 or len(ss[-1]) != utils.len_minus_line_ending(ss[-1]): ss.append('') # change the format to that of the target pane if pane.format == 0: @@ -2807,7 +2750,7 @@ class FileDiffViewer(Gtk.Grid): lastcol = 0 if len(ss) > 0: last = ss[-1] - if len(last) == len_minus_line_ending(last): + if len(last) == utils.len_minus_line_ending(last): del ss[-1] lastcol = len(last) cur_line = line0 + len(ss) @@ -3200,7 +3143,7 @@ class FileDiffViewer(Gtk.Grid): x, y = -1, 0 i = min(y // self.font_height, len(self.panes[f].lines)) if self.mode == CHAR_MODE and f == self.current_pane: - text = strip_eol(self.getLineText(f, i)) + text = utils.strip_eol(self.getLineText(f, i)) j = self._getPickedCharacter(text, x, True) if extend: si, sj = self.selection_line, self.selection_char @@ -3241,7 +3184,7 @@ class FileDiffViewer(Gtk.Grid): self.emit('mode_changed') elif self.mode == CHAR_MODE and self.current_pane == f: # select word - text = strip_eol(self.getLineText(f, i)) + text = utils.strip_eol(self.getLineText(f, i)) if text is not None: n = len(text) j = self._getPickedCharacter(text, x, False) @@ -3323,7 +3266,7 @@ class FileDiffViewer(Gtk.Grid): else: s = s2 if self.prefs.getBool('display_ignore_endofline'): - s = strip_eol(s) + s = utils.strip_eol(s) s1 = nullToEmpty(self.getCompareString(f, i)) s2 = nullToEmpty(self.getCompareString(f + 1, i)) @@ -3394,7 +3337,7 @@ class FileDiffViewer(Gtk.Grid): s = line.getText() if s is not None: if self.prefs.getBool('display_ignore_endofline'): - s = strip_eol(s) + s = utils.strip_eol(s) if self.prefs.getBool('display_ignore_blanklines') and isBlank(s): return None if self.prefs.getBool('display_ignore_whitespace'): @@ -3906,7 +3849,7 @@ class FileDiffViewer(Gtk.Grid): # returns the maximum valid offset for a cursor position # cursors cannot be moved to the right of line ending characters def getMaxCharPosition(self, i): - return len_minus_line_ending(self.getLineText(self.current_pane, i)) + return utils.len_minus_line_ending(self.getLineText(self.current_pane, i)) # 'enter_align_mode' keybinding action def _line_mode_enter_align_mode(self): @@ -4132,7 +4075,7 @@ class FileDiffViewer(Gtk.Grid): # move the cursor to column 'col' if possible s = self.getLineText(f, i) if s is not None: - s = strip_eol(s) + s = utils.strip_eol(s) idx = 0 for c in s: w = self.characterWidth(idx, c) @@ -4282,7 +4225,7 @@ class FileDiffViewer(Gtk.Grid): self.recordEditMode() for i in range(start, end + 1): text = self.getLineText(f, i) - if text is not None and len_minus_line_ending(text) > 0: + if text is not None and utils.len_minus_line_ending(text) > 0: # count spacing before the first non-whitespace character j, w = 0, 0 while j < len(text) and text[j] in ' \t': @@ -4807,7 +4750,7 @@ class FileDiffViewer(Gtk.Grid): text = self.getLineText(f, i) if text is not None: # locate trailing whitespace - old_n = n = len_minus_line_ending(text) + old_n = n = utils.len_minus_line_ending(text) while n > 0 and text[n - 1] in whitespace: n -= 1 # update line if it changed @@ -4914,7 +4857,7 @@ class FileDiffViewer(Gtk.Grid): self.recordEditMode() for i in range(start, end + 1): text = self.getLineText(f, i) - if text is not None and len_minus_line_ending(text) > 0: + if text is not None and utils.len_minus_line_ending(text) > 0: # count spacing before the first non-whitespace character j, w = 0, 0 while j < len(text) and text[j] in ' \t': @@ -5628,8 +5571,8 @@ class Diffuse(Gtk.Window): s, encoding = self.prefs.convertToUnicode(s) else: s = str(s, encoding=encoding) - ss = splitlines(s) - except (IOError, OSError, UnicodeDecodeError, WindowsError, LookupError): + ss = utils.splitlines(s) + except (IOError, OSError, UnicodeDecodeError, LookupError): # FIXME: this can occur before the toplevel window is drawn if rev is not None: msg = _('Error reading revision %(rev)s of %(file)s.') % { 'rev': rev, 'file': name } @@ -6460,7 +6403,7 @@ class Diffuse(Gtk.Window): name, rev = spec viewer.load(i, FileInfo(name, encoding, vcs, rev)) viewer.setOptions(options) - except (IOError, OSError, WindowsError): + except (IOError, OSError): utils.logErrorAndDialog(_('Error retrieving commits for %s.') % (dn, ), self.get_toplevel()) # create a new viewer for each modified file found in 'items' @@ -6489,7 +6432,7 @@ class Diffuse(Gtk.Window): name, rev = spec viewer.load(i, FileInfo(name, encoding, vcs, rev)) viewer.setOptions(options) - except (IOError, OSError, WindowsError): + except (IOError, OSError): utils.logErrorAndDialog(_('Error retrieving modifications for %s.') % (dn, ), self.get_toplevel()) # close all tabs without differences diff --git a/src/utils.py b/src/utils.py index 9b8dadd..d499d6d 100644 --- a/src/utils.py +++ b/src/utils.py @@ -96,8 +96,8 @@ def relpath(a, b): # helper function prevent files from being confused with command line options # by prepending './' to the basename def safeRelativePath(abspath1, name, prefs, cygwin_pref): - s = os.path.join(os.curdir, utils.relpath(abspath1, os.path.abspath(name))) - if utils.isWindows(): + s = os.path.join(os.curdir, relpath(abspath1, os.path.abspath(name))) + if isWindows(): if prefs.getBool(cygwin_pref): s = s.replace('\\', '/') else: @@ -182,6 +182,55 @@ def globEscape(s): m = dict([ (c, f'[{c}]') for c in '[]?*' ]) return ''.join([ m.get(c, c) for c in s ]) +# returns the number of characters in the string excluding any line ending +# characters +def len_minus_line_ending(s): + if s is None: + return 0 + n = len(s) + if s.endswith('\r\n'): + n -= 2 + elif s.endswith('\r') or s.endswith('\n'): + n -= 1 + return n + +# returns the string without the line ending characters +def strip_eol(s): + if s is not None: + return s[:len_minus_line_ending(s)] + +# split string into lines based upon DOS, Mac, and Unix line endings +def splitlines(s): + # split on new line characters + temp, i, n = [], 0, len(s) + while i < n: + j = s.find('\n', i) + if j < 0: + temp.append(s[i:]) + break + j += 1 + temp.append(s[i:j]) + i = j + # split on carriage return characters + ss = [] + for s in temp: + i, n = 0, len(s) + while i < n: + j = s.find('\r', i) + if j < 0: + ss.append(s[i:]) + break + j += 1 + if j < n and s[j] == '\n': + j += 1 + ss.append(s[i:j]) + i = j + return ss + +# also recognize old Mac OS line endings +def readlines(fd): + return [ strip_eol(s) for s in splitlines(fd.read()) ] + # use the program's location as a starting place to search for supporting files # such as icon and help documentation if hasattr(sys, 'frozen'): diff --git a/src/vcs/cvs.py b/src/vcs/cvs.py index 6b887bd..b39318f 100644 --- a/src/vcs/cvs.py +++ b/src/vcs/cvs.py @@ -41,11 +41,11 @@ class Cvs(VcsInterface): if len(r): prev = '.'.join(r) for k in sorted(names): - if prev is None: - k0 = None - else: - k0 = k - result.append([ (k0, prev), (k, rev) ]) + if prev is None: + k0 = None + else: + k0 = k + result.append([ (k0, prev), (k, rev) ]) except ValueError: utils.logError(_('Error parsing revision %s.') % (rev, )) return result @@ -73,7 +73,6 @@ class Cvs(VcsInterface): if s[0] == 'R': # removed modified[k] = [ (k, prev), (None, None) ] - pass elif s[0] == 'A': # added modified[k] = [ (None, None), (k, None) ] diff --git a/src/vcs/folder_set.py b/src/vcs/folder_set.py index a674acc..9fe050e 100644 --- a/src/vcs/folder_set.py +++ b/src/vcs/folder_set.py @@ -42,4 +42,3 @@ class FolderSet: if abspath.startswith(f): return True return False - diff --git a/src/vcs/git.py b/src/vcs/git.py index 46e2903..5aabef5 100644 --- a/src/vcs/git.py +++ b/src/vcs/git.py @@ -65,7 +65,14 @@ class Git(VcsInterface): def getFolderTemplate(self, prefs, names): # build command - args = [ prefs.getString('git_bin'), 'status', '--porcelain', '-s', '--untracked-files=no', '--ignore-submodules=all' ] + args = [ + prefs.getString('git_bin'), + 'status', + '--porcelain', + '-s', + '--untracked-files=no', + '--ignore-submodules=all' + ] # build list of interesting files pwd = os.path.abspath(os.curdir) isabs = False @@ -142,14 +149,13 @@ class Git(VcsInterface): return result def getRevision(self, prefs, name, rev): + relpath = utils.relpath(self.root, os.path.abspath(name)).replace(os.sep, '/') return utils.popenRead( self.root, [ prefs.getString('git_bin'), 'show', - '{}:{}'.format( - rev, - utils.relpath(self.root, os.path.abspath(name)).replace(os.sep, '/')) + f'{rev}:{relpath}' ], prefs, 'git_bash') diff --git a/src/vcs/hg.py b/src/vcs/hg.py index cd42d92..5cf550e 100644 --- a/src/vcs/hg.py +++ b/src/vcs/hg.py @@ -32,7 +32,11 @@ class Hg(VcsInterface): def _getPreviousRevision(self, prefs, rev): if rev is None: if self.working_rev is None: - ss = utils.popenReadLines(self.root, [ prefs.getString('hg_bin'), 'id', '-i', '-t' ], prefs, 'hg_bash') + ss = utils.popenReadLines( + self.root, + [ prefs.getString('hg_bin'), 'id', '-i', '-t' ], + prefs, + 'hg_bash') if len(ss) != 1: raise IOError('Unknown working revision') ss = ss[0].split(' ') @@ -81,7 +85,11 @@ class Hg(VcsInterface): return [ modified[k] for k in sorted(modified.keys()) ] def getCommitTemplate(self, prefs, rev, names): - return self._getCommitTemplate(prefs, names, [ 'log', '--template', 'A\t{file_adds}\nM\t{file_mods}\nR\t{file_dels}\n', '-r', rev ], rev) + return self._getCommitTemplate( + prefs, + names, + [ 'log', '--template', 'A\t{file_adds}\nM\t{file_mods}\nR\t{file_dels}\n', '-r', rev ], + rev) def getFolderTemplate(self, prefs, names): return self._getCommitTemplate(prefs, names, [ 'status', '-q' ], None) diff --git a/src/vcs/mtn.py b/src/vcs/mtn.py index f70f6f4..fc551f4 100644 --- a/src/vcs/mtn.py +++ b/src/vcs/mtn.py @@ -33,7 +33,11 @@ class Mtn(VcsInterface): def getCommitTemplate(self, prefs, rev, names): # build command vcs_bin = prefs.getString('mtn_bin') - ss = utils.popenReadLines(self.root, [ vcs_bin, 'automate', 'select', '-q', rev ], prefs, 'mtn_bash') + ss = utils.popenReadLines( + self.root, + [ vcs_bin, 'automate', 'select', '-q', rev ], + prefs, + 'mtn_bash') if len(ss) != 1: raise IOError('Ambiguous revision specifier') args = [ vcs_bin, 'automate', 'get_revision', ss[0] ] @@ -59,7 +63,7 @@ class Mtn(VcsInterface): break prev = arg1[1:-1] continue - elif prev is None: + if prev is None: continue if arg == 'delete': # deleted file @@ -88,7 +92,11 @@ class Mtn(VcsInterface): if removed or renamed: # remove directories removed_dirs = set() - for s in utils.popenReadLines(self.root, [ vcs_bin, 'automate', 'get_manifest_of', prev ], prefs, 'mtn_bash'): + for s in utils.popenReadLines( + self.root, + [ vcs_bin, 'automate', 'get_manifest_of', prev ], + prefs, + 'mtn_bash'): s = shlex.split(s) if len(s) > 1 and s[0] == 'dir': removed_dirs.add(s[1]) @@ -130,7 +138,14 @@ class Mtn(VcsInterface): fs = FolderSet(names) result = [] pwd, isabs = os.path.abspath(os.curdir), False - args = [ prefs.getString('mtn_bin'), 'automate', 'inventory', '--no-ignored', '--no-unchanged', '--no-unknown' ] + args = [ + prefs.getString('mtn_bin'), + 'automate', + 'inventory', + '--no-ignored', + '--no-unchanged', + '--no-unknown' + ] for name in names: isabs |= os.path.isabs(name) # build list of interesting files @@ -168,7 +183,11 @@ class Mtn(VcsInterface): k = utils.relpath(pwd, k) added[k] = [ (None, None), (k, None) ] processed = True - if 'rename_target' in s and 'file' in m.get('new_type', []) and len(m.get('old_path', [])) > 0: + if ( + 'rename_target' in s and + 'file' in m.get('new_type', []) and + len(m.get('old_path', [])) > 0 + ): # renamed file k0 = os.path.join(self.root, prefs.convertToNativePath(m['old_path'][0])) k1 = os.path.join(self.root, prefs.convertToNativePath(p)) diff --git a/src/vcs/rcs.py b/src/vcs/rcs.py index e8ed73c..f75a7ab 100644 --- a/src/vcs/rcs.py +++ b/src/vcs/rcs.py @@ -25,7 +25,12 @@ from diffuse.vcs.vcs_interface import VcsInterface # RCS support class Rcs(VcsInterface): def getFileTemplate(self, prefs, name): - args = [ prefs.getString('rcs_bin_rlog'), '-L', '-h', utils.safeRelativePath(self.root, name, prefs, 'rcs_cygwin') ] + args = [ + prefs.getString('rcs_bin_rlog'), + '-L', + '-h', + utils.safeRelativePath(self.root, name, prefs, 'rcs_cygwin') + ] rev = '' for line in utils.popenReadLines(self.root, args, prefs, 'rcs_bash'): if line.startswith('head: '): @@ -69,12 +74,12 @@ class Rcs(VcsInterface): recurse = os.path.isdir(os.path.join(s, 'RCS')) if ex or recurse: ex = False - for v in os.listdir(s): - dn = os.path.join(s, v) - if v.endswith(',v') and os.path.isfile(dn): + for d in os.listdir(s): + dn = os.path.join(s, d) + if d.endswith(',v') and os.path.isfile(dn): # map to checkout name r.append(dn[:-2]) - elif v == 'RCS' and os.path.isdir(dn): + elif d == 'RCS' and os.path.isdir(dn): for v in os.listdir(dn): if os.path.isfile(os.path.join(dn, v)): if v.endswith(',v'): diff --git a/src/vcs/svk.py b/src/vcs/svk.py index 4ee1696..1e021c1 100644 --- a/src/vcs/svk.py +++ b/src/vcs/svk.py @@ -18,7 +18,6 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os -import glob from diffuse import utils from diffuse.vcs.svn import Svn @@ -32,7 +31,7 @@ class Svk(Svn): def _parseStatusLine(self, s): if len(s) < 4 or s[0] not in 'ACDMR': - return + return '', '' return s[0], s[4:] def _getPreviousRevision(self, rev): @@ -43,6 +42,7 @@ class Svk(Svn): return str(int(rev) - 1) def getRevision(self, prefs, name, rev): + relpath = utils.relpath(self.root, os.path.abspath(name)).replace(os.sep, '/') return utils.popenRead( self.root, [ @@ -50,9 +50,7 @@ class Svk(Svn): 'cat', '-r', rev, - '{}/{}'.format( - self._getURL(prefs), - utils.relpath(self.root, os.path.abspath(name)).replace(os.sep, '/')) + f'{self._getURL(prefs)}/{relpath}' ], prefs, 'svk_bash') diff --git a/src/vcs/svn.py b/src/vcs/svn.py index b6d3ca7..339d0a5 100644 --- a/src/vcs/svn.py +++ b/src/vcs/svn.py @@ -39,7 +39,7 @@ class Svn(VcsInterface): def _parseStatusLine(self, s): if len(s) < 8 or s[0] not in 'ACDMR': - return + return '', '' # subversion 1.6 adds a new column k = 7 if k < len(s) and s[k] == ' ': @@ -50,8 +50,7 @@ class Svn(VcsInterface): if rev is None: return 'BASE' m = int(rev) - if m > 1: - return str(m - 1) + return str(max(m > 1, 0)) def _getURL(self, prefs): if self.url is None: @@ -152,7 +151,7 @@ class Svn(VcsInterface): m[d] = set() m[d].add(b) # remove items we can easily determine to be directories - for k in m.keys(): + for k in m: d = os.path.dirname(k) if d in m: m[d].discard(os.path.basename(k)) @@ -161,7 +160,18 @@ class Svn(VcsInterface): # determine which are directories added = {} for p, v in m.items(): - for s in utils.popenReadLines(self.root, [ vcs_bin, 'list', '-r', rev, '{}/{}'.format(self._getURL(prefs), p.replace(os.sep, '/')) ], prefs, vcs_bash): + lines = utils.popenReadLines( + self.root, + [ + vcs_bin, + 'list', + '-r', + rev, + f"{self._getURL(prefs)}/{p.replace(os.sep, '/')}" + ], + prefs, + vcs_bash) + for s in lines: if s in v: # confirmed as added file k = os.path.join(self.root, os.path.join(p, s)) @@ -187,7 +197,18 @@ class Svn(VcsInterface): m[d].add(b) removed_dir, removed = set(), {} for p, v in m.items(): - for s in utils.popenReadLines(self.root, [ vcs_bin, 'list', '-r', prev, '{}/{}'.format(self._getURL(prefs), p.replace(os.sep, '/')) ], prefs, vcs_bash): + lines = utils.popenReadLines( + self.root, + [ + vcs_bin, + 'list', + '-r', + prev, + f"{self._getURL(prefs)}/{p.replace(os.sep, '/')}" + ], + prefs, + vcs_bash) + for s in lines: if s.endswith('/'): s = s[:-1] if s in v: @@ -205,7 +226,18 @@ class Svn(VcsInterface): tmp = removed_dir removed_dir = set() for p in tmp: - for s in utils.popenReadLines(self.root, [ vcs_bin, 'list', '-r', prev, '{}/{}'.format(self._getURL(prefs), p.replace(os.sep, '/')) ], prefs, vcs_bash): + lines = utils.popenReadLines( + self.root, + [ + vcs_bin, + 'list', + '-r', + prev, + f"{self._getURL(prefs)}/{p.replace(os.sep, '/')}" + ], + prefs, + vcs_bash) + for s in lines: if s.endswith('/'): # confirmed item as directory removed_dir.add(os.path.join(p, s[:-1])) @@ -239,7 +271,7 @@ class Svn(VcsInterface): [ vcs_bin, 'cat', - '{}@{}'.format(utils.safeRelativePath(self.root, name, prefs, 'svn_cygwin'), rev) + f"{utils.safeRelativePath(self.root, name, prefs, 'svn_cygwin')}@{rev}" ], prefs, 'svn_bash') @@ -248,10 +280,10 @@ class Svn(VcsInterface): [ vcs_bin, 'cat', - '{}/{}@{}'.format( - self._getURL(prefs), - utils.relpath(self.root, os.path.abspath(name)).replace(os.sep, '/'), - rev) + ( + f"{self._getURL(prefs)}/" + f"{utils.relpath(self.root, os.path.abspath(name)).replace(os.sep, '/')}@{rev}" + ) ], prefs, 'svn_bash') diff --git a/src/vcs/vcs_interface.py b/src/vcs/vcs_interface.py index 16b57a0..c2b2b15 100644 --- a/src/vcs/vcs_interface.py +++ b/src/vcs/vcs_interface.py @@ -18,22 +18,21 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. class VcsInterface: + """Interface for the VCSs.""" + def __init__(self, root): """The object will initialized with the repository's root folder.""" - self.root = root def getFileTemplate(self, prefs, name): - """Indicates which revisions to display for a file when none were explicitly requested.""" - pass + """Indicates which revisions to display for a file when none were explicitly + requested.""" def getCommitTemplate(self, prefs, rev, names): """Indicates which file revisions to display for a commit.""" - pass def getFolderTemplate(self, prefs, names): """Indicates which file revisions to display for a set of folders.""" def getRevision(self, prefs, name, rev): """Returns the contents of the specified file revision""" - pass diff --git a/src/vcs/vcs_registry.py b/src/vcs/vcs_registry.py index ac17175..a08b792 100644 --- a/src/vcs/vcs_registry.py +++ b/src/vcs/vcs_registry.py @@ -46,9 +46,6 @@ class VcsRegistry: 'svn': _get_svn_repo } - def setSearchOrder(self, ordering): - self._search_order = ordering - # determines which VCS to use for files in the named folder def findByFolder(self, path, prefs): path = os.path.abspath(path) @@ -57,11 +54,13 @@ class VcsRegistry: repo = self._get_repo[vcs](path, prefs) if repo: return repo + return None # determines which VCS to use for the named file def findByFilename(self, name, prefs): if name is not None: return self.findByFolder(os.path.dirname(name), prefs) + return None # utility method to help find folders used by version control systems @@ -77,29 +76,34 @@ def _find_parent_dir_with(path, dir_name): def _get_bzr_repo(path, prefs): p = _find_parent_dir_with(path, '.bzr') - if p: - return Bzr(p) + return Bzr(p) if p else None def _get_cvs_repo(path, prefs): - if os.path.isdir(os.path.join(path, 'CVS')): - return Cvs(path) + return Cvs(path) if os.path.isdir(os.path.join(path, 'CVS')) else None def _get_darcs_repo(path, prefs): p = _find_parent_dir_with(path, '_darcs') - if p: - return Darcs(p) + return Darcs(p) if p else None def _get_git_repo(path, prefs): if 'GIT_DIR' in os.environ: try: d = path - ss = utils.popenReadLines(d, [ prefs.getString('git_bin'), 'rev-parse', '--show-prefix' ], prefs, 'git_bash') + ss = utils.popenReadLines( + d, + [ + prefs.getString('git_bin'), + 'rev-parse', + '--show-prefix' + ], + prefs, + 'git_bash') if len(ss) > 0: # be careful to handle trailing slashes d = d.split(os.sep) if d[-1] != '': d.append('') - ss = strip_eol(ss[0]).split('/') + ss = utils.strip_eol(ss[0]).split('/') if ss[-1] != '': ss.append('') n = len(ss) @@ -110,7 +114,7 @@ def _get_git_repo(path, prefs): else: d = os.sep.join(d) return Git(d) - except (IOError, OSError, WindowsError): + except (IOError, OSError): # working tree not found pass # search for .git directory (project) or .git file (submodule) @@ -125,13 +129,11 @@ def _get_git_repo(path, prefs): def _get_hg_repo(path, prefs): p = _find_parent_dir_with(path, '.hg') - if p: - return Hg(p) + return Hg(p) if p else None def _get_mtn_repo(path, prefs): p = _find_parent_dir_with(path, '_MTN') - if p: - return Mtn(p) + return Mtn(p) if p else None def _get_rcs_repo(path, prefs): if os.path.isdir(os.path.join(path, 'RCS')): @@ -147,11 +149,11 @@ def _get_rcs_repo(path, prefs): except OSError: # the user specified an invalid folder name pass + return None def _get_svn_repo(path, prefs): p = _find_parent_dir_with(path, '.svn') - if p: - return Svn(p) + return Svn(p) if p else None def _get_svk_repo(path, prefs): name = path @@ -166,9 +168,8 @@ def _get_svk_repo(path, prefs): if os.path.isfile(svkconfig): try: # find working copies by parsing the config file - f = open(svkconfig, 'r') - ss = readlines(f) - f.close() + with open(svkconfig, 'r', encoding='utf-8') as f: + ss = utils.readlines(f) projs, sep = [], os.sep # find the separator character for s in ss: @@ -183,7 +184,11 @@ def _get_svk_repo(path, prefs): while i < len(ss) and ss[i].startswith(' '): s = ss[i] i += 1 - if s.endswith(': ') and i < len(ss) and ss[i].startswith(' depotpath: '): + if ( + s.endswith(': ') and + i < len(ss) and + ss[i].startswith(' depotpath: ') + ): key = s[4:-2].replace(sep, os.sep) # parse directory path j, n, tt = 0, len(key), [] @@ -195,7 +200,7 @@ def _get_svk_repo(path, prefs): if key[j] == '"': j += 1 break - elif key[j] == '\\': + if key[j] == '\\': # escaped character j += 1 if j < n: @@ -214,3 +219,4 @@ def _get_svk_repo(path, prefs): return Svk(path) except IOError: utils.logError(_('Error parsing %s.') % (svkconfig, )) + return None