Ejemplo n.º 1
0
def init_sync_project_directory(project_id):
    """
    Initial sync of project dir and database.

    :param project_id: Project id from database.
    :type project_id: Integer

    """
    log_msg = 'init_sync_project_directory project ID = {project_id}'
    log_msg = log_msg.format(project_id=project_id)
    _logger().debug(log_msg)
    project = Project.get_by_id(project_id)
    if not project:
        return
    project_files = File.get('all', File.project_id == project_id)
    project_filepaths = [f.path for f in project_files]
    try:
        for (root, directories, basenames) in os.walk(project.dir):
            for filepath in [os.path.join(root, n) for n in basenames]:
                if filepath in project_filepaths:
                    # Simultaneously remove the element from both lists.
                    index = project_filepaths.index(filepath)
                    project_files.pop(index)
                    project_filepaths.pop(index)
                else:
                    # Add the new file to project.
                    add_file_to_project(filepath, project_id)
        # project_files now only contains the files that have been deleted!
        for file_ in project_files:
            deleted = REVERSE_ACTIONS['Deleted']
            try:
                controller.create_file_action(file_.path, deleted, 0,
                                              project_id)
            except Exception as excp:
                _logger().exception('{0!s}'.format(excp))
            file_.project_id = None
        File.update_many(project_files)
    except OSError as excp:
        log_msg = ('Exception in Initial project directory synchronization '
                   'call: {exception!s}')
        log_msg = log_msg.format(exception=excp)
        _logger().exception(log_msg)