# 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


def get_path_owner(path):
    owner = ''
    try:
        owner = pwd.getpwuid(os.stat(path).st_uid).pw_name
    except (IOError, OSError) as exc_err:
        logger.warn(
            "Can't get path owner on {} due to permission/IO issues - {}"
            .format(path, exc_err)
        )

    return owner


def read_file_contents(path):
    if os.path.exists(path):
        with open(path) as infile:
            return infile.read().strip()


def write_file_contents(path, data):
    with open(path, 'w') as outfile:
        outfile.write(data)


def read_file_contents_as_lines(path):
    if os.path.exists(path):
        with open(path) as infile:
            content = infile.readlines()

# 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



def get_path_owner(path):
    owner = ''
    try:
        owner = pwd.getpwuid(os.stat(path).st_uid).pw_name
    except (IOError, OSError) as exc_err:
        logger.warn(
            "Can't get path owner on {} due to permission/IO issues - {}"
            .format(path, exc_err)
        )

    return owner


def read_file_contents(path):
    if os.path.exists(path):
        with open(path) as infile:
            return infile.read().strip()


def write_file_contents(path, data):
    with open(path, 'w') as outfile:
        outfile.write(data)


def read_file_contents_as_lines(path):
    if os.path.exists(path):
        with open(path) as infile:
            content = infile.readlines()

            lines = [line.strip() for line in content]