Static Code Analyzer. Stage 3/5

Analyze multi-file projects

Report a typo

Description

As a rule, real projects contain more than a single file. Also, project directories often contain not only Python files, and we don't need to check if an HTML file follows PEP8.

We recommend that you check out a tutorial on tutorialspoint.com that can help you to familiarize yourself with os.walk().

Objectives

In this stage, you need to improve your program so that it can analyze all Python files inside a specified directory.

Please note that:

  1. You also need to change the input format. Instead of reading the path from the standard input, the program must obtain it as a command-line argument:

    > python code_analyzer.py directory-or-file
  2. The output format also needs to be changed slightly. It should include the path to the analyzed file:

    Path: Line X: Code Message 
  3. All output lines must be sorted in ascending order according to the file name, line number, and issue code.

Once again:

  • It is important that all the checks implemented in the previous stages continue to work properly.

  • If a line contains the same stylistic issue several times, your program must print the information only once. If a line has several issues with different types of error codes, they should be printed in ascending order.

  • To simplify the solution, we consider it acceptable if your program finds some false-positive stylistic issues in strings, especially in multi-lines ('''...''' and """...""").

  • We recommend that you break your program code into a set of functions and classes to avoid confusion.

Examples

Example 1. Only a single file is specified as the input:

> python code_analyzer.py /path/to/file/script.py
/path/to/file/script.py: Line 1: S004 At least two spaces required before inline comments
/path/to/file/script.py: Line 2: S003 Unnecessary semicolon
/path/to/file/script.py: Line 3: S001 Too long line
/path/to/file/script.py: Line 3: S003 Unnecessary semicolon
/path/to/file/script.py: Line 6: S001 Too long line
/path/to/file/script.py: Line 11: S006 More than two blank lines used before this line
/path/to/file/script.py: Line 13: S003 Unnecessary semicolon
/path/to/file/script.py: Line 13: S004 At least two spaces required before inline comments
/path/to/file/script.py: Line 13: S005 TODO found

Example 2. The input path is a directory; the output should contain all Python files from it:

> python code_analyzer.py /path/to/project
/path/to/project/__init__.py: Line 1: S001 Too long line
/path/to/project/script1.py: Line 1: S004 At least two spaces required before inline comments
/path/to/project/script1.py: Line 2: S003 Unnecessary semicolon
/path/to/project/script2.py: Line 1: S004 At least two spaces required before inline comments
/path/to/project/script2.py: Line 3: S001 Too long line
/path/to/project/somedir/script.py: Line 3: S001 Too long line
/path/to/project/test.py: Line 3: Line 13: S003 Unnecessary semicolon
Write a program
IDE integration
Checking the IDE status
___

Create a free account to access the full topic