]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Temporarily restore apache/license linters
authorepriestley <git@epriestley.com>
Mon, 11 Feb 2013 22:12:10 +0000 (14:12 -0800)
committerepriestley <git@epriestley.com>
Mon, 11 Feb 2013 22:12:10 +0000 (14:12 -0800)
Summary:
Restores linters only, without unit tests or entry in ComprehensiveLinter. Marks them deprecated.

If use at Facebook isn't widespread I'd prefer to simply delete them.

Test Plan: none

Reviewers: vrana

Reviewed By: vrana

CC: aran
Maniphest Tasks: T2274

Differential Revision: https://secure.phabricator.com/D4906

src/__phutil_library_map__.php
src/lint/linter/ArcanistApacheLicenseLinter.php [new file with mode: 0644]
src/lint/linter/ArcanistLicenseLinter.php [new file with mode: 0644]

index e0c89d1fb535dd8ea96e7d304b70580f7241c334..efa1857677281ee517a3c706cc34d75d3e7e1185 100644 (file)
@@ -13,6 +13,7 @@ phutil_register_library_map(array(
     'ArcanistAliasWorkflow' => 'workflow/ArcanistAliasWorkflow.php',
     'ArcanistAmendWorkflow' => 'workflow/ArcanistAmendWorkflow.php',
     'ArcanistAnoidWorkflow' => 'workflow/ArcanistAnoidWorkflow.php',
+    'ArcanistApacheLicenseLinter' => 'lint/linter/ArcanistApacheLicenseLinter.php',
     'ArcanistArcanistLinterTestCase' => 'lint/linter/__tests__/ArcanistArcanistLinterTestCase.php',
     'ArcanistBaseCommitParser' => 'parser/ArcanistBaseCommitParser.php',
     'ArcanistBaseCommitParserTestCase' => 'parser/__tests__/ArcanistBaseCommitParserTestCase.php',
@@ -73,6 +74,7 @@ phutil_register_library_map(array(
     'ArcanistJSHintLinter' => 'lint/linter/ArcanistJSHintLinter.php',
     'ArcanistLandWorkflow' => 'workflow/ArcanistLandWorkflow.php',
     'ArcanistLiberateWorkflow' => 'workflow/ArcanistLiberateWorkflow.php',
+    'ArcanistLicenseLinter' => 'lint/linter/ArcanistLicenseLinter.php',
     'ArcanistLintConsoleRenderer' => 'lint/renderer/ArcanistLintConsoleRenderer.php',
     'ArcanistLintEngine' => 'lint/engine/ArcanistLintEngine.php',
     'ArcanistLintJSONRenderer' => 'lint/renderer/ArcanistLintJSONRenderer.php',
@@ -168,6 +170,7 @@ phutil_register_library_map(array(
     'ArcanistAliasWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistAmendWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistAnoidWorkflow' => 'ArcanistBaseWorkflow',
+    'ArcanistApacheLicenseLinter' => 'ArcanistLicenseLinter',
     'ArcanistArcanistLinterTestCase' => 'ArcanistLinterTestCase',
     'ArcanistBaseCommitParserTestCase' => 'ArcanistTestCase',
     'ArcanistBaseWorkflow' => 'Phobject',
@@ -211,6 +214,7 @@ phutil_register_library_map(array(
     'ArcanistJSHintLinter' => 'ArcanistLinter',
     'ArcanistLandWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistLiberateWorkflow' => 'ArcanistBaseWorkflow',
+    'ArcanistLicenseLinter' => 'ArcanistLinter',
     'ArcanistLintConsoleRenderer' => 'ArcanistLintRenderer',
     'ArcanistLintJSONRenderer' => 'ArcanistLintRenderer',
     'ArcanistLintLikeCompilerRenderer' => 'ArcanistLintRenderer',
diff --git a/src/lint/linter/ArcanistApacheLicenseLinter.php b/src/lint/linter/ArcanistApacheLicenseLinter.php
new file mode 100644 (file)
index 0000000..fbed31c
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * @deprecated
+ */
+final class ArcanistApacheLicenseLinter extends ArcanistLicenseLinter {
+  public function getLinterName() {
+    return 'APACHELICENSE';
+  }
+
+  protected function getLicenseText($copyright_holder) {
+    $year = date('Y');
+
+    return <<<EOLICENSE
+
+/*
+ * Copyright {$year} {$copyright_holder}
+ *
+ * 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.
+ */
+
+
+EOLICENSE;
+  }
+
+  protected function getLicensePatterns() {
+    $maybe_php_or_script = '(#![^\n]+?[\n])?(<[?]php\s+?)?';
+    return array(
+      "@^{$maybe_php_or_script}//[^\n]*Copyright[^\n]*[\n]\s*@i",
+
+      // We need to be careful about matching after "/*", since otherwise we'll
+      // end up in trouble on code like this, and consume the entire thing:
+      //
+      //  /* a */
+      //  copyright();
+      //  /* b */
+      "@^{$maybe_php_or_script}/[*](?:[^*]|[*][^/])*?Copyright.*?[*]/\s*@is",
+      "@^{$maybe_php_or_script}\s*@",
+    );
+  }
+}
diff --git a/src/lint/linter/ArcanistLicenseLinter.php b/src/lint/linter/ArcanistLicenseLinter.php
new file mode 100644 (file)
index 0000000..536f35a
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+
+/**
+ * @deprecated
+ */
+abstract class ArcanistLicenseLinter extends ArcanistLinter {
+
+  const LINT_NO_LICENSE_HEADER = 1;
+
+  public function willLintPaths(array $paths) {
+    return;
+  }
+
+  public function getLintSeverityMap() {
+    return array();
+  }
+
+  public function getLintNameMap() {
+    return array(
+      self::LINT_NO_LICENSE_HEADER   => 'No License Header',
+    );
+  }
+
+  abstract protected function getLicenseText($copyright_holder);
+  abstract protected function getLicensePatterns();
+
+  public function lintPath($path) {
+    $copyright_holder = $this->getConfig('copyright_holder');
+    if ($copyright_holder === null) {
+      $working_copy = $this->getEngine()->getWorkingCopy();
+      $copyright_holder = $working_copy->getConfig('copyright_holder');
+    }
+
+    if (!$copyright_holder) {
+      return;
+    }
+
+    $patterns = $this->getLicensePatterns();
+    $license = $this->getLicenseText($copyright_holder);
+
+    $data = $this->getData($path);
+    $matches = 0;
+
+    foreach ($patterns as $pattern) {
+      if (preg_match($pattern, $data, $matches)) {
+        $expect = rtrim(implode('', array_slice($matches, 1)))."\n".$license;
+        if (trim($matches[0]) != trim($expect)) {
+          $this->raiseLintAtOffset(
+            0,
+            self::LINT_NO_LICENSE_HEADER,
+            'This file has a missing or out of date license header.',
+            $matches[0],
+            ltrim($expect));
+        }
+        break;
+      }
+    }
+  }
+}
+
+