Adding a presubmit check for .proto files EOF newline

We want to ensure that all the .proto files in WebRTC are terminated
with a newline.

NOTRY= True

Bug: webrtc:7904
Change-Id: Ifbea958bd449e24101049c971c463e4ccec7b90d
Reviewed-on: https://chromium-review.googlesource.com/555150
Reviewed-by: Henrik Kjellander <kjellander@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#18835}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 78f4272..637dce8 100755
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -577,6 +577,7 @@
   results.extend(_RunPythonTests(input_api, output_api))
   results.extend(_CheckUsageOfGoogleProtobufNamespace(input_api, output_api))
   results.extend(_CheckOrphanHeaders(input_api, output_api))
+  results.extend(_CheckNewLineAtTheEndOfProtoFiles(input_api, output_api))
   return results
 
 
@@ -632,3 +633,18 @@
         results.append(output_api.PresubmitError(error_msg.format(
             file_path, gn_file_path)))
   return results
+
+
+def _CheckNewLineAtTheEndOfProtoFiles(input_api, output_api):
+  """Checks that all .proto files are terminated with a newline."""
+  error_msg = 'File {} must end with exactly one newline.'
+  results = []
+  source_file_filter = lambda x: input_api.FilterSourceFile(
+      x, white_list=(r'.+\.proto$',))
+  for f in input_api.AffectedSourceFiles(source_file_filter):
+    file_path = f.LocalPath()
+    with open(file_path) as f:
+      lines = f.readlines()
+      if lines[-1] != '\n' or lines[-2] == '\n':
+        results.append(output_api.PresubmitError(error_msg.format(file_path)))
+  return results