|
1 | 1 | # sonar-scanner-python
|
2 | 2 | A wrapper around SonarScanner CLI, available on PyPI.
|
3 | 3 |
|
4 |
| -# Installation |
| 4 | +# Installation |
| 5 | + |
| 6 | +Install with pip: |
| 7 | +``` |
| 8 | +pip install sonar-scanner-python FIXME -- The actual package name is not yet defined. Refer to PYSCAN-35 |
| 9 | +``` |
| 10 | + |
| 11 | +# Usage |
| 12 | + |
| 13 | +Once installed, the `sonar-scanner-python` can be run from the command line to perform an analysis. |
| 14 | +It assumes a running SonarQube server or a project configured on SonarCloud. |
| 15 | + |
| 16 | +## Setting up analysis properties |
| 17 | + |
| 18 | +In order for the analysis to run, analysis properties need to be defined. |
| 19 | +There are multiple ways of providing these properties, described below in descending order of priority: |
| 20 | + |
| 21 | +* Through CLI arguments to the `sonar-scanner-python` command |
| 22 | +* Under the `[tool.sonar]` key of the `pyproject.toml` file |
| 23 | +* In a dedicated `sonar-project.properties` file |
| 24 | +* Through environment variables |
| 25 | + |
| 26 | +### Through CLI arguments |
| 27 | + |
| 28 | +Analysis properties can be provided as CLI arguments to the `sonar-scanner-python` command. |
| 29 | +They follow the same convention as when running the SonarScanner CLI directly |
| 30 | +(see [documentation](https://docs.sonarsource.com/sonarqube/9.9/analyzing-source-code/scanners/sonarscanner/#running-from-zip-file)). |
| 31 | +This means that analysis properties provided that way should be prepended with `-D`, for instance: |
| 32 | + |
| 33 | +``` |
| 34 | +$ sonar-scanner-python -Dsonar.login=myAuthenticationToken FIXME -- The actual command name is not yet defined. Refer to PYSCAN-35 |
| 35 | +``` |
| 36 | + |
| 37 | +You can use all the argument allowed by __SonarScanner__. |
| 38 | +For more information on __SonarScanner__ please refer to the [SonarScanner documentation](https://docs.sonarsource.com/sonarqube/9.9/analyzing-source-code/scanners/sonarscanner/) |
| 39 | + |
| 40 | +### With a pyproject.toml file |
| 41 | + |
| 42 | +Inside a `pyproject.toml`, Sonar analysis properties can be defined under the `tool.sonar` table. |
| 43 | + |
| 44 | +``` |
| 45 | +[tool.sonar] |
| 46 | +# must be unique in a given SonarQube/SonarCloud instance |
| 47 | +projectKey=my:project |
| 48 | +
|
| 49 | +# --- optional properties --- |
| 50 | +# defaults to project key |
| 51 | +#projectName=My project |
| 52 | +# defaults to 'not provided' |
| 53 | +#projectVersion=1.0 |
| 54 | + |
| 55 | +# Path is relative to the pyproject.toml file. Defaults to . |
| 56 | +#sources=. |
| 57 | + |
| 58 | +# Encoding of the source code. Default is default system encoding |
| 59 | +#sourceEncoding=UTF-8 |
| 60 | +``` |
| 61 | + |
| 62 | +The configuration parameters can be found in the [SonarQube documentation](https://docs.sonarsource.com/sonarqube/9.9/analyzing-source-code/analysis-parameters/). |
| 63 | + |
| 64 | +In the `pyproject.toml` file the prefix `sonar.` for parameter keys should be omitted. |
| 65 | +For example, `sonar.scm.provider` in the documentation will become `scm.provider` in the `pyproject.toml` file. |
| 66 | + |
| 67 | +By default, the scanner will expect the `pyproject.toml` file to be present in the current directory. |
| 68 | +However, its path can be provided manually through the `toml.path` ([PYSCAN-40](https://sonarsource.atlassian.net/jira/software/c/projects/PYSCAN/issues/PYSCAN-40)) CLI argument as well as through the `sonar.projectHome` argument. For instance: |
| 69 | + |
| 70 | +``` |
| 71 | +sonar-scanner-python -Dtoml.path="path/to/pyproject.toml" |
| 72 | +``` |
| 73 | + |
| 74 | +Or: |
| 75 | + |
| 76 | +``` |
| 77 | +sonar-scanner-python -Dsonar.projectHome="path/to/projectHome" |
| 78 | +``` |
| 79 | + |
| 80 | +### With a sonar-project.properties file |
| 81 | + |
| 82 | +Exactly like [__SonarScanner__](https://docs.sonarsource.com/sonarqube/9.9/analyzing-source-code/scanners/sonarscanner/), |
| 83 | +the analysis can also be configured with a `sonar-project.properties` file: |
| 84 | + |
| 85 | +``` |
| 86 | +# must be unique in a given SonarQube/SonarCloud instance |
| 87 | +sonar.projectKey=my:project |
| 88 | +
|
| 89 | +# --- optional properties --- |
| 90 | +
|
| 91 | +# defaults to project key |
| 92 | +#sonar.projectName=My project |
| 93 | +# defaults to 'not provided' |
| 94 | +#sonar.projectVersion=1.0 |
| 95 | + |
| 96 | +# Path is relative to the sonar-project.properties file. Defaults to . |
| 97 | +#sonar.sources=. |
| 98 | + |
| 99 | +# Encoding of the source code. Default is default system encoding |
| 100 | +#sonar.sourceEncoding=UTF-8 |
| 101 | +``` |
| 102 | + |
| 103 | +### Through environment variables |
| 104 | + |
| 105 | +It is also possible to define configure the scanner through environment variables: |
| 106 | + |
| 107 | +``` |
| 108 | +$ export SONAR_HOST_URL="http://localhost:9000" |
| 109 | +$ sonar-scanner-python FIXME -- The actual command name is not yet defined. Refer to PYSCAN-35 |
| 110 | +``` |
| 111 | + |
| 112 | +See the __SonarScanner__ [documentation](https://docs.sonarsource.com/sonarqube/9.9/analyzing-source-code/scanners/sonarscanner/) for more information. |
| 113 | + |
| 114 | +# For developers |
| 115 | + |
5 | 116 | ## Prerequisites
|
6 | 117 |
|
7 | 118 | - Python 3.12
|
@@ -49,7 +160,8 @@ For more options on the version update see [the hatch documentation](https://hat
|
49 | 160 | # Tooling
|
50 | 161 | ## Formatting
|
51 | 162 |
|
52 |
| -Run `hatch run tool:format` to run the formatter on all files. |
| 163 | +Run `hatch run tool:format` to run the check the formatting on all files. |
| 164 | +To automatically apply formatting, run `hatch run tool:apply_format`. |
53 | 165 |
|
54 | 166 | ## Type checking
|
55 | 167 |
|
|
0 commit comments