"""
Copyright (c) 2015 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the MIT license. See the LICENSE file for details.
"""
import os
import shutil

from cct.module import Module
from cct.lib.file_utils import create_dir

class File(Module):

    def copy(self, source, destination):
        """
        Copies file.

        Args:
            source: path to file
            destination: path where file should be copied
        """
        create_dir(destination)
        shutil.copy(source, destination)

    def link(self, source, destination):
        """
        Creates symbolik link.

        Args:
            source: path to symbolik link destination
            destination: Symbolik link name
        """
        create_dir(destination)
        os.symlink(source, destination)

    def move(self, source, destination):
        """
        Moves file.

        Args:
            source: path to file
            destination: path where file should be moved
        """
        create_dir(destination)
        shutil.move(source, destination)

    def remove(self, path):
        """
        Removes file.

        Args:
            source: path to file to be removed
        """
        shutil.rmtree(path)

@@ -194,16 +194,38 @@
 t shutil
+%0Aimport grp%0Aimport pwd
 %0A%0Afrom c
@@ -1288,8 +1288,1692 @@
 e(path)%0A
+%0A    def chown(self, owner, group, path, recursive=False):%0A        %22%22%22%0A        Change the ownership of a path.%0A%0A        Args:%0A            owner: the owner (numeric or name) to change ownership to%0A            group: the group (numeric or name) to change groupship to%0A            path: the path to operate on%0A            recursive: if path is a directory, recursively change ownership for all%0A                       paths within%0A        %22%22%22%0A        # supplied owner/group might be symbolic (e.g. 'wheel') or numeric.%0A        # Try interpreting symbolically first%0A        try:%0A            gid = grp.getgrnam(group).gr_gid%0A        except KeyError:%0A            gid = int(group,0)%0A        try:%0A            uid = pwd.getpwnam(owner).pw_uid%0A        except KeyError:%0A            uid = int(owner,0)%0A%0A        # Beware: argument order is different%0A        os.chown(path, uid, gid)%0A%0A        if recursive and os.path.isdir(path):%0A            for dirpath, dirnames, filenames in os.walk(path):%0A                for f in (dirnames + filenames):%0A                    os.chown(os.path.join(dirpath, f), uid, gid)%0A%0A    def chmod(self, mode, path, recursive=False):%0A        %22%22%22%0A        Change the permissions of a path.%0A%0A        Args:%0A            path: the path to operate on%0A            mode: the numeric mode to set%0A            recursive: whether to change mode recursively%0A        %22%22%22%0A        mode = int(mode,0)%0A%0A        # Beware: argument order swapped%0A        os.chmod(path, mode)%0A%0A        if recursive and os.path.isdir(path):%0A            for dirpath, dirnames, filenames in os.walk(path):%0A                for f in (dirnames + filenames):%0A                    os.chmod(os.path.join(dirpath, f), mode)%0A

add chmod and chown to base.File