"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.71.1/tools/clean_python.py" (14 Mar 2019, 5199 Bytes) of package /linux/privat/zim-0.71.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "clean_python.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 #!/usr/bin/python3
    2 
    3 """
    4     This module is meant to be run as script to clean up all of Zim's python
    5     code. It mostly works by applying fixes provided by autopep8 to make code
    6     comply to the PEP8 standard, https://www.python.org/dev/peps/pep-0008/ .
    7 
    8     Copyright: Christian Stadelmann, 2017
    9 """
   10 
   11 
   12 import os
   13 import subprocess
   14 import tempfile
   15 
   16 
   17 class CleanupTask(object):
   18     """
   19         An abstract base class for cleanup tasks.
   20     """
   21 
   22     def __init__(self, name, description):
   23         self._name = name
   24         self._description = description
   25 
   26     def cleanup_func(self):
   27         """
   28             Child classes should implement their cleanup code here.
   29         """
   30         raise NotImplementedError()
   31 
   32     def commit_to_git(self):
   33         """
   34             A convenience function to commit any changes to git
   35         """
   36         git_cmd = ["git", "diff", "--name-only"]
   37         complproc = subprocess.run(git_cmd, stdout=subprocess.PIPE, check=True)
   38         if not complproc.stdout or complproc.stdout.isspace():
   39             print(("Not commiting anything because nothing changed in cleanup "
   40                   "task %s" % self._name))
   41             return
   42 
   43         print(("Commiting changes for cleanup task %s" % self._name))
   44 
   45         with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpfile:
   46             tmpfile.write(self._name)
   47             tmpfile.write("\n\n")
   48             tmpfile.write(self._description)
   49 
   50         git_cmd = ["git", "commit", "-a", "-F", tmpfile.name]
   51         subprocess.run(git_cmd, check=True)
   52 
   53         os.remove(tmpfile.name)
   54 
   55 
   56 class UnifyLineEndings(CleanupTask):
   57 
   58     def __init__(self):
   59         CleanupTask.__init__(self,
   60                              "Fix line separation characters",
   61                              "Replace \\r\\n by \\n everywhere.")
   62 
   63     def cleanup_func(self):
   64         for root, dirs, files in os.walk('.'):
   65             for name in files:
   66                 if name[-3:] == '.py':
   67                     filename = os.path.join(root, name)
   68                     UnifyLineEndings.__cleanup_file(filename)
   69 
   70     @staticmethod
   71     def __cleanup_file(filename):
   72         data = None
   73         dirty = False
   74         with open(filename, mode='r', newline='') as file:
   75             data = file.read()
   76             if '\r\n' in data:
   77                 data = data.replace('\r\n', '\n')
   78                 dirty = True
   79             if '\r' in data:
   80                 data = data.replace('\r', '\n')
   81                 dirty = True
   82         if dirty:
   83             new_filename = filename + '.new'
   84             with open(new_filename, mode='w', newline='\n') as new_file:
   85                 new_file.write(data)
   86             os.replace(new_filename, filename)
   87 
   88 
   89 class Autopep8Task(CleanupTask):
   90     """
   91         An abstract class for running autopep8 to clean up code.
   92     """
   93 
   94     def __init__(self, name, pep8_classes):
   95         self.__cmd = ["autopep8", "--jobs", "4", "--aggressive",
   96                       "--in-place", "--recursive", "--select", pep8_classes,
   97                       "."]
   98         name = "Autopep8: %s" % name
   99         description = \
  100             "Run the '%s' fixer(s) from autopep8 over Zim source: \n\t`%s`" % (
  101                 name, " ".join(self.__cmd)
  102             )
  103         CleanupTask.__init__(self, name, description)
  104 
  105     def cleanup_func(self):
  106         print((self._description))
  107         subprocess.run(self.__cmd, check=True)
  108 
  109 
  110 def _get_tests():
  111     unify_line_endings = UnifyLineEndings()
  112     autopep8_indentation_tabs = Autopep8Task(
  113         "Indentation contains mixed spaces and tabs", "E101")
  114     autopep8_statements_on_separate_lines = Autopep8Task(
  115         "Statements on separate lines", "E701,E702,E703")
  116     autopep8_more_statements = Autopep8Task(
  117         "More Statements", "E7")
  118     autopep8_deprecations = Autopep8Task(
  119         "Deprecation warning", "W6")
  120     autopep8_blackslash = Autopep8Task(
  121         "The backslash is redundant between brackets", "E502")
  122     autopep8_imports = Autopep8Task(
  123         "Imports", "E4")
  124     autopep8_whitespace_in_code = Autopep8Task(
  125         "Whitespace in code", "E20,E21,E22,E23,E24,E27")
  126     autopep8_whitespace_in_comments = Autopep8Task(
  127         "Whitespace in comments", "E26")
  128     autopep8_blank_lines = Autopep8Task(
  129         "Blank line", "E3,W3")
  130     autopep8_indentation_in_comments = Autopep8Task(
  131         "Indentation in comments", "E11")
  132     autopep8_indentation_in_code = Autopep8Task(
  133         "Indentation in code", "E12,W1")
  134 
  135     # please note: the order is important here!
  136     return [unify_line_endings,
  137             # autopep8_indentation_tabs,
  138             autopep8_statements_on_separate_lines,
  139             autopep8_more_statements,
  140             autopep8_deprecations,
  141             autopep8_blackslash,
  142             autopep8_imports,
  143             autopep8_whitespace_in_code,
  144             #autopep8_whitespace_in_comments,
  145             #autopep8_blank_lines,
  146             #autopep8_indentation_in_comments,
  147             #autopep8_indentation_in_code,
  148            ]
  149 
  150 
  151 def main():
  152     subprocess.run(['make', 'clean'])
  153 
  154     cleaners = _get_tests()
  155 
  156     for cleaner in cleaners:
  157         cleaner.cleanup_func()
  158         cleaner.commit_to_git()
  159     exit(0)
  160 
  161 if __name__ == '__main__':
  162     main()