- """
-
-
-def EnsurePermission(path, mode, uid=-1, gid=-1, must_exist=True,
- _chmod_fn=os.chmod, _chown_fn=os.chown):
- """Ensures that given path has given mode.
-
- @param path: The path to the file
- @param mode: The mode of the file
- @param uid: The uid of the owner of this file
- @param gid: The gid of the owner of this file
- @param must_exist: Specifies if non-existance of path will be an error
- @param _chmod_fn: chmod function to use (unittest only)
- @param _chown_fn: chown function to use (unittest only)
-
- """
- try:
- _chmod_fn(path, mode)
-
- if max(uid, gid) > -1:
- _chown_fn(path, uid, gid)
- except EnvironmentError, err:
- if err.errno == errno.ENOENT:
- if must_exist:
- raise EnsureError("Path %s does not exists, but should" % path)
- else:
- raise EnsureError("Error while changing permission on %s: %s" %
- (path, err))
-
-
-def EnsureDir(path, mode, uid, gid, _stat_fn=os.lstat, _mkdir_fn=os.mkdir,
- _ensure_fn=EnsurePermission):
- """Ensures that given path is a dir and has given mode, uid and gid set.
-
- @param path: The path to the file
- @param mode: The mode of the file
- @param uid: The uid of the owner of this file
- @param gid: The gid of the owner of this file
- @param _stat_fn: Stat function to use (unittest only)
- @param _mkdir_fn: mkdir function to use (unittest only)
- @param _ensure_fn: ensure function to use (unittest only)
-
- """
- try:
- # We don't want to follow symlinks
- st_mode = _stat_fn(path)[stat.ST_MODE]
-
- if not stat.S_ISDIR(st_mode):
- raise EnsureError("Path %s is expected to be a directory, but it's not" %
- path)
- except EnvironmentError, err:
- if err.errno == errno.ENOENT:
- _mkdir_fn(path)
- else:
- raise EnsureError("Error while do a stat() on %s: %s" % (path, err))
-
- _ensure_fn(path, mode, uid=uid, gid=gid)