Wednesday 14 September 2016

Install and configure SonarQube for Python code

In this blog post I will show you how to install and configure SonarQube in order to manage the code quality of your Python project.

Installing SonarQube

  1. Go to the SonarQube official website and download the latest version. At the time of writing this blog post the latest version is 6.0.
  2. The downloaded file is in a ZIP format so you have to unzip it in a folder of your choice. I am using Ubuntu so I extracted the zip in /opt/sonarqube.
  3. Now open a terminal/command prompt and go to $SONAR_INSTALL_PATH/bin/$OS where $OS is your operating system. For me it is linux-x86-64.
  4. Depending on you operating system you can start the SonarQube server as follows:
    1. For Windows - type StartSonar.bat and hit Enter.
    2. For Linux/MacOS - type ./sonar.sh console and hit Enter.
      If you have some issues related access denied then run the command as sudo.
      If you have some issues related to JVM such as "wrapper  | Unable to start JVM: No such file or directory" or similar then go to $SONAR_INSTALL_PATH/conf and open the wrapper.conf file. Edit the wrapper.java.command property and set it to an appropriate Java path. In my case it is /opt/java/jdk1.8.0_101/bin/java.
  5. Open a browser and go to http://localhost:9000/. If you see something then everything is ok. Otherwise you should take an eye on the console output and investigate the issue.

Installing the Python plug-in

  1. Open a browser and go to http://localhost:9000/
  2. Log in with admin/admin.
  3. Go to the  Administration tab -> System -> Update Center (these may vary due to your SonarQube server version).
  4. Click on the Available button.
  5. Search for the Python plug-in and choose Install.
  6. Restart the SonarQube server if needed.

Installing SonarQube Scanner

  1. Go to the SonarQube Scanner page and download the latest version. At the time of writing this blog post the latest version is 2.7.
  2. And again the downloaded file is in a ZIP format and you have to unzip it in a folder of your choice. In my case it is in /opt/sonar-scanner.
  3. Optional but a good idea is to add the $SONAR_SCANNER_PATH/bin to your path variable because the sonar-scanner command will be used later.
  4. Now is the time to configure some settings of the scanner. Open the $SONAR_SCANNER_PATH/conf/sonar-scanner.properties file. You can set some properties for your project. The most common and their defaults are:
    1. sonar.host.url=http://localhost:9000 - the URL of the SonarQube server
    2. sonar.jdbc.username=sonar - username for the database (if you want an external database)
    3. sonar.jdbc.password=sonar - password for the database
    4. sonar.jdbc.url - the URL string to the database (Oracle, MySQL, Postgre and so on)

Configuring the project to be scanned

  1. Go to the Python project you want to scan and create a file named "sonar-project.properties".
  2. Now edit the file and set some properties:
    1. sonar.projectKey=my:project - the project id
    2. sonar.projectName=My project - the project name
    3. sonar.projectVersion=1.0 - the project version
      These 3 properties are required
    4. sonar.sources=src1,src2 - the directories with sources you want to scan (comma-separated)
    5. sonar.language=py - the programming language (Java, Python, etc.)
    6. sonar.python.pylint=/usr/local/bin/pylint - the path to the pylin, if you want to include some quality rules from it

Running the scan

  1. Make sure the SonarQube server is up and running.
  2. Open a terminal/command prompt and navigate to the project folder where the "sonar-project.properties" file is located.
  3. Type sonar-scanner and hit Enter (if you added the sonar-scanner path to you path variable, otherwise use the absolute path to the command).
  4. When the execution is done go to the SonarQube server URL, e.g. http://localhost:9000.
  5. You should see something like this:
  6. You can log with admin/admin credentials and make some investigation of the features.

    Conclusion

    That was just a brief explanation of how to run a Sonar scan over your Python project and the aim was not a review of the Sonar functionalities.

12 comments:

  1. hi...
    im getting error while creating sonar-project.properties file in my project directory . sonar-scanner command is not able to find the .properties file.

    ReplyDelete
    Replies
    1. Hello,

      1) Are you trying to run .sonar-scanner from the project directory?
      2) If so, did you add the {SONAR_SCANNER_PATH}/bin to your path variable?

      Regards,
      Ivan

      Delete

  2. Great Article. its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    python online training

    ReplyDelete
  3. Pylint report is not uploaded in sonarqube even when all the rules of pylint are imported and activated in sonarqube server

    ReplyDelete
  4. I'm new for python. How can I find the sonar.projectKey in my Python project folders?

    ReplyDelete
    Replies
    1. Hi,

      sonar.projectKey can be whatever you want. Just make sure there is no other projects with this key.

      Delete
  5. Can anyone share the working python application repository.
    Thanks in advance

    ReplyDelete
    Replies
    1. Which application? If you have an existing Python application, just use it. This article supposes that you already have a Python project.

      Delete
    2. Unfortunately I don't have any Python projects and I'm new to Python. So, if you can provide me any existing project. That would be great.

      Delete
    3. You can find some beginner projects here:
      https://github.com/MunGell/awesome-for-beginners#python

      Delete