Skip to content

Commit 84d4394

Browse files
authored
Merge pull request github#5755 from RasmusWL/non-alert-data-part1
Approved by tausbn
2 parents 0cb826a + f2b4e31 commit 84d4394

19 files changed

+124
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @name Python extraction errors
3+
* @description List all extraction errors for Python files in the source code directory.
4+
* @kind diagnostic
5+
* @id py/diagnostics/extraction-errors
6+
*/
7+
8+
import python
9+
10+
/**
11+
* Gets the SARIF severity for errors.
12+
*
13+
* See point 3.27.10 in https://docs.oasis-open.org/sarif/sarif/v2.0/sarif-v2.0.html for
14+
* what error means.
15+
*/
16+
int getErrorSeverity() { result = 2 }
17+
18+
from SyntaxError error, File file
19+
where
20+
file = error.getFile() and
21+
exists(file.getRelativePath())
22+
select error, "Extraction failed in " + file + " with error " + error.getMessage(),
23+
getErrorSeverity()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Successfully extracted Python files
3+
* @description Lists all Python files in the source code directory that were extracted
4+
* without encountering an error.
5+
* @kind diagnostic
6+
* @id py/diagnostics/successfully-extracted-files
7+
*/
8+
9+
import python
10+
11+
from File file
12+
where
13+
not exists(SyntaxError e | e.getFile() = file) and
14+
exists(file.getRelativePath())
15+
select file, ""

python/ql/src/Summary/LinesOfCode.ql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Total lines of Python code in the database
3+
* @description The total number of lines of Python code across all files, including
4+
* external libraries and auto-generated files. This is a useful metric of the size of a
5+
* database. This query counts the lines of code, excluding whitespace or comments.
6+
* @kind metric
7+
* @tags summary
8+
* @id py/summary/lines-of-code
9+
*/
10+
11+
import python
12+
13+
select sum(Module m | | m.getMetrics().getNumberOfLinesOfCode())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Total lines of user written Python code in the database
3+
* @description The total number of lines of Python code from the source code directory,
4+
* excluding auto-generated files. This query counts the lines of code, excluding
5+
* whitespace or comments. Note: If external libraries are included in the codebase
6+
* either in a checked-in virtual environment or as vendored code, that will currently
7+
* be counted as user written code.
8+
* @kind metric
9+
* @tags summary
10+
* @id py/summary/lines-of-user-code
11+
*/
12+
13+
import python
14+
import semmle.python.filters.GeneratedCode
15+
16+
select sum(Module m |
17+
exists(m.getFile().getRelativePath()) and
18+
not m.getFile() instanceof GeneratedFile
19+
|
20+
m.getMetrics().getNumberOfLinesOfCode()
21+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| bad_encoding.py:2:11:2:11 | Encoding Error | Extraction failed in bad_encoding.py with error 'utf-8' codec can't decode byte 0x9d in position 87: invalid start byte | 2 |
2+
| syntax_error.py:1:31:1:31 | Syntax Error | Extraction failed in syntax_error.py with error Syntax Error | 2 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/ExtractionErrors.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| good_file.py:0:0:0:0 | good_file.py | |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/SuccessfullyExtractedFiles.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Note: This file has been encoded in Windows 1252 to provoke encoding error
2+
print("wat")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("hello world")

0 commit comments

Comments
 (0)