From e1e1aaf0410cdc22e2129ee2dc45ed0d5b223226 Mon Sep 17 00:00:00 2001 From: skullY Date: Sun, 30 Jun 2019 20:03:14 -0700 Subject: [PATCH] Use flit to package instead of setuptools --- pyproject.toml | 33 +++++++++++++ qmk | 99 ++------------------------------------ qmk_cli/__init__.py | 3 ++ qmk_cli/doctor.py | 2 +- milc.py => qmk_cli/milc.py | 0 qmk_cli/script_qmk.py | 98 +++++++++++++++++++++++++++++++++++++ requirements.txt | 7 ++- setup.py | 43 ----------------- 8 files changed, 141 insertions(+), 144 deletions(-) create mode 100644 pyproject.toml rename milc.py => qmk_cli/milc.py (100%) create mode 100644 qmk_cli/script_qmk.py delete mode 100644 setup.py diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..438c7d5 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,33 @@ +[build-system] +requires = ["flit"] +build-backend = "flit.buildapi" + +[tool.flit.metadata] +dist-name = "qmk" +module = "qmk_cli" +author = "skullydazed" +author-email = "skullydazed@gmail.com" +home-page = "https://github.com/qmk/qmk_cli" +classifiers = [ + 'Development Status :: 3 - Alpha', + 'Environment :: Console', + 'Intended Audience :: Developers', + 'Intended Audience :: System Administrators', + 'Intended Audience :: End Users/Desktop', + 'License :: OSI Approved :: MIT License', + 'Natural Language :: English', + 'Programming Language :: Python :: 3 :: Only', + 'Topic :: Scientific/Engineering', + 'Topic :: Software Development', + 'Topic :: Utilities', +] +requires-python = ">=3.5" +requires = [ + #"milc", #FIXME(skullydazed): Included in the repo for now. + "argcomplete", + "colorama", + #"halo" +] + +[tool.flit.scripts] +qmk = "qmk_cli.script_qmk:main" diff --git a/qmk b/qmk index f3c6c37..92c039f 100755 --- a/qmk +++ b/qmk @@ -1,98 +1,5 @@ #!/usr/bin/env python3 -"""CLI wrapper for running QMK commands. - -This program can be run from anywhere, with or without a qmk_firmware checkout. It provides a small set of subcommands for working with QMK and otherwise dispatches to the `qmk_firmware/bin/qmk` script for the repo you are currently in, or your default repo if you are not currently in a qmk_firmware checkout. +"""Wrapper to call the qmk cli script entrypoint. """ -import argparse -import os -import subprocess -import sys -from glob import glob -from time import strftime -from functools import lru_cache -from importlib import import_module -from importlib.util import find_spec -from pathlib import Path - - -@lru_cache(maxsize=2) -def in_qmk_firmware(): - """Returns the path to the qmk_firmware we are currently in, or None if we are not inside qmk_firmware. - """ - cur_dir = Path.cwd() - while len(cur_dir.parents) > 0: - found_bin = cur_dir / 'bin' / 'qmk' - if found_bin.is_file(): - command = [found_bin, '--version'] - result = subprocess.run(command) - - if result.returncode == 0: - return cur_dir - - # Move up a directory before the next iteration - cur_dir = cur_dir / '..' - cur_dir = cur_dir.resolve() - - -def find_qmk_firmware(): - """Look for qmk_firmware in the usual places. - - This function returns the path to qmk_firmware, or the default location if one does not exist. - - FIXME(skullydazed): add config file support - """ - if in_qmk_firmware(): - return in_qmk_firmware() - - if 'QMK_HOME' in os.environ: - return Path(os.environ['QMK_HOME']) - - return Path.home() / 'qmk_firmware' - - -def parse_args(): - """Process arguments outside milc. - """ - parser = argparse.ArgumentParser(description='CLI wrapper for running QMK commands.') - parser.add_argument('-H', '--home', help='Path to the qmk_firmware directory.') - parser.add_argument('subcommand', help='Subcommand to run') - parser.add_argument('subcommand_args', nargs=argparse.REMAINDER, help='Arguments to pass to the subcommand.') - args = parser.parse_args() - - if args.home: - os.environ['QMK_HOME'] = args.home - - return (args.subcommand, args.subcommand_args) - - -def main(): - subcommand, subcommand_args = parse_args() - subcommand_module = 'qmk_cli.' + subcommand - sys.argv = ['qmk-'+subcommand] + subcommand_args[1:] - qmk_firmware = find_qmk_firmware() - qmk_bin = qmk_firmware / 'bin' / 'qmk' - os.environ['QMK_HOME'] = str(qmk_firmware) - - try: - # Attempt to import the subcommand from qmk_cli first - import milc - import_module(subcommand_module) - milc.cli() - - except ModuleNotFoundError as e: - # Check to make sure there's not a bad import statement in qmk_cli - if e.name != subcommand_module: - raise - - # Dispatch to the underlying `qmk_firmware/bin/qmk` - if qmk_bin.is_file() and os.access(str(qmk_bin), os.X_OK): - argv = ['python3', str(qmk_bin)] + subcommand_args - os.execvp('python3', argv) - - # Tell the user we can't continue - print('*** Could not locate qmk_firmware directory!') - exit(255) - - -if __name__ == '__main__': - main() +import qmk_cli.script_qmk +qmk_cli.script_qmk.main() diff --git a/qmk_cli/__init__.py b/qmk_cli/__init__.py index e69de29..193e13f 100644 --- a/qmk_cli/__init__.py +++ b/qmk_cli/__init__.py @@ -0,0 +1,3 @@ +"""A program to help you work with qmk_firmware.""" + +__version__ = '0.0.1' diff --git a/qmk_cli/doctor.py b/qmk_cli/doctor.py index e6778cb..ace05fe 100755 --- a/qmk_cli/doctor.py +++ b/qmk_cli/doctor.py @@ -7,7 +7,7 @@ import platform import os from pathlib import Path -from milc import cli +from qmk_cli.milc import cli def check_qmk_firmware(): diff --git a/milc.py b/qmk_cli/milc.py similarity index 100% rename from milc.py rename to qmk_cli/milc.py diff --git a/qmk_cli/script_qmk.py b/qmk_cli/script_qmk.py new file mode 100644 index 0000000..fe37674 --- /dev/null +++ b/qmk_cli/script_qmk.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +"""CLI wrapper for running QMK commands. + +This program can be run from anywhere, with or without a qmk_firmware checkout. It provides a small set of subcommands for working with QMK and otherwise dispatches to the `qmk_firmware/bin/qmk` script for the repo you are currently in, or your default repo if you are not currently in a qmk_firmware checkout. +""" +import argparse +import os +import subprocess +import sys +from glob import glob +from time import strftime +from functools import lru_cache +from importlib import import_module +from importlib.util import find_spec +from pathlib import Path + + +@lru_cache(maxsize=2) +def in_qmk_firmware(): + """Returns the path to the qmk_firmware we are currently in, or None if we are not inside qmk_firmware. + """ + cur_dir = Path.cwd() + while len(cur_dir.parents) > 0: + found_bin = cur_dir / 'bin' / 'qmk' + if found_bin.is_file(): + command = [found_bin, '--version'] + result = subprocess.run(command) + + if result.returncode == 0: + return cur_dir + + # Move up a directory before the next iteration + cur_dir = cur_dir / '..' + cur_dir = cur_dir.resolve() + + +def find_qmk_firmware(): + """Look for qmk_firmware in the usual places. + + This function returns the path to qmk_firmware, or the default location if one does not exist. + + FIXME(skullydazed): add config file support + """ + if in_qmk_firmware(): + return in_qmk_firmware() + + if 'QMK_HOME' in os.environ: + return Path(os.environ['QMK_HOME']) + + return Path.home() / 'qmk_firmware' + + +def parse_args(): + """Process arguments outside milc. + """ + parser = argparse.ArgumentParser(description='CLI wrapper for running QMK commands.') + parser.add_argument('-H', '--home', help='Path to the qmk_firmware directory.') + parser.add_argument('subcommand', help='Subcommand to run') + parser.add_argument('subcommand_args', nargs=argparse.REMAINDER, help='Arguments to pass to the subcommand.') + args = parser.parse_args() + + if args.home: + os.environ['QMK_HOME'] = args.home + + return (args.subcommand, args.subcommand_args) + + +def main(): + subcommand, subcommand_args = parse_args() + subcommand_module = 'qmk_cli.' + subcommand + sys.argv = ['qmk-'+subcommand] + subcommand_args[1:] + qmk_firmware = find_qmk_firmware() + qmk_bin = qmk_firmware / 'bin' / 'qmk' + os.environ['QMK_HOME'] = str(qmk_firmware) + + try: + # Attempt to import the subcommand from qmk_cli first + import qmk_cli.milc + import_module(subcommand_module) + qmk_cli.milc.cli() + + except ModuleNotFoundError as e: + # Check to make sure there's not a bad import statement in qmk_cli + if e.name != subcommand_module: + raise + + # Dispatch to the underlying `qmk_firmware/bin/qmk` + if qmk_bin.is_file() and os.access(str(qmk_bin), os.X_OK): + argv = ['python3', str(qmk_bin), subcommand] + subcommand_args + os.execvp('python3', argv) + + # Tell the user we can't continue + print('*** Could not locate qmk_firmware directory!') + exit(255) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt index 0c73d8b..cd8c47d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -# milc FIXME(skullydazed): Included in the repo for now. -argcomplete -colorama -#halo +--index-url https://pypi.python.org/simple/ + +-e . diff --git a/setup.py b/setup.py deleted file mode 100644 index 6a823d1..0000000 --- a/setup.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python -# Special thanks to Hynek Schlawack for providing excellent documentation: -# -# https://hynek.me/articles/sharing-your-labor-of-love-pypi-quick-and-dirty/ -import os -from setuptools import setup, find_packages, Command - - -def read(*paths): - """Build a file path from *paths* and return the contents.""" - with open(os.path.join(*paths), 'r') as f: - return f.read() - - -setup( - name='qmk', - version='0.0.1', - description='Program to help you work with qmk_firmware.', - long_description=read('README.md'), - long_description_content_type='text/markdown', - url='https://github.com/qmk/qmk_cli', - license='MIT License', - author='skullydazed', - author_email='skullydazed@gmail.com', - install_requires=['argcomplete', 'colorama'], - packages=find_packages(), - py_modules=['milc'], - scripts=['qmk'], - include_package_data=True, - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Console', - 'Intended Audience :: Developers', - 'Intended Audience :: System Administrators', - 'Intended Audience :: End Users/Desktop', - 'License :: OSI Approved :: MIT License', - 'Natural Language :: English', - 'Programming Language :: Python :: 3 :: Only', - 'Topic :: Scientific/Engineering', - 'Topic :: Software Development', - 'Topic :: Utilities', - ], -)