Beispiel #1
0
 def __init__(self, folder_object, username):
     self.folder_path = Path(
         expand_windows_var(folder_object.path,
                            username).replace('\\', '/'))
     self.action = action_letter2enum(folder_object.action)
     self.delete_files = str2bool(folder_object.delete_files)
     self.delete_folder = str2bool(folder_object.delete_folder)
     self.delete_sub_folders = str2bool(folder_object.delete_sub_folders)
def write_shortcut(shortcut, username=None):
    '''
    Write the single shortcut file to the disk.

    :username: None means working with machine variables and paths
    '''
    dest_abspath = expand_windows_var(shortcut.dest, username).replace(
        '\\', '/') + '.desktop'
    logging.debug(slogm('Writing shortcut file to {}'.format(dest_abspath)))
    shortcut.write_desktop(dest_abspath)
Beispiel #3
0
def apply_shortcut(shortcut, username=None):
    '''
    Apply the single shortcut file to the disk.

    :username: None means working with machine variables and paths
    '''
    dest_abspath = shortcut.dest
    if not dest_abspath.startswith('/') and not dest_abspath.startswith('%'):
        dest_abspath = '%HOME%/' + dest_abspath

    logging.debug(
        slogm('Try to expand path for shortcut: {} for {}'.format(
            dest_abspath, username)))
    dest_abspath = expand_windows_var(dest_abspath, username).replace(
        '\\', '/') + '.desktop'

    # Check that we're working for user, not on global system level
    if username:
        # Check that link destination path starts with specification of
        # user's home directory
        if dest_abspath.startswith(get_homedir(username)):
            # Don't try to operate on non-existent directory
            if not homedir_exists(username):
                logging.warning(
                    slogm(
                        'No home directory exists for user {}: will not apply link {}'
                        .format(username, dest_abspath)))
                return None
        else:
            logging.warning(
                slogm(
                    'User\'s shortcut not placed to home directory for {}: bad path {}'
                    .format(username, dest_abspath)))
            return None

    if '%' in dest_abspath:
        logging.debug(
            slogm('Fail for applying shortcut to file with \'%\': {}'.format(
                dest_abspath)))
        return None

    if not dest_abspath.startswith('/'):
        logging.debug(
            slogm('Fail for applying shortcut to not absolute path \'%\': {}'.
                  format(dest_abspath)))
        return None

    logging.debug(
        slogm('Applying shortcut file to {} with action {}'.format(
            dest_abspath, shortcut.action)))
    shortcut.apply_desktop(dest_abspath)
Beispiel #4
0
def storage_get_shortcuts(storage, sid, username=None):
    '''
    Query storage for shortcuts' rows for specified SID.
    '''
    shortcut_objs = storage.get_shortcuts(sid)
    shortcuts = list()

    for sc_obj in shortcut_objs:
        sc = json2sc(sc_obj.shortcut)
        if username:
            sc.set_expanded_path(expand_windows_var(sc.path, username))
        shortcuts.append(sc)

    return shortcuts
Beispiel #5
0
    def act(self):
        if isfile(self.envvar_file_path):
            with open(self.envvar_file_path, 'r') as f:
                lines = f.readlines()
        else:
            lines = list()

        file_changed = False
        for envvar_object in self.envvars:
            action = action_letter2enum(envvar_object.action)
            name = envvar_object.name
            value = expand_windows_var(envvar_object.value, self.username)
            if value != envvar_object.value:
                #slashes are replaced only if the change of variables was performed and we consider the variable as a path to a file or directory
                value = value.replace('\\', '/')
            exist_line = None
            for line in lines:
                if line.split()[0] == name:
                    exist_line = line
                    break
            if exist_line != None:
                if action == FileAction.CREATE:
                    pass
                if action == FileAction.DELETE:
                    lines.remove(exist_line)
                    file_changed = True
                if action == FileAction.UPDATE or action == FileAction.REPLACE:
                    if exist_line.split()[1].split('=')[1].replace('"', '') != value: #from 'NAME DEFAULT=value' cut value and compare, don`t change if it matches
                        lines.remove(exist_line)
                        lines.append(name + ' ' + 'DEFAULT=\"' + value + '\"\n')
                        file_changed = True
            else:
                if action == FileAction.CREATE or action == FileAction.UPDATE or action == FileAction.REPLACE:
                    lines.append(name + ' ' + 'DEFAULT=\"' + value + '\"\n')
                    file_changed = True
                if action == FileAction.DELETE:
                    pass

        if file_changed:
            with open(self.envvar_file_path, 'w') as f:
                f.writelines(lines)
Beispiel #6
0
def write_shortcut(shortcut, username=None):
    '''
    Write the single shortcut file to the disk.

    :username: None means working with machine variables and paths
    '''
    dest_abspath = expand_windows_var(shortcut.dest, username).replace(
        '\\', '/') + '.desktop'

    # Check that we're working for user, not on global system level
    if username:
        # Check that link destination path starts with specification of
        # user's home directory
        if dest_abspath.startswith(get_homedir(username)):
            # Don't try to operate on non-existent directory
            if not homedir_exists(username):
                logging.warning(
                    slogm(
                        'No home directory exists for user {}: will not create link {}'
                        .format(username, dest_abspath)))
                return None

    logging.debug(slogm('Writing shortcut file to {}'.format(dest_abspath)))
    shortcut.write_desktop(dest_abspath)