Page MenuHomePhabricator
Paste P1719

custom autofix linter
ActivePublic

Authored by NorthIsUp on Feb 4 2015, 6:32 PM.
Tags
None
Referenced Files
F282980: custom_autofix_linter
Feb 4 2015, 6:32 PM
Subscribers
```
>>> Lint for tempest/adserver/apps/adserver/views/public.py:
Auto-Fix (Improper imports) ISORT
ERROR:
/Users/adam/src/tempest/tempest/adserver/apps/adserver/views/public.py
Imports are incorrectly sorted.
1 from __future__ import absolute_import
2
3 # Standard Library
4 import json
5 import logging
+ import os
6 from collections import defaultdict
7 from functools import partial
8
Auto-Fix (Improper imports) ISORT
ERROR:
/Users/adam/src/tempest/tempest/adserver/apps/adserver/views/public.py
Imports are incorrectly sorted.
10 from django.conf import settings
11 from django.http import HttpResponse
12 from redis import StrictRedis
13 from toolz.dicttoolz import get_in
14
15 # Project Library
>>> - 16 from tempest.adserver.lib.gutter.bin import Bin
- 17 from tempest.adserver.lib.gutter.helpers import active
- 18 from tempest.adserver.apps.adserver.stats import (
- 19 big_stats,
- 20 stats
- 21 )
+ from tempest.adserver.apps.adserver.stats import (
+ big_stats,
+ stats
+ )
+ from tempest.adserver.lib.gutter.bin import Bin
+ from tempest.adserver.lib.gutter.helpers import active
22 from tempest.adserver.lib.helpers.ads import (
23 adsnative,
24 get_ads_copy,
Auto-Fix (Improper imports) ISORT
ERROR:
/Users/adam/src/tempest/tempest/adserver/apps/adserver/views/public.py
Imports are incorrectly sorted.
33 username_for_request
34 )
35 from tempest.adserver.lib.score.all import (
36 DEFAULT_ALGORITHM,
37 RANDOM
38 )
>>> - 39 import os
- 40 from tempest.adserver.settings import constants
+
41 from tempest.core.response import is_valid_callback_name
42
43 # TODO: Consolidate with Django CACHES
>>> [21] <exec> $ diff -u '/Users/adam/src/tempest/tempest/adserver/apps/adserver/views/public.py' '/private/var/folders/qy/72xp6q453dj5q88w6_wxgnmw0000gn/T/ax99kaz2nows8so4/93309-E45dyg'
<<< [21] <exec> 5,796 us
--- /Users/adam/src/tempest/tempest/adserver/apps/adserver/views/public.py 2015-02-03 18:08:29.000000000 -0800
+++ /private/var/folders/qy/72xp6q453dj5q88w6_wxgnmw0000gn/T/ax99kaz2nows8so4/93309-E45dyg 2015-02-04 10:27:50.000000000 -0800
@@ -1,27 +1,27 @@
from __future__ import absolute_import
-
# Standard Library
import json
import logging
+import os
from collections import defaultdict
from functools import partial
+
# External Libraries
from django.conf import settings
from django.http import HttpResponse
-from redis import StrictRedis
-from toolz.dicttoolz import get_in
+from redis import StrictRedisfrom toolz.dicttoolz import get_in
# Project Library
-from tempest.adserver.lib.gutter.bin import Bin
-from tempest.adserver.lib.gutter.helpers import active
from tempest.adserver.apps.adserver.stats import (
big_stats,
stats
)
+from tempest.adserver.lib.gutter.bin import Bin
+from tempest.adserver.lib.gutter.helpers import active
from tempest.adserver.lib.helpers.ads import (
adsnative,
- get_ads_copy,
+ get_ads_copy,,
get_ads_copy_for_segment,
online_filtering,
taboola
@@ -32,14 +32,13 @@
userid_for_request,
username_for_request
)
-from tempest.adserver.lib.score.all import (
- DEFAULT_ALGORITHM,
+from tempest.adserver.lib.score.all import ( DEFAULT_ALGORITHM,
RANDOM
)
-import os
from tempest.adserver.settings import constants
from tempest.core.response import is_valid_callback_name
+
# TODO: Consolidate with Django CACHES
redis_remote = StrictRedis(settings.REDIS_CACHE_HOST, settings.REDIS_CACHE_PORT)
redis_local = StrictRedis(settings.REDIS_LOCAL_HOST, settings.REDIS_CACHE_PORT)
Apply this patch to tempest/adserver/apps/adserver/views/public.py? [y/N]
```

Event Timeline

NorthIsUp changed the title of this paste from untitled to custom autofix linter.
NorthIsUp updated the paste's language from autodetect to autodetect.

It appears that the ArcanistLintMessage has correctly determined the diff, but the diff that is proposed to be applied is wrong.

wrong stuff:

  • remove line 70
  • add line 78
  • line 106-108 came from nowhere (a newline was removed for some reason)
  • line 115 is added

apparently my replacement text required a leading newline.

how I ended up doing it:

protected function parseLinterOutput($path, $err, $stdout, $stderr) {
  $messages = array();
  $matches = split("\n", $stdout, 2);

  $description = $matches[0];
  $diff = $matches[1];

  $parser = new ArcanistDiffParser();
  $changes = $parser->parseDiff($diff);

  foreach ($changes as $change) {
    foreach ($change->getHunks() as $hunk) {
      $oldText = array();
      $newText = array();

      $replacementText = "";
      $originalText = "";

      $lines = phutil_split_lines($hunk->getCorpus(), false);
      foreach ($lines as $line) {
        $char = strlen($line) ? $line[0] : '~';
        $rest = "\n" . strlen($line) == 1 ? '' : substr($line, 1);

        switch ($char) {
          case '-':
          $originalText .= $rest;
          break;

          case '+':
          $replacementText .= $rest;
          break;

          case '~':
          break;

          case ' ':
          $originalText .= $rest;
          $replacementText .= $rest;
          break;
        }
      }

      $message = new ArcanistLintMessage();
      $message->setPath($path);
      $message->setLine($hunk->getOldOffset());
      $message->setChar(0);
      $message->setCode('Improper imports');
      $message->setName('ISORT');
      // $message->setSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX);
      $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
      $message->setReplacementText($replacementText);
      $message->setOriginalText($originalText);

      $messages[] = $message;
    }
  }

  if ($err && !$messages) {
    return false;
  }

  return $messages;
}