On Nov 8, 2010, at 9:13 AM, Ryan May wrote:

> On Thu, Nov 4, 2010 at 10:49 AM, Philip Semanchuk <phi...@semanchuk.com> 
> wrote:
>> Hi all,
>> I've run into an aspect of matplotlib's setup that seems awkward. I'm seeing 
>> this on Ubuntu, but I imagine it would happen on any *nix platform.
>> 
>> If python is running under sudo the first time matplotlib is imported, then 
>> matplotlib creates its config dir (~/.matplotlib) with root as the owner. 
>> Subsequent attempts to import matplotlib while running python as a 
>> non-privileged user result in this:
>> 
>> -----------------------------------------------------------------
>> RuntimeError: '/home/philip' is not a writable dir; you must set 
>> /home/philip/.matplotlib to be a writable dir.  You can also set environment 
>> variable MPLCONFIGDIR to any writable directory where you want matplotlib 
>> data stored
>> -----------------------------------------------------------------
>> 
>> A simple way to re-create this --
>> 1. Delete or rename ~/.matplotlib
>> 2. sudo python -c "import matplotlib"
>> 3. python -c "import matplotlib"
>> 
>> 
>> This not-improbable real-world scenario would create ~/.matplotlib owned by 
>> root  --
>> 1) Download app FooBar that has matplotlib as a dependency
>> 2) Install matplotlib
>> 3) Run FooBar's setup.py as sudo. It imports matplotlib, perhaps just to 
>> ensure that matplotlib is installed and working.
>> 
>> 
>> We ran into a similar situation with our app ('sudo python setup.py install' 
>> created desktop icons owned by root) and we resolved it by invoking chown 
>> after using a getenv() call to sniff out who we really wanted to own the 
>> file.
>> 
>> It looks like the diff below (untested!) applied to 
>> lib/matplotlib/__init__.py would prevent this from happening. Does it seems 
>> reasonable to add it?
>> 
>> 474a475,485
>>>         if not sys.platform.lower().startswith("win"):
>>>             # Ensure that we didn't just create a root-owned directory in 
>>> the
>>>             # user's HOME directory. That happens if this is being run under
>>>             # sudo. If the SUDO_USER env. var (which contains the user that
>>>             # invoked sudo) then we're running under sudo. If it doesn't
>>>             # exist, we're not running under sudo.
>>>             current_user = os.getenv("SUDO_USER")
>>>             if current_user:
>>>                 subprocess.call(["chown", "-R", current_user, p])
> 
> I'm not sure how I feel about this. On one hand, it is a pain to have
> this not work under sudo. On the other, I consider the fact that $HOME
> is not set to "/root" when running as root to be a bit of a problem.
> Personally, the better solution to me would be to make sure $HOME is
> one of the variables that gets reset by sudo.

Hi Ryan,
Thanks. I don't know why sudo behaves the way it does with regard to $HOME, but 
the behavior of sudo is not under my control (nor matplotlib's). Also, I expect 
that *lots* of software depends on this behavior of sudo so changing it is 
probably not an option.

@Matthieu --

Merci Matthieu, yours is an elegant solution. In our setup.py we're just 
importing matplotlib to ensure it exists, so that fits your scenario. 

The workaround I used was this --

    MATPLOTLIB_CONFIG_PATH = os.path.join(os.path.expanduser('~'), 
".matplotlib")

    if not sys.platform.lower().startswith("win"):
        if not os.path.exists(MATPLOTLIB_CONFIG_PATH):
            check_matplotlib_permissions = True
        
        
    # Do stuff that triggers an import of matplotlib...


    if check_matplotlib_permissions:
        if os.path.exists(MATPLOTLIB_CONFIG_PATH):
            sudo_user = os.getenv("SUDO_USER")
            if sudo_user:
                subprocess.call(["chown", "-R", sudo_user, 
MATPLOTLIB_CONFIG_PATH])


I think I like your idea better.


Cheers
Philip

PS - Matthieu, sorry for not quoting you directly but I was unable to subscribe 
to the list before I received your mail.






------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to