def ReadFile(self, file_item, mode='r'): """Reads an arbitrary file. Deny reading anything outside the repository. """ if isinstance(file_item, AffectedFile): file_item = file_item.AbsoluteLocalPath() if not file_item.startswith(self.change.RepositoryRoot()): raise IOError('Access outside the repository root is denied.') return read_file(file_item, mode)
def NewContents(self): """Returns an iterator over the lines in the new version of file. The new version is the file in the user's workspace, i.e. the "right hand side". Contents will be empty if the file is a directory or does not exist. Note: The cariage returns (LF or CR) are stripped off. """ if self.IsDirectory(): return [] else: return read_file(self.AbsoluteLocalPath(), 'rU').splitlines()
def DoGetTrySlaves(changed_files, repository_root, default_review, verbose, output_stream): """Get the list of try servers from the review scripts. Args: changed_files: List of modified files. repository_root: The repository root. default_review: A default review script to execute in any case. verbose: Prints debug info. output_stream: A stream to write debug output to. Return: List of try slaves """ review_files = ListRelevantReviewFiles(changed_files, repository_root) if not review_files and verbose: output_stream.write("Warning, no review script found.\n") results = [] executer = GetTrySlavesExecuter() if default_review: if verbose: output_stream.write("Running default review script.\n") results += executer.ExecReviewScript(default_review) for filename in review_files: filename = os.path.abspath(filename) if verbose: output_stream.write("Running %s\n" % filename) # Accept CRLF review script. review_script = read_file(filename, 'rU') results += executer.ExecReviewScript(review_script) slaves = list(set(results)) if slaves and verbose: output_stream.write(', '.join(slaves)) output_stream.write('\n') return slaves
import sys from pyutil.env import read_file from demjson import decode, JSONDecodeError def validate(content, source_id="<source>"): """Return whether the content is valid JSON.""" try: decode(content, strict=True) except JSONDecodeError, error: print "\nInvalid JSON source: %s" % source_id print "\n\t%s\n" % error.pretty_description() return False return True if __name__ == '__main__': if len(sys.argv) != 2: print "Usage: jsoncheck.py path/to/file.json" sys.exit(2) filename = sys.argv[1] content = read_file(filename) sys.exit(int(not validate(content, filename)))
def DoReviewChecks(change, committing, verbose, output_stream, input_stream, default_review, may_prompt): """Runs all review checks that apply to the files in the change. This finds all .review.py files in directories enclosing the files in the change (up to the repository root) and calls the relevant entrypoint function depending on whether the change is being committed or uploaded. Prints errors, warnings and notifications. Prompts the user for warnings when needed. Args: change: The Change object. committing: True if 'gcl commit' is running, False if 'gcl upload' is. verbose: Prints debug info. output_stream: A stream to write output from review tests to. input_stream: A stream to read input from the user. default_review: A default review script to execute in any case. may_prompt: Enable (y/n) questions on warning or error. Warning: If may_prompt is true, output_stream SHOULD be sys.stdout and input_stream SHOULD be sys.stdin. Return: True if execution can continue, False if not. """ start_time = time.time() review_files = ListRelevantReviewFiles(change.AbsoluteLocalPaths(True), change.RepositoryRoot()) if not review_files and verbose: output_stream.write("Warning, no .review.py found.\n") results = [] executer = ReviewExecuter(change, committing) if default_review: if verbose: output_stream.write("Running default review script.\n") fake_path = os.path.join(change.RepositoryRoot(), '.review.py') results += executer.ExecReviewScript(default_review, fake_path) for filename in review_files: filename = os.path.abspath(filename) if verbose: output_stream.write("Running %s\n" % filename) # Accept CRLF review script. review_script = read_file(filename, 'rU') results += executer.ExecReviewScript(review_script, filename) errors = [] notifications = [] warnings = [] for result in results: if not result.IsFatal() and not result.ShouldPrompt(): notifications.append(result) elif result.ShouldPrompt(): warnings.append(result) else: errors.append(result) error_count = 0 for name, items in (('Messages', notifications), ('Warnings', warnings), ('ERRORS', errors)): if items: output_stream.write('** Review %s **\n' % name) for item in items: if not item._Handle( output_stream, input_stream, may_prompt=False): error_count += 1 output_stream.write('\n') total_time = time.time() - start_time if total_time > 1.0: print "Review checks took %.1fs to calculate." % total_time if not errors and warnings and may_prompt: if not PromptYesNo( input_stream, output_stream, 'There were review warnings. ' 'Are you sure you wish to continue? (y/N): '): error_count += 1 return (error_count == 0)
def DoReviewChecks(change, committing, verbose, output_stream, input_stream, default_review, may_prompt): """Runs all review checks that apply to the files in the change. This finds all .review.py files in directories enclosing the files in the change (up to the repository root) and calls the relevant entrypoint function depending on whether the change is being committed or uploaded. Prints errors, warnings and notifications. Prompts the user for warnings when needed. Args: change: The Change object. committing: True if 'gcl commit' is running, False if 'gcl upload' is. verbose: Prints debug info. output_stream: A stream to write output from review tests to. input_stream: A stream to read input from the user. default_review: A default review script to execute in any case. may_prompt: Enable (y/n) questions on warning or error. Warning: If may_prompt is true, output_stream SHOULD be sys.stdout and input_stream SHOULD be sys.stdin. Return: True if execution can continue, False if not. """ start_time = time.time() review_files = ListRelevantReviewFiles(change.AbsoluteLocalPaths(True), change.RepositoryRoot()) if not review_files and verbose: output_stream.write("Warning, no .review.py found.\n") results = [] executer = ReviewExecuter(change, committing) if default_review: if verbose: output_stream.write("Running default review script.\n") fake_path = os.path.join(change.RepositoryRoot(), '.review.py') results += executer.ExecReviewScript(default_review, fake_path) for filename in review_files: filename = os.path.abspath(filename) if verbose: output_stream.write("Running %s\n" % filename) # Accept CRLF review script. review_script = read_file(filename, 'rU') results += executer.ExecReviewScript(review_script, filename) errors = [] notifications = [] warnings = [] for result in results: if not result.IsFatal() and not result.ShouldPrompt(): notifications.append(result) elif result.ShouldPrompt(): warnings.append(result) else: errors.append(result) error_count = 0 for name, items in (('Messages', notifications), ('Warnings', warnings), ('ERRORS', errors)): if items: output_stream.write('** Review %s **\n' % name) for item in items: if not item._Handle(output_stream, input_stream, may_prompt=False): error_count += 1 output_stream.write('\n') total_time = time.time() - start_time if total_time > 1.0: print "Review checks took %.1fs to calculate." % total_time if not errors and warnings and may_prompt: if not PromptYesNo(input_stream, output_stream, 'There were review warnings. ' 'Are you sure you wish to continue? (y/N): '): error_count += 1 return (error_count == 0)