project_permissions.py (AEN 4.1.3)
==================================

.. raw:: html

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

    import argparse
    import os
    import <span class="nb">pwd</span>
    from os.path import join
    import subprocess

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

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

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