def inspect(args):
    '''Print the body of the command'''

    # sanity check
    util.verify_lib_dir()

    # remember the current directory
    curr_dir = os.getcwd()

    # go to the directory containing all shell files
    os.chdir(util.lib_dir())

    # try to print the body of each command
    for command in args.command:
        print('')
        if os.path.exists(command + '.sh'):
            print('# Contents for \'' + command + '\' (' + os.path.abspath(command + '.sh') + ')\n')
            # the actual contents of the shell file
            lines = ''
            with open(command + '.sh', mode='r', encoding='utf-8') as f:
                for line in f.readlines():
                    lines = lines + '    ' + line
            # show a message if the file is empty
            if lines:
                print(lines)
            else:
                print('    ### The body for the command \'' + command + '\' has not been defined yet ###')
        else:
            print ('# File not found for \'' + command + '\' (' + os.path.abspath(command + '.sh') + ')')
    
    # go back to the original directory
    os.chdir(curr_dir)
Beispiel #2
0
def create_entry_point(command):
    '''Create an empty shell file in the 'lib' directory for the command's body'''

    # remember the current directory
    curr_dir = os.getcwd()
    # go to the root directory where shell files exist for the manager
    os.chdir(util.lib_dir())
    # attempt to write the file
    file_name = util.shell_file_name(command)
    if not os.path.exists(file_name):
        with open(file_name, mode='w', encoding='utf-8') as f:
            print('#')
            print('# A new entry point file has been created.')
            print('# The contents of this file will be used as the body')
            print('# of a function that will be called every time you')
            print('# invoke \'' + command + '\' from the command line.')
            print('# You may create files in the \'public\' directory and')
            print('# reference them as needed.')
            print('#')
            print('# Entry point --> ' + os.path.abspath(file_name))
            print('#')
    else:
        print('Can\'t write over file: ' + file_name)
        exit(1)
    # return to the current directory
    os.chdir(curr_dir)
Beispiel #3
0
def rm(args):
    '''Remove a command from the manager'''

    #sanity check
    util.verify_lib_dir()
    util.verify_metadata_dir()

    # remember the current directory
    curr_dir = os.getcwd()

    # iterate through the commands to remove
    for command in args.command:
        # first from metadata
        metadata_file_name = util.metadata_file_name(command)
        os.chdir(util.metadata_dir())
        if os.path.exists(metadata_file_name):
            os.remove(metadata_file_name)
        else:
            print('File not found: ' + metadata_file_name)
        # then from lib
        shell_file_name = util.shell_file_name(command)
        os.chdir(util.lib_dir())
        if os.path.exists(shell_file_name):
            os.remove(shell_file_name)
        else:
            print('File not found: ' + shell_file_name)

    # regenerate the __functions__.sh file
    util.repl_it(['load', '--quiet'])
Beispiel #4
0
def load(args):
    '''Genereate the '__functions.sh__' file with all declared commands.'''

    # sanity check
    util.verify_lib_dir()
    util.verify_internal_dir()

    # remember the current directory
    curr_dir = os.getcwd()

    # go to the directory with only internal resources
    os.chdir(util.internal_dir())

    # attempt to extract all contents from shell files and generate a '__functions.sh__' file
    with open('__functions__.sh', mode='w', encoding='utf-8') as func_file:
        # add all aliases (if any)
        with open('__aliases__.sh', mode='r',
                  encoding='utf-8') as aliases_file:
            func_file.write('# aliases defined through clmanager\n')
            func_file.writelines(aliases_file.readlines())
            func_file.write('\n')

        # add all defined functions
        functions = os.listdir(util.lib_dir())
        os.chdir(util.lib_dir())
        for f in functions:
            with open(f, mode='r', encoding='utf-8') as body_file:
                body = body_file.readlines()
                func_file.write('# function added for clmanager\n')
                func_file.write(
                    util.format_function(os.path.splitext(f)[0], body))
                func_file.write('\n\n')

    # if there's no quiet flag print a summary of the changes
    if not args.quiet:
        os.chdir(util.internal_dir())
        print('#')
        print('# The \'__functions__.sh\' file has been regenerated.')
        print('#')
        print('# Run the following command to load the functions:')
        print('#')
        print('# \tsource ' + os.path.abspath('__functions__.sh'))

    # go back to the original directory
    os.chdir(curr_dir)
Beispiel #5
0
def edit(args):
    '''Edit an existing command in the manager'''

    # sanity check
    util.verify_metadata_dir()
    util.verify_lib_dir()

    # remember the current directory
    curr_dir = os.getcwd()

    # at the moment args.command should always be an array of one element
    for command in args.command:
        # make sure we are on the right directory
        os.chdir(util.metadata_dir())
        # the name of the metadata file
        metadata_file_name = util.metadata_file_name(command)
        # verify that the file actually exists
        if os.path.exists(metadata_file_name):
            print('# Editing ' + metadata_file_name)
            # the metadata needed
            name = None
            file_command = None
            description = None
            # extract any info available in flags
            if args.name:
                name = args.name
            if args.command:
                file_command = args.command_name
            if args.description:
                description = args.description
            # open the file to update to read existing contents and prompt for new values
            with open(metadata_file_name, mode='r', encoding='utf-8') as f:
                yaml_file = yaml.full_load(f)
                try:
                    if not name:
                        name = input('Name (' + yaml_file['name'] + '): ')
                    if not file_command:
                        file_command = input('Command name (' +
                                             yaml_file['command'] + '): ')
                    if not description:
                        description_postfix = ''
                        if len(yaml_file['description']) > 10:
                            description_postfix = '...'
                        description = input('Description (' +
                                            yaml_file['description'][:10] +
                                            description_postfix + '): ')
                    # only save values that are not empty strings
                    if not name.strip():
                        name = yaml_file['name']
                    if not file_command.strip():
                        file_command = yaml_file['command']
                    if not description.strip():
                        description = yaml_file['description']
                except EOFError:
                    exit(2)

            # attempt to rewrite the metadata file
            with open(metadata_file_name, mode='w', encoding='utf-8') as f:
                yaml.dump(
                    {
                        'name': name,
                        'command': file_command,
                        'description': description
                    }, f)

            # rename the file to match the new command name
            os.rename(metadata_file_name,
                      util.metadata_file_name(file_command))

            # go to the directory with all shell files
            os.chdir(util.lib_dir())
            # rename the corresponding shell file to match the new command name
            os.rename(util.shell_file_name(command),
                      util.shell_file_name(file_command))

            # print a summary message of the changes
            note = ''
            if command != file_command:
                note = ' (now ' + file_command + ')'
            print('#')
            print('# The metadata for \'' + command + '\'' + note +
                  ' has been updated.')
            print('#')
            print('# To modify the command itself, edit:')
            print('#')
            print('#\t' + os.path.abspath(file_command + '.sh'))

    # return to the original directory
    os.chdir(curr_dir)