Skip to main content

Python Support

OneDev analyzes python code and CI/CD reports to make important information readily available to aid code navigation, comprehensation and review.

For python projects, it is able to:

  1. Analyze code for symbol navigation and search
  2. Display/search outline while view source code
  3. Suggest CI/CD job templates
  4. Show unit test, coverage and lint report, as well as statistics over time
  5. Annotate source code with coverage and lint information

Let’s see how to get all of these with a real-world python project poetry.

Prepare the Environment

Firstable, run OneDev with below command:

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock -v ./onedev:/opt/onedev -p 6610:6610 -p 6611:6611 1dev/server

Follow on screen instructions to set up the server, then select import from url like below:

import from url

Specify import url as https://github.com/python-poetry/poetry on next screen, and click import. Two projects will be created after a while like below:

project list

Here project python-poetry corresponds to organization python-poetry, and its child project python-poetry/poetry contains imported repository. The hierarchical structure makes project management and setting inheritance a lot easier.

Code Analysis and Indexing

Now open project python-poetry/poetry to get into the repository page:

repository indexing

For just imported repository, a indexing in progress message will be shown to indicate that the source code is currently being analyzed and indexed. After that we can press ‘T’ to search symbols. For instance, type application to list all related definitions:

application search

If we select the first entry, python file defining Application class will be shown:

application class

Symbols can be highlighted and navigated in source view page. It also shows the code outline, which can be searched by pressing ‘O’.

CI/CD Support and Report Analysis

Now let’s add CI/CD jobs for this project. To do it, go back to repository root and click the link adding .onedev-buildspec.yml like below:

add build spec

OneDev shows a GUI for CI/CD job setup, and the result will be saved into file .onedev-buildspec.yml in root of the repository. You may add job manually at this point, but OneDev will analyze your repository files and provide appropriate templates for you to start with:

job template

For python, the template is created by inspecting files such as pyproject.toml, tox.ini, poetry.lock, setup.py, setup.cfg, or environment.yaml. Let’s use provided template and head on to job detail page:

job steps

You may edit steps and other settings as necessary here. For this project, we just save and commit without any change. The job will run automatically as it defines a trigger to fire upon commit:

job committed

Switch to running build of the job, wait a while, and it should be successful like below:

build successful

We can see from this page that the build version is detected from pyproject.toml, and also unit test and coverage report is published:

unit test report

coverage report

A ruff lint report can also be published by uncommenting the ruff check line in test and lint step of the job:


#poetry run ruff check - exit-zero - output-format=json - output-file=ruff-result.json - exclude=.git

Also edit file pyproject.toml to remove the line ignoring rule N818 in section tool.ruff.lint to introduce some rule violations. Save and commit the file, and the new build will fail. A ruff report tab is now available showing all violations:

ruff report

We can click the line numbers to drill down violations in source view:

source annotation

Besides violations, the source view is also marked with code coverage info. When review code via pull request, these information will also be available in diff view if applicable.

Thanks for reading.