Page MenuHomePhabricator

D16922.id40738.diff
No OneTemporary

D16922.id40738.diff

Index: src/__phutil_library_map__.php
===================================================================
--- src/__phutil_library_map__.php
+++ src/__phutil_library_map__.php
@@ -308,6 +308,8 @@
'ArcanistPyFlakesLinterTestCase' => 'lint/linter/__tests__/ArcanistPyFlakesLinterTestCase.php',
'ArcanistPyLintLinter' => 'lint/linter/ArcanistPyLintLinter.php',
'ArcanistPyLintLinterTestCase' => 'lint/linter/__tests__/ArcanistPyLintLinterTestCase.php',
+ 'ArcanistPythonImportsLinter' => 'lint/linter/ArcanistPythonImportsLinter.php',
+ 'ArcanistPythonImportsLinterTestCase' => 'lint/linter/__tests__/ArcanistPythonImportsLinterTestCase.php',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistRaggedClassTreeEdgeXHPASTLinterRule.php',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistRaggedClassTreeEdgeXHPASTLinterRuleTestCase.php',
'ArcanistRepositoryAPI' => 'repository/api/ArcanistRepositoryAPI.php',
@@ -746,6 +748,8 @@
'ArcanistPyFlakesLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistPyLintLinter' => 'ArcanistExternalLinter',
'ArcanistPyLintLinterTestCase' => 'ArcanistExternalLinterTestCase',
+ 'ArcanistPythonImportsLinter' => 'ArcanistLinter',
+ 'ArcanistPythonImportsLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistRepositoryAPI' => 'Phobject',
Index: src/lint/linter/ArcanistPythonImportsLinter.php
===================================================================
--- /dev/null
+++ src/lint/linter/ArcanistPythonImportsLinter.php
@@ -0,0 +1,104 @@
+<?php
+
+/*
+ * Code obtained from https://github.com/pinterest/arcanist-linters
+ */
+
+/**
+ * Copyright 2016 Pinterest, Inc.
+ *
+ * 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.
+ */
+
+/**
+ * Lints for illegal Python module imports.
+ */
+final class ArcanistPythonImportsLinter extends ArcanistLinter {
+
+ const LINT_IMPORT = 1;
+
+ private $pattern;
+ private $message;
+
+ public function getInfoName() {
+ return 'Python Imports Linter';
+ }
+
+ public function getInfoDescription() {
+ return pht('Lints for illegal Python module imports.');
+ }
+
+ public function getLinterName() {
+ return 'PYTHON-IMPORTS';
+ }
+
+ public function getLinterConfigurationName() {
+ return 'python-imports';
+ }
+
+ public function getLintNameMap() {
+ return array(
+ self::LINT_IMPORT => pht('Illegal Import'),
+ );
+ }
+
+ public function getLinterConfigurationOptions() {
+ $options = array(
+ 'python-imports.pattern' => array(
+ 'type' => 'string',
+ 'help' => pht('Module name pattern.'),
+ ),
+ 'python-imports.message' => array(
+ 'type' => 'optional string',
+ 'help' => pht('Message to display when caught.'),
+ ),
+ );
+
+ return $options + parent::getLinterConfigurationOptions();
+ }
+
+ public function setLinterConfigurationValue($key, $value) {
+ switch ($key) {
+ case 'python-imports.pattern':
+ $this->pattern = $value;
+ return;
+ case 'python-imports.message':
+ $this->message = $value;
+ return;
+
+ default:
+ return parent::setLinterConfigurationValue($key, $value);
+ }
+ }
+
+ public function lintPath($path) {
+ $lines = phutil_split_lines($this->getData($path), false);
+ $regex = "/^\s*(from|import)\s+(?P<module>{$this->pattern})\b/";
+
+ foreach ($lines as $lineno => $line) {
+ $matches = array();
+ if (preg_match($regex, $line, $matches, PREG_OFFSET_CAPTURE)) {
+ list($module, $offset) = $matches['module'];
+ $this->raiseLintAtLine(
+ $lineno + 1,
+ $offset + 1,
+ self::LINT_IMPORT,
+ pht(
+ 'This line imports a module ("%s") that is not allowed in '.
+ 'this file. %s',
+ $module, $this->message),
+ $module);
+ }
+ }
+ }
+}
Index: src/lint/linter/__tests__/ArcanistPythonImportsLinterTestCase.php
===================================================================
--- /dev/null
+++ src/lint/linter/__tests__/ArcanistPythonImportsLinterTestCase.php
@@ -0,0 +1,10 @@
+<?php
+
+final class ArcanistPythonImportsLinterTestCase
+ extends ArcanistExternalLinterTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(dirname(__FILE__).'/python-imports/');
+ }
+
+}
Index: src/lint/linter/__tests__/python-imports/illegal_import.lint-test
===================================================================
--- /dev/null
+++ src/lint/linter/__tests__/python-imports/illegal_import.lint-test
@@ -0,0 +1,3 @@
+import illegal.module
+~~~~~~~~~~
+error:1:8

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 7, 9:39 AM (12 h, 1 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
17396663
Default Alt Text
D16922.id40738.diff (5 KB)

Event Timeline