git-linter/modules/git_lint/git_lint.html

548 lines
74 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>git_lint.git_lint &#8212; Git Lint 0.0.6 documentation</title>
<link rel="stylesheet" href="../../static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../../static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../',
VERSION: '0.0.6',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../static/jquery.js"></script>
<script type="text/javascript" src="../../static/underscore.js"></script>
<script type="text/javascript" src="../../static/doctools.js"></script>
<link rel="top" title="Git Lint 0.0.6 documentation" href="../../index.html" />
<link rel="up" title="Module code" href="../index.html" />
<link rel="stylesheet" href="../../static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for git_lint.git_lint</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>
<span class="kn">import</span> <span class="nn">getopt</span>
<span class="kn">import</span> <span class="nn">gettext</span>
<span class="kn">import</span> <span class="nn">operator</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">pprint</span>
<span class="k">try</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">configparser</span>
<span class="k">except</span> <span class="ne">ImportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">ConfigParser</span> <span class="k">as</span> <span class="nn">configparser</span>
<span class="n">_</span> <span class="o">=</span> <span class="n">gettext</span><span class="o">.</span><span class="n">gettext</span>
<span class="c1"># ___ __ _ ___ _</span>
<span class="c1"># / __|___ _ _ / _(_)__ _ | _ \___ __ _ __| |___ _ _</span>
<span class="c1"># | (__/ _ \ &#39; \| _| / _` | | / -_) _` / _` / -_) &#39;_|</span>
<span class="c1"># \___\___/_||_|_| |_\__, | |_|_\___\__,_\__,_\___|_|</span>
<span class="c1"># |___/</span>
<span class="c1"># (commandLineDictionary, repositoryLocation) -&gt; (configurationDictionary | exit)</span>
<div class="viewcode-block" id="load_config"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.load_config">[docs]</a><span class="k">def</span> <span class="nf">load_config</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">base</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Loads the git-lint configuration file.</span>
<span class="sd"> Returns the configuration file as a dictionary of dictionaries.</span>
<span class="sd"> Performs substitutions as specified in the SafeConfigParser</span>
<span class="sd"> specification; the only one performed currently is the &#39;repodir&#39;</span>
<span class="sd"> will be replaced with the base directory of the repository.</span>
<span class="sd"> Combined with the option to specify the .git-lint configuration as</span>
<span class="sd"> a directory, this allows users to keep per-project configuration</span>
<span class="sd"> files for specific linters.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">find_config_file</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">base</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Returns the configuration file from a prioritized list of locations.</span>
<span class="sd"> </span>
<span class="sd"> Locations are prioritized as:</span>
<span class="sd"> 1. From the command line. Fail if specified but not found</span>
<span class="sd"> 2. The repository&#39;s root directory, as the file .git-lint</span>
<span class="sd"> 3. The repository&#39;s root directory, as the file .git-lint/config</span>
<span class="sd"> 4. The user&#39;s home directory, as file .git-lint</span>
<span class="sd"> 5. The user&#39;s home directory, as the file .git-lint/config</span>
<span class="sd"> </span>
<span class="sd"> If no configuration file is found, this is an error.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="s1">&#39;config&#39;</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
<span class="n">config</span> <span class="o">=</span> <span class="n">options</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">]</span>
<span class="n">configpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">configpath</span><span class="p">):</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;Configuration file not found: </span><span class="si">{}</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">config</span><span class="p">))</span>
<span class="k">return</span> <span class="n">configpath</span>
<span class="n">home</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;HOME&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">possibles</span> <span class="o">=</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;.git-lint&#39;</span><span class="p">),</span>
<span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;.git-lint/config&#39;</span><span class="p">)]</span> <span class="o">+</span> <span class="p">((</span><span class="n">home</span> <span class="ow">and</span> <span class="p">[</span>
<span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">home</span><span class="p">,</span> <span class="s1">&#39;.git-lint&#39;</span><span class="p">),</span>
<span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">home</span><span class="p">,</span> <span class="s1">&#39;.git-lint/config&#39;</span><span class="p">)])</span> <span class="ow">or</span> <span class="p">[])</span>
<span class="n">matches</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">possibles</span> <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">p</span><span class="p">)]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">matches</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;No configuration file found, tried: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">possibles</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">matches</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">Linter</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Linter&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;linter&#39;</span><span class="p">])</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">find_config_file</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">base</span><span class="p">)</span>
<span class="n">configloader</span> <span class="o">=</span> <span class="n">configparser</span><span class="o">.</span><span class="n">SafeConfigParser</span><span class="p">()</span>
<span class="n">configloader</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="n">configloader</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s1">&#39;DEFAULT&#39;</span><span class="p">,</span> <span class="s1">&#39;repodir&#39;</span><span class="p">,</span> <span class="n">base</span><span class="p">)</span>
<span class="k">return</span> <span class="p">[</span><span class="n">Linter</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">configloader</span><span class="o">.</span><span class="n">items</span><span class="p">(</span><span class="n">section</span><span class="p">)})</span>
<span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">configloader</span><span class="o">.</span><span class="n">sections</span><span class="p">()]</span></div>
<span class="c1"># ___ _ _</span>
<span class="c1"># / __(_) |_</span>
<span class="c1"># | (_ | | _|</span>
<span class="c1"># \___|_|\__|</span>
<span class="c1">#</span>
<div class="viewcode-block" id="get_git_response_raw"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_git_response_raw">[docs]</a><span class="k">def</span> <span class="nf">get_git_response_raw</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
<span class="n">fullcmd</span> <span class="o">=</span> <span class="p">([</span><span class="s1">&#39;git&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">cmd</span><span class="p">)</span>
<span class="n">process</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">fullcmd</span><span class="p">,</span>
<span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
<span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
<span class="n">universal_newlines</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span> <span class="o">=</span> <span class="n">process</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
<span class="k">return</span> <span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="n">process</span><span class="o">.</span><span class="n">returncode</span><span class="p">)</span></div>
<div class="viewcode-block" id="get_git_response"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_git_response">[docs]</a><span class="k">def</span> <span class="nf">get_git_response</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">returncode</span><span class="p">)</span> <span class="o">=</span> <span class="n">get_git_response_raw</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
<span class="k">return</span> <span class="n">out</span></div>
<div class="viewcode-block" id="split_git_response"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.split_git_response">[docs]</a><span class="k">def</span> <span class="nf">split_git_response</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">returncode</span><span class="p">)</span> <span class="o">=</span> <span class="n">get_git_response_raw</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
<span class="k">return</span> <span class="n">out</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span></div>
<div class="viewcode-block" id="run_git_command"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.run_git_command">[docs]</a><span class="k">def</span> <span class="nf">run_git_command</span><span class="p">(</span><span class="n">cmd</span><span class="p">):</span>
<span class="n">fullcmd</span> <span class="o">=</span> <span class="p">([</span><span class="s1">&#39;git&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">cmd</span><span class="p">)</span>
<span class="k">return</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">fullcmd</span><span class="p">,</span>
<span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
<span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
<span class="n">universal_newlines</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>
<div class="viewcode-block" id="get_shell_response"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_shell_response">[docs]</a><span class="k">def</span> <span class="nf">get_shell_response</span><span class="p">(</span><span class="n">fullcmd</span><span class="p">):</span>
<span class="n">process</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">fullcmd</span><span class="p">,</span>
<span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
<span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
<span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">universal_newlines</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span> <span class="o">=</span> <span class="n">process</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
<span class="k">return</span> <span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="n">process</span><span class="o">.</span><span class="n">returncode</span><span class="p">)</span></div>
<div class="viewcode-block" id="get_git_base"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_git_base">[docs]</a><span class="k">def</span> <span class="nf">get_git_base</span><span class="p">():</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">returncode</span><span class="p">)</span> <span class="o">=</span> <span class="n">get_git_response_raw</span><span class="p">(</span>
<span class="p">[</span><span class="s1">&#39;rev-parse&#39;</span><span class="p">,</span> <span class="s1">&#39;--show-toplevel&#39;</span><span class="p">])</span>
<span class="k">return</span> <span class="p">(</span><span class="n">returncode</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">out</span><span class="o">.</span><span class="n">rstrip</span><span class="p">())</span> <span class="ow">or</span> <span class="kc">None</span></div>
<div class="viewcode-block" id="get_git_head"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_git_head">[docs]</a><span class="k">def</span> <span class="nf">get_git_head</span><span class="p">():</span>
<span class="n">empty_repository_hash</span> <span class="o">=</span> <span class="s1">&#39;4b825dc642cb6eb9a060e54bf8d69288fbee4904&#39;</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="n">returncode</span><span class="p">)</span> <span class="o">=</span> <span class="n">get_git_response_raw</span><span class="p">(</span>
<span class="p">[</span><span class="s1">&#39;rev-parse&#39;</span><span class="p">,</span> <span class="s1">&#39;--verify HEAD&#39;</span><span class="p">])</span>
<span class="k">return</span> <span class="p">((</span><span class="n">err</span> <span class="ow">and</span> <span class="n">empty_repository_hash</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">&#39;HEAD&#39;</span><span class="p">)</span></div>
<span class="n">git_base</span> <span class="o">=</span> <span class="n">get_git_base</span><span class="p">()</span>
<span class="n">git_head</span> <span class="o">=</span> <span class="n">get_git_head</span><span class="p">()</span>
<span class="c1"># _ _ _ _ _ _ _ _</span>
<span class="c1"># | | | | |_(_) (_) |_(_)___ ___</span>
<span class="c1"># | |_| | _| | | | _| / -_|_-&lt;</span>
<span class="c1"># \___/ \__|_|_|_|\__|_\___/__/</span>
<span class="c1">#</span>
<div class="viewcode-block" id="MatchFilter"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.MatchFilter">[docs]</a><span class="k">class</span> <span class="nc">MatchFilter</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">config</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">matcher</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">make_match_filter_matcher</span><span class="p">([</span><span class="n">v</span><span class="o">.</span><span class="n">linter</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;match&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">config</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">matcher</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="MatchFilter.make_match_filter_matcher"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.MatchFilter.make_match_filter_matcher">[docs]</a> <span class="k">def</span> <span class="nf">make_match_filter_matcher</span><span class="p">(</span><span class="n">extensions</span><span class="p">):</span>
<span class="n">trimmed</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span>
<span class="p">[</span><span class="n">ex</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">ex</span> <span class="ow">in</span> <span class="n">extensions</span><span class="p">],</span> <span class="p">[])]</span>
<span class="n">cleaned</span> <span class="o">=</span> <span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">r&#39;^\.&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">trimmed</span><span class="p">]</span>
<span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">r&#39;\.&#39;</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">cleaned</span><span class="p">)</span> <span class="o">+</span> <span class="s1">r&#39;$&#39;</span><span class="p">)</span></div></div>
<span class="c1"># ___ _ _ _ _ _</span>
<span class="c1"># / __| |_ ___ __| |__ | (_)_ _| |_ ___ _ _ ___</span>
<span class="c1"># | (__| &#39; \/ -_) _| / / | | | &#39; \ _/ -_) &#39;_(_-&lt;</span>
<span class="c1"># \___|_||_\___\__|_\_\ |_|_|_||_\__\___|_| /__/</span>
<span class="c1">#</span>
<div class="viewcode-block" id="executable_exists"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.executable_exists">[docs]</a><span class="k">def</span> <span class="nf">executable_exists</span><span class="p">(</span><span class="n">script</span><span class="p">,</span> <span class="n">label</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">script</span><span class="p">):</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;Syntax error in command configuration for </span><span class="si">{}</span><span class="s1"> &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">label</span><span class="p">))</span>
<span class="n">scriptname</span> <span class="o">=</span> <span class="n">script</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">scriptname</span><span class="p">):</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;Syntax error in command configuration for </span><span class="si">{}</span><span class="s1"> &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">label</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">is_executable</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">access</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">X_OK</span><span class="p">)</span>
<span class="k">if</span> <span class="n">scriptname</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="n">is_executable</span><span class="p">(</span><span class="n">scriptname</span><span class="p">)</span> <span class="ow">and</span> <span class="n">scriptname</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">None</span>
<span class="c1"># shutil.which() doesn&#39;t appear until Python 3, darnit.</span>
<span class="n">possibles</span> <span class="o">=</span> <span class="p">[</span><span class="n">path</span> <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span>
<span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">scriptname</span><span class="p">)</span>
<span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;PATH&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)]</span>
<span class="k">if</span> <span class="n">is_executable</span><span class="p">(</span><span class="n">path</span><span class="p">)]</span>
<span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">possibles</span><span class="p">)</span> <span class="ow">and</span> <span class="n">possibles</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span> <span class="ow">or</span> <span class="kc">False</span></div>
<div class="viewcode-block" id="get_linter_status"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_linter_status">[docs]</a><span class="k">def</span> <span class="nf">get_linter_status</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">get_working_linter_names</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">config</span>
<span class="k">if</span> <span class="n">executable_exists</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">linter</span><span class="p">[</span><span class="s1">&#39;command&#39;</span><span class="p">],</span> <span class="n">i</span><span class="o">.</span><span class="n">name</span><span class="p">)]</span>
<span class="n">working_linter_names</span> <span class="o">=</span> <span class="n">get_working_linter_names</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
<span class="n">broken_linter_names</span> <span class="o">=</span> <span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="n">i</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">config</span><span class="p">])</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">working_linter_names</span><span class="p">))</span>
<span class="k">return</span> <span class="n">working_linter_names</span><span class="p">,</span> <span class="n">broken_linter_names</span></div>
<span class="c1"># ___ _ _ _ _ __ __ _ _</span>
<span class="c1"># / __|___| |_ | (_)__| |_ ___ / _| / _(_) |___ ___</span>
<span class="c1"># | (_ / -_) _| | | (_-&lt; _| / _ \ _| | _| | / -_|_-&lt;</span>
<span class="c1"># \___\___|\__| |_|_/__/\__| \___/_| |_| |_|_\___/__/</span>
<span class="c1">#</span>
<div class="viewcode-block" id="get_filelist"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_filelist">[docs]</a><span class="k">def</span> <span class="nf">get_filelist</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Returns the list of files against which we&#39;ll run the linters. &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">base_file_filter</span><span class="p">(</span><span class="n">files</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Return the full path for all files &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">git_base</span><span class="p">,</span> <span class="n">file</span><span class="p">)</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">cwd_file_filter</span><span class="p">(</span><span class="n">filenames</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Return the full path for only those files in the cwd and down &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> <span class="n">git_base</span><span class="p">):</span>
<span class="k">return</span> <span class="n">base_file_filter</span><span class="p">(</span><span class="n">filenames</span><span class="p">)</span>
<span class="n">gitcwd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">relpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> <span class="n">git_base</span><span class="p">),</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">base_file_filter</span><span class="p">([</span><span class="n">filename</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">filenames</span>
<span class="k">if</span> <span class="n">filename</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">gitcwd</span><span class="p">)])</span>
<span class="k">def</span> <span class="nf">check_for_conflicts</span><span class="p">(</span><span class="n">filesets</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Scan list of porcelain files for merge conflic state. &quot;&quot;&quot;</span>
<span class="n">MERGE_CONFLICT_PAIRS</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="s1">&#39;DD&#39;</span><span class="p">,</span> <span class="s1">&#39;DU&#39;</span><span class="p">,</span> <span class="s1">&#39;AU&#39;</span><span class="p">,</span> <span class="s1">&#39;AA&#39;</span><span class="p">,</span> <span class="s1">&#39;UD&#39;</span><span class="p">,</span> <span class="s1">&#39;UA&#39;</span><span class="p">,</span> <span class="s1">&#39;UU&#39;</span><span class="p">])</span>
<span class="n">status_pairs</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="s1">&#39;&#39;</span> <span class="o">+</span> <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">filesets</span><span class="p">])</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">status_pairs</span> <span class="o">&amp;</span> <span class="n">MERGE_CONFLICT_PAIRS</span><span class="p">):</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span>
<span class="n">_</span><span class="p">(</span><span class="s1">&#39;Current repository contains merge conflicts. Linters will not be run.&#39;</span><span class="p">))</span>
<span class="k">return</span> <span class="n">filesets</span>
<span class="k">def</span> <span class="nf">remove_submodules</span><span class="p">(</span><span class="n">files</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Remove all submodules from the list of files git-lint cares about. &quot;&quot;&quot;</span>
<span class="n">fixer_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">&#39;^(</span><span class="se">\\</span><span class="s1">.</span><span class="se">\\</span><span class="s1">.</span><span class="se">\\</span><span class="s1">/)+&#39;</span><span class="p">)</span>
<span class="n">submodules</span> <span class="o">=</span> <span class="n">split_git_response</span><span class="p">([</span><span class="s1">&#39;submodule&#39;</span><span class="p">,</span> <span class="s1">&#39;status&#39;</span><span class="p">])</span>
<span class="n">submodule_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">fixer_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">submodule</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)[</span><span class="mi">2</span><span class="p">])</span>
<span class="k">for</span> <span class="n">submodule</span> <span class="ow">in</span> <span class="n">submodules</span><span class="p">]</span>
<span class="k">return</span> <span class="p">[</span><span class="n">file</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">files</span> <span class="k">if</span> <span class="p">(</span><span class="n">file</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">submodule_names</span><span class="p">)]</span>
<span class="k">def</span> <span class="nf">get_porcelain_status</span><span class="p">():</span>
<span class="sd">&quot;&quot;&quot; Return the status of all files in the system. &quot;&quot;&quot;</span>
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;status&#39;</span><span class="p">,</span> <span class="s1">&#39;-z&#39;</span><span class="p">,</span> <span class="s1">&#39;--porcelain&#39;</span><span class="p">,</span>
<span class="s1">&#39;--untracked-files=all&#39;</span><span class="p">,</span> <span class="s1">&#39;--ignore-submodules=all&#39;</span><span class="p">]</span>
<span class="n">stream</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry</span> <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">get_git_response</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">u&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="c1"># Yeah, baby, recursion, the way this is meant to be handled.</span>
<span class="c1"># If you have more than 999 files that need linting, you have</span>
<span class="c1"># a bigger problem...</span>
<span class="k">def</span> <span class="nf">parse_stream</span><span class="p">(</span><span class="n">acc</span><span class="p">,</span> <span class="n">stream</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Parse the list of files. T</span>
<span class="sd"> The list is null-terminated, but is not columnar. If</span>
<span class="sd"> there&#39;s an &#39;R&#39; in the index state, it means the file was</span>
<span class="sd"> renamed and the old name is added as a column, so it&#39;s a</span>
<span class="sd"> special case as we accumulate the list of files.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">stream</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="n">acc</span>
<span class="n">entry</span> <span class="o">=</span> <span class="n">stream</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">workspace</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">entry</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">entry</span><span class="p">[</span><span class="mi">3</span><span class="p">:])</span>
<span class="k">if</span> <span class="n">index</span> <span class="o">==</span> <span class="s1">&#39;R&#39;</span><span class="p">:</span>
<span class="n">stream</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">parse_stream</span><span class="p">(</span><span class="n">acc</span> <span class="o">+</span> <span class="p">[(</span><span class="n">index</span><span class="p">,</span> <span class="n">workspace</span><span class="p">,</span> <span class="n">filename</span><span class="p">)],</span> <span class="n">stream</span><span class="p">)</span>
<span class="k">return</span> <span class="n">check_for_conflicts</span><span class="p">(</span><span class="n">parse_stream</span><span class="p">([],</span> <span class="n">stream</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">staging_list</span><span class="p">():</span>
<span class="sd">&quot;&quot;&quot; Return the list of files added or modified to the stage &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">[</span><span class="n">filename</span> <span class="k">for</span> <span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">workspace</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span> <span class="ow">in</span> <span class="n">get_porcelain_status</span><span class="p">()</span>
<span class="k">if</span> <span class="n">index</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;M&#39;</span><span class="p">]]</span>
<span class="k">def</span> <span class="nf">working_list</span><span class="p">():</span>
<span class="sd">&quot;&quot;&quot; Return the list of files that have been modified in the workspace.</span>
<span class="sd"> Includes the &#39;?&#39; to include files that git is not currently tracking.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">[</span><span class="n">filename</span> <span class="k">for</span> <span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">workspace</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span> <span class="ow">in</span> <span class="n">get_porcelain_status</span><span class="p">()</span>
<span class="k">if</span> <span class="n">workspace</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;M&#39;</span><span class="p">,</span> <span class="s1">&#39;?&#39;</span><span class="p">]]</span>
<span class="k">def</span> <span class="nf">all_list</span><span class="p">():</span>
<span class="sd">&quot;&quot;&quot; Return all the files git is currently tracking for this repository. &quot;&quot;&quot;</span>
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;ls-tree&#39;</span><span class="p">,</span> <span class="s1">&#39;--name-only&#39;</span><span class="p">,</span> <span class="s1">&#39;--full-tree&#39;</span><span class="p">,</span> <span class="s1">&#39;-r&#39;</span><span class="p">,</span> <span class="s1">&#39;-z&#39;</span><span class="p">,</span> <span class="n">git_head</span><span class="p">]</span>
<span class="k">return</span> <span class="p">[</span><span class="n">file</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">get_git_response</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">u&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">extras</span><span class="p">):</span>
<span class="n">cwd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">())</span>
<span class="n">extras_fullpathed</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">cwd</span><span class="p">,</span> <span class="n">f</span><span class="p">))</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">extras</span><span class="p">])</span>
<span class="n">not_found</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">extras_fullpathed</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">f</span><span class="p">)])</span>
<span class="k">return</span> <span class="p">([</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">relpath</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">cwd</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">(</span><span class="n">extras_fullpathed</span> <span class="o">-</span> <span class="n">not_found</span><span class="p">)],</span> <span class="n">not_found</span><span class="p">)</span>
<span class="n">working_directory_trans</span> <span class="o">=</span> <span class="n">cwd_file_filter</span>
<span class="k">if</span> <span class="s1">&#39;base&#39;</span> <span class="ow">in</span> <span class="n">options</span> <span class="ow">or</span> <span class="s1">&#39;every&#39;</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
<span class="n">working_directory_trans</span> <span class="o">=</span> <span class="n">base_file_filter</span>
<span class="n">file_list_generator</span> <span class="o">=</span> <span class="n">working_list</span>
<span class="k">if</span> <span class="s1">&#39;all&#39;</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
<span class="n">file_list_generator</span> <span class="o">=</span> <span class="n">all_list</span>
<span class="k">if</span> <span class="s1">&#39;staging&#39;</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
<span class="n">file_list_generator</span> <span class="o">=</span> <span class="n">staging_list</span>
<span class="k">return</span> <span class="p">(</span><span class="n">working_directory_trans</span><span class="p">(</span><span class="n">remove_submodules</span><span class="p">(</span><span class="n">file_list_generator</span><span class="p">())),</span> <span class="p">[])</span></div>
<span class="c1"># ___ _ _</span>
<span class="c1"># / __| |_ __ _ __ _(_)_ _ __ _ __ __ ___ _ __ _ _ __ _ __ ___ _ _</span>
<span class="c1"># \__ \ _/ _` / _` | | &#39; \/ _` | \ V V / &#39;_/ _` | &#39;_ \ &#39;_ \/ -_) &#39;_|</span>
<span class="c1"># |___/\__\__,_\__, |_|_||_\__, | \_/\_/|_| \__,_| .__/ .__/\___|_|</span>
<span class="c1"># |___/ |___/ |_| |_|</span>
<div class="viewcode-block" id="StagingRunner"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.StagingRunner">[docs]</a><span class="k">class</span> <span class="nc">StagingRunner</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filenames</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">filenames</span> <span class="o">=</span> <span class="n">filenames</span>
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">time_gather</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
<span class="n">stats</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">stats</span><span class="o">.</span><span class="n">st_atime</span><span class="p">,</span> <span class="n">stats</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">times</span> <span class="o">=</span> <span class="p">[</span><span class="n">time_gather</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="p">]</span>
<span class="n">run_git_command</span><span class="p">([</span><span class="s1">&#39;stash&#39;</span><span class="p">,</span> <span class="s1">&#39;--keep-index&#39;</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
<span class="n">run_git_command</span><span class="p">([</span><span class="s1">&#39;reset&#39;</span><span class="p">,</span> <span class="s1">&#39;--hard&#39;</span><span class="p">])</span>
<span class="n">run_git_command</span><span class="p">([</span><span class="s1">&#39;stash&#39;</span><span class="p">,</span> <span class="s1">&#39;pop&#39;</span><span class="p">,</span> <span class="s1">&#39;--quiet&#39;</span><span class="p">,</span> <span class="s1">&#39;--index&#39;</span><span class="p">])</span>
<span class="k">for</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">timepair</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">times</span><span class="p">:</span>
<span class="n">os</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">timepair</span><span class="p">)</span></div>
<div class="viewcode-block" id="WorkspaceRunner"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.WorkspaceRunner">[docs]</a><span class="k">class</span> <span class="nc">WorkspaceRunner</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filenames</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
<span class="k">pass</span></div>
<span class="c1"># ___ _ _ _</span>
<span class="c1"># | _ \_ _ _ _ | (_)_ _| |_ _ __ __ _ ______</span>
<span class="c1"># | / || | &#39; \ | | | &#39; \ _| | &#39;_ \/ _` (_-&lt;_-&lt;</span>
<span class="c1"># |_|_\\_,_|_||_| |_|_|_||_\__| | .__/\__,_/__/__/</span>
<span class="c1"># |_|</span>
<div class="viewcode-block" id="Linters"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.Linters">[docs]</a><span class="k">class</span> <span class="nc">Linters</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">linters</span><span class="p">,</span> <span class="n">filenames</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">linters</span> <span class="o">=</span> <span class="n">linters</span>
<span class="bp">self</span><span class="o">.</span><span class="n">filenames</span> <span class="o">=</span> <span class="n">filenames</span>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Linters.encode_shell_messages"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.Linters.encode_shell_messages">[docs]</a> <span class="k">def</span> <span class="nf">encode_shell_messages</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">messages</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="s1">&#39;</span><span class="si">{}{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">messages</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()]</span></div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Linters.run_external_linter"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.Linters.run_external_linter">[docs]</a> <span class="k">def</span> <span class="nf">run_external_linter</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">linter</span><span class="p">,</span> <span class="n">linter_name</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Run one linter against one file.</span>
<span class="sd"> If the result matches the error condition specified in the configuration file,</span>
<span class="sd"> return the error code and messages, otherwise return nothing.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">cmd</span> <span class="o">=</span> <span class="n">linter</span><span class="p">[</span><span class="s1">&#39;command&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39; &quot;&#39;</span> <span class="o">+</span> <span class="n">filename</span> <span class="o">+</span> <span class="s1">&#39;&quot;&#39;</span>
<span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">err</span><span class="p">,</span> <span class="n">returncode</span><span class="p">)</span> <span class="o">=</span> <span class="n">get_shell_response</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
<span class="n">failed</span> <span class="o">=</span> <span class="p">((</span><span class="n">out</span> <span class="ow">and</span> <span class="p">(</span><span class="n">linter</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;condition&#39;</span><span class="p">,</span> <span class="s1">&#39;error&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;output&#39;</span><span class="p">))</span> <span class="ow">or</span> <span class="n">err</span> <span class="ow">or</span> <span class="p">(</span><span class="ow">not</span> <span class="p">(</span><span class="n">returncode</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)))</span>
<span class="n">trimmed_filename</span> <span class="o">=</span> <span class="n">filename</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">git_base</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">failed</span><span class="p">:</span>
<span class="k">return</span> <span class="p">(</span><span class="n">trimmed_filename</span><span class="p">,</span> <span class="n">linter_name</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[])</span>
<span class="n">prefix</span> <span class="o">=</span> <span class="p">(((</span><span class="n">linter</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;print&#39;</span><span class="p">,</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;true&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="s1">&#39; &#39;</span><span class="p">)</span> <span class="ow">or</span>
<span class="s1">&#39; </span><span class="si">{}</span><span class="s1">: &#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trimmed_filename</span><span class="p">))</span>
<span class="n">output</span> <span class="o">=</span> <span class="p">(</span><span class="n">Linters</span><span class="o">.</span><span class="n">encode_shell_messages</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">out</span><span class="p">)</span> <span class="o">+</span>
<span class="p">((</span><span class="n">err</span> <span class="ow">and</span> <span class="n">Linters</span><span class="o">.</span><span class="n">encode_shell_messages</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">err</span><span class="p">))</span> <span class="ow">or</span> <span class="p">[]))</span>
<span class="k">return</span> <span class="p">(</span><span class="n">trimmed_filename</span><span class="p">,</span> <span class="n">linter_name</span><span class="p">,</span> <span class="p">(</span><span class="n">returncode</span> <span class="ow">or</span> <span class="mi">1</span><span class="p">),</span> <span class="n">output</span><span class="p">)</span></div>
<span class="nd">@staticmethod</span>
<div class="viewcode-block" id="Linters.run_one_linter"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.Linters.run_one_linter">[docs]</a> <span class="k">def</span> <span class="nf">run_one_linter</span><span class="p">(</span><span class="n">linter</span><span class="p">,</span> <span class="n">filenames</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Runs one linter against a set of files</span>
<span class="sd"> Creates a match filter for the linter, extract the files to be</span>
<span class="sd"> linted, and runs the linter against each file, returning the</span>
<span class="sd"> result as a list of successes and failures. Failures have a</span>
<span class="sd"> return code and the output of the lint process.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">match_filter</span> <span class="o">=</span> <span class="n">MatchFilter</span><span class="p">([</span><span class="n">linter</span><span class="p">])</span>
<span class="n">files</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">filename</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">filenames</span> <span class="k">if</span> <span class="n">match_filter</span><span class="p">(</span><span class="n">filename</span><span class="p">)])</span>
<span class="k">return</span> <span class="p">[</span><span class="n">Linters</span><span class="o">.</span><span class="n">run_external_linter</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">linter</span><span class="o">.</span><span class="n">linter</span><span class="p">,</span> <span class="n">linter</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">files</span><span class="p">]</span></div>
<span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Returns a function to run a set of linters against a set of filenames</span>
<span class="sd"> This returns a function because it&#39;s going to be wrapped in a</span>
<span class="sd"> runner to better handle stashing and restoring a staged commit.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span>
<span class="p">[</span><span class="n">Linters</span><span class="o">.</span><span class="n">run_one_linter</span><span class="p">(</span><span class="n">linter</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="p">)</span> <span class="k">for</span> <span class="n">linter</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">linters</span><span class="p">],</span> <span class="p">[])</span>
<div class="viewcode-block" id="Linters.dryrun"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.Linters.dryrun">[docs]</a> <span class="k">def</span> <span class="nf">dryrun</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">dryrunonefile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">linter</span><span class="p">):</span>
<span class="n">trimmed_filename</span> <span class="o">=</span> <span class="n">filename</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">git_base</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="n">trimmed_filename</span><span class="p">,</span> <span class="n">linter</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39; </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trimmed_filename</span><span class="p">)])</span>
<span class="k">def</span> <span class="nf">dryrunonce</span><span class="p">(</span><span class="n">linter</span><span class="p">,</span> <span class="n">filenames</span><span class="p">):</span>
<span class="n">match_filter</span> <span class="o">=</span> <span class="n">MatchFilter</span><span class="p">([</span><span class="n">linter</span><span class="p">])</span>
<span class="n">files_to_check</span> <span class="o">=</span> <span class="p">[</span><span class="n">filename</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">filenames</span> <span class="k">if</span> <span class="n">match_filter</span><span class="p">(</span><span class="n">filename</span><span class="p">)]</span>
<span class="k">return</span> <span class="p">[</span><span class="n">dryrunonefile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">linter</span><span class="p">)</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">files_to_check</span><span class="p">]</span>
<span class="k">return</span> <span class="n">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="p">[</span><span class="n">dryrunonce</span><span class="p">(</span><span class="n">linter</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="p">)</span> <span class="k">for</span> <span class="n">linter</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">linters</span><span class="p">],</span> <span class="p">[])</span></div></div>
<div class="viewcode-block" id="run_linters"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.run_linters">[docs]</a><span class="k">def</span> <span class="nf">run_linters</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="n">extras</span><span class="o">=</span><span class="p">[]):</span>
<span class="k">def</span> <span class="nf">build_config_subset</span><span class="p">(</span><span class="n">keys</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot; Returns a subset of the configuration, with only those linters mentioned in keys &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">[</span><span class="n">item</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">config</span> <span class="k">if</span> <span class="n">item</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">]</span>
<span class="sd">&quot;&quot;&quot; Runs the requested linters &quot;&quot;&quot;</span>
<span class="n">all_filenames</span><span class="p">,</span> <span class="n">unfindable_filenames</span> <span class="o">=</span> <span class="n">get_filelist</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">extras</span><span class="p">)</span>
<span class="n">is_lintable</span> <span class="o">=</span> <span class="n">MatchFilter</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
<span class="n">lintable_filenames</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">filename</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">all_filenames</span>
<span class="k">if</span> <span class="n">is_lintable</span><span class="p">(</span><span class="n">filename</span><span class="p">)])</span>
<span class="n">unlintable_filenames</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">all_filenames</span><span class="p">)</span> <span class="o">-</span> <span class="n">lintable_filenames</span>
<span class="n">working_linter_names</span><span class="p">,</span> <span class="n">broken_linter_names</span> <span class="o">=</span> <span class="n">get_linter_status</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
<span class="n">cant_lint_filter</span> <span class="o">=</span> <span class="n">MatchFilter</span><span class="p">(</span><span class="n">build_config_subset</span><span class="p">(</span>
<span class="n">broken_linter_names</span><span class="p">))</span>
<span class="n">cant_lint_filenames</span> <span class="o">=</span> <span class="p">[</span><span class="n">filename</span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">lintable_filenames</span>
<span class="k">if</span> <span class="n">cant_lint_filter</span><span class="p">(</span><span class="n">filename</span><span class="p">)]</span>
<span class="n">runner</span> <span class="o">=</span> <span class="n">WorkspaceRunner</span>
<span class="k">if</span> <span class="s1">&#39;staging&#39;</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
<span class="n">runner</span> <span class="o">=</span> <span class="n">StagingRunner</span>
<span class="n">linters</span> <span class="o">=</span> <span class="n">Linters</span><span class="p">(</span><span class="n">build_config_subset</span><span class="p">(</span><span class="n">working_linter_names</span><span class="p">),</span>
<span class="nb">sorted</span><span class="p">(</span><span class="n">lintable_filenames</span><span class="p">))</span>
<span class="k">if</span> <span class="s1">&#39;dryrun&#39;</span> <span class="ow">in</span> <span class="n">options</span><span class="p">:</span>
<span class="n">dryrun_results</span> <span class="o">=</span> <span class="n">linters</span><span class="o">.</span><span class="n">dryrun</span><span class="p">()</span>
<span class="k">return</span> <span class="p">(</span><span class="n">dryrun_results</span><span class="p">,</span> <span class="n">unlintable_filenames</span><span class="p">,</span> <span class="n">cant_lint_filenames</span><span class="p">,</span>
<span class="n">broken_linter_names</span><span class="p">,</span> <span class="n">unfindable_filenames</span><span class="p">)</span>
<span class="k">with</span> <span class="n">runner</span><span class="p">(</span><span class="n">lintable_filenames</span><span class="p">):</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">linters</span><span class="p">()</span>
<span class="k">return</span> <span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">unlintable_filenames</span><span class="p">,</span> <span class="n">cant_lint_filenames</span><span class="p">,</span>
<span class="n">broken_linter_names</span><span class="p">,</span> <span class="n">unfindable_filenames</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../../index.html">Documentation overview</a><ul>
<li><a href="../index.html">Module code</a><ul>
</ul></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2016, Kenneth M. "Elf" Sternberg.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.6</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.9</a>
</div>
</body>
</html>