2010-06-02 02:31:19 +04:00
|
|
|
import platform
|
2010-07-31 00:44:54 +04:00
|
|
|
import uuid
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-05-28 04:37:43 +04:00
|
|
|
import json
|
|
|
|
|
2010-06-02 02:31:19 +04:00
|
|
|
if platform.system() != "Windows":
|
|
|
|
from outputhandlers.shellcolors import OutputHandler
|
2010-06-22 05:28:16 +04:00
|
|
|
else: # pragma: no cover
|
2010-06-02 02:31:19 +04:00
|
|
|
from outputhandlers.windowscolors import OutputHandler
|
2010-05-29 01:42:23 +04:00
|
|
|
|
2010-06-12 01:14:07 +04:00
|
|
|
class ErrorBundle(object):
|
2010-05-21 04:24:14 +04:00
|
|
|
"""This class does all sorts of cool things. It gets passed around
|
|
|
|
from test to test and collects up all the errors like the candy man
|
|
|
|
'separating the sorrow and collecting up all the cream.' It's
|
|
|
|
borderline magical."""
|
|
|
|
|
2010-10-26 03:58:53 +04:00
|
|
|
def __init__(self, pipe=None, no_color=False, determined=True,
|
|
|
|
listed=False):
|
2010-05-29 00:21:05 +04:00
|
|
|
"""Specifying pipe allows the output of the bundler to be
|
|
|
|
written to a file rather than to the screen."""
|
|
|
|
|
2010-05-21 04:24:14 +04:00
|
|
|
self.errors = []
|
|
|
|
self.warnings = []
|
2010-10-26 03:58:53 +04:00
|
|
|
self.notices = []
|
2010-07-30 02:06:57 +04:00
|
|
|
self.message_tree = {}
|
2010-10-22 04:46:47 +04:00
|
|
|
|
|
|
|
self.tier = 0
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-07-27 22:17:25 +04:00
|
|
|
self.metadata = {}
|
2010-10-26 03:58:53 +04:00
|
|
|
self.determined = determined
|
2010-07-27 22:17:25 +04:00
|
|
|
|
2010-06-01 22:38:23 +04:00
|
|
|
self.subpackages = []
|
|
|
|
self.package_stack = []
|
|
|
|
|
2010-05-21 04:24:14 +04:00
|
|
|
self.detected_type = 0
|
2010-05-22 03:59:54 +04:00
|
|
|
self.resources = {}
|
2010-05-25 21:42:35 +04:00
|
|
|
self.reject = False
|
2010-08-18 04:27:37 +04:00
|
|
|
self.unfinished = False
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-10-26 03:58:53 +04:00
|
|
|
if listed:
|
|
|
|
self.resources["listed"] = True
|
|
|
|
|
2010-06-02 22:31:14 +04:00
|
|
|
self.handler = OutputHandler(pipe, no_color)
|
2010-05-29 01:42:23 +04:00
|
|
|
|
|
|
|
|
2010-07-27 22:17:25 +04:00
|
|
|
def error(self, err_id, error, description='', filename='', line=0):
|
2010-05-21 04:24:14 +04:00
|
|
|
"Stores an error message for the validation process"
|
2010-07-30 02:06:57 +04:00
|
|
|
self._save_message(self.errors,
|
2010-07-31 00:44:54 +04:00
|
|
|
"errors",
|
2010-07-30 02:06:57 +04:00
|
|
|
{"id": err_id,
|
2010-07-27 22:17:25 +04:00
|
|
|
"message": error,
|
2010-06-03 06:08:47 +04:00
|
|
|
"description": description,
|
|
|
|
"file": filename,
|
|
|
|
"line": line})
|
2010-05-21 04:24:14 +04:00
|
|
|
return self
|
|
|
|
|
2010-07-27 22:17:25 +04:00
|
|
|
def warning(self, err_id, warning, description='', filename='', line=0):
|
2010-05-21 04:24:14 +04:00
|
|
|
"Stores a warning message for the validation process"
|
2010-07-30 02:06:57 +04:00
|
|
|
self._save_message(self.warnings,
|
2010-07-31 00:44:54 +04:00
|
|
|
"warnings",
|
2010-07-30 02:06:57 +04:00
|
|
|
{"id": err_id,
|
|
|
|
"message": warning,
|
|
|
|
"description": description,
|
|
|
|
"file": filename,
|
|
|
|
"line": line})
|
2010-05-21 04:24:14 +04:00
|
|
|
return self
|
2010-05-26 02:08:32 +04:00
|
|
|
|
2010-10-26 03:58:53 +04:00
|
|
|
def info(self, err_id, info, description="", filename="", line=0):
|
|
|
|
"An alias for notice"
|
|
|
|
self.notice(err_id, info, description, filename, line)
|
|
|
|
|
|
|
|
def notice(self, err_id, notice, description="", filename="", line=0):
|
2010-05-26 02:08:32 +04:00
|
|
|
"Stores an informational message about the validation"
|
2010-10-26 03:58:53 +04:00
|
|
|
self._save_message(self.notices,
|
|
|
|
"notices",
|
2010-07-30 02:06:57 +04:00
|
|
|
{"id": err_id,
|
2010-10-26 03:58:53 +04:00
|
|
|
"message": notice,
|
2010-07-30 02:06:57 +04:00
|
|
|
"description": description,
|
|
|
|
"file": filename,
|
|
|
|
"line": line})
|
2010-05-26 02:08:32 +04:00
|
|
|
return self
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-07-31 00:44:54 +04:00
|
|
|
def _save_message(self, stack, type_, message):
|
2010-07-30 02:06:57 +04:00
|
|
|
"Stores a message in the appropriate message stack."
|
|
|
|
|
2010-07-31 00:44:54 +04:00
|
|
|
uid = uuid.uuid1().hex
|
|
|
|
|
|
|
|
message["uid"] = uid
|
2010-07-30 02:06:57 +04:00
|
|
|
stack.append(message)
|
|
|
|
|
2010-10-22 04:46:47 +04:00
|
|
|
# Mark the tier that the error occurred at
|
|
|
|
message["tier"] = self.tier
|
|
|
|
|
2010-07-31 04:44:18 +04:00
|
|
|
if message["id"]:
|
|
|
|
tree = self.message_tree
|
|
|
|
last_id = None
|
|
|
|
for eid in message["id"]:
|
|
|
|
if last_id is not None:
|
|
|
|
tree = tree[last_id]
|
|
|
|
if eid not in tree:
|
|
|
|
tree[eid] = {"__errors": 0,
|
|
|
|
"__warnings": 0,
|
2010-10-22 04:46:47 +04:00
|
|
|
"__notices": 0,
|
2010-07-31 04:44:18 +04:00
|
|
|
"__messages": []}
|
|
|
|
tree[eid]["__%s" % type_] += 1
|
|
|
|
last_id = eid
|
|
|
|
|
|
|
|
tree[last_id]['__messages'].append(uid)
|
2010-07-30 02:06:57 +04:00
|
|
|
|
2010-05-27 04:37:34 +04:00
|
|
|
def set_type(self, type_):
|
2010-05-21 04:24:14 +04:00
|
|
|
"Stores the type of addon we're scanning"
|
2010-05-27 04:37:34 +04:00
|
|
|
self.detected_type = type_
|
2010-05-21 04:24:14 +04:00
|
|
|
|
|
|
|
def failed(self):
|
|
|
|
"""Returns a boolean value describing whether the validation
|
|
|
|
succeeded or not."""
|
|
|
|
|
|
|
|
return self.errors or self.warnings
|
|
|
|
|
2010-05-22 03:59:54 +04:00
|
|
|
def get_resource(self, name):
|
|
|
|
"Retrieves an object that has been stored by another test."
|
|
|
|
|
2010-05-25 02:40:11 +04:00
|
|
|
if not name in self.resources:
|
|
|
|
return False
|
|
|
|
|
2010-05-22 03:59:54 +04:00
|
|
|
return self.resources[name]
|
|
|
|
|
|
|
|
def save_resource(self, name, resource):
|
|
|
|
"Saves an object such that it can be used by other tests."
|
|
|
|
|
|
|
|
self.resources[name] = resource
|
|
|
|
|
2010-06-14 22:29:32 +04:00
|
|
|
def is_nested_package(self):
|
|
|
|
"Returns whether the current package is within a PACKAGE_MULTI"
|
|
|
|
|
|
|
|
return bool(self.package_stack)
|
|
|
|
|
2010-06-01 22:38:23 +04:00
|
|
|
def push_state(self, new_file=""):
|
|
|
|
"Saves the current error state to parse subpackages"
|
|
|
|
|
|
|
|
self.subpackages.append({"errors": self.errors,
|
|
|
|
"warnings": self.warnings,
|
2010-10-26 03:58:53 +04:00
|
|
|
"notices": self.notices,
|
2010-06-01 22:38:23 +04:00
|
|
|
"detected_type": self.detected_type,
|
2010-07-30 02:06:57 +04:00
|
|
|
"resources": self.resources,
|
|
|
|
"message_tree": self.message_tree})
|
|
|
|
|
2010-06-01 22:38:23 +04:00
|
|
|
self.errors = []
|
|
|
|
self.warnings = []
|
2010-10-26 03:58:53 +04:00
|
|
|
self.notices = []
|
2010-06-01 22:38:23 +04:00
|
|
|
self.resources = {}
|
2010-07-30 02:06:57 +04:00
|
|
|
self.message_tree = {}
|
2010-06-01 22:38:23 +04:00
|
|
|
|
|
|
|
self.package_stack.append(new_file)
|
|
|
|
|
|
|
|
def pop_state(self):
|
|
|
|
"Retrieves the last saved state and restores it."
|
|
|
|
|
|
|
|
# Save a copy of the current state.
|
|
|
|
state = self.subpackages.pop()
|
|
|
|
errors = self.errors
|
|
|
|
warnings = self.warnings
|
2010-10-26 03:58:53 +04:00
|
|
|
notices = self.notices
|
2010-07-30 02:06:57 +04:00
|
|
|
# We only rebuild message_tree anyway. No need to restore.
|
2010-06-01 22:38:23 +04:00
|
|
|
|
|
|
|
# Copy the existing state back into place
|
|
|
|
self.errors = state["errors"]
|
|
|
|
self.warnings = state["warnings"]
|
2010-10-26 03:58:53 +04:00
|
|
|
self.notices = state["notices"]
|
2010-06-01 22:38:23 +04:00
|
|
|
self.detected_type = state["detected_type"]
|
|
|
|
self.resources = state["resources"]
|
2010-07-30 02:06:57 +04:00
|
|
|
self.message_tree = state["message_tree"]
|
2010-06-01 22:38:23 +04:00
|
|
|
|
|
|
|
name = self.package_stack.pop()
|
|
|
|
|
2010-07-30 02:06:57 +04:00
|
|
|
self._merge_messages(errors, self.error, name)
|
|
|
|
self._merge_messages(warnings, self.warning, name)
|
2010-10-26 03:58:53 +04:00
|
|
|
self._merge_messages(notices, self.notice, name)
|
2010-07-30 02:06:57 +04:00
|
|
|
|
|
|
|
|
|
|
|
def _merge_messages(self, messages, callback, name):
|
|
|
|
"Merges a stack of messages into another stack of messages"
|
|
|
|
|
2010-06-01 22:38:23 +04:00
|
|
|
# Overlay the popped warnings onto the existing ones.
|
2010-07-30 02:06:57 +04:00
|
|
|
for message in messages:
|
2010-06-04 04:48:09 +04:00
|
|
|
trace = [name]
|
2010-06-08 21:41:43 +04:00
|
|
|
# If there are sub-sub-packages, they'll be in a list.
|
2010-07-30 02:06:57 +04:00
|
|
|
if isinstance(message["file"], list):
|
|
|
|
trace.extend(message["file"])
|
2010-06-04 04:48:09 +04:00
|
|
|
else:
|
2010-07-30 02:06:57 +04:00
|
|
|
trace.append(message["file"])
|
2010-06-04 04:48:09 +04:00
|
|
|
|
2010-06-08 21:41:43 +04:00
|
|
|
# Write the errors with the file structure delimited by
|
|
|
|
# right carets.
|
2010-07-30 02:06:57 +04:00
|
|
|
callback(message["id"],
|
2010-08-14 03:46:52 +04:00
|
|
|
message["message"],
|
2010-07-30 02:06:57 +04:00
|
|
|
message["description"],
|
|
|
|
trace,
|
|
|
|
message["line"])
|
2010-08-14 03:46:52 +04:00
|
|
|
|
2010-06-01 22:38:23 +04:00
|
|
|
|
2010-07-27 01:06:06 +04:00
|
|
|
def _clean_description(self, message, json=False):
|
2010-06-04 04:48:09 +04:00
|
|
|
"Cleans all the nasty whitespace from the descriptions."
|
|
|
|
|
2010-07-28 02:09:23 +04:00
|
|
|
output = self._clean_message(message["description"], json)
|
|
|
|
message["description"] = output
|
|
|
|
|
|
|
|
def _clean_message(self, desc, json=False):
|
|
|
|
"Cleans all the nasty whitespace from a string."
|
|
|
|
|
2010-07-27 01:06:06 +04:00
|
|
|
output = []
|
2010-07-28 02:09:23 +04:00
|
|
|
|
2010-07-30 02:06:57 +04:00
|
|
|
if not isinstance(desc, list):
|
|
|
|
lines = desc.splitlines()
|
|
|
|
for line in lines:
|
|
|
|
output.append(line.strip())
|
|
|
|
return " ".join(output)
|
|
|
|
else:
|
|
|
|
for line in desc:
|
|
|
|
output.append(self._clean_message(line, json))
|
|
|
|
if json:
|
|
|
|
return output
|
|
|
|
else:
|
|
|
|
return "\n".join(output)
|
2010-06-04 04:48:09 +04:00
|
|
|
|
2010-07-31 00:44:54 +04:00
|
|
|
def print_json(self, cluster=False):
|
2010-05-28 04:37:43 +04:00
|
|
|
"Prints a JSON summary of the validation operation."
|
|
|
|
|
2010-07-24 01:45:09 +04:00
|
|
|
types = {0: "unknown",
|
|
|
|
1: "extension",
|
|
|
|
2: "theme",
|
|
|
|
3: "dictionary",
|
|
|
|
4: "langpack",
|
|
|
|
5: "search"}
|
|
|
|
output = {"detected_type": types[self.detected_type],
|
2010-05-28 04:37:43 +04:00
|
|
|
"success": not self.failed(),
|
2010-06-08 21:41:43 +04:00
|
|
|
"rejected": self.reject,
|
2010-07-24 01:45:09 +04:00
|
|
|
"messages":[],
|
|
|
|
"errors": len(self.errors),
|
|
|
|
"warnings": len(self.warnings),
|
2010-10-26 03:58:53 +04:00
|
|
|
"notices": len(self.notices),
|
2010-08-17 01:15:15 +04:00
|
|
|
"message_tree": self.message_tree,
|
|
|
|
"metadata": self.metadata}
|
2010-05-28 04:37:43 +04:00
|
|
|
|
|
|
|
messages = output["messages"]
|
|
|
|
|
|
|
|
# Copy messages to the JSON output
|
|
|
|
for error in self.errors:
|
2010-06-04 04:48:09 +04:00
|
|
|
error["type"] = "error"
|
2010-07-27 01:06:06 +04:00
|
|
|
self._clean_description(error, True)
|
2010-06-04 04:48:09 +04:00
|
|
|
messages.append(error)
|
|
|
|
|
2010-05-28 04:37:43 +04:00
|
|
|
for warning in self.warnings:
|
2010-06-04 04:48:09 +04:00
|
|
|
warning["type"] = "warning"
|
2010-07-27 01:06:06 +04:00
|
|
|
self._clean_description(warning, True)
|
2010-06-04 04:48:09 +04:00
|
|
|
messages.append(warning)
|
|
|
|
|
2010-10-26 03:58:53 +04:00
|
|
|
for notice in self.notices:
|
|
|
|
notice["type"] = "notice"
|
|
|
|
self._clean_description(notice, True)
|
|
|
|
messages.append(notice)
|
2010-05-28 04:37:43 +04:00
|
|
|
|
2010-05-29 01:42:23 +04:00
|
|
|
# Output the JSON.
|
2010-05-29 00:21:05 +04:00
|
|
|
json_output = json.dumps(output)
|
2010-06-03 03:07:37 +04:00
|
|
|
self.handler.write(json_output)
|
2010-05-28 04:37:43 +04:00
|
|
|
|
2010-06-02 22:31:14 +04:00
|
|
|
def print_summary(self, verbose=False):
|
2010-05-21 04:24:14 +04:00
|
|
|
"Prints a summary of the validation process so far."
|
|
|
|
|
2010-05-26 21:43:56 +04:00
|
|
|
types = {0: "Unknown",
|
|
|
|
1: "Extension/Multi-Extension",
|
|
|
|
2: "Theme",
|
|
|
|
3: "Dictionary",
|
|
|
|
4: "Language Pack",
|
2010-06-01 22:38:23 +04:00
|
|
|
5: "Search Provider",
|
|
|
|
7: "Subpackage"}
|
2010-05-29 00:21:05 +04:00
|
|
|
detected_type = types[self.detected_type]
|
2010-05-21 04:24:14 +04:00
|
|
|
|
|
|
|
# Make a neat little printout.
|
2010-06-02 22:31:14 +04:00
|
|
|
self.handler.write("\n<<GREEN>>Summary:") \
|
2010-06-02 02:31:19 +04:00
|
|
|
.write("-" * 30) \
|
2010-06-02 22:31:14 +04:00
|
|
|
.write("Detected type: <<BLUE>>%s" % detected_type) \
|
2010-06-02 02:31:19 +04:00
|
|
|
.write("-" * 30)
|
2010-05-21 04:24:14 +04:00
|
|
|
|
|
|
|
if self.failed():
|
2010-06-02 22:31:14 +04:00
|
|
|
self.handler.write("<<BLUE>>Test failed! Errors:")
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-06-03 06:46:25 +04:00
|
|
|
# Print out all the errors/warnings:
|
2010-05-21 04:24:14 +04:00
|
|
|
for error in self.errors:
|
2010-06-04 04:48:09 +04:00
|
|
|
self._print_message("<<RED>>Error:<<NORMAL>>\t", error, verbose)
|
2010-05-21 04:24:14 +04:00
|
|
|
for warning in self.warnings:
|
2010-06-04 04:48:09 +04:00
|
|
|
self._print_message("<<YELLOW>>Warning:<<NORMAL>> ", warning, verbose)
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-06-03 06:46:25 +04:00
|
|
|
# Prints things that only happen during verbose (infos).
|
2010-06-02 22:31:14 +04:00
|
|
|
self._print_verbose(verbose)
|
2010-05-26 02:08:32 +04:00
|
|
|
|
2010-05-25 21:42:35 +04:00
|
|
|
# Awwww... have some self esteem!
|
|
|
|
if self.reject:
|
2010-06-02 02:31:19 +04:00
|
|
|
self.handler.write("Extension Rejected")
|
2010-05-25 21:42:35 +04:00
|
|
|
|
2010-05-21 04:24:14 +04:00
|
|
|
else:
|
2010-06-02 22:31:14 +04:00
|
|
|
self.handler.write("<<GREEN>>All tests succeeded!")
|
|
|
|
self._print_verbose(verbose)
|
2010-05-29 01:42:23 +04:00
|
|
|
|
2010-06-02 02:31:19 +04:00
|
|
|
self.handler.write("\n")
|
2010-08-18 04:27:37 +04:00
|
|
|
if self.unfinished:
|
|
|
|
self.handler.write("<<RED>>Validation terminated early")
|
|
|
|
self.handler.write("Errors during validation are preventing"
|
|
|
|
"the validation proecss from completing.")
|
|
|
|
self.handler.write("Use the <<YELLOW>>--determined<<NORMAL>> "
|
|
|
|
"flag to ignore these errors.")
|
|
|
|
self.handler.write("\n")
|
2010-05-26 02:08:32 +04:00
|
|
|
|
2010-06-03 06:46:25 +04:00
|
|
|
def _print_message(self, prefix, message, verbose=True):
|
|
|
|
"Prints a message and takes care of all sorts of nasty code"
|
|
|
|
|
|
|
|
# Load up the standard output.
|
2010-10-26 04:34:04 +04:00
|
|
|
output = ["\n",
|
|
|
|
prefix,
|
|
|
|
self._clean_message([message["message"]]),
|
|
|
|
"\n"]
|
2010-06-03 06:46:25 +04:00
|
|
|
|
|
|
|
# We have some extra stuff for verbose mode.
|
|
|
|
if verbose:
|
|
|
|
verbose_output = []
|
|
|
|
|
|
|
|
# Detailed problem description.
|
|
|
|
if message["description"]:
|
|
|
|
# These are dirty, so strip out whitespace and concat.
|
2010-07-30 02:06:57 +04:00
|
|
|
verbose_output.append(
|
|
|
|
self._clean_message(message["description"]))
|
2010-06-03 06:46:25 +04:00
|
|
|
|
2010-10-26 04:34:04 +04:00
|
|
|
# Show the user what tier we're on
|
|
|
|
verbose_output.append("\tTier: %d" % message["tier"])
|
|
|
|
|
2010-06-03 06:46:25 +04:00
|
|
|
# If file information is availe, output that as well.
|
|
|
|
files = message["file"]
|
2010-06-04 04:48:09 +04:00
|
|
|
if files is not None and files != "":
|
|
|
|
fmsg = "\tFile:\t%s"
|
2010-06-03 06:46:25 +04:00
|
|
|
|
|
|
|
# Nested files (subpackes) are stored in a list.
|
2010-06-04 04:48:09 +04:00
|
|
|
if type(files) is list:
|
|
|
|
if files[-1] == "":
|
|
|
|
files[-1] = "(none)"
|
2010-06-03 06:46:25 +04:00
|
|
|
verbose_output.append(fmsg % ' > '.join(files))
|
|
|
|
else:
|
|
|
|
verbose_output.append(fmsg % files)
|
|
|
|
|
|
|
|
# If there is a line number, that gets put on the end.
|
|
|
|
if message["line"]:
|
2010-06-04 04:48:09 +04:00
|
|
|
verbose_output.append("\tLine:\t%s" % message["line"])
|
2010-06-03 06:46:25 +04:00
|
|
|
|
|
|
|
# Stick it in with the standard items.
|
2010-10-26 04:34:04 +04:00
|
|
|
output.append("\n")
|
|
|
|
output.append("\n".join(verbose_output))
|
2010-06-03 06:46:25 +04:00
|
|
|
|
|
|
|
# Send the final output to the handler to be rendered.
|
|
|
|
self.handler.write(''.join(output))
|
|
|
|
|
|
|
|
|
2010-06-02 22:31:14 +04:00
|
|
|
def _print_verbose(self, verbose):
|
2010-05-27 04:37:34 +04:00
|
|
|
"Prints info code to help prevent code duplication"
|
2010-05-21 04:24:14 +04:00
|
|
|
|
2010-10-26 03:58:53 +04:00
|
|
|
if self.notices and verbose:
|
2010-07-30 02:06:57 +04:00
|
|
|
mesg = "<<WHITE>>Notice:<<NORMAL>>\t"
|
2010-10-26 03:58:53 +04:00
|
|
|
for notice in self.notices:
|
2010-10-26 04:34:04 +04:00
|
|
|
self._print_message(prefix=mesg, message=notice)
|
2010-10-22 04:46:47 +04:00
|
|
|
|