lorax/lorax-composer/_modules/pylorax/api/yumbase.html

211 lines
20 KiB
HTML
Raw Normal View History

<!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>pylorax.api.yumbase &mdash; Lorax 19.7.15 documentation</title>
<link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: '19.7.15',
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="Lorax 19.7.15 documentation" href="../../../index.html" />
<link rel="up" title="pylorax.api" href="../api.html" />
</head>
<body>
<div class="related">
<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><a href="../../../index.html">Lorax 19.7.15 documentation</a> &raquo;</li>
<li><a href="../../index.html" >Module code</a> &raquo;</li>
<li><a href="../../pylorax.html" >pylorax</a> &raquo;</li>
<li><a href="../api.html" accesskey="U">pylorax.api</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<h1>Source code for pylorax.api.yumbase</h1><div class="highlight"><pre>
<span class="c">#</span>
<span class="c"># Copyright (C) 2017 Red Hat, Inc.</span>
<span class="c">#</span>
<span class="c"># This program is free software; you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation; either version 2 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># This program is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c">#</span>
<span class="c"># pylint: disable=bad-preconf-access</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&quot;lorax-composer&quot;</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">ConfigParser</span>
<span class="kn">from</span> <span class="nn">fnmatch</span> <span class="kn">import</span> <span class="n">fnmatchcase</span>
<span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">yum</span>
<span class="c"># This is a hack to short circuit yum&#39;s internal logging</span>
<span class="n">yum</span><span class="o">.</span><span class="n">logginglevels</span><span class="o">.</span><span class="n">_added_handlers</span> <span class="o">=</span> <span class="bp">True</span>
<span class="kn">from</span> <span class="nn">pylorax.sysutils</span> <span class="kn">import</span> <span class="n">joinpaths</span>
<div class="viewcode-block" id="get_base_object"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.yumbase.get_base_object">[docs]</a><span class="k">def</span> <span class="nf">get_base_object</span><span class="p">(</span><span class="n">conf</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Get the Yum object with settings from the config file</span>
<span class="sd"> :param conf: configuration object</span>
<span class="sd"> :type conf: ComposerParser</span>
<span class="sd"> :returns: A Yum base object</span>
<span class="sd"> :rtype: YumBase</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">cachedir</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">conf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;cache_dir&quot;</span><span class="p">))</span>
<span class="n">yumconf</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">conf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;yum_conf&quot;</span><span class="p">))</span>
<span class="n">yumroot</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">conf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;yum_root&quot;</span><span class="p">))</span>
<span class="n">repodir</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">conf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;repo_dir&quot;</span><span class="p">))</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">ConfigParser</span><span class="p">()</span>
<span class="c"># add the main section</span>
<span class="n">section</span> <span class="o">=</span> <span class="s">&quot;main&quot;</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;cachedir&quot;</span><span class="p">:</span> <span class="n">cachedir</span><span class="p">,</span>
<span class="s">&quot;keepcache&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;gpgcheck&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;plugins&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s">&quot;assumeyes&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="s">&quot;reposdir&quot;</span><span class="p">:</span> <span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="s">&quot;tsflags&quot;</span><span class="p">:</span> <span class="s">&quot;nodocs&quot;</span><span class="p">}</span>
<span class="k">if</span> <span class="n">conf</span><span class="o">.</span><span class="n">get_default</span><span class="p">(</span><span class="s">&quot;yum&quot;</span><span class="p">,</span> <span class="s">&quot;proxy&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">):</span>
<span class="n">data</span><span class="p">[</span><span class="s">&quot;proxy&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">conf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;yum&quot;</span><span class="p">,</span> <span class="s">&quot;proxy&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">conf</span><span class="o">.</span><span class="n">has_option</span><span class="p">(</span><span class="s">&quot;yum&quot;</span><span class="p">,</span> <span class="s">&quot;sslverify&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">conf</span><span class="o">.</span><span class="n">getboolean</span><span class="p">(</span><span class="s">&quot;yum&quot;</span><span class="p">,</span> <span class="s">&quot;sslverify&quot;</span><span class="p">):</span>
<span class="n">data</span><span class="p">[</span><span class="s">&quot;sslverify&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;0&quot;</span>
<span class="n">c</span><span class="o">.</span><span class="n">add_section</span><span class="p">(</span><span class="n">section</span><span class="p">)</span>
<span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="n">c</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">),</span> <span class="n">data</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="c"># write the yum configuration file</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">yumconf</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">c</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="c"># create the yum base object</span>
<span class="n">yb</span> <span class="o">=</span> <span class="n">yum</span><span class="o">.</span><span class="n">YumBase</span><span class="p">()</span>
<span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">fn</span> <span class="o">=</span> <span class="n">yumconf</span>
<span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">root</span> <span class="o">=</span> <span class="n">yumroot</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">isdir</span><span class="p">(</span><span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">root</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">root</span><span class="p">)</span>
<span class="n">_releasever</span> <span class="o">=</span> <span class="n">conf</span><span class="o">.</span><span class="n">get_default</span><span class="p">(</span><span class="s">&quot;composer&quot;</span><span class="p">,</span> <span class="s">&quot;releasever&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">_releasever</span><span class="p">:</span>
<span class="n">distroverpkg</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;system-release(releasever)&#39;</span><span class="p">,</span> <span class="s">&#39;redhat-release&#39;</span><span class="p">]</span>
<span class="c"># Use yum private function to guess the releasever</span>
<span class="n">_releasever</span> <span class="o">=</span> <span class="n">yum</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">_getsysver</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">,</span> <span class="n">distroverpkg</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;releasever = </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">_releasever</span><span class="p">)</span>
<span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">releasever</span> <span class="o">=</span> <span class="n">_releasever</span>
<span class="c"># Turn on as much yum logging as we can</span>
<span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">debuglevel</span> <span class="o">=</span> <span class="mi">6</span>
<span class="n">yb</span><span class="o">.</span><span class="n">preconf</span><span class="o">.</span><span class="n">errorlevel</span> <span class="o">=</span> <span class="mi">6</span>
<span class="n">yb</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="n">yb</span><span class="o">.</span><span class="n">verbose_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="c"># Gather up all the available repo files, add the ones matching &quot;repos&quot;:&quot;enabled&quot; patterns</span>
<span class="n">enabled_repos</span> <span class="o">=</span> <span class="n">conf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;repos&quot;</span><span class="p">,</span> <span class="s">&quot;enabled&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">)</span>
<span class="n">repo_files</span> <span class="o">=</span> <span class="n">glob</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">repodir</span><span class="p">,</span> <span class="s">&quot;*.repo&quot;</span><span class="p">))</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">conf</span><span class="o">.</span><span class="n">has_option</span><span class="p">(</span><span class="s">&quot;repos&quot;</span><span class="p">,</span> <span class="s">&quot;use_system_repos&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">conf</span><span class="o">.</span><span class="n">getboolean</span><span class="p">(</span><span class="s">&quot;repos&quot;</span><span class="p">,</span> <span class="s">&quot;use_system_repos&quot;</span><span class="p">):</span>
<span class="n">repo_files</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">glob</span><span class="p">(</span><span class="s">&quot;/etc/yum.repos.d/*.repo&quot;</span><span class="p">))</span>
<span class="k">for</span> <span class="n">repo_file</span> <span class="ow">in</span> <span class="n">repo_files</span><span class="p">:</span>
<span class="n">name</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">basename</span><span class="p">(</span><span class="n">repo_file</span><span class="p">)[:</span><span class="o">-</span><span class="mi">5</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">pattern</span><span class="p">:</span> <span class="n">fnmatchcase</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">pattern</span><span class="p">),</span> <span class="n">enabled_repos</span><span class="p">)):</span> <span class="c"># pylint: disable=cell-var-from-loop</span>
<span class="n">yb</span><span class="o">.</span><span class="n">getReposFromConfigFile</span><span class="p">(</span><span class="n">repo_file</span><span class="p">)</span>
<span class="c"># Update the metadata from the enabled repos to speed up later operations</span>
<span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Updating yum repository metadata&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">yb</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">sort</span><span class="p">():</span>
<span class="n">r</span><span class="o">.</span><span class="n">metadata_expire</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">r</span><span class="o">.</span><span class="n">mdpolicy</span> <span class="o">=</span> <span class="s">&quot;group:all&quot;</span>
<span class="n">yb</span><span class="o">.</span><span class="n">doRepoSetup</span><span class="p">()</span>
<span class="n">yb</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">doSetup</span><span class="p">()</span>
<span class="n">yb</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">populateSack</span><span class="p">(</span><span class="n">mdtype</span><span class="o">=</span><span class="s">&#39;all&#39;</span><span class="p">,</span> <span class="n">cacheonly</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="n">yb</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<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><a href="../../../index.html">Lorax 19.7.15 documentation</a> &raquo;</li>
<li><a href="../../index.html" >Module code</a> &raquo;</li>
<li><a href="../../pylorax.html" >pylorax</a> &raquo;</li>
<li><a href="../api.html" >pylorax.api</a> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2018, Red Hat, Inc..
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>