Example #1
0
    def push(self, force_push, only_update_config):

        # print('Pushing command: {command_name}'.format(command_name=self.command_name))

        self.init_local_repo()

        if self.command_file_destination_path.exists():
            self.command_already_created = True
            print('File: {file_path} already exists.'.format(file_path=self.command_file_destination_path_string))

        else:
            self.command_already_created = False

        copy_file(
            join_paths(ConfigUtil().user_commands_directory, self.command_file_string_path),
            join_paths(ConfigUtil().user_lk_dir, self.local_repo_commands_dir_string_path)
        )

        if self.changes_exist():
            self.show_git_status()
            self.commit_changes()
            self.push_changes()
            self.update_commands_config()
        elif force_push:
            self.push_changes()
            self.update_commands_config()
        elif only_update_config:
            self.update_commands_config()
        else:
            print('No changes in commands repo.')
Example #2
0
    def get_commands(self):

        click_commands = {}

        commands_dirs = [ConfigUtil().user_commands_directory, ConfigUtil().core_commands_directory]

        for command_dir in commands_dirs:

            for file_name in os.listdir(command_dir):

                if file_name.endswith('.py') and file_name != '__init__.py':

                    args = {}

                    file_path = os.path.join(command_dir, file_name)

                    with open(file_path) as _file:
                        code = compile(_file.read(), file_name, 'exec')
                        eval(code, args, args)

                    command = args['cli']
                    click_commands[command.name] = command

        lk_commands = {}

        for command_name, click_command in click_commands.iteritems():
            lk_command = Command(command_name)
            lk_commands[command_name] = lk_command

        return lk_commands
Example #3
0
    def __init__(self):

        # self.lk_config_dir_string = str(Path.home()) + '/' + '.config/lk'
        self.lk_config_dir_string = ConfigUtil().lk_config_dir
        self.lk_config_dir_path = Path(self.lk_config_dir_string)

        # self.lk_config_file_path_string = self.lk_config_dir_string + '/' + 'lk_config.yaml'
        self.lk_config_file_path_string = ConfigUtil().user_lk_config_file_path
        self.lk_config_file_path = Path(self.lk_config_file_path_string)
Example #4
0
File: cli.py Project: eyalev/lk
    def __init__(self, **kwargs):

        MultiCommand.__init__(self, **kwargs)

        self.user_commands_dir = ConfigUtil().user_commands_directory
        self.core_commands_dir = ConfigUtil().core_commands_directory

        self.commands = self._init_commands()
        # self.commands = self._init_commands_new()

        self._repo = None
Example #5
0
    def init_file_path(self, file_path_param, name):

        if name:
            file_path = ConfigUtil().lk_dir + '/env/{name}.json'.format(
                name=name)

        elif file_path_param is None:
            file_path = ConfigUtil().lk_dir + '/env/env.json'

        else:
            file_path = file_path_param

        return file_path
Example #6
0
    def path_object(self):

        user_commands_dir_path = Path(ConfigUtil().user_commands_directory)

        command_path = user_commands_dir_path.joinpath(self.file_name)

        return command_path
Example #7
0
def cli(name):

    print('# Adding dev command: ' + name)

    template = """

import click


@click.command('{command_name}')
def cli():

    print('Starting command: {command_name}')

    print('Finished command: {command_name}')

"""

    command_source = template.format(command_name=name)

    command_path = '{commands_directory}/{command_name}_command.py'.format(
        commands_directory=ConfigUtil().dev_commands_directory,
        command_name=name.replace('-', '_'))

    Path(command_path).write_text(command_source.decode('utf-8'))

    print('# Command path: ' + command_path)

    print('# Finished adding dev command: ' + name)
Example #8
0
    def repo_command_path(self):

        repos_path = ConfigUtil().local_repos_dir_path

        file_name = self.file_name

        import fnmatch
        import os

        matches = []
        for root, dirnames, filenames in os.walk(repos_path):
            for filename in fnmatch.filter(filenames, file_name):
                matches.append(os.path.join(root, filename))

        if len(matches) > 1:
            print_lines(matches)
            raise ValueError('More than one repo found for file {file}'.format(file=file_name))

        elif len(matches) == 0:
            raise ValueError('No repo found for file {file}'.format(file=file_name))

        elif len(matches) == 1:
            return matches[0]

        else:
            raise NotImplementedError
Example #9
0
    def yaml_path_object(self):

        commands_config_file_path = ConfigUtil().commands_config_yaml_file_path

        commands_config_path_object = Path(commands_config_file_path)

        return commands_config_path_object
Example #10
0
    def local_repo_string_path(self):

        commands_repo_local_path = '{local_repos_dir}/{repo_service}/{repo_user}/{commands_repo_name}'.format(
            local_repos_dir=ConfigUtil().local_repos_dir,
            repo_service=self.hosting_service,
            repo_user=self.user,
            commands_repo_name=self.repo_name)

        return commands_repo_local_path
Example #11
0
    def create_config_if_needed(self):

        self.yaml_file.touch()
        self.json_file.touch()

        if not self.odict:

            odict_object = odict()
            odict_object[commands_dir_key] = ConfigUtil().user_commands_directory
            odict_object[commands_key] = odict()

            self.update_config(odict_object=odict_object)
Example #12
0
    def repo(self):

        """
        :rtype: SourceCodeRepo
        """

        file_name = self.file_name

        repos_path = ConfigUtil().local_repos_dir_path

        import fnmatch
        import os

        matches = []
        for root, dirnames, filenames in os.walk(repos_path):
            for filename in fnmatch.filter(filenames, file_name):
                matches.append(os.path.join(root, filename))

        if len(matches) > 1:
            print_lines(matches)
            raise ValueError('More than one repo found for file {file}'.format(file=file_name))

        elif len(matches) == 0:
            raise ValueError('No repo found for file {file}'.format(file=file_name))

        elif len(matches) == 1:

            match = matches[0]
            split_list = match.split('/')
            repos_index = split_list.index(app_config.local_repos_dir)
            new_list = split_list[repos_index + 1:]
            service = new_list[0]
            user = new_list[1]
            repo_name = new_list[2]

            repo = SourceCodeRepo(
                service=service,
                user=user,
                repo_name=repo_name
            )
            return repo

        else:
            raise NotImplementedError
Example #13
0
    def path(self):

        # https://github.com/lk-commands/default

        repo_service = self.remote_commands_repo_url.split('/')[2].split('.')[0]

        repo_user = self.remote_commands_repo_url.split('/')[3]

        commands_repo_name = self.remote_commands_repo_url.split('/')[4]

        commands_repo_local_rel_path = '{local_repos_dir}/{repo_service}/{repo_user}/{commands_repo_name}'.format(
            local_repos_dir=ConfigUtil().local_repos_dir,
            repo_service=repo_service,
            repo_user=repo_user,
            commands_repo_name=commands_repo_name
        )

        commands_repo_local_path = full_path(commands_repo_local_rel_path)

        return commands_repo_local_path
Example #14
0
def cli(command, content):

    print('# Creating shell command: ' + command)

    if content:
        shell_command_to_run = content
    else:
        shell_command_to_run = click.prompt('# Enter shell command')

    if shell_command_to_run.endswith("'"):
        quotes = '"""'
    else:
        quotes = "'''"

    template = """

import click

from lk.utils.shell_util import run_and_print


@click.command('{command_name}')
def cli():

    run_and_print({quotes}{command_to_run}{quotes})
"""

    command_source = template.format(command_name=command,
                                     command_to_run=shell_command_to_run,
                                     quotes=quotes)

    command_path = '{commands_directory}/{command_name}_command.py'.format(
        commands_directory=ConfigUtil().user_commands_directory,
        command_name=command.replace('-', '_'))

    # import pdb; pdb.set_trace()
    # Path(command_path).write_text(unicode(command_source))
    Path(command_path).write_text(command_source.decode("utf-8"))

    print('# Finished creating shell command: ' + command)
Example #15
0
    def delete_with_backup(self):

        now_datetime_string = DatetimeUtil().now_iso_string()
        new_suffix = '.{timestamp}'.format(timestamp=now_datetime_string)
        new_file_name = self.file_name + new_suffix

        home_path = ConfigUtil().user_home_path
        tmp_dir = join_paths(home_path, 'tmp')

        new_file_path = join_paths(tmp_dir, new_file_name)

        if self.exists():
            try:
                self.path_object.rename(new_file_path)
            except OSError as e:
                cli_print(e.message)
                cli_print('Trying with sudo')
                command = 'sudo mv {source_path} {target_path}'.format(
                    source_path=self.path, target_path=new_file_path)
                run(command)

        else:
            cli_print("File doesn't exists.")
Example #16
0
import os

import click
from lk.classes.multi_command import LKMultiCommand

from lk.utils.config_util import ConfigUtil

commands_dir = os.path.join(ConfigUtil().lk_dir,
                            'commands/core_commands/group1/commands')


@click.command('group1', cls=LKMultiCommand, commands_dir=commands_dir)
def cli():

    print('Starting command: group1')

    print('Finished command: group1')
Example #17
0
    def json_file_path(self):

        commands_config_json_file_path = ConfigUtil().commands_config_json_file_path
        return commands_config_json_file_path
Example #18
0
    def path(self):

        path = ConfigUtil().user_lk_config_file_path

        return path
Example #19
0
 def lk_config_file_path_string(self):
     lk_config_file_path_string = ConfigUtil().user_lk_config_file_path
     return lk_config_file_path_string
Example #20
0
    def lk_config_dir_string(self):

        lk_config_dir_string = ConfigUtil().lk_config_dir

        return lk_config_dir_string