Browse Source

License header checking script (#587)

Motivation:

It's easy to miss license headers when creating new files and often this
is missed during PRs.

Modifications:

- Add a script which checks whether licenses headers are present.
- Run the script before the install step in the CI.
- Fix missing/badly formatted headers.

Result:

More correct copyright headers.
George Barnett 6 years ago
parent
commit
d8c8ee8772

+ 1 - 0
.travis.yml

@@ -70,5 +70,6 @@ addons:
       - zlib1g-dev
       - python3
 
+before_install: ./scripts/license-check.sh
 install: ./.travis-install.sh
 script: ./.travis-script.sh

+ 0 - 1
Examples/Google/Datastore/Package.swift

@@ -1,5 +1,4 @@
 // swift-tools-version:4.0
-
 /*
  * Copyright 2017, gRPC Authors All rights reserved.
  *

+ 0 - 1
Examples/Google/NaturalLanguage/Package.swift

@@ -1,5 +1,4 @@
 // swift-tools-version:5.0
-
 /*
  * Copyright 2019, gRPC Authors All rights reserved.
  *

+ 0 - 1
Examples/Google/Spanner/Package.swift

@@ -1,5 +1,4 @@
 // swift-tools-version:4.0
-
 /*
  * Copyright 2017, gRPC Authors All rights reserved.
  *

+ 1 - 1
Sources/Examples/Echo/Model/Generated/echo.pb.swift

@@ -3,7 +3,7 @@
 // Generated by the Swift generator plugin for the protocol buffer compiler.
 // Source: echo.proto
 //
-// For information on using the generated types, please see the documenation:
+// For information on using the generated types, please see the documentation:
 //   https://github.com/apple/swift-protobuf/
 
 // Copyright (c) 2015, Google Inc.

+ 14 - 14
Sources/GRPC/Stopwatch.swift

@@ -1,18 +1,18 @@
 /*
-* Copyright 2019, gRPC Authors All rights reserved.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
+ * Copyright 2019, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 import Foundation
 
 internal class Stopwatch {

+ 14 - 14
Tests/GRPCTests/GRPCStatusTests.swift

@@ -1,18 +1,18 @@
 /*
-* Copyright 2019, gRPC Authors All rights reserved.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
+ * Copyright 2019, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 import GRPC
 import XCTest
 

+ 15 - 0
Tests/GRPCTests/PlatformSupportTests.swift

@@ -1,3 +1,18 @@
+/*
+ * Copyright 2019, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 import Foundation
 import GRPC
 import NIO

+ 14 - 14
Tests/GRPCTests/StopwatchTests.swift

@@ -1,18 +1,18 @@
 /*
-* Copyright 2019, gRPC Authors All rights reserved.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*     http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
+ * Copyright 2019, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 import Foundation
 import XCTest
 @testable import GRPC

+ 137 - 0
scripts/license-check.sh

@@ -0,0 +1,137 @@
+#!/bin/bash
+
+# Copyright 2019, gRPC Authors All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This script checks the copyright headers in source *.swift source files and
+# exits if they do not match the expected header. The year, or year range in
+# headers is replaced with 'YEARS' for comparison.
+
+# Copyright header text and SHA for *.swift files
+read -r -d '' COPYRIGHT_HEADER_SWIFT << 'EOF'
+/*
+ * Copyright YEARS, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+EOF
+SWIFT_SHA=$(echo "$COPYRIGHT_HEADER_SWIFT" | shasum | awk '{print $1}')
+
+# Copyright header text and SHA for *.grpc.swift files
+read -r -d '' COPYRIGHT_HEADER_SWIFT_GRPC << 'EOF'
+// Copyright YEARS, gRPC Authors All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+EOF
+SWIFT_GRPC_SHA=$(echo "$COPYRIGHT_HEADER_SWIFT_GRPC" | shasum | awk '{print $1}')
+
+# Copyright header text and SHA for *.pb.swift files
+read -r -d '' COPYRIGHT_HEADER_SWIFT_PB << 'EOF'
+// Copyright YEARS gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+EOF
+SWIFT_GRPC_PB=$(echo "$COPYRIGHT_HEADER_SWIFT_PB" | shasum | awk '{print $1}')
+
+replace_years() {
+  sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/201[56789]/YEARS/'
+}
+
+# Checks the Copyright headers for *.swift files in this repository against the
+# expected headers.
+#
+# Prints the names of all files with invalid or missing headers and exits with
+# a non-zero status code.
+check_copyright_headers() {
+  # Exceptions:
+  # - echo.pb.swift: Google is the author of the corresponding .proto, so the
+  #   generated header has Google as the author.
+  # - LinuxMain.swift, XCTestManifests.swift: Both of these files are generated
+  #   by SwiftPM and do not have headers.
+  find . -name '*.swift' \
+    ! -name 'echo.pb.swift' \
+    ! -name 'LinuxMain.swift' \
+    ! -name 'XCTestManifests.swift' \
+    ! -path './.build/*' \
+    | while read -r filename; do
+  case $filename in
+    # The .grpc.swift and .pb.swift files have additional generated headers with
+    # warnings that they have been generated and should not be edited.
+    # Package.swift is preceeded by a "swift-tools-version" line.
+    *.grpc.swift)
+      expected_sha="$SWIFT_GRPC_SHA"
+      drop_first=8
+      expected_lines=13
+      ;;
+    *.pb.swift)
+      expected_sha="$SWIFT_GRPC_PB"
+      drop_first=8
+      expected_lines=13
+      ;;
+    */Package.swift)
+      expected_sha="$SWIFT_SHA"
+      drop_first=1
+      expected_lines=15
+      ;;
+    *)
+      expected_sha="$SWIFT_SHA"
+      drop_first=0
+      expected_lines=15
+      ;;
+  esac
+
+  actual_sha=$(head -n "$((drop_first + expected_lines))" "$filename" \
+    | tail -n "$expected_lines" \
+    | sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/201[56789]/YEARS/' \
+    | shasum \
+    | awk '{print $1}')
+
+  if [ "$actual_sha" != "$expected_sha" ]; then
+    printf "\033[0;31mMissing or invalid copyright headers in '$filename'\033[0m\n"
+    exit 1
+  fi
+  done
+}
+
+check_copyright_headers