Skip to content

Commit

Permalink
re-work
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasofthings committed May 26, 2020
1 parent c576c23 commit f38c3c7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 112 deletions.
1 change: 0 additions & 1 deletion action/__init__.py

This file was deleted.

37 changes: 0 additions & 37 deletions action/status.py

This file was deleted.

121 changes: 47 additions & 74 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

# from action import build
from sphinx.cmd.build import main
from typing import Tuple, List


LineInfo = Tuple[str, int]

class AnnotationLevel:
# Notices are not currently supported.
# NOTICE = "notice"
NOTICE = "notice"
WARNING = "warning"
FAILURE = "failure"

Expand All @@ -24,7 +24,7 @@ class AnnotationLevel:
)


def output(annotation, where_to_print=sys.stdout):
def annotate(annotation: str, out=sys.stdout):
level_to_command = {
AnnotationLevel.WARNING: "warning",
AnnotationLevel.FAILURE: "error",
Expand All @@ -33,26 +33,18 @@ def output(annotation, where_to_print=sys.stdout):
command = level_to_command[annotation.annotation_level]

print(
"::{command} file={file},line={line}::{message}".format(
command=command,
file=annotation.path,
line=annotation.start_line,
message=annotation.message,
),
file=where_to_print,
f"::{command} file={annotation.path},line={annotation.start_line}::{annotation.message}",
file=out
)




def extract_line_information(line_information):
r"""Lines from sphinx log files look like this
C:\Users\ammar\workspace\sphinx-action\tests\test_projects\warnings\index.rst:22: WARNING: Problems with "include" directive path:
InputError: [Errno 2] No such file or directory: 'I_DONT_EXIST'.
/home/users/ammar/workspace/sphix-action/tests/test_projects/warnings/index.rst:22: WARNING: Problems with "include" directive path:
def extract_line_information(line_information: str) -> LineInfo:
"""
Lines from sphinx log files look like this
/warnings/index.rst:22: WARNING: Problems with "include" directive path:
InputError: [Errno 2] No such file or directory: 'I_DONT_EXIST'.
/home/users/ammar/workspace/sphix-action/tests/test_projects/warnings/index.rst: Something went wrong with this whole ifle
This method is responsible for parsing out the line number and file name from these lines.
"""
file_and_line = line_information.split(":")
# This is a dirty windows specific hack to deal with drive letters in the
Expand All @@ -64,7 +56,8 @@ def extract_line_information(line_information):
file_and_line = file_and_line[1:]

if len(file_and_line) != 2 and len(file_and_line) != 3:
return None
raise ValueError("Generic Warning")

# The case where we have no line number, in this case we return the line
# number as 1 to mark the whole file.
if len(file_and_line) == 2:
Expand All @@ -73,70 +66,50 @@ def extract_line_information(line_information):
try:
line_num = int(file_and_line[1])
except ValueError:
return None
raise ValueError("Another Generic Error")

file_name = os.path.relpath(file_and_line[0])
return file_name, line_num

def parse_sphinx_warnings_log(logfile):
"""Parses a sphinx file containing warnings and errors into a list of
status_check.CheckAnnotation objects.
Inputs look like this:
/warnings_and_errors/index.rst:19: WARNING: Error in "code-block" directive:
maximum 1 argument(s) allowed, 2 supplied.
"""
annotations = []
with open(logfile) as logs:
for i, line in enumerate(logs.readlines()):
if "WARNING" not in line:
continue

warning_tokens = line.split("WARNING:")

if len(warning_tokens) != 2:
continue
file_and_line, message = warning_tokens

file_and_line = extract_line_information(file_and_line)
if not file_and_line:
continue
file_name, line_number = file_and_line

warning_message = message
# If this isn't the last line and the next line isn't a warning,
# treat it as part of this warning message.
if (i != len(logs) - 1) and "WARNING" not in logs[i + 1]:
warning_message += logs[i + 1]
warning_message = warning_message.strip()

annotations.append(
CheckAnnotation(
path=file_name,
message=warning_message,
start_line=line_number,
end_line=line_number,
annotation_level=AnnotationLevel.WARNING,
)
)

return annotations



if __name__=='__main__':
time = datetime.datetime.now()

logging.debug("[build documentation] Starting documentation-action build.")

log_file = os.path.join(tempfile.gettempdir(), "sphinx-log")
if os.path.exists(log_file):
os.unlink(log_file)
logfile = os.path.join(tempfile.gettempdir(), "sphinx-log")
if os.path.exists(logfile):
os.unlink(logfile)

input = os.environ.get("INPUT_DOCS", 'docs')
output = os.environ.get("INPUT_DEST", 'build')

sphinx_options = f'--keep-going --no-color -a -w {log_file} {input} {output}'
main(sphinx_options.split(" "))
for annotation in parse_sphinx_warnings_log(log_file):
output(annotation)
print(f"::set-output name=time::{time}")
options = f'--keep-going --no-color -a -q -w {logfile} {input} {output}'

main(options.split(" "))

with open(logfile) as logs:
loglines = logs.readlines()
for i, line in enumerate(loglines):
if "WARNING" in line:
location, message = line.split("WARNING:")
logging.debug(f"{i}: {location} - {message}")

try:
file_name, line_number = extract_line_information(location)
except ValueError as e:
logging.debug(e)
continue

# If this isn't the last line and the next line isn't a warning,
# treat it as part of this warning message.
if (i != len(loglines) - 1) and "WARNING" not in loglines[i + 1]:
message += loglines[i + 1]

annotate(
CheckAnnotation(
path=file_name,
message=message.strip(),
start_line=line_number,
end_line=line_number,
annotation_level=AnnotationLevel.WARNING,
)
)

0 comments on commit f38c3c7

Please sign in to comment.