root/cleverbox/trunk/cleverbox/utils/filesystem.py

Revision 355, 2.7 KB (checked in by trivoallan, 3 years ago)

Fixed SVN permission issues.

Line 
1# This file is part of the "Cleverbox" program.
2#
3# Cleverbox is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# Cleverbox is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with Cleverbox.  If not, see <http://www.gnu.org/licenses/>.
15#
16# Copyright 2008, Tristan Rivoallan
17
18"""
19This module regroups miscelaneous filesystem related functions.
20"""
21
22import pwd, grp, os, stat
23
24def chowntree(directory, uid, gid):
25    """
26    Recursive chown.
27    """
28    try:
29        if (os.path.isdir(directory)):
30            os.chown(directory, uid, gid)
31            direntries = os.listdir(directory)
32            for direntry in direntries:
33                direntry = os.path.join(directory, direntry)
34                if (os.path.isdir(direntry)):
35                    os.chown(direntry, uid, gid)
36                    os.chdir(direntry)
37                    chowntree(direntry, uid, gid)
38                else:
39                    os.chown(direntry, uid, gid)
40        else:
41            os.chown(directory, uid, gid)
42    except (IOError, os.error), why:
43        print "rchown %s: %s" % (str(directory), str(why))
44
45def chmodtree(arg, dirname, fnames):
46    """ To be used with os.path.walk. """
47
48    os.chmod(dirname, arg)
49    for file in fnames:
50        os.chmod(os.path.join(dirname, file), arg)
51
52
53def get_uid_from_name(username):
54    """
55    Returns uid corresponding to given username.
56    """
57    return pwd.getpwnam(username)[2]
58       
59def get_gid_from_name(groupname):
60    """
61    Returns gid corresponding to given groupname.
62    """
63    return grp.getgrnam(groupname)[2]
64
65def is_file(path):
66   """
67   Returns true if path points to a regular file (stat.S_ISREG).
68   """
69   mode = os.stat(path)[stat.ST_MODE]
70   return stat.S_ISREG(mode)
71
72def is_directory(path):
73   """
74   Returns true if path points to a directory (stat.S_ISDIR).
75   """
76   mode = os.stat(path)[stat.ST_MODE]
77   return stat.S_ISDIR(mode)
78
79def is_symlink(path):
80   """
81   Returns true if path points to a symlink (stat.S_ISLNK).
82   """
83   mode = os.stat(path)[stat.ST_MODE]
84   return stat.S_ISLNK(mode)
85
86def set_permissions(permissions):
87    for (entry, spec) in permissions.items():
88        os.chmod(entry, spec[0])
89        os.chown(entry,
90                 get_uid_from_name(spec[1]),
91                 get_gid_from_name(spec[2]))
Note: See TracBrowser for help on using the browser.