Skip to content

Instantly share code, notes, and snippets.

@rixx rixx/covsource.py
Last active May 14, 2019

Embed
What would you like to do?
import sqlite3
import sys
def usage():
print("Usage: covsource.py <file> <linenos>")
print(" e.g. covsource.py foo/bar.py 14,15\n")
print(" or covsource.py foo/bar.py 14-27\n")
sys.exit(1)
def get_range_from_linenos(linenos):
if linenos.isnumeric():
return f"arc.fromno={linenos}"
if "," in linenos:
return " or ".join(f"arc.fromno={int(no)}" for no in linenos.split(","))
if "-" in linenos:
lower, upper = linenos.split("-")
return f"arc.fromno > {int(lower)} and arc.tono < {int(upper)}"
print("Could not parse line numbers.")
usage()
def show_coverage(path, linenos):
line_expression = get_range_from_linenos(linenos)
connection = sqlite3.connect(".coverage")
cursor = connection.cursor()
query = f"""
SELECT DISTINCT context.context
FROM arc
JOIN context ON arc.context_id=context.id
JOIN file ON arc.file_id=file.id
WHERE file.path like '%{path}'
AND {line_expression};"""
cursor.execute(query)
lines = cursor.fetchall()
connection.close()
lines = sorted(line[0].split("|")[0] for line in lines)
for line in lines:
print(line)
def main():
if len(sys.argv) < 3:
usage()
show_coverage(sys.argv[-2], sys.argv[-1])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.