Skip to content

Commit 354dee1

Browse files
committed
Python: Add non-alert data for lines of code
`py/summary/lines-of-code` is just a port of the C++/JS queries added in: - github#5271 (C++) - github#5304 (JS) We are the first to implement the `lines-of-user-code` query, so nothing to compare with in other languages -- but it makes a lot of sense to do for Python 👍
1 parent 2b8afe5 commit 354dee1

File tree

9 files changed

+76
-0
lines changed

9 files changed

+76
-0
lines changed

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+
* @id py/summary/lines-of-code
3+
* @name Total lines of Python code in the database
4+
* @description The total number of lines of Python code across all files, including
5+
* external libraries and auto-generated files. This is a useful metric of the size of a
6+
* database. This query counts the lines of code, excluding whitespace or comments.
7+
* @kind metric
8+
* @tags summary
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+
* @id py/summary/lines-of-user-code
3+
* @name Total lines of user written Python code in the database
4+
* @description The total number of lines of Python code from the source code directory,
5+
* excluding auto-generated files. This query counts the lines of code, excluding
6+
* whitespace or comments. Note: If external libraries are included in the codebase
7+
* either in a checked-in virtual environment or as vendored code, that will currently
8+
* be counted as user written code.
9+
* @kind metric
10+
* @tags summary
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 38 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Summary/LinesOfCode.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 11 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Summary/LinesOfUserCode.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
3+
# although this is actually Python code, it is not included by the extractor by default.
4+
5+
print("this is also code")
6+
7+
print("but just dummy code")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
module level docstring
3+
4+
is not included
5+
"""
6+
# this line is not code
7+
8+
# `tty` was chosen for stability over python versions (so we don't get diffrent results
9+
# on different computers, that has different versions of Python).
10+
#
11+
# According to https://github.com/python/cpython/tree/master/Lib (at 2021-04-23) `tty`
12+
# was last changed in 2001, so chances of this being changed in the future are slim.
13+
import tty
14+
15+
s = """
16+
all these lines are code
17+
"""
18+
19+
print(s)
20+
21+
def func():
22+
"""
23+
this string is a doc-string. Although the module-level docstring is not considered
24+
code, this one apparently is ¯\_(ツ)_/¯
25+
"""
26+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
# Although this is valid python code, it should not be counted as such.
4+
5+
print("foo")

0 commit comments

Comments
 (0)