Exemple #1
0
def setFileOwner(user, path):
    """
    Update file ownership for 1 file or folder.\n
    `Chown` function works ONLY in Linux.
    """
    logFull('helpers:setFileOwner user `{}`.'.format(user))
    try:
        from pwd import getpwnam
        uid = getpwnam(user)[2]
        gid = getpwnam(user)[3]
    except:
        return False

    if os.path.isdir(path):
        try:
            proc = subprocess.Popen(
                ['chown',
                 str(uid) + ':' + str(gid), path, '-R', '--silent'], )
            proc.wait()
        except:
            logWarning(
                'Cannot change owner! Cannot chown folder `{}:{}` on `{} -R`!'.
                format(uid, gid, path))
            return False

    else:
        try:
            os.chown(path, uid, gid)
        except:
            logWarning(
                'Cannot set owner! Cannot chown file `{}:{}` on `{}`!'.format(
                    uid, gid, path))
            return False

    return True
Exemple #2
0
def setFileOwner(user, path):
    """
    Update file ownership for 1 file or folder.\n
    `Chown` function works ONLY in Linux.
    """
    try:
        from pwd import getpwnam

        uid = getpwnam(user)[2]
        gid = getpwnam(user)[3]
    except:
        return False

    if os.path.isdir(path):
        try:
            proc = subprocess.Popen(["chown", str(uid) + ":" + str(gid), path, "-R"])
            proc.wait()
        except:
            logWarning("Cannot change owner! Cannot chown folder `{}:{}` on `{} -R`!".format(uid, gid, path))
            return False

    else:
        try:
            os.chown(path, uid, gid)
        except:
            logWarning("Cannot set owner! Cannot chown file `{}:{}` on `{}`!".format(uid, gid, path))
            return False

    return True
Exemple #3
0
def execScript(script_path):
    """
    Execute a user script and return the text printed on the screen.
    """
    if not os.path.exists(script_path):
        logWarning("Exec script: The path `{}` does not exist!".format(script_path))
        return False

    try:
        os.system("chmod +x {}".format(script_path))
    except:
        pass

    logDebug("Executing script `{}`...".format(script_path))

    try:
        txt = subprocess.check_output(script_path, shell=True)
        return txt.strip()
    except Exception as e:
        logWarning("Exec script `{}`: Exception - `{}`.".format(script_path, e))
        return False
Exemple #4
0
def execScript(script_path):
    """
    Execute a user script and return the text printed on the screen.
    """
    if not os.path.exists(script_path):
        logWarning(
            'Exec script: The path `{}` does not exist!'.format(script_path))
        return False

    try:
        os.system('chmod +x {}'.format(script_path))
    except:
        pass

    logDebug('Executing script `{}`...'.format(script_path))

    try:
        txt = subprocess.check_output(script_path, shell=True)
        return txt.strip()
    except Exception as e:
        logWarning('Exec script `{}`: Exception - `{}`.'.format(
            script_path, e))
        return False