# Path: kano/utils/user.py
# def get_user_unsudoed():
#     if 'SUDO_USER' in os.environ:
#         return os.environ['SUDO_USER']
#     elif 'LOGNAME' in os.environ:
#         return os.environ['LOGNAME']
#     else:
#         return 'root'
# 
# Path: kano/utils/shell.py
# def run_cmd(cmd, localised=False, unsudo=False):
#     '''
#     Executes cmd, returning stdout, stderr, return code
#     if localised is False, LC_ALL will be set to "C"
#     '''
#     env = os.environ.copy()
#     if not localised:
#         env['LC_ALL'] = 'C'
# 
#     if unsudo and \
#             'SUDO_USER' in os.environ and \
#             os.environ['SUDO_USER'] != 'root':
#         cmd = "sudo -u {} bash -c '{}' ".format(os.environ['SUDO_USER'], cmd)
# 
#     process = subprocess.Popen(cmd, shell=True, env=env,
#                                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
#                                preexec_fn=restore_signals)
# 
#     stdout, stderr = process.communicate()
#     returncode = process.returncode
#     return stdout, stderr, returncode


import os
import re
import pwd
import getpass
import grp
import shutil
import json
import fcntl
import errno
import stat
import time
import tempfile
from kano.utils.user import get_user_unsudoed
from kano.utils.shell import run_cmd
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        gid = grp.getgrnam(group).gr_gid
        os.chown(path, uid, gid)
    except KeyError as e:
        logger.error(
            'user {} or group {} do not match with existing'.format(user, group))
        ret_val = False
    except OSError as e:
        logger.error(
            'Error while trying to chown, root priviledges needed {}'.format(e))
        ret_val = False
    else:
        ret_val = True
    return ret_val


def read_json(filepath, silent=True):
    try:
        return json.loads(read_file_contents(filepath))
    except Exception:
        if not silent:
            raise


def write_json(filepath, data, prettyprint=False, sort_keys=True):
    with open(filepath, 'w') as outfile:
        json.dump(data, outfile, indent=2, sort_keys=sort_keys)
    if prettyprint:
        _, _, rc = run_cmd('which underscore')
        if rc == 0:
            cmd = 'underscore print -i {filepath} -o {filepath}'.format(filepath=filepath)

# Path: kano/utils/user.py
# def get_user_unsudoed():
#     if 'SUDO_USER' in os.environ:
#         return os.environ['SUDO_USER']
#     elif 'LOGNAME' in os.environ:
#         return os.environ['LOGNAME']
#     else:
#         return 'root'
# 
# Path: kano/utils/shell.py
# def run_cmd(cmd, localised=False, unsudo=False):
#     '''
#     Executes cmd, returning stdout, stderr, return code
#     if localised is False, LC_ALL will be set to "C"
#     '''
#     env = os.environ.copy()
#     if not localised:
#         env['LC_ALL'] = 'C'
# 
#     if unsudo and \
#             'SUDO_USER' in os.environ and \
#             os.environ['SUDO_USER'] != 'root':
#         cmd = "sudo -u {} bash -c '{}' ".format(os.environ['SUDO_USER'], cmd)
# 
#     process = subprocess.Popen(cmd, shell=True, env=env,
#                                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
#                                preexec_fn=restore_signals)
# 
#     stdout, stderr = process.communicate()
#     returncode = process.returncode
#     return stdout, stderr, returncode


# Path: kano/utils/file_operations.py
import os
import re
import pwd
import getpass
import grp
import shutil
import json
import fcntl
import errno
import stat
import time
import tempfile
from kano.utils.user import get_user_unsudoed
from kano.utils.shell import run_cmd
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger
        from kano.logging import logger

        gid = grp.getgrnam(group).gr_gid
        os.chown(path, uid, gid)
    except KeyError as e:
        logger.error(
            'user {} or group {} do not match with existing'.format(user, group))
        ret_val = False
    except OSError as e:
        logger.error(
            'Error while trying to chown, root priviledges needed {}'.format(e))
        ret_val = False
    else:
        ret_val = True
    return ret_val


def read_json(filepath, silent=True):
    try:
        return json.loads(read_file_contents(filepath))
    except Exception:
        if not silent:
            raise


def write_json(filepath, data, prettyprint=False, sort_keys=True):
    with open(filepath, 'w') as outfile:
        json.dump(data, outfile, indent=2, sort_keys=sort_keys)
    if prettyprint:
        _, _, rc = run_cmd('which underscore')
        if rc == 0:
            cmd = 'underscore print -i {filepath} -o {filepath}'.format(filepath=filepath)

            run_cmd(cmd)