#!/usr/bin/env python3 import os import click from git import Repo from setuptools_scm import get_version import cleantools.settings class Library(): """A Clean library""" def __init__(self, path): self.path = Library.get_path(path) @staticmethod def get_path(lib): """Get the path of a library in the installation dir""" install_dir = settings.read()['General']['install_dir'] for path in settings.get_search_paths(): if os.path.isdir(path + '/' + lib): return path + '/' + lib raise ValueError('\'%s\': no such library' % lib) @staticmethod def clone(repo, local_dir): """New library by cloning a repo to the installation dir""" install_dir = settings.read()['General']['install_dir'] settings.make_install_dir(install_dir) Repo.clone_from(repo, install_dir + '/' + local_dir) return Library(local_dir) @click.group() def cli(): pass @cli.command('version') def cli_version(): print(get_version()) @cli.command('init') @click.argument('install_dir', type=click.Path(), metavar='install_dir', default='~/.clmgr') def cli_init(install_dir): """Initialise the installation directory.""" settings.init(install_dir) @cli.command('clone') @click.option('--abs/--github', help='Use an absolute path (default: use GitHub)') @click.argument('repo', metavar='repo') @click.argument('local_dir', metavar='local_dir', required=False) def cli_clone(abs, repo, local_dir): """Clone a repository. If local_dir is empty, the last part of repo will be used. Examples: \b clmgr clone username/MyLibrary (local_dir will be MyLibrary) clmgr clone --abs /var/git/my-library LocalName""" if local_dir == None: local_dir = list(filter(lambda x: len(x)>0, repo.split('/')))[-1] click.echo('No local_dir given, using \'%s\'' % local_dir, err=True) if abs: Library.clone(repo, local_dir) else: Library.clone('https://github.com/' + repo, local_dir) @cli.command('echo') @click.option('--indent/--no-indent', help='Indent hierarchically') @click.argument('what', metavar='what', default='') def cli_echo(indent, what): """Give information about settings. Examples: \b clmgr echo clmgr echo --indent clmgr echo General.install_dir Magic examples: \b clmgr echo search_path""" if what == 'search_path': click.echo('\n'.join(settings.get_search_paths())) else: settings_path = what.split('.') config = dict(settings.read()) for name in [p for p in settings_path if p != '']: try: config = dict(config[name]) except KeyError: click.echo('No setting \'%s\'' % name, err=True) return settings.print_config(config, indent_step=2 if indent else 0) def main(): cli() if __name__ == '__main__': main()