aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2016-02-08 23:07:49 +0100
committerCamil Staps2016-02-08 23:07:49 +0100
commit434fc770b88764df382eefddcea76e81969f5ce2 (patch)
treed63039e3b3b6031cc54fc34690319746c9402b8f
Initial commit
-rwxr-xr-xcleantools/clim.py29
-rwxr-xr-xcleantools/clmgr.py107
-rw-r--r--cleantools/settings.py62
-rw-r--r--setup.py53
4 files changed, 251 insertions, 0 deletions
diff --git a/cleantools/clim.py b/cleantools/clim.py
new file mode 100755
index 0000000..6c227bf
--- /dev/null
+++ b/cleantools/clim.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+import argparse
+import subprocess
+
+from setuptools_scm import get_version
+
+import cleantools.clmgr
+import cleantools.settings
+
+def main():
+ parser = argparse.ArgumentParser(description='Improved clm')
+ parser.add_argument('--version', action='version', version=get_version())
+ parser.add_argument('-W', '--with', metavar='lib', nargs='*', dest='libs',
+ help='Add libraries installed with clmgr')
+ parser.add_argument('-clm', metavar='arg', type=str, nargs='*',
+ help='Extra clm arguments', required=False)
+
+ args = parser.parse_args()
+
+ libs = [] if args.libs == None else map(clmgr.Library.get_path, args.libs)
+ libs = [a for t in [('-I', l) for l in libs] for a in t]
+
+ clmargs = [] if args.clm == None else args.clm
+
+ subprocess.call(['clm'] + libs + clmargs)
+
+if __name__ == '__main__':
+ main()
+
diff --git a/cleantools/clmgr.py b/cleantools/clmgr.py
new file mode 100755
index 0000000..ec5764d
--- /dev/null
+++ b/cleantools/clmgr.py
@@ -0,0 +1,107 @@
+#!/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()
+
diff --git a/cleantools/settings.py b/cleantools/settings.py
new file mode 100644
index 0000000..67ecd3b
--- /dev/null
+++ b/cleantools/settings.py
@@ -0,0 +1,62 @@
+import configparser
+import os
+
+def expand_dir(d):
+ return os.path.expanduser(d)
+
+config_file = expand_dir('~/.clmgr.cfg')
+
+def make_install_dir(install_dir):
+ install_dir = expand_dir(install_dir)
+ if not os.path.exists(install_dir):
+ os.makedirs(install_dir)
+
+def init(install_dir):
+ install_dir = expand_dir(install_dir)
+
+ config = configparser.ConfigParser()
+ config.add_section('General')
+ config.set('General', 'install_dir', install_dir)
+ with open(config_file, 'w') as f:
+ config.write(f)
+
+ make_install_dir(install_dir)
+
+def read():
+ config = configparser.ConfigParser()
+ config.read(config_file)
+ return config
+
+def get_search_paths():
+ config = read()
+ paths = []
+
+ def try_append(path):
+ if path != None and os.path.isdir(path):
+ paths.append(path)
+
+ try:
+ try_append(config['General']['install_dir'])
+ except:
+ pass
+
+ try:
+ home = config['General']['clean_home']
+ try_append(home + '/lib')
+ except:
+ pass
+
+ return paths
+
+def print_config(conf, indent=0, indent_step=2):
+ conf = dict(conf)
+ for k, v in sorted(conf.items()):
+ if type(v) == configparser.SectionProxy:
+ if v.name == 'DEFAULT':
+ print_config(v, indent)
+ else:
+ print('{0}[{1}]'.format(' '*indent, v.name))
+ print_config(v, indent + indent_step, indent_step)
+ else:
+ print('{0}{1: <{f}} = {2}'.format(' '*indent, k, v, f=24-indent))
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..8afd4f1
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,53 @@
+from setuptools import setup
+
+setup(
+ name='clean-tools',
+ description='Clean tools',
+ long_description='Clean package manager and improved make script',
+
+ use_scm_version=True,
+ setup_requires=['setuptools_scm'],
+
+ url='https://github.com/camilstaps/clean-tools',
+
+ author='Camil Staps',
+ author_email='info@camilstaps.nl',
+
+ license='MIT',
+
+ classifiers=[
+ 'Development Status :: 3 - Alpha',
+
+ 'Intended Audience :: Developers',
+ 'Topic :: Software Development :: Build Tools',
+
+ 'License :: OSI Approved :: MIT License',
+
+ 'Natural Language :: English',
+
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 3 :: Only',
+ 'Programming Language :: Python :: 3.2',
+ 'Programming Language :: Python :: 3.3',
+ 'Programming Language :: Python :: 3.4',
+ 'Programming Language :: Python :: 3.5',
+
+ 'Operating System :: MacOS',
+ 'Operating System :: POSIX',
+ 'Operating System :: Unix',
+ ],
+
+ keywords='packaging library clean development',
+
+ packages=['cleantools'],
+
+ install_requires=['click', 'gitpython'],
+
+ entry_points={
+ 'console_scripts': [
+ 'clmgr = cleantools.clmgr:main',
+ 'clim = cleantools.clim:main',
+ ],
+ },
+)
+