From bf56a97251d0f5e5e84910a56e4c8e3036d6b057 Mon Sep 17 00:00:00 2001 From: James Edwards Date: Sat, 3 Feb 2024 11:27:03 -0700 Subject: [PATCH 1/2] working on poetry --- pyproject.toml | 28 ++++++++++++ setup.cfg | 8 ---- setup.py | 55 ----------------------- src/{fleximod => }/__init__.py | 0 src/classes/__init__.py | 0 src/{fleximod => classes}/gitinterface.py | 2 +- src/{fleximod => classes}/gitmodules.py | 2 +- src/{fleximod => classes}/lstripreader.py | 0 src/{fleximod => classes}/utils.py | 0 src/{fleximod => classes}/version.py | 0 src/git-fleximod | 11 ++--- tbump.toml | 43 ++++++++++++++++++ tests/__init__.py | 3 ++ tests/test_import.py | 9 ++++ 14 files changed, 91 insertions(+), 70 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.cfg delete mode 100644 setup.py rename src/{fleximod => }/__init__.py (100%) create mode 100644 src/classes/__init__.py rename src/{fleximod => classes}/gitinterface.py (98%) rename src/{fleximod => classes}/gitmodules.py (98%) rename src/{fleximod => classes}/lstripreader.py (100%) rename src/{fleximod => classes}/utils.py (100%) rename src/{fleximod => classes}/version.py (100%) create mode 100644 tbump.toml create mode 100644 tests/__init__.py create mode 100644 tests/test_import.py diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000000..28e0e35a09 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,28 @@ +[tool.poetry] +name = "git-fleximod" +version = "0.3.0" +description = "Extended support for git-submodule and git-sparse-checkout" +authors = ["Jim Edwards "] +maintainers = ["Jim Edwards "] +license = "MIT" +readme = "README.md" +homepage = "https://github.com/jedwards4b/git-fleximod" +keywords = ["git", "submodule", "sparse-checkout"] +packages = [ + { include = "src/git-fleximod" }, + { include = "src/classes/*.py" }, + { include = "License" }, + { include = "tests" }, + +] + +[tool.poetry.dependencies] +python = "^3.8" +GitPython = "^3.1.0" + +[tools.poetry.urls] +"Bug Tracker" = "https://github.com/jedwards4b/git-fleximod/issues" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 66ff24ada6..0000000000 --- a/setup.cfg +++ /dev/null @@ -1,8 +0,0 @@ -[metadata] -license_file = License -[build_manpages] -manpages = - # solution 1: -- use parser function - man/git-fleximod.1:function=get_parser:pyfile=src/git-fleximod - # solution 2 -- make parser global - #man/git-fleximod.2:pyfile=src/git-fleximod:object=parser diff --git a/setup.py b/setup.py deleted file mode 100644 index 048db75957..0000000000 --- a/setup.py +++ /dev/null @@ -1,55 +0,0 @@ -import sys -import os - -sys.path.insert(0, os.path.join(os.getenv("CONDA_PREFIX"), "lib", "python3.12")) -sys.path.insert( - 0, os.path.join(os.getenv("CONDA_PREFIX"), "lib", "python3.12", "site-packages") -) - -import setuptools -from setuptools import find_packages, convert_path -from build_manpages import build_manpages, get_build_py_cmd, get_install_cmd - -main_ns = {} -ver_path = convert_path( - os.path.join(os.path.dirname(__file__), "src", "fleximod", "version.py") -) -print(f"ver_path is {ver_path}") -with open(ver_path) as ver_file: - exec(ver_file.read(), main_ns) - -if __name__ == "__main__": - with open("README.md", "r") as fh: - long_description = fh.read() - - setuptools.setup( - name="git-fleximod", # package name - scripts=["src/git-fleximod"], # This is the name of the package - version=main_ns["__version__"], # The initial release version - author="Jim Edwards", # Full name of the author - maintainer="jedwards4b", - license="MIT License", - description="Extended support for git-submodule and git-sparse-checkout", - long_description=long_description, # Long description read from the the readme file - long_description_content_type="text/markdown", - packages=find_packages( - where="src" - ), # List of all python modules to be installed - package_dir={"": "src", "git-fleximod": "src", "fleximod": "src/fleximod"}, - package_data={"": ["src/fleximod/version.txt", "man/git-fleximod.1"]}, - classifiers=[ - "Programming Language :: Python :: 3", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - ], # Information to filter the project on PyPi website - python_requires=">=3.6", # Minimum version requirement of the package - # py_modules=['git-fleximod'], # Name of the python package - install_requires=["GitPython"], # Install other dependencies if any - cmdclass={ - "build_manpages": build_manpages, - # Re-define build_py and install commands so the manual pages - # are automatically re-generated and installed - "build_py": get_build_py_cmd(), - "install": get_install_cmd(), - }, - ) diff --git a/src/fleximod/__init__.py b/src/__init__.py similarity index 100% rename from src/fleximod/__init__.py rename to src/__init__.py diff --git a/src/classes/__init__.py b/src/classes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/fleximod/gitinterface.py b/src/classes/gitinterface.py similarity index 98% rename from src/fleximod/gitinterface.py rename to src/classes/gitinterface.py index 96bb9ac52b..35c593a095 100644 --- a/src/fleximod/gitinterface.py +++ b/src/classes/gitinterface.py @@ -1,5 +1,5 @@ import os -from fleximod import utils +from classes import utils class GitInterface: diff --git a/src/fleximod/gitmodules.py b/src/classes/gitmodules.py similarity index 98% rename from src/fleximod/gitmodules.py rename to src/classes/gitmodules.py index 11a2457247..dd2e1fc948 100644 --- a/src/fleximod/gitmodules.py +++ b/src/classes/gitmodules.py @@ -1,7 +1,7 @@ import os import shutil from configparser import ConfigParser -from fleximod.lstripreader import LstripReader +from classes.lstripreader import LstripReader class GitModules(ConfigParser): diff --git a/src/fleximod/lstripreader.py b/src/classes/lstripreader.py similarity index 100% rename from src/fleximod/lstripreader.py rename to src/classes/lstripreader.py diff --git a/src/fleximod/utils.py b/src/classes/utils.py similarity index 100% rename from src/fleximod/utils.py rename to src/classes/utils.py diff --git a/src/fleximod/version.py b/src/classes/version.py similarity index 100% rename from src/fleximod/version.py rename to src/classes/version.py diff --git a/src/git-fleximod b/src/git-fleximod index aa587ea3a9..1c06d0d3d0 100755 --- a/src/git-fleximod +++ b/src/git-fleximod @@ -6,10 +6,10 @@ import logging import argparse import textwrap from pathlib import Path -from fleximod import utils -from fleximod.gitinterface import GitInterface -from fleximod.gitmodules import GitModules -from fleximod.version import __version__ +from classes import utils +from classes.gitinterface import GitInterface +from classes.gitmodules import GitModules +from classes.version import __version__ from configparser import NoOptionError # logger variable is global @@ -43,7 +43,7 @@ def get_parser(): "action", choices=choices, default="checkout", - help=f"Subcommand of fleximod, choices are {choices[:-1]}", + help=f"Subcommand of git-fleximod, choices are {choices[:-1]}", ) parser.add_argument( @@ -394,6 +394,7 @@ def submodules_update(gitmodules, root_dir, force): if fxtag and fxtag not in tags: git.git_operation("fetch", newremote, "--tags") atag = git.git_operation("describe", "--tags", "--always").rstrip() + if fxtag and fxtag != atag: print(f"{name:>20} updated to {fxtag}") git.git_operation("checkout", fxtag) diff --git a/tbump.toml b/tbump.toml new file mode 100644 index 0000000000..0e0a633036 --- /dev/null +++ b/tbump.toml @@ -0,0 +1,43 @@ +# Uncomment this if your project is hosted on GitHub: +github_url = "https://github.com/jedwards4b/git-fleximod/" + +[version] +current = "0.3.0" + +# Example of a semver regexp. +# Make sure this matches current_version before +# using tbump +regex = ''' + (?P\d+) + \. + (?P\d+) + \. + (?P\d+) + ''' + +[git] +message_template = "Bump to {new_version}" +tag_template = "v{new_version}" + +# For each file to patch, add a [[file]] config +# section containing the path of the file, relative to the +# tbump.toml location. +[[file]] +src = "src/classes/version.py" + +[[file]] +src = "pyproject.toml" + +# You can specify a list of commands to +# run after the files have been patched +# and before the git commit is made + +# [[before_commit]] +# name = "check changelog" +# cmd = "grep -q {new_version} Changelog.rst" + +# Or run some commands after the git tag and the branch +# have been pushed: +# [[after_push]] +# name = "publish" +# cmd = "./publish.sh" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000000..4d4c66c78e --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +import sys, os + +sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir, "src")) diff --git a/tests/test_import.py b/tests/test_import.py new file mode 100644 index 0000000000..becf751441 --- /dev/null +++ b/tests/test_import.py @@ -0,0 +1,9 @@ +# pylint: disable=unused-import +from classes import utils +from classes.gitinterface import GitInterface +from classes.gitmodules import GitModules +from classes.version import __version__ + + +def test_import(): + print("here") From 4f3bb556e165f9f1087de841c9b408cedb62f4a3 Mon Sep 17 00:00:00 2001 From: James Edwards Date: Sat, 3 Feb 2024 13:59:24 -0700 Subject: [PATCH 2/2] more poetry and sphinx --- doc/Makefile | 20 +++++++ doc/conf.py | 26 +++++++++ doc/index.rst | 24 ++++++++ doc/make.bat | 35 ++++++++++++ pyproject.toml | 5 +- src/classes/cli.py | 120 ++++++++++++++++++++++++++++++++++++++++ src/classes/version.py | 1 - src/git-fleximod | 122 +---------------------------------------- tbump.toml | 2 +- 9 files changed, 232 insertions(+), 123 deletions(-) create mode 100644 doc/Makefile create mode 100644 doc/conf.py create mode 100644 doc/index.rst create mode 100644 doc/make.bat create mode 100644 src/classes/cli.py delete mode 100644 src/classes/version.py diff --git a/doc/Makefile b/doc/Makefile new file mode 100644 index 0000000000..d4bb2cbb9e --- /dev/null +++ b/doc/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/doc/conf.py b/doc/conf.py new file mode 100644 index 0000000000..423099eec9 --- /dev/null +++ b/doc/conf.py @@ -0,0 +1,26 @@ +# Configuration file for the Sphinx documentation builder. +# +# For the full list of built-in configuration values, see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Project information ----------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information + +project = "git-fleximod" +author = "Jim Edwards " +release = "0.4.0" + +# -- General configuration --------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration + +extensions = ["sphinx_argparse_cli"] + +templates_path = ["_templates"] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] + + +# -- Options for HTML output ------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + +html_theme = "alabaster" +html_static_path = ["_static"] diff --git a/doc/index.rst b/doc/index.rst new file mode 100644 index 0000000000..fa534d0d10 --- /dev/null +++ b/doc/index.rst @@ -0,0 +1,24 @@ +.. git-fleximod documentation master file, created by + sphinx-quickstart on Sat Feb 3 12:02:22 2024. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to git-fleximod's documentation! +======================================== + +.. toctree:: + :maxdepth: 2 + :caption: Contents: +.. module:: sphinxcontrib.autoprogram +.. sphinx_argparse_cli:: + :module: classes.cli + :func: get_parser + :prog: git-fleximod + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/doc/make.bat b/doc/make.bat new file mode 100644 index 0000000000..32bb24529f --- /dev/null +++ b/doc/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=. +set BUILDDIR=_build + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/pyproject.toml b/pyproject.toml index 28e0e35a09..d49cbae72b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,10 @@ packages = [ { include = "src/classes/*.py" }, { include = "License" }, { include = "tests" }, - + { include = "doc/*.rst" }, + { include = "doc/Makefile" }, + { include = "doc/make.bat" }, + { include = "doc/conf.py" }, ] [tool.poetry.dependencies] diff --git a/src/classes/cli.py b/src/classes/cli.py new file mode 100644 index 0000000000..437d9beec4 --- /dev/null +++ b/src/classes/cli.py @@ -0,0 +1,120 @@ +from pathlib import Path +import argparse + +__version__ = "0.3.0" + + +def find_root_dir(filename=".git"): + d = Path.cwd() + root = Path(d.root) + while d != root: + attempt = d / filename + if attempt.is_dir(): + return attempt + d = d.parent + return None + + +def get_parser(): + description = """ + %(prog)s manages checking out groups of gitsubmodules with addtional support for Earth System Models + """ + parser = argparse.ArgumentParser( + description=description, formatter_class=argparse.RawDescriptionHelpFormatter + ) + + # + # user options + # + choices = ["update", "checkout", "status", "test"] + parser.add_argument( + "action", + choices=choices, + default="checkout", + help=f"Subcommand of git-fleximod, choices are {choices[:-1]}", + ) + + parser.add_argument( + "components", + nargs="*", + help="Specific component(s) to checkout. By default, " + "all required submodules are checked out.", + ) + + parser.add_argument( + "-C", + "--path", + default=find_root_dir(), + help="Toplevel repository directory. Defaults to top git directory relative to current.", + ) + + parser.add_argument( + "-g", + "--gitmodules", + nargs="?", + default=".gitmodules", + help="The submodule description filename. " "Default: %(default)s.", + ) + + parser.add_argument( + "-x", + "--exclude", + nargs="*", + help="Component(s) listed in the gitmodules file which should be ignored.", + ) + parser.add_argument( + "-f", + "--force", + action="store_true", + default=False, + help="Override cautions and update or checkout over locally modified repository.", + ) + + parser.add_argument( + "-o", + "--optional", + action="store_true", + default=False, + help="By default only the required submodules " + "are checked out. This flag will also checkout the " + "optional submodules relative to the toplevel directory.", + ) + + parser.add_argument( + "-v", + "--verbose", + action="count", + default=0, + help="Output additional information to " + "the screen and log file. This flag can be " + "used up to two times, increasing the " + "verbosity level each time.", + ) + + parser.add_argument( + "-V", + "--version", + action="version", + version=f"%(prog)s {__version__}", + help="Print version and exit.", + ) + + # + # developer options + # + parser.add_argument( + "--backtrace", + action="store_true", + help="DEVELOPER: show exception backtraces as extra " "debugging output", + ) + + parser.add_argument( + "-d", + "--debug", + action="store_true", + default=False, + help="DEVELOPER: output additional debugging " + "information to the screen and log file.", + ) + + return parser diff --git a/src/classes/version.py b/src/classes/version.py deleted file mode 100644 index 493f7415d7..0000000000 --- a/src/classes/version.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = "0.3.0" diff --git a/src/git-fleximod b/src/git-fleximod index 1c06d0d3d0..385515819c 100755 --- a/src/git-fleximod +++ b/src/git-fleximod @@ -3,137 +3,19 @@ import sys import os import shutil import logging -import argparse import textwrap -from pathlib import Path from classes import utils +from classes import cli from classes.gitinterface import GitInterface from classes.gitmodules import GitModules -from classes.version import __version__ from configparser import NoOptionError # logger variable is global logger = None -def find_root_dir(filename=".git"): - d = Path.cwd() - root = Path(d.root) - while d != root: - attempt = d / filename - if attempt.is_dir(): - return attempt - d = d.parent - return None - - -def get_parser(): - description = """ - %(prog)s manages checking out groups of gitsubmodules with addtional support for Earth System Models - """ - parser = argparse.ArgumentParser( - description=description, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - # - # user options - # - choices = ["update", "checkout", "status", "test"] - parser.add_argument( - "action", - choices=choices, - default="checkout", - help=f"Subcommand of git-fleximod, choices are {choices[:-1]}", - ) - - parser.add_argument( - "components", - nargs="*", - help="Specific component(s) to checkout. By default, " - "all required submodules are checked out.", - ) - - parser.add_argument( - "-C", - "--path", - default=find_root_dir(), - help="Toplevel repository directory. Defaults to top git directory relative to current.", - ) - - parser.add_argument( - "-g", - "--gitmodules", - nargs="?", - default=".gitmodules", - help="The submodule description filename. " "Default: %(default)s.", - ) - - parser.add_argument( - "-x", - "--exclude", - nargs="*", - help="Component(s) listed in the gitmodules file which should be ignored.", - ) - parser.add_argument( - "-f", - "--force", - action="store_true", - default=False, - help="Override cautions and update or checkout over locally modified repository.", - ) - - parser.add_argument( - "-o", - "--optional", - action="store_true", - default=False, - help="By default only the required submodules " - "are checked out. This flag will also checkout the " - "optional submodules relative to the toplevel directory.", - ) - - parser.add_argument( - "-v", - "--verbose", - action="count", - default=0, - help="Output additional information to " - "the screen and log file. This flag can be " - "used up to two times, increasing the " - "verbosity level each time.", - ) - - parser.add_argument( - "-V", - "--version", - action="version", - version=f"%(prog)s {__version__}", - help="Print version and exit.", - ) - - # - # developer options - # - parser.add_argument( - "--backtrace", - action="store_true", - help="DEVELOPER: show exception backtraces as extra " "debugging output", - ) - - parser.add_argument( - "-d", - "--debug", - action="store_true", - default=False, - help="DEVELOPER: output additional debugging " - "information to the screen and log file.", - ) - - return parser - - def commandline_arguments(args=None): - parser = get_parser() + parser = cli.get_parser() if args: options = parser.parse_args(args) diff --git a/tbump.toml b/tbump.toml index 0e0a633036..35521d04d1 100644 --- a/tbump.toml +++ b/tbump.toml @@ -23,7 +23,7 @@ tag_template = "v{new_version}" # section containing the path of the file, relative to the # tbump.toml location. [[file]] -src = "src/classes/version.py" +src = "src/classes/cli.py" [[file]] src = "pyproject.toml"