Example #1
0
def main(opts):
    try:
        # Load a context with initialization
        ctx = Context.load(opts.workspace, load_env=False)

        if not ctx.initialized():
            print(
                "A catkin workspace must be initialized before profiles can be managed."
            )
            return 1

        profiles = get_profile_names(ctx.workspace)
        active_profile = get_active_profile(ctx.workspace)

        if opts.subcommand == 'list':
            print(
                list_profiles(profiles,
                              active_profile,
                              unformatted=opts.unformatted,
                              active=opts.active))

        elif opts.subcommand == 'add':
            if opts.name in profiles:
                if opts.force:
                    print(
                        clr('[profile] @{yf}Warning:@| Overwriting existing profile named @{cf}%s@|'
                            % (opts.name)))
                else:
                    print(
                        clr('catkin profile: error: A profile named '
                            '@{cf}%s@| already exists. Use `--force` to '
                            'overwrite.' % (opts.name)))
                    return 1
            if opts.copy_active:
                ctx.profile = opts.name
                Context.save(ctx)
                print(
                    clr('[profile] Created a new profile named @{cf}%s@| '
                        'based on active profile @{cf}%s@|' %
                        (opts.name, active_profile)))
            elif opts.copy:
                if opts.copy in profiles:
                    new_ctx = Context.load(opts.workspace, profile=opts.copy)
                    new_ctx.profile = opts.name
                    Context.save(new_ctx)
                    print(
                        clr('[profile] Created a new profile named @{cf}%s@| '
                            'based on profile @{cf}%s@|' %
                            (opts.name, opts.copy)))
                else:
                    print(
                        clr('[profile] @{rf}A profile with this name does not exist: %s@|'
                            % opts.copy))
            else:
                new_ctx = Context(workspace=ctx.workspace, profile=opts.name)
                Context.save(new_ctx)
                print(
                    clr('[profile] Created a new profile named @{cf}%s@| with default settings.'
                        % (opts.name)))

            profiles = get_profile_names(ctx.workspace)
            active_profile = get_active_profile(ctx.workspace)
            print(list_profiles(profiles, active_profile))

        elif opts.subcommand == 'set':
            if opts.name in profiles:
                set_active_profile(ctx.workspace, opts.name)

                active_profile = get_active_profile(ctx.workspace)
                print(
                    clr('[profile] Activated catkin metadata profile: @{cf}%s@|'
                        % active_profile))
            else:
                print(
                    'catkin profile: error: Profile `%s` does not exist in workspace `%s`.'
                    % (opts.name, ctx.workspace))
                return 1

            profiles = get_profile_names(ctx.workspace)
            active_profile = get_active_profile(ctx.workspace)
            print(list_profiles(profiles, active_profile))

        elif opts.subcommand == 'rename':
            if opts.current_name in profiles:
                if opts.new_name in profiles:
                    if opts.force:
                        print(
                            clr('[profile] @{yf}Warning:@| Overwriting '
                                'existing profile named @{cf}%s@|' %
                                (opts.new_name)))
                    else:
                        print(
                            clr('catkin profile: error: A profile named '
                                '@{cf}%s@| already exists. Use `--force` to '
                                'overwrite.' % (opts.new_name)))
                        return 1
                ctx.profile = opts.new_name
                Context.save(ctx)
                remove_profile(ctx.workspace, opts.current_name)
                if opts.current_name == active_profile:
                    set_active_profile(ctx.workspace, opts.new_name)
                print(
                    clr('[profile] Renamed profile @{cf}%s@| to @{cf}%s@|' %
                        (opts.current_name, opts.new_name)))
            else:
                print(
                    'catkin profile: error: Profile `%s` does not exist in workspace `%s`.'
                    % (opts.current_name, ctx.workspace))
                return 1

            profiles = get_profile_names(ctx.workspace)
            active_profile = get_active_profile(ctx.workspace)
            print(list_profiles(profiles, active_profile))

        elif opts.subcommand == 'remove':
            for name in opts.name:
                if name == active_profile:
                    print(
                        'Profile `%s` is currently active. Re-setting active profile to `%s`.'
                        % (name, DEFAULT_PROFILE_NAME))
                    set_active_profile(ctx.workspace, DEFAULT_PROFILE_NAME)

                if name in profiles:
                    remove_profile(ctx.workspace, name)
                else:
                    print(
                        'catkin profile: error: Profile `%s` does not exist in workspace `%s`.'
                        % (name, ctx.workspace))
                    return 1

                print(clr('[profile] Removed profile: @{rf}%s@|' % name))
            profiles = get_profile_names(ctx.workspace)
            active_profile = get_active_profile(ctx.workspace)
            print(list_profiles(profiles, active_profile))

    except IOError as exc:
        # Usually happens if workspace is already underneath another catkin_tools workspace
        print('error: could not %s catkin profile: %s' %
              (opts.subcommand, exc.message))
        return 1

    return 0