def main(): setup(go_main=False) # So PyLint finds nuitka package. addPYTHONPATH(getHomePath()) setupPATH() parser = OptionParser() parser.add_option( "--diff", action="store_true", dest="diff", default=False, help="""\ Analyse the changed files in git. Default is %default.""", ) parser.add_option( "--show-todos", "--todos", action="store_true", dest="todos", default=False, help="""\ Show TODO items. Default is %default.""", ) parser.add_option( "--verbose", action="store_true", dest="verbose", default=False, help="""\ Be verbose in output. Default is %default.""", ) parser.add_option( "--one-by-one", action="store_true", dest="one_by_one", default=False, help="""\ Check files one by one. Default is %default.""", ) parser.add_option( "--not-installed-is-no-error", action="store_true", dest="not_installed_is_no_error", default=False, help="""\ Insist on PyLint to be installed. Default is %default.""", ) options, positional_args = parser.parse_args() if options.not_installed_is_no_error and not hasModule("pylint"): print("PyLint is not installed for this interpreter version: SKIPPED") sys.exit(0) if positional_args: if options.diff: sys.exit("Error, no filenames argument allowed in git diff mode.") else: goHome() if options.diff: positional_args = [ filename for filename in getModifiedPaths() if isPythonFile(filename) ] else: positional_args = [ "bin", "nuitka", "setup.py", "tests/*/run_all.py" ] positional_args = sum( (resolveShellPatternToFilenames(positional_arg) for positional_arg in positional_args), [], ) if not positional_args: sys.exit("No files found.") print("Working on:", positional_args) ignore_list = [] # Avoid checking the Python2 runner along with the one for Python3, it has name collisions. if python_version >= 0x300: ignore_list.append("nuitka") filenames = list( scanTargets(positional_args, suffixes=(".py", ".scons"), ignore_list=ignore_list)) PyLint.executePyLint( filenames=filenames, show_todos=options.todos, verbose=options.verbose, one_by_one=options.one_by_one, ) if not filenames: sys.exit("No files found.") sys.exit(PyLint.our_exit_code)
def autoformat(filename, git_stage, abort, effective_filename=None, trace=True): """Format source code with external tools Args: filename: filename to work on git_stage: indicate if this is to be done on staged content abort: error exit in case a tool shows a problem effective_filename: derive type of file from this name Notes: The effective filename can be used in case this is already a temporary filename intended to replace another. Returns: None """ # This does a lot of distinctions, pylint: disable=too-many-branches,too-many-statements if effective_filename is None: effective_filename = filename if os.path.isdir(effective_filename): return filename = os.path.normpath(filename) effective_filename = os.path.normpath(effective_filename) if trace: my_print("Consider", filename, end=": ") is_python = isPythonFile(filename, effective_filename) is_c = effective_filename.endswith((".c", ".h")) is_txt = effective_filename.endswith( ( ".patch", ".txt", ".rst", ".sh", ".in", ".md", ".asciidoc", ".nuspec", ".yml", ".stylesheet", ".j2", ".gitignore", ".json", ".spec", "-rpmlintrc", ) ) or os.path.basename(filename) in ( "changelog", "compat", "control", "copyright", "lintian-overrides", ) is_rst = effective_filename.endswith(".rst") # Some parts of Nuitka must not be re-formatted with black or clang-format # as they have different intentions. if not (is_python or is_c or is_txt or is_rst): my_print("Ignored file type.") return # Work on a temporary copy tmp_filename = filename + ".tmp" if git_stage: old_code = getFileHashContent(git_stage["dst_hash"]) else: old_code = getFileContents(filename, "rb") with open(tmp_filename, "wb") as output_file: output_file.write(old_code) try: if is_python: cleanupWindowsNewlines(tmp_filename) if not _shouldNotFormatCode(effective_filename): _cleanupImportSortOrder(tmp_filename) _cleanupPyLintComments(tmp_filename, abort) black_call = _getPythonBinaryCall("black") subprocess.call(black_call + ["-q", "--fast", tmp_filename]) cleanupWindowsNewlines(tmp_filename) elif is_c: cleanupWindowsNewlines(tmp_filename) if not _shouldNotFormatCode(effective_filename): _cleanupClangFormat(tmp_filename) cleanupWindowsNewlines(tmp_filename) elif is_txt: cleanupWindowsNewlines(tmp_filename) _cleanupTrailingWhitespace(tmp_filename) cleanupWindowsNewlines(tmp_filename) if is_rst: _cleanupRstFmt(tmp_filename) _transferBOM(filename, tmp_filename) changed = False if old_code != getFileContents(tmp_filename, "rb"): if trace: my_print("Updated.") with withPreserveFileMode(filename): if git_stage: new_hash_value = putFileHashContent(tmp_filename) updateFileIndex(git_stage, new_hash_value) updateWorkingFile(filename, git_stage["dst_hash"], new_hash_value) else: renameFile(tmp_filename, filename) changed = True else: if trace: my_print("OK.") return changed finally: if os.path.exists(tmp_filename): os.unlink(tmp_filename)