project_permissions.py (AEN 4.1.1)
==================================

.. raw:: html

    <div class="highlight-default"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>

    <span class="kn">import</span> <span class="nn">argparse</span>
    <span class="kn">import</span> <span class="nn">os</span>
    <span class="kn">import</span> <span class="nn">pwd</span>
    <span class="kn">from</span> <span class="nn">os.path</span> <span class="k">import</span> <span class="n">join</span>
    <span class="kn">import</span> <span class="nn">subprocess</span>

    <span class="k">def</span> <span class="nf">parser</span><span class="p">():</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
        <span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&quot;project_directory&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">p</span>

    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">project_directory</span><span class="p">):</span>
        <span class="c1">#this will be something like /projects/&lt;username&gt;</span>
        <span class="n">project_dirs</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">project_directory</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">dirname</span> <span class="ow">in</span> <span class="n">project_dirs</span><span class="p">:</span>
            <span class="nb">print</span> <span class="n">dirname</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">project_directory</span><span class="p">,</span> <span class="n">dirname</span><span class="p">)</span>
            <span class="n">owner</span> <span class="o">=</span> <span class="n">pwd</span><span class="o">.</span><span class="n">getpwuid</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">st_uid</span><span class="p">)</span><span class="o">.</span><span class="n">pw_name</span>
            <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;chown&#39;</span><span class="p">,</span> <span class="s1">&#39;-R&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">owner</span><span class="p">,</span> <span class="n">owner</span><span class="p">),</span> <span class="n">path</span><span class="p">])</span>
            <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;chmod&#39;</span><span class="p">,</span> <span class="s1">&#39;-R&#39;</span><span class="p">,</span> <span class="s1">&#39;g-s&#39;</span><span class="p">,</span> <span class="n">path</span><span class="p">])</span>
    <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;chown&#39;</span><span class="p">,</span> <span class="s1">&#39;-R&#39;</span><span class="p">,</span> <span class="s1">&#39;wakari:wakari&#39;</span><span class="p">,</span> <span class="s1">&#39;/opt/wakari/anaconda&#39;</span><span class="p">])</span>
    <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;chmod&#39;</span><span class="p">,</span> <span class="s1">&#39;-R&#39;</span><span class="p">,</span> <span class="s1">&#39;a+r&#39;</span><span class="p">,</span> <span class="s1">&#39;/opt/wakari/anaconda&#39;</span><span class="p">])</span>
    <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;chmod&#39;</span><span class="p">,</span> <span class="s1">&#39;-R&#39;</span><span class="p">,</span> <span class="s1">&#39;o-w&#39;</span><span class="p">,</span> <span class="s1">&#39;/opt/wakari/anaconda&#39;</span><span class="p">])</span>

    <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">parser</span><span class="p">()</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
        <span class="n">run</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">project_directory</span><span class="p">)</span>
    </pre></div>
    </div>
