552 lines
75 KiB
HTML
552 lines
75 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 — Git Lint 0.0.4 documentation</title>
|
|
|
|
<link rel="stylesheet" href="../../_static/classic.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.4',
|
|
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.4 documentation" href="../../index.html" />
|
|
<link rel="up" title="Module code" href="../index.html" />
|
|
</head>
|
|
<body role="document">
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../index.html">Git Lint 0.0.4 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<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"># | (__/ _ \ ' \| _| / _` | | / -_) _` / _` / -_) '_|</span>
|
|
<span class="c1"># \___\___/_||_|_| |_\__, | |_|_\___\__,_\__,_\___|_|</span>
|
|
<span class="c1"># |___/</span>
|
|
|
|
|
|
<div class="viewcode-block" id="find_config_file"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.find_config_file">[docs]</a><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">""" Returns the configuration file from a prioritized list of locations.</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's root directory, as the file .git-lint</span>
|
|
<span class="sd"> 3. The repository's root directory, as the file .git-lint/config</span>
|
|
<span class="sd"> 4. The user's home directory, as file .git-lint</span>
|
|
<span class="sd"> 5. The user's home directory, as the file .git-lint/config</span>
|
|
|
|
<span class="sd"> If no configuration file is found, this is an error.</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="k">if</span> <span class="s1">'config'</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">'config'</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">'Configuration file not found: </span><span class="si">{}</span><span class="se">\n</span><span class="s1">'</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">'HOME'</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">'.git-lint'</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">'.git-lint/config'</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">'.git-lint'</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">'.git-lint/config'</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">'No configuration file found, tried: </span><span class="si">{}</span><span class="s1">'</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">':'</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></div>
|
|
|
|
|
|
<span class="c1"># (commandLineDictionary, repositoryLocation) -> (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">"""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 'repodir'</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"> """</span>
|
|
|
|
<span class="n">Linter</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">'Linter'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'linter'</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">'DEFAULT'</span><span class="p">,</span> <span class="s1">'repodir'</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">'git'</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">'git'</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">'rev-parse'</span><span class="p">,</span> <span class="s1">'--show-toplevel'</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">'4b825dc642cb6eb9a060e54bf8d69288fbee4904'</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">'rev-parse'</span><span class="p">,</span> <span class="s1">'--verify HEAD'</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">'HEAD'</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"># | |_| | _| | | | _| / -_|_-<</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">'match'</span><span class="p">,</span> <span class="s1">''</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">','</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'^\.'</span><span class="p">,</span> <span class="s1">''</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'\.'</span> <span class="o">+</span> <span class="s1">'|'</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'$'</span><span class="p">)</span></div></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="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">'Syntax error in command configuration for </span><span class="si">{}</span><span class="s1"> '</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">' '</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">'Syntax error in command configuration for </span><span class="si">{}</span><span class="s1"> '</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">'/'</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'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">'PATH'</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">':'</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_working_linter_names"><a class="viewcode-back" href="../../git_lint.html#git_lint.git_lint.get_working_linter_names">[docs]</a><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">'command'</span><span class="p">],</span> <span class="n">i</span><span class="o">.</span><span class="n">name</span><span class="p">)]</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="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"># | (_ / -_) _| | | (_-< _| / _ \ _| | _| | / -_|_-<</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">""" Returns the list of files against which we'll run the linters. """</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">""" Return the full path for all files """</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">""" Return the full path for only those files in the cwd and down """</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">''</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">""" Scan list of porcelain files for merge conflic state. """</span>
|
|
<span class="n">MERGE_CONFLICT_PAIRS</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="s1">'DD'</span><span class="p">,</span> <span class="s1">'DU'</span><span class="p">,</span> <span class="s1">'AU'</span><span class="p">,</span> <span class="s1">'AA'</span><span class="p">,</span> <span class="s1">'UD'</span><span class="p">,</span> <span class="s1">'UA'</span><span class="p">,</span> <span class="s1">'UU'</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">''</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">&</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">'Current repository contains merge conflicts. Linters will not be run.'</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">""" Remove all submodules from the list of files git-lint cares about. """</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">'^(</span><span class="se">\\</span><span class="s1">.</span><span class="se">\\</span><span class="s1">.</span><span class="se">\\</span><span class="s1">/)+'</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">'submodule'</span><span class="p">,</span> <span class="s1">'status'</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">''</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">' '</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">""" Return the status of all files in the system. """</span>
|
|
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'status'</span><span class="p">,</span> <span class="s1">'-z'</span><span class="p">,</span> <span class="s1">'--porcelain'</span><span class="p">,</span>
|
|
<span class="s1">'--untracked-files=all'</span><span class="p">,</span> <span class="s1">'--ignore-submodules=all'</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'</span><span class="se">\x00</span><span class="s1">'</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">></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">"""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's an 'R' 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's a</span>
|
|
<span class="sd"> special case as we accumulate the list of files.</span>
|
|
<span class="sd"> """</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">'R'</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">""" Return the list of files added or modified to the stage """</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">'A'</span><span class="p">,</span> <span class="s1">'M'</span><span class="p">]]</span>
|
|
|
|
<span class="k">def</span> <span class="nf">working_list</span><span class="p">():</span>
|
|
<span class="sd">""" Return the list of files that have been modified in the workspace.</span>
|
|
|
|
<span class="sd"> Includes the '?' to include files that git is not currently tracking.</span>
|
|
<span class="sd"> """</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">'A'</span><span class="p">,</span> <span class="s1">'M'</span><span class="p">,</span> <span class="s1">'?'</span><span class="p">]]</span>
|
|
|
|
<span class="k">def</span> <span class="nf">all_list</span><span class="p">():</span>
|
|
<span class="sd">""" Return all the files git is currently tracking for this repository. """</span>
|
|
<span class="n">cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'ls-tree'</span><span class="p">,</span> <span class="s1">'--name-only'</span><span class="p">,</span> <span class="s1">'--full-tree'</span><span class="p">,</span> <span class="s1">'-r'</span><span class="p">,</span> <span class="s1">'-z'</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'</span><span class="se">\x00</span><span class="s1">'</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">></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">'base'</span> <span class="ow">in</span> <span class="n">options</span> <span class="ow">or</span> <span class="s1">'every'</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">'all'</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">'staging'</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"># \__ \ _/ _` / _` | | ' \/ _` | \ V V / '_/ _` | '_ \ '_ \/ -_) '_|</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">'stash'</span><span class="p">,</span> <span class="s1">'--keep-index'</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">'reset'</span><span class="p">,</span> <span class="s1">'--hard'</span><span class="p">])</span>
|
|
<span class="n">run_git_command</span><span class="p">([</span><span class="s1">'stash'</span><span class="p">,</span> <span class="s1">'pop'</span><span class="p">,</span> <span class="s1">'--quiet'</span><span class="p">,</span> <span class="s1">'--index'</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"># | / || | ' \ | | | ' \ _| | '_ \/ _` (_-<_-<</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">'</span><span class="si">{}{}</span><span class="s1">'</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">"""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"> """</span>
|
|
|
|
<span class="n">cmd</span> <span class="o">=</span> <span class="n">linter</span><span class="p">[</span><span class="s1">'command'</span><span class="p">]</span> <span class="o">+</span> <span class="s1">' "'</span> <span class="o">+</span> <span class="n">filename</span> <span class="o">+</span> <span class="s1">'"'</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">'condition'</span><span class="p">,</span> <span class="s1">'error'</span><span class="p">)</span> <span class="o">==</span> <span class="s1">'output'</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">'/'</span><span class="p">,</span> <span class="s1">''</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">'print'</span><span class="p">,</span> <span class="s1">'false'</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">'true'</span><span class="p">)</span> <span class="ow">and</span> <span class="s1">' '</span><span class="p">)</span> <span class="ow">or</span>
|
|
<span class="s1">' </span><span class="si">{}</span><span class="s1">: '</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">""" 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"> """</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">""" Returns a function to run a set of linters against a set of filenames</span>
|
|
|
|
<span class="sd"> This returns a function because it'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"> """</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">'/'</span><span class="p">,</span> <span class="s1">''</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">' </span><span class="si">{}</span><span class="s1">'</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">""" Returns a subset of the configuration, with only those linters mentioned in keys """</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">""" Runs the requested linters """</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">'staging'</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">'dryrun'</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 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="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../index.html">Git Lint 0.0.4 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2016, Kenneth M. "Elf" Sternberg.
|
|
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
|
|
</div>
|
|
</body>
|
|
</html> |