lorax/docs/html/_modules/pylorax/api/projects.html

897 lines
86 KiB
HTML
Raw Normal View History

2018-05-12 00:18:21 +00:00
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2021-03-04 00:36:38 +00:00
<title>pylorax.api.projects &mdash; Lorax 35.0 documentation</title>
2018-05-12 00:18:21 +00:00
2019-03-27 23:44:14 +00:00
<script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
2018-05-12 00:18:21 +00:00
2019-03-27 23:44:14 +00:00
<script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
2020-09-08 18:59:15 +00:00
<script src="../../../_static/jquery.js"></script>
<script src="../../../_static/underscore.js"></script>
<script src="../../../_static/doctools.js"></script>
<script src="../../../_static/language_data.js"></script>
2019-03-27 23:44:14 +00:00
<script type="text/javascript" src="../../../_static/js/theme.js"></script>
2018-05-12 00:18:21 +00:00
2019-03-27 23:44:14 +00:00
2018-05-12 00:18:21 +00:00
2019-03-27 23:44:14 +00:00
2018-11-13 17:53:31 +00:00
<link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
2018-05-12 00:18:21 +00:00
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
2019-03-27 23:44:14 +00:00
<div class="wy-side-nav-search" >
2018-05-12 00:18:21 +00:00
<a href="../../../index.html" class="icon icon-home"> Lorax
</a>
<div class="version">
2021-03-04 00:36:38 +00:00
35.0
2018-05-12 00:18:21 +00:00
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../intro.html">Introduction to Lorax</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../intro.html#before-lorax">Before Lorax</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../lorax.html">Lorax</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../livemedia-creator.html">livemedia-creator</a></li>
2018-10-24 17:07:32 +00:00
<li class="toctree-l1"><a class="reference internal" href="../../../composer-cli.html">composer-cli</a></li>
2019-11-05 22:36:46 +00:00
<li class="toctree-l1"><a class="reference internal" href="../../../mkksiso.html">mkksiso</a></li>
2018-05-12 00:18:21 +00:00
<li class="toctree-l1"><a class="reference internal" href="../../../product-images.html">Product and Updates Images</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../modules.html">src</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../../index.html">Lorax</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../../index.html">Docs</a> &raquo;</li>
<li><a href="../../index.html">Module code</a> &raquo;</li>
<li><a href="../../pylorax.html">pylorax</a> &raquo;</li>
<li>pylorax.api.projects</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for pylorax.api.projects</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Copyright (C) 2017 Red Hat, Inc.</span>
<span class="c1">#</span>
<span class="c1"># This program is free software; you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU General Public License as published by</span>
<span class="c1"># the Free Software Foundation; either version 2 of the License, or</span>
<span class="c1"># (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># This program is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span class="c1"># GNU General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU General Public License</span>
<span class="c1"># along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c1">#</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="s2">&quot;lorax-composer&quot;</span><span class="p">)</span>
2020-09-08 18:59:15 +00:00
<span class="kn">from</span> <span class="nn">configparser</span> <span class="kn">import</span> <span class="n">ConfigParser</span>
2018-05-12 00:18:21 +00:00
<span class="kn">import</span> <span class="nn">dnf</span>
2020-09-08 18:59:15 +00:00
<span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
2019-03-27 23:44:14 +00:00
<span class="kn">import</span> <span class="nn">os</span>
2018-05-12 00:18:21 +00:00
<span class="kn">import</span> <span class="nn">time</span>
2020-09-08 18:59:15 +00:00
<span class="kn">from</span> <span class="nn">pylorax.api.bisect</span> <span class="kn">import</span> <span class="n">insort_left</span>
<span class="kn">from</span> <span class="nn">pylorax.sysutils</span> <span class="kn">import</span> <span class="n">joinpaths</span>
2019-03-27 23:44:14 +00:00
2018-05-12 00:18:21 +00:00
<span class="n">TIME_FORMAT</span> <span class="o">=</span> <span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2">T%H:%M:%S&quot;</span>
<div class="viewcode-block" id="ProjectsError"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.ProjectsError">[docs]</a><span class="k">class</span> <span class="nc">ProjectsError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="k">pass</span></div>
<div class="viewcode-block" id="api_time"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.api_time">[docs]</a><span class="k">def</span> <span class="nf">api_time</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Convert time since epoch to a string</span>
<span class="sd"> :param t: Seconds since epoch</span>
<span class="sd"> :type t: int</span>
<span class="sd"> :returns: Time string</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="n">TIME_FORMAT</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">t</span><span class="p">))</span></div>
<div class="viewcode-block" id="api_changelog"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.api_changelog">[docs]</a><span class="k">def</span> <span class="nf">api_changelog</span><span class="p">(</span><span class="n">changelog</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Convert the changelog to a string</span>
<span class="sd"> :param changelog: A list of time, author, string tuples.</span>
<span class="sd"> :type changelog: tuple</span>
<span class="sd"> :returns: The most recent changelog text or &quot;&quot;</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> This returns only the most recent changelog entry.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">entry</span> <span class="o">=</span> <span class="n">changelog</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span>
<span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
<span class="n">entry</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
<span class="k">return</span> <span class="n">entry</span></div>
<div class="viewcode-block" id="pkg_to_project"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.pkg_to_project">[docs]</a><span class="k">def</span> <span class="nf">pkg_to_project</span><span class="p">(</span><span class="n">pkg</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Extract the details from a hawkey.Package object</span>
<span class="sd"> :param pkgs: hawkey.Package object with package details</span>
<span class="sd"> :type pkgs: hawkey.Package</span>
<span class="sd"> :returns: A dict with the name, summary, description, and url.</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> upstream_vcs is hard-coded to UPSTREAM_VCS</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;summary&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">summary</span><span class="p">,</span>
<span class="s2">&quot;description&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">description</span><span class="p">,</span>
<span class="s2">&quot;homepage&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">url</span><span class="p">,</span>
<span class="s2">&quot;upstream_vcs&quot;</span><span class="p">:</span> <span class="s2">&quot;UPSTREAM_VCS&quot;</span><span class="p">}</span></div>
2019-03-27 23:44:14 +00:00
<div class="viewcode-block" id="pkg_to_build"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.pkg_to_build">[docs]</a><span class="k">def</span> <span class="nf">pkg_to_build</span><span class="p">(</span><span class="n">pkg</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Extract the build details from a hawkey.Package object</span>
<span class="sd"> :param pkg: hawkey.Package object with package details</span>
<span class="sd"> :type pkg: hawkey.Package</span>
<span class="sd"> :returns: A dict with the build details, epoch, release, arch, build_time, changelog, ...</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> metadata entries are hard-coded to {}</span>
<span class="sd"> Note that this only returns the build dict, it does not include the name, description, etc.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">{</span><span class="s2">&quot;epoch&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span>
<span class="s2">&quot;release&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">release</span><span class="p">,</span>
<span class="s2">&quot;arch&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">arch</span><span class="p">,</span>
<span class="s2">&quot;build_time&quot;</span><span class="p">:</span> <span class="n">api_time</span><span class="p">(</span><span class="n">pkg</span><span class="o">.</span><span class="n">buildtime</span><span class="p">),</span>
<span class="s2">&quot;changelog&quot;</span><span class="p">:</span> <span class="s2">&quot;CHANGELOG_NEEDED&quot;</span><span class="p">,</span> <span class="c1"># XXX Not in hawkey.Package</span>
<span class="s2">&quot;build_config_ref&quot;</span><span class="p">:</span> <span class="s2">&quot;BUILD_CONFIG_REF&quot;</span><span class="p">,</span>
<span class="s2">&quot;build_env_ref&quot;</span><span class="p">:</span> <span class="s2">&quot;BUILD_ENV_REF&quot;</span><span class="p">,</span>
<span class="s2">&quot;metadata&quot;</span><span class="p">:</span> <span class="p">{},</span>
<span class="s2">&quot;source&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;license&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">license</span><span class="p">,</span>
<span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">version</span><span class="p">,</span>
<span class="s2">&quot;source_ref&quot;</span><span class="p">:</span> <span class="s2">&quot;SOURCE_REF&quot;</span><span class="p">,</span>
<span class="s2">&quot;metadata&quot;</span><span class="p">:</span> <span class="p">{}}}</span></div>
2018-05-12 00:18:21 +00:00
<div class="viewcode-block" id="pkg_to_project_info"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.pkg_to_project_info">[docs]</a><span class="k">def</span> <span class="nf">pkg_to_project_info</span><span class="p">(</span><span class="n">pkg</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Extract the details from a hawkey.Package object</span>
<span class="sd"> :param pkg: hawkey.Package object with package details</span>
<span class="sd"> :type pkg: hawkey.Package</span>
<span class="sd"> :returns: A dict with the project details, as well as epoch, release, arch, build_time, changelog, ...</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> metadata entries are hard-coded to {}</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;summary&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">summary</span><span class="p">,</span>
<span class="s2">&quot;description&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">description</span><span class="p">,</span>
<span class="s2">&quot;homepage&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">url</span><span class="p">,</span>
<span class="s2">&quot;upstream_vcs&quot;</span><span class="p">:</span> <span class="s2">&quot;UPSTREAM_VCS&quot;</span><span class="p">,</span>
2019-03-27 23:44:14 +00:00
<span class="s2">&quot;builds&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">pkg_to_build</span><span class="p">(</span><span class="n">pkg</span><span class="p">)]}</span></div>
2018-05-12 00:18:21 +00:00
<div class="viewcode-block" id="pkg_to_dep"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.pkg_to_dep">[docs]</a><span class="k">def</span> <span class="nf">pkg_to_dep</span><span class="p">(</span><span class="n">pkg</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Extract the info from a hawkey.Package object</span>
<span class="sd"> :param pkg: A hawkey.Package object</span>
<span class="sd"> :type pkg: hawkey.Package</span>
<span class="sd"> :returns: A dict with name, epoch, version, release, arch</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;epoch&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">epoch</span><span class="p">,</span>
<span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">version</span><span class="p">,</span>
<span class="s2">&quot;release&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">release</span><span class="p">,</span>
<span class="s2">&quot;arch&quot;</span><span class="p">:</span> <span class="n">pkg</span><span class="o">.</span><span class="n">arch</span><span class="p">}</span></div>
<div class="viewcode-block" id="proj_to_module"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.proj_to_module">[docs]</a><span class="k">def</span> <span class="nf">proj_to_module</span><span class="p">(</span><span class="n">proj</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Extract the name from a project_info dict</span>
<span class="sd"> :param pkg: dict with package details</span>
<span class="sd"> :type pkg: dict</span>
<span class="sd"> :returns: A dict with name, and group_type</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> group_type is hard-coded to &quot;rpm&quot;</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">proj</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">],</span>
<span class="s2">&quot;group_type&quot;</span><span class="p">:</span> <span class="s2">&quot;rpm&quot;</span><span class="p">}</span></div>
<div class="viewcode-block" id="dep_evra"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.dep_evra">[docs]</a><span class="k">def</span> <span class="nf">dep_evra</span><span class="p">(</span><span class="n">dep</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the epoch:version-release.arch for the dep</span>
<span class="sd"> :param dep: dependency dict</span>
<span class="sd"> :type dep: dict</span>
<span class="sd"> :returns: epoch:version-release.arch</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">dep</span><span class="p">[</span><span class="s2">&quot;epoch&quot;</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">dep</span><span class="p">[</span><span class="s2">&quot;version&quot;</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;-&quot;</span><span class="o">+</span><span class="n">dep</span><span class="p">[</span><span class="s2">&quot;release&quot;</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;.&quot;</span><span class="o">+</span><span class="n">dep</span><span class="p">[</span><span class="s2">&quot;arch&quot;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">dep</span><span class="p">[</span><span class="s2">&quot;epoch&quot;</span><span class="p">])</span><span class="o">+</span><span class="s2">&quot;:&quot;</span><span class="o">+</span><span class="n">dep</span><span class="p">[</span><span class="s2">&quot;version&quot;</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;-&quot;</span><span class="o">+</span><span class="n">dep</span><span class="p">[</span><span class="s2">&quot;release&quot;</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;.&quot;</span><span class="o">+</span><span class="n">dep</span><span class="p">[</span><span class="s2">&quot;arch&quot;</span><span class="p">]</span></div>
<div class="viewcode-block" id="dep_nevra"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.dep_nevra">[docs]</a><span class="k">def</span> <span class="nf">dep_nevra</span><span class="p">(</span><span class="n">dep</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return the name-epoch:version-release.arch&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">dep</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;-&quot;</span><span class="o">+</span><span class="n">dep_evra</span><span class="p">(</span><span class="n">dep</span><span class="p">)</span></div>
<div class="viewcode-block" id="projects_list"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.projects_list">[docs]</a><span class="k">def</span> <span class="nf">projects_list</span><span class="p">(</span><span class="n">dbo</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a list of projects</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :returns: List of project info dicts with name, summary, description, homepage, upstream_vcs</span>
<span class="sd"> :rtype: list of dicts</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">projects_info</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span></div>
<div class="viewcode-block" id="projects_info"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.projects_info">[docs]</a><span class="k">def</span> <span class="nf">projects_info</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">project_names</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return details about specific projects</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :param project_names: List of names of projects to get info about</span>
<span class="sd"> :type project_names: str</span>
<span class="sd"> :returns: List of project info dicts with pkg_to_project as well as epoch, version, release, etc.</span>
<span class="sd"> :rtype: list of dicts</span>
<span class="sd"> If project_names is None it will return the full list of available packages</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="n">project_names</span><span class="p">:</span>
<span class="n">pkgs</span> <span class="o">=</span> <span class="n">dbo</span><span class="o">.</span><span class="n">sack</span><span class="o">.</span><span class="n">query</span><span class="p">()</span><span class="o">.</span><span class="n">available</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__glob</span><span class="o">=</span><span class="n">project_names</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">pkgs</span> <span class="o">=</span> <span class="n">dbo</span><span class="o">.</span><span class="n">sack</span><span class="o">.</span><span class="n">query</span><span class="p">()</span><span class="o">.</span><span class="n">available</span><span class="p">()</span>
2019-03-27 23:44:14 +00:00
<span class="c1"># iterate over pkgs</span>
<span class="c1"># - if pkg.name isn&#39;t in the results yet, add pkg_to_project_info in sorted position</span>
<span class="c1"># - if pkg.name is already in results, get its builds. If the build for pkg is different</span>
<span class="c1"># in any way (version, arch, etc.) add it to the entry&#39;s builds list. If it is the same,</span>
<span class="c1"># skip it.</span>
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">results_names</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pkgs</span><span class="p">:</span>
<span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">results_names</span><span class="p">:</span>
<span class="n">idx</span> <span class="o">=</span> <span class="n">insort_left</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">pkg_to_project_info</span><span class="p">(</span><span class="n">p</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="n">results_names</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span> <span class="o">=</span> <span class="n">idx</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">build</span> <span class="o">=</span> <span class="n">pkg_to_build</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="k">if</span> <span class="n">build</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">results</span><span class="p">[</span><span class="n">results_names</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]][</span><span class="s2">&quot;builds&quot;</span><span class="p">]:</span>
<span class="n">results</span><span class="p">[</span><span class="n">results_names</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()]][</span><span class="s2">&quot;builds&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">build</span><span class="p">)</span>
<span class="k">return</span> <span class="n">results</span></div>
2018-05-12 00:18:21 +00:00
2018-07-20 22:51:06 +00:00
<span class="k">def</span> <span class="nf">_depsolve</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">projects</span><span class="p">,</span> <span class="n">groups</span><span class="p">):</span>
2018-06-04 23:27:56 +00:00
<span class="sd">&quot;&quot;&quot;Add projects to a new transaction</span>
2018-05-12 00:18:21 +00:00
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :param projects: The projects and version globs to find the dependencies for</span>
<span class="sd"> :type projects: List of tuples</span>
2018-07-20 22:51:06 +00:00
<span class="sd"> :param groups: The groups to include in dependency solving</span>
<span class="sd"> :type groups: List of str</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :returns: None</span>
<span class="sd"> :rtype: None</span>
<span class="sd"> :raises: ProjectsError if there was a problem installing something</span>
2018-05-12 00:18:21 +00:00
<span class="sd"> &quot;&quot;&quot;</span>
2018-10-04 23:55:20 +00:00
<span class="c1"># This resets the transaction and updates the cache.</span>
<span class="c1"># It is important that the cache always be synchronized because Anaconda will grab its own copy</span>
<span class="c1"># and if that is different the NEVRAs will not match and the build will fail.</span>
2018-05-12 00:18:21 +00:00
<span class="n">dbo</span><span class="o">.</span><span class="n">reset</span><span class="p">(</span><span class="n">goal</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
2018-07-20 22:51:06 +00:00
<span class="n">install_errors</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">groups</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">group_install</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;mandatory&quot;</span><span class="p">,</span> <span class="s2">&quot;default&quot;</span><span class="p">])</span>
<span class="k">except</span> <span class="n">dnf</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">MarkingError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">install_errors</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s2">&quot;Group </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
2018-06-04 23:27:56 +00:00
<span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">version</span> <span class="ow">in</span> <span class="n">projects</span><span class="p">:</span>
2018-10-24 17:07:32 +00:00
<span class="c1"># Find the best package matching the name + version glob</span>
<span class="c1"># dnf can return multiple packages if it is in more than 1 repository</span>
<span class="n">query</span> <span class="o">=</span> <span class="n">dbo</span><span class="o">.</span><span class="n">sack</span><span class="o">.</span><span class="n">query</span><span class="p">()</span><span class="o">.</span><span class="n">filterm</span><span class="p">(</span><span class="n">provides__glob</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
<span class="k">if</span> <span class="n">version</span><span class="p">:</span>
<span class="n">query</span><span class="o">.</span><span class="n">filterm</span><span class="p">(</span><span class="n">version__glob</span><span class="o">=</span><span class="n">version</span><span class="p">)</span>
<span class="n">query</span><span class="o">.</span><span class="n">filterm</span><span class="p">(</span><span class="n">latest</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">query</span><span class="p">:</span>
<span class="n">install_errors</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">-</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">version</span><span class="p">),</span> <span class="s2">&quot;No match&quot;</span><span class="p">))</span>
<span class="k">continue</span>
<span class="n">sltr</span> <span class="o">=</span> <span class="n">dnf</span><span class="o">.</span><span class="n">selector</span><span class="o">.</span><span class="n">Selector</span><span class="p">(</span><span class="n">dbo</span><span class="o">.</span><span class="n">sack</span><span class="p">)</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">pkg</span><span class="o">=</span><span class="n">query</span><span class="p">)</span>
<span class="c1"># NOTE: dnf says in near future there will be a &quot;goal&quot; attribute of Base class</span>
<span class="c1"># so yes, we&#39;re using a &#39;private&#39; attribute here on purpose and with permission.</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">_goal</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="n">select</span><span class="o">=</span><span class="n">sltr</span><span class="p">,</span> <span class="n">optional</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
2018-07-20 22:51:06 +00:00
<span class="k">if</span> <span class="n">install_errors</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">ProjectsError</span><span class="p">(</span><span class="s2">&quot;The following package(s) had problems: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span> <span class="k">for</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">err</span> <span class="ow">in</span> <span class="n">install_errors</span><span class="p">]))</span>
2018-06-04 23:27:56 +00:00
2018-07-20 22:51:06 +00:00
<div class="viewcode-block" id="projects_depsolve"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.projects_depsolve">[docs]</a><span class="k">def</span> <span class="nf">projects_depsolve</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">projects</span><span class="p">,</span> <span class="n">groups</span><span class="p">):</span>
2018-06-04 23:27:56 +00:00
<span class="sd">&quot;&quot;&quot;Return the dependencies for a list of projects</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :param projects: The projects to find the dependencies for</span>
<span class="sd"> :type projects: List of Strings</span>
2018-07-20 22:51:06 +00:00
<span class="sd"> :param groups: The groups to include in dependency solving</span>
<span class="sd"> :type groups: List of str</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :returns: NEVRA&#39;s of the project and its dependencies</span>
<span class="sd"> :rtype: list of dicts</span>
<span class="sd"> :raises: ProjectsError if there was a problem installing something</span>
<span class="sd"> &quot;&quot;&quot;</span>
2018-07-20 22:51:06 +00:00
<span class="n">_depsolve</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">projects</span><span class="p">,</span> <span class="n">groups</span><span class="p">)</span>
2018-05-12 00:18:21 +00:00
<span class="k">try</span><span class="p">:</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span>
<span class="k">except</span> <span class="n">dnf</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">DepsolveError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
2018-06-04 23:27:56 +00:00
<span class="k">raise</span> <span class="n">ProjectsError</span><span class="p">(</span><span class="s2">&quot;There was a problem depsolving </span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">projects</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
2018-05-12 00:18:21 +00:00
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">dbo</span><span class="o">.</span><span class="n">transaction</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="p">[]</span>
<span class="k">return</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">pkg_to_dep</span><span class="p">,</span> <span class="n">dbo</span><span class="o">.</span><span class="n">transaction</span><span class="o">.</span><span class="n">install_set</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span></div>
<div class="viewcode-block" id="estimate_size"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.estimate_size">[docs]</a><span class="k">def</span> <span class="nf">estimate_size</span><span class="p">(</span><span class="n">packages</span><span class="p">,</span> <span class="n">block_size</span><span class="o">=</span><span class="mi">6144</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Estimate the installed size of a package list</span>
<span class="sd"> :param packages: The packages to be installed</span>
<span class="sd"> :type packages: list of hawkey.Package objects</span>
<span class="sd"> :param block_size: The block size to use for rounding up file sizes.</span>
<span class="sd"> :type block_size: int</span>
<span class="sd"> :returns: The estimated size of installed packages</span>
<span class="sd"> :rtype: int</span>
<span class="sd"> Estimating actual requirements is difficult without the actual file sizes, which</span>
<span class="sd"> dnf doesn&#39;t provide access to. So use the file count and block size to estimate</span>
<span class="sd"> a minimum size for each package.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">installed_size</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">packages</span><span class="p">:</span>
<span class="n">installed_size</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">files</span><span class="p">)</span> <span class="o">*</span> <span class="n">block_size</span>
<span class="n">installed_size</span> <span class="o">+=</span> <span class="n">p</span><span class="o">.</span><span class="n">installsize</span>
<span class="k">return</span> <span class="n">installed_size</span></div>
2018-07-20 22:51:06 +00:00
<div class="viewcode-block" id="projects_depsolve_with_size"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.projects_depsolve_with_size">[docs]</a><span class="k">def</span> <span class="nf">projects_depsolve_with_size</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">projects</span><span class="p">,</span> <span class="n">groups</span><span class="p">,</span> <span class="n">with_core</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
2018-05-12 00:18:21 +00:00
<span class="sd">&quot;&quot;&quot;Return the dependencies and installed size for a list of projects</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :param project_names: The projects to find the dependencies for</span>
<span class="sd"> :type project_names: List of Strings</span>
2018-07-20 22:51:06 +00:00
<span class="sd"> :param groups: The groups to include in dependency solving</span>
<span class="sd"> :type groups: List of str</span>
2018-05-12 00:18:21 +00:00
<span class="sd"> :returns: installed size and a list of NEVRA&#39;s of the project and its dependencies</span>
<span class="sd"> :rtype: tuple of (int, list of dicts)</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :raises: ProjectsError if there was a problem installing something</span>
2018-05-12 00:18:21 +00:00
<span class="sd"> &quot;&quot;&quot;</span>
2018-07-20 22:51:06 +00:00
<span class="n">_depsolve</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">projects</span><span class="p">,</span> <span class="n">groups</span><span class="p">)</span>
2018-05-12 00:18:21 +00:00
<span class="k">if</span> <span class="n">with_core</span><span class="p">:</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">group_install</span><span class="p">(</span><span class="s2">&quot;core&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;mandatory&#39;</span><span class="p">,</span> <span class="s1">&#39;default&#39;</span><span class="p">,</span> <span class="s1">&#39;optional&#39;</span><span class="p">])</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span>
<span class="k">except</span> <span class="n">dnf</span><span class="o">.</span><span class="n">exceptions</span><span class="o">.</span><span class="n">DepsolveError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
2018-06-04 23:27:56 +00:00
<span class="k">raise</span> <span class="n">ProjectsError</span><span class="p">(</span><span class="s2">&quot;There was a problem depsolving </span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">projects</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
2018-05-12 00:18:21 +00:00
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">dbo</span><span class="o">.</span><span class="n">transaction</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="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="p">[])</span>
<span class="n">installed_size</span> <span class="o">=</span> <span class="n">estimate_size</span><span class="p">(</span><span class="n">dbo</span><span class="o">.</span><span class="n">transaction</span><span class="o">.</span><span class="n">install_set</span><span class="p">)</span>
<span class="n">deps</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">pkg_to_dep</span><span class="p">,</span> <span class="n">dbo</span><span class="o">.</span><span class="n">transaction</span><span class="o">.</span><span class="n">install_set</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="k">return</span> <span class="p">(</span><span class="n">installed_size</span><span class="p">,</span> <span class="n">deps</span><span class="p">)</span></div>
<div class="viewcode-block" id="modules_list"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.modules_list">[docs]</a><span class="k">def</span> <span class="nf">modules_list</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">module_names</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a list of modules</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :param offset: Number of modules to skip</span>
<span class="sd"> :type limit: int</span>
<span class="sd"> :param limit: Maximum number of modules to return</span>
<span class="sd"> :type limit: int</span>
<span class="sd"> :returns: List of module information and total count</span>
<span class="sd"> :rtype: tuple of a list of dicts and an Int</span>
<span class="sd"> Modules don&#39;t exist in RHEL7 so this only returns projects</span>
<span class="sd"> and sets the type to &quot;rpm&quot;</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># TODO - Figure out what to do with this for Fedora &#39;modules&#39;</span>
2019-03-27 23:44:14 +00:00
<span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">proj_to_module</span><span class="p">,</span> <span class="n">projects_info</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">module_names</span><span class="p">)))</span></div>
2018-05-12 00:18:21 +00:00
<div class="viewcode-block" id="modules_info"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.modules_info">[docs]</a><span class="k">def</span> <span class="nf">modules_info</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">module_names</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return details about a module, including dependencies</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :param module_names: Names of the modules to get info about</span>
<span class="sd"> :type module_names: str</span>
<span class="sd"> :returns: List of dicts with module details and dependencies.</span>
<span class="sd"> :rtype: list of dicts</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">modules</span> <span class="o">=</span> <span class="n">projects_info</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">module_names</span><span class="p">)</span>
<span class="c1"># Add the dependency info to each one</span>
<span class="k">for</span> <span class="n">module</span> <span class="ow">in</span> <span class="n">modules</span><span class="p">:</span>
2018-07-20 22:51:06 +00:00
<span class="n">module</span><span class="p">[</span><span class="s2">&quot;dependencies&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">projects_depsolve</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="p">[(</span><span class="n">module</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">],</span> <span class="s2">&quot;*.*&quot;</span><span class="p">)],</span> <span class="p">[])</span>
2018-05-12 00:18:21 +00:00
<span class="k">return</span> <span class="n">modules</span></div>
2018-06-04 23:27:56 +00:00
<div class="viewcode-block" id="dnf_repo_to_file_repo"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.dnf_repo_to_file_repo">[docs]</a><span class="k">def</span> <span class="nf">dnf_repo_to_file_repo</span><span class="p">(</span><span class="n">repo</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a string representation of a DNF Repo object suitable for writing to a .repo file</span>
<span class="sd"> :param repo: DNF Repository</span>
<span class="sd"> :type repo: dnf.RepoDict</span>
<span class="sd"> :returns: A string</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> The DNF Repo.dump() function does not produce a string that can be used as a dnf .repo file,</span>
<span class="sd"> it ouputs baseurl and gpgkey as python lists which DNF cannot read. So do this manually with</span>
<span class="sd"> only the attributes we care about.</span>
<span class="sd"> &quot;&quot;&quot;</span>
2018-10-24 17:07:32 +00:00
<span class="n">repo_str</span> <span class="o">=</span> <span class="s2">&quot;[</span><span class="si">%s</span><span class="s2">]</span><span class="se">\n</span><span class="s2">name = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">repo</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">repo</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
2018-06-04 23:27:56 +00:00
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">metalink</span><span class="p">:</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;metalink = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">repo</span><span class="o">.</span><span class="n">metalink</span>
<span class="k">elif</span> <span class="n">repo</span><span class="o">.</span><span class="n">mirrorlist</span><span class="p">:</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;mirrorlist = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">repo</span><span class="o">.</span><span class="n">mirrorlist</span>
<span class="k">elif</span> <span class="n">repo</span><span class="o">.</span><span class="n">baseurl</span><span class="p">:</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;baseurl = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">repo</span><span class="o">.</span><span class="n">baseurl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Repo has no baseurl, metalink, or mirrorlist&quot;</span><span class="p">)</span>
<span class="c1"># proxy is optional</span>
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;proxy = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">repo</span><span class="o">.</span><span class="n">proxy</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;sslverify = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">repo</span><span class="o">.</span><span class="n">sslverify</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;gpgcheck = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">repo</span><span class="o">.</span><span class="n">gpgcheck</span>
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">gpgkey</span><span class="p">:</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;gpgkey = </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">repo</span><span class="o">.</span><span class="n">gpgkey</span><span class="p">)</span>
2019-07-29 22:17:38 +00:00
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">skip_if_unavailable</span><span class="p">:</span>
<span class="n">repo_str</span> <span class="o">+=</span> <span class="s2">&quot;skip_if_unavailable=1</span><span class="se">\n</span><span class="s2">&quot;</span>
2018-06-04 23:27:56 +00:00
<span class="k">return</span> <span class="n">repo_str</span></div>
2019-10-16 21:01:30 +00:00
<div class="viewcode-block" id="repo_to_source"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.repo_to_source">[docs]</a><span class="k">def</span> <span class="nf">repo_to_source</span><span class="p">(</span><span class="n">repo</span><span class="p">,</span> <span class="n">system_source</span><span class="p">,</span> <span class="n">api</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
2018-06-04 23:27:56 +00:00
<span class="sd">&quot;&quot;&quot;Return a Weldr Source dict created from the DNF Repository</span>
<span class="sd"> :param repo: DNF Repository</span>
<span class="sd"> :type repo: dnf.RepoDict</span>
<span class="sd"> :param system_source: True if this source is an immutable system source</span>
<span class="sd"> :type system_source: bool</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> :param api: Select which api version of the dict to return (default 1)</span>
<span class="sd"> :type api: int</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :returns: A dict with Weldr Source fields filled in</span>
<span class="sd"> :rtype: dict</span>
<span class="sd"> Example::</span>
<span class="sd"> {</span>
<span class="sd"> &quot;check_gpg&quot;: true,</span>
<span class="sd"> &quot;check_ssl&quot;: true,</span>
<span class="sd"> &quot;gpgkey_url&quot;: [</span>
<span class="sd"> &quot;file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-28-x86_64&quot;</span>
<span class="sd"> ],</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> &quot;id&quot;: &quot;fedora&quot;,</span>
<span class="sd"> &quot;name&quot;: &quot;Fedora $releasever - $basearch&quot;,</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> &quot;proxy&quot;: &quot;http://proxy.brianlane.com:8123&quot;,</span>
<span class="sd"> &quot;system&quot;: true</span>
<span class="sd"> &quot;type&quot;: &quot;yum-metalink&quot;,</span>
<span class="sd"> &quot;url&quot;: &quot;https://mirrors.fedoraproject.org/metalink?repo=fedora-28&amp;arch=x86_64&quot;</span>
<span class="sd"> }</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> The ``name`` field has changed in v1 of the API.</span>
<span class="sd"> In v0 of the API ``name`` is the repo.id, in v1 it is the repo.name and a new field,</span>
<span class="sd"> ``id`` has been added for the repo.id</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> &quot;&quot;&quot;</span>
2019-10-16 21:01:30 +00:00
<span class="k">if</span> <span class="n">api</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
<span class="n">source</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">repo</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="s2">&quot;system&quot;</span><span class="p">:</span> <span class="n">system_source</span><span class="p">}</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">source</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="n">repo</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">repo</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s2">&quot;system&quot;</span><span class="p">:</span> <span class="n">system_source</span><span class="p">}</span>
2018-06-04 23:27:56 +00:00
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">baseurl</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">repo</span><span class="o">.</span><span class="n">baseurl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;yum-baseurl&quot;</span>
<span class="k">elif</span> <span class="n">repo</span><span class="o">.</span><span class="n">metalink</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">repo</span><span class="o">.</span><span class="n">metalink</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;yum-metalink&quot;</span>
<span class="k">elif</span> <span class="n">repo</span><span class="o">.</span><span class="n">mirrorlist</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">repo</span><span class="o">.</span><span class="n">mirrorlist</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;yum-mirrorlist&quot;</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Repo has no baseurl, metalink, or mirrorlist&quot;</span><span class="p">)</span>
<span class="c1"># proxy is optional</span>
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">proxy</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;proxy&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">repo</span><span class="o">.</span><span class="n">proxy</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">repo</span><span class="o">.</span><span class="n">sslverify</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;check_ssl&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;check_ssl&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">repo</span><span class="o">.</span><span class="n">gpgcheck</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;check_gpg&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;check_gpg&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="n">repo</span><span class="o">.</span><span class="n">gpgkey</span><span class="p">:</span>
2018-10-24 17:07:32 +00:00
<span class="n">source</span><span class="p">[</span><span class="s2">&quot;gpgkey_urls&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">repo</span><span class="o">.</span><span class="n">gpgkey</span><span class="p">)</span>
2018-06-04 23:27:56 +00:00
<span class="k">return</span> <span class="n">source</span></div>
2019-10-16 21:01:30 +00:00
<div class="viewcode-block" id="source_to_repodict"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.source_to_repodict">[docs]</a><span class="k">def</span> <span class="nf">source_to_repodict</span><span class="p">(</span><span class="n">source</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a tuple suitable for use with dnf.add_new_repo</span>
<span class="sd"> :param source: A Weldr source dict</span>
<span class="sd"> :type source: dict</span>
<span class="sd"> :returns: A tuple of dnf.Repo attributes</span>
<span class="sd"> :rtype: (str, list, dict)</span>
<span class="sd"> Return a tuple with (id, baseurl|(), kwargs) that can be used</span>
<span class="sd"> with dnf.repos.add_new_repo</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">kwargs</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">if</span> <span class="s2">&quot;id&quot;</span> <span class="ow">in</span> <span class="n">source</span><span class="p">:</span>
<span class="c1"># This is an API v1 source definition</span>
<span class="n">repoid</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;id&quot;</span><span class="p">]</span>
<span class="k">if</span> <span class="s2">&quot;name&quot;</span> <span class="ow">in</span> <span class="n">source</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">repoid</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span>
<span class="c1"># This will allow errors to be raised so we can catch them</span>
<span class="c1"># without this they are logged, but the repo is silently disabled</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;skip_if_unavailable&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">if</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;yum-baseurl&quot;</span><span class="p">:</span>
<span class="n">baseurl</span> <span class="o">=</span> <span class="p">[</span><span class="n">source</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]]</span>
<span class="k">elif</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;yum-metalink&quot;</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;metalink&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span>
<span class="n">baseurl</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">elif</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;yum-mirrorlist&quot;</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;mirrorlist&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span>
<span class="n">baseurl</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">if</span> <span class="s2">&quot;proxy&quot;</span> <span class="ow">in</span> <span class="n">source</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;proxy&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;proxy&quot;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;check_ssl&quot;</span><span class="p">]:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;sslverify&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;sslverify&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">if</span> <span class="n">source</span><span class="p">[</span><span class="s2">&quot;check_gpg&quot;</span><span class="p">]:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;gpgcheck&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;gpgcheck&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">if</span> <span class="s2">&quot;gpgkey_urls&quot;</span> <span class="ow">in</span> <span class="n">source</span><span class="p">:</span>
<span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;gpgkey&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">source</span><span class="p">[</span><span class="s2">&quot;gpgkey_urls&quot;</span><span class="p">])</span>
<span class="k">return</span> <span class="p">(</span><span class="n">repoid</span><span class="p">,</span> <span class="n">baseurl</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span></div>
2018-06-04 23:27:56 +00:00
<div class="viewcode-block" id="source_to_repo"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.source_to_repo">[docs]</a><span class="k">def</span> <span class="nf">source_to_repo</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">dnf_conf</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a dnf Repo object created from a source dict</span>
<span class="sd"> :param source: A Weldr source dict</span>
<span class="sd"> :type source: dict</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> :param dnf_conf: The dnf Config object</span>
<span class="sd"> :type dnf_conf: dnf.conf</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :returns: A dnf Repo object</span>
<span class="sd"> :rtype: dnf.Repo</span>
<span class="sd"> Example::</span>
<span class="sd"> {</span>
<span class="sd"> &quot;check_gpg&quot;: True,</span>
<span class="sd"> &quot;check_ssl&quot;: True,</span>
<span class="sd"> &quot;gpgkey_urls&quot;: [</span>
<span class="sd"> &quot;file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-28-x86_64&quot;</span>
<span class="sd"> ],</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> &quot;id&quot;: &quot;fedora&quot;,</span>
<span class="sd"> &quot;name&quot;: &quot;Fedora $releasever - $basearch&quot;,</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> &quot;proxy&quot;: &quot;http://proxy.brianlane.com:8123&quot;,</span>
<span class="sd"> &quot;system&quot;: True</span>
<span class="sd"> &quot;type&quot;: &quot;yum-metalink&quot;,</span>
<span class="sd"> &quot;url&quot;: &quot;https://mirrors.fedoraproject.org/metalink?repo=fedora-28&amp;arch=x86_64&quot;</span>
<span class="sd"> }</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> If the ``id`` field is included it is used for the repo id, otherwise ``name`` is used.</span>
<span class="sd"> v0 of the API only used ``name``, v1 added the distinction between ``id`` and ``name``.</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> &quot;&quot;&quot;</span>
2019-10-16 21:01:30 +00:00
<span class="n">repoid</span><span class="p">,</span> <span class="n">baseurl</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">source_to_repodict</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
<span class="n">repo</span> <span class="o">=</span> <span class="n">dnf</span><span class="o">.</span><span class="n">repo</span><span class="o">.</span><span class="n">Repo</span><span class="p">(</span><span class="n">repoid</span><span class="p">,</span> <span class="n">dnf_conf</span><span class="p">)</span>
<span class="k">if</span> <span class="n">baseurl</span><span class="p">:</span>
<span class="n">repo</span><span class="o">.</span><span class="n">baseurl</span> <span class="o">=</span> <span class="n">baseurl</span>
2018-06-04 23:27:56 +00:00
2019-10-16 21:01:30 +00:00
<span class="c1"># Apply the rest of the kwargs to the Repo object</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="nb">setattr</span><span class="p">(</span><span class="n">repo</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
2018-06-04 23:27:56 +00:00
<span class="n">repo</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>
<span class="k">return</span> <span class="n">repo</span></div>
<div class="viewcode-block" id="get_source_ids"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.get_source_ids">[docs]</a><span class="k">def</span> <span class="nf">get_source_ids</span><span class="p">(</span><span class="n">source_path</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a list of the source ids in a file</span>
<span class="sd"> :param source_path: Full path and filename of the source (yum repo) file</span>
<span class="sd"> :type source_path: str</span>
<span class="sd"> :returns: A list of source id strings</span>
<span class="sd"> :rtype: list of str</span>
<span class="sd"> &quot;&quot;&quot;</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">exists</span><span class="p">(</span><span class="n">source_path</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[]</span>
<span class="n">cfg</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">()</span>
<span class="n">cfg</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">source_path</span><span class="p">)</span>
<span class="k">return</span> <span class="n">cfg</span><span class="o">.</span><span class="n">sections</span><span class="p">()</span></div>
<div class="viewcode-block" id="get_repo_sources"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.get_repo_sources">[docs]</a><span class="k">def</span> <span class="nf">get_repo_sources</span><span class="p">(</span><span class="n">source_glob</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Return a list of sources from a directory of yum repositories</span>
<span class="sd"> :param source_glob: A glob to use to match the source files, including full path</span>
<span class="sd"> :type source_glob: str</span>
<span class="sd"> :returns: A list of the source ids in all of the matching files</span>
<span class="sd"> :rtype: list of str</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">sources</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">glob</span><span class="p">(</span><span class="n">source_glob</span><span class="p">):</span>
<span class="n">sources</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">get_source_ids</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
<span class="k">return</span> <span class="n">sources</span></div>
2019-10-16 21:01:30 +00:00
<div class="viewcode-block" id="delete_repo_source"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.delete_repo_source">[docs]</a><span class="k">def</span> <span class="nf">delete_repo_source</span><span class="p">(</span><span class="n">source_glob</span><span class="p">,</span> <span class="n">source_id</span><span class="p">):</span>
2018-06-04 23:27:56 +00:00
<span class="sd">&quot;&quot;&quot;Delete a source from a repo file</span>
<span class="sd"> :param source_glob: A glob of the repo sources to search</span>
<span class="sd"> :type source_glob: str</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> :param source_id: The repo id to delete</span>
<span class="sd"> :type source_id: str</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> :returns: None</span>
<span class="sd"> :raises: ProjectsError if there was a problem</span>
<span class="sd"> A repo file may have multiple sources in it, delete only the selected source.</span>
<span class="sd"> If it is the last one in the file, delete the file.</span>
<span class="sd"> WARNING: This will delete ANY source, the caller needs to ensure that a system</span>
2019-10-16 21:01:30 +00:00
<span class="sd"> source_id isn&#39;t passed to it.</span>
2018-06-04 23:27:56 +00:00
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">glob</span><span class="p">(</span><span class="n">source_glob</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">cfg</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">()</span>
<span class="n">cfg</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
2019-10-16 21:01:30 +00:00
<span class="k">if</span> <span class="n">source_id</span> <span class="ow">in</span> <span class="n">cfg</span><span class="o">.</span><span class="n">sections</span><span class="p">():</span>
2018-06-04 23:27:56 +00:00
<span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
2019-10-16 21:01:30 +00:00
<span class="n">cfg</span><span class="o">.</span><span class="n">remove_section</span><span class="p">(</span><span class="n">source_id</span><span class="p">)</span>
2018-06-04 23:27:56 +00:00
<span class="c1"># If there are other sections, rewrite the file without the deleted one</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cfg</span><span class="o">.</span><span class="n">sections</span><span class="p">())</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">cfg_file</span><span class="p">:</span>
<span class="n">cfg</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">cfg_file</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># No sections left, just delete the file</span>
<span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
2019-10-16 21:01:30 +00:00
<span class="k">raise</span> <span class="n">ProjectsError</span><span class="p">(</span><span class="s2">&quot;Problem deleting repo source </span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">source_id</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>
2018-06-04 23:27:56 +00:00
<span class="k">if</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
2019-10-16 21:01:30 +00:00
<span class="k">raise</span> <span class="n">ProjectsError</span><span class="p">(</span><span class="s2">&quot;source </span><span class="si">%s</span><span class="s2"> not found&quot;</span> <span class="o">%</span> <span class="n">source_id</span><span class="p">)</span></div>
<div class="viewcode-block" id="new_repo_source"><a class="viewcode-back" href="../../../pylorax.api.html#pylorax.api.projects.new_repo_source">[docs]</a><span class="k">def</span> <span class="nf">new_repo_source</span><span class="p">(</span><span class="n">dbo</span><span class="p">,</span> <span class="n">repoid</span><span class="p">,</span> <span class="n">source</span><span class="p">,</span> <span class="n">repo_dir</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Add a new repo source from a Weldr source dict</span>
<span class="sd"> :param dbo: dnf base object</span>
<span class="sd"> :type dbo: dnf.Base</span>
<span class="sd"> :param id: The repo id (API v0 uses the name, v1 uses the id)</span>
<span class="sd"> :type id: str</span>
<span class="sd"> :param source: A Weldr source dict</span>
<span class="sd"> :type source: dict</span>
<span class="sd"> :returns: None</span>
<span class="sd"> :raises: ...</span>
<span class="sd"> Make sure access to the dbo has been locked before calling this.</span>
<span class="sd"> The `id` parameter will the the &#39;name&#39; field for API v0, and the &#39;id&#39; field for API v1</span>
<span class="sd"> DNF variables will be substituted at load time, and on restart.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">try</span><span class="p">:</span>
<span class="c1"># Remove it from the RepoDict (NOTE that this isn&#39;t explicitly supported by the DNF API)</span>
<span class="c1"># If this repo already exists, delete it and replace it with the new one</span>
<span class="n">repos</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">id</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">dbo</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">iter_enabled</span><span class="p">())</span>
<span class="k">if</span> <span class="n">repoid</span> <span class="ow">in</span> <span class="n">repos</span><span class="p">:</span>
<span class="k">del</span> <span class="n">dbo</span><span class="o">.</span><span class="n">repos</span><span class="p">[</span><span class="n">repoid</span><span class="p">]</span>
<span class="c1"># Add the repo and substitute any dnf variables</span>
<span class="n">_</span><span class="p">,</span> <span class="n">baseurl</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">source_to_repodict</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;repoid=</span><span class="si">%s</span><span class="s2">, baseurl=</span><span class="si">%s</span><span class="s2">, kwargs=</span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">repoid</span><span class="p">,</span> <span class="n">baseurl</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">dbo</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">add_new_repo</span><span class="p">(</span><span class="n">repoid</span><span class="p">,</span> <span class="n">dbo</span><span class="o">.</span><span class="n">conf</span><span class="p">,</span> <span class="n">baseurl</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">r</span><span class="o">.</span><span class="n">enable</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="s2">&quot;Updating repository metadata after adding </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">repoid</span><span class="p">)</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">fill_sack</span><span class="p">(</span><span class="n">load_system_repo</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">read_comps</span><span class="p">()</span>
<span class="c1"># Remove any previous sources with this id, ignore it if it isn&#39;t found</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">delete_repo_source</span><span class="p">(</span><span class="n">joinpaths</span><span class="p">(</span><span class="n">repo_dir</span><span class="p">,</span> <span class="s2">&quot;*.repo&quot;</span><span class="p">),</span> <span class="n">repoid</span><span class="p">)</span>
<span class="k">except</span> <span class="n">ProjectsError</span><span class="p">:</span>
<span class="k">pass</span>
<span class="c1"># Make sure the source id can&#39;t contain a path traversal by taking the basename</span>
<span class="n">source_path</span> <span class="o">=</span> <span class="n">joinpaths</span><span class="p">(</span><span class="n">repo_dir</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">basename</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.repo&quot;</span> <span class="o">%</span> <span class="n">repoid</span><span class="p">))</span>
<span class="c1"># Write the un-substituted version of the repo to disk</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">source_path</span><span class="p">,</span> <span class="s2">&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">repo</span> <span class="o">=</span> <span class="n">source_to_repo</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">dbo</span><span class="o">.</span><span class="n">conf</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">dnf_repo_to_file_repo</span><span class="p">(</span><span class="n">repo</span><span class="p">))</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;(new_repo_source) adding </span><span class="si">%s</span><span class="s2"> failed: </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">repoid</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
<span class="c1"># Cleanup the mess, if loading it failed we don&#39;t want to leave it in memory</span>
<span class="n">repos</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">id</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">dbo</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">iter_enabled</span><span class="p">())</span>
<span class="k">if</span> <span class="n">repoid</span> <span class="ow">in</span> <span class="n">repos</span><span class="p">:</span>
<span class="k">del</span> <span class="n">dbo</span><span class="o">.</span><span class="n">repos</span><span class="p">[</span><span class="n">repoid</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="s2">&quot;Updating repository metadata after adding </span><span class="si">%s</span><span class="s2"> failed&quot;</span><span class="p">,</span> <span class="n">repoid</span><span class="p">)</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">fill_sack</span><span class="p">(</span><span class="n">load_system_repo</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">dbo</span><span class="o">.</span><span class="n">read_comps</span><span class="p">()</span>
<span class="k">raise</span></div>
2018-05-12 00:18:21 +00:00
</pre></div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
2018-11-13 17:53:31 +00:00
&copy; Copyright 2018, Red Hat, Inc.
2018-05-12 00:18:21 +00:00
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
2018-06-04 23:27:56 +00:00
SphinxRtdTheme.Navigation.enable(true);
2018-05-12 00:18:21 +00:00
});
2019-03-27 23:44:14 +00:00
</script>
2018-05-12 00:18:21 +00:00
</body>
</html>