~ubuntu-branches/ubuntu/saucy/terminator/saucy

« back to all changes in this revision

Viewing changes to terminatorlib/cwd.py

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Valcárcel Scerpella (Canonical)
  • Date: 2010-04-07 17:10:31 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407171031-35nsuj0tmbub0bj5
Tags: 0.92-0ubuntu1
* New upstream release
* Remove python-xdg from Recommends. (Closes: #567967)
* Downgrade python-gnome2 to Recommends.
* Update python-gtk2 dependency to (>= 2.14.0)
* Add python-keybinder to Recommends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Terminator by Chris Jones <cmsj@tenshu.net>
 
3
# GPL v2 only
 
4
"""cwd.py - function necessary to get the cwd for a given pid on various OSes
 
5
 
 
6
>>> cwd = get_default_cwd()
 
7
>>> cwd.__class__.__name__
 
8
'str'
 
9
>>> func = get_pid_cwd()
 
10
>>> func.__class__.__name__
 
11
'function'
 
12
 
 
13
"""
 
14
 
 
15
import platform
 
16
import os
 
17
import pwd
 
18
from util import dbg
 
19
 
 
20
def get_default_cwd():
 
21
    """Determine a reasonable default cwd"""
 
22
    cwd = os.getcwd()
 
23
    if not os.path.exists(cwd) or not os.path.isdir(cwd):
 
24
        cwd = pwd.getpwuid(os.getuid())[5]
 
25
    
 
26
    return(cwd)
 
27
 
 
28
def get_pid_cwd():
 
29
    """Determine an appropriate cwd function for the OS we are running on"""
 
30
 
 
31
    func = lambda pid: None
 
32
    system = platform.system()
 
33
 
 
34
    if system == 'Linux':
 
35
        dbg('Using Linux get_pid_cwd')
 
36
        func = lambda pid: os.path.realpath('/proc/%s/cwd' % pid)
 
37
    elif system == 'FreeBSD':
 
38
        try:
 
39
            import freebsd
 
40
            func = freebsd.get_process_cwd
 
41
            dbg('Using FreeBSD get_pid_cwd')
 
42
        except (OSError, NotImplementedError, ImportError):
 
43
            dbg('FreeBSD version too old for get_pid_cwd')
 
44
    elif system == 'SunOS':
 
45
        dbg('Using SunOS get_pid_cwd')
 
46
        func = lambda pid: os.path.realpath('/proc/%s/path/cwd' % pid)
 
47
    else:
 
48
        dbg('Unable to determine a get_pid_cwd for OS: %s' % system)
 
49
 
 
50
    return(func)
 
51
 
 
52
# vim: set expandtab ts=4 sw=4: