Skip to content

Commit 19a5939

Browse files
committed
Bulk refactoring so multigit can be also used as an imported module.
1 parent 2b1701e commit 19a5939

File tree

8 files changed

+89
-85
lines changed

8 files changed

+89
-85
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next Release
44
* Differences from [previous tag](/../../compare/v0.11.6…main).
5+
* Code and metadata refactoring for an easier *"librarizing* in the future.
56

67
## 0.11.6 (2024-MAR-10)
78
* Differences from [previous tag](/../../compare/v0.11.5…v0.11.6).

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Pending actions, general notes, etc. (in no particular order):
66
* https://www.codingwiththomas.com/blog/my-sphinx-best-practice-for-a-multiversion-documentation-in-different-languages
77
* Refactor code so Makefile, pyproject.toml are at the repository's root?
88
* [#10](../../issues/10): Review [vag](https://github.com/charlyoleg2/vag) and [vcstool](https://github.com/dirk-thomas/vcstool) for inspiration.
9-
* Refactor multigit so it can be used in library mode by other applications.
109
* Find the way to simplify subrepos' format allowing for *gitref* instead of *branch|tag|commit* (the code should find what kind of object *gitref* references).
1110
* Add a `--check-version` option (or something like that) that looks for updates.
1211
* Refactor tests so they go into subdirectories by "main" feature.
1312

1413
## IN PROGRESS
14+
* Refactor multigit so it can be used in library mode by other applications.

src/multigit/__init__.py

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
"""
3-
This script is a Python implementation of `simplest-git-subrepos <https://github.com/jmnavarrol/simplest-git-subrepos>`_.
42

5-
:package: multigit |release|
6-
:author: `Jesús M. Navarro <mailto:[email protected]>`_
7-
:license: `GNU General Public License v3.0 <https://github.com/jmnavarrol/python-multigit/blob/main/LICENSE>`_
8-
:source: https://github.com/jmnavarrol/python-multigit
9-
"""
10-
11-
__version__ = '0.11.6'
12-
13-
# Import stuff
14-
import os, sys
15-
import argparse
16-
17-
# "local" imports
18-
from .subrepos import Subrepos, SUBREPOS_FILE
19-
# Other imports so there's visibility of all classes in the module
3+
from .__main__ import __version__
204
from .gitrepo import Gitrepo
21-
from .subrepofile import Subrepofile
22-
23-
24-
25-
# MAIN entry point
26-
def main():
27-
'''Processes command line parameters'''
28-
parser = argparse.ArgumentParser(
29-
description="Manages git repos within git repos.",
30-
add_help=False, # this way I can force help to be an exclusion option along the others
31-
)
32-
33-
# Main options
34-
main_parser = parser.add_mutually_exclusive_group()
35-
main_parser.add_argument('-h', '--help', action='store_true', help="Shows this help.")
36-
main_parser.add_argument('-V', '--version', action='store_true', help="Shows " + parser.prog + " version and quits.")
37-
main_parser.add_argument('-r', '--run', action='store_true', help="Recursively processes '" + SUBREPOS_FILE + "' files found.")
38-
main_parser.add_argument('-s', '--status', action='store_true', help="Shows repositories' current status.")
39-
40-
# Ready to parse args
41-
args = parser.parse_args()
42-
#print(args)
43-
44-
# Run on the options
45-
if len(sys.argv) > 1:
46-
if args.help:
47-
print("%s (%s)\n" % (parser.prog, __version__))
48-
parser.print_help()
49-
elif args.version:
50-
print("%s %s" % (parser.prog, __version__))
51-
else:
52-
my_subrepos = Subrepos()
53-
my_subrepos.process(os.getcwd(), report_only=args.status)
54-
else:
55-
# Program called with no arguments (shows help)
56-
print("%s (%s): arguments required.\n" % (parser.prog, __version__))
57-
parser.print_help()
58-
59-
60-
if __name__ == '__main__':
61-
# execute only if run as a script
62-
sys.exit(main())
5+
from .subrepos import Subrepos

src/multigit/__main__.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
11
# -*- coding: utf-8 -*-
22

3-
from . import main
3+
"""
4+
**multigit** script is a Python implementation of `simplest-git-subrepos <https://github.com/jmnavarrol/simplest-git-subrepos>`_.
5+
6+
:package: multigit |release|
7+
:author: `Jesús M. Navarro <mailto:[email protected]>`_
8+
:license: `GNU General Public License v3.0 <https://github.com/jmnavarrol/python-multigit/blob/main/LICENSE>`_
9+
:source: https://github.com/jmnavarrol/python-multigit
10+
"""
11+
12+
__version__ = '0.11.7-dev1'
13+
14+
# Import stuff
15+
import os, sys
16+
import argparse
17+
18+
# "local" imports
19+
from .subrepos import Subrepos, SUBREPOS_FILE
20+
# Other imports so there's visibility of all classes in the module
21+
from .gitrepo import Gitrepo
22+
from .subrepofile import Subrepofile
23+
24+
25+
# MAIN entry point
26+
def main():
27+
'''Processes command line parameters'''
28+
parser = argparse.ArgumentParser(
29+
description="Manages git repos within git repos.",
30+
add_help=False, # this way I can force help to be an exclusion option along the others
31+
)
32+
33+
# Main options
34+
main_parser = parser.add_mutually_exclusive_group()
35+
main_parser.add_argument('-h', '--help', action='store_true', help="Shows this help.")
36+
main_parser.add_argument('-V', '--version', action='store_true', help="Shows " + parser.prog + " version and quits.")
37+
main_parser.add_argument('-r', '--run', action='store_true', help="Recursively processes '" + SUBREPOS_FILE + "' files found.")
38+
main_parser.add_argument('-s', '--status', action='store_true', help="Shows repositories' current status.")
39+
40+
# Ready to parse args
41+
args = parser.parse_args()
42+
#print(args)
43+
44+
# Run on the options
45+
if len(sys.argv) > 1:
46+
if args.help:
47+
print("%s (%s)\n" % (parser.prog, __version__))
48+
parser.print_help()
49+
elif args.version:
50+
print("%s %s" % (parser.prog, __version__))
51+
else:
52+
my_subrepos = Subrepos()
53+
my_subrepos.process(os.getcwd(), report_only=args.status)
54+
else:
55+
# Program called with no arguments (shows help)
56+
print("%s (%s): arguments required.\n" % (parser.prog, __version__))
57+
parser.print_help()
58+
459

560
if __name__ == '__main__':
6-
main()
7-
61+
sys.exit(
62+
main()
63+
)

src/multigit/gitrepo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
# Import stuff
4+
import sys
45
from git import Repo, exc as git_exception
56

67

@@ -223,4 +224,6 @@ def update(self, repoconf):
223224

224225
if __name__ == '__main__':
225226
# execute only if run as a script
226-
main()
227+
sys.exit(
228+
main()
229+
)

src/multigit/subrepos.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,6 @@ def __print_subrepo_status(self, subrepo):
186186

187187
if __name__ == '__main__':
188188
# execute only if run as a script
189-
main()
189+
sys.exit(
190+
main()
191+
)

src/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ development = [
4646
]
4747

4848
[project.scripts]
49-
multigit = "multigit:main"
49+
multigit = "multigit:__main__.main"
5050

5151
[build-system]
5252
requires = [
@@ -56,4 +56,4 @@ requires = [
5656
build-backend = "hatchling.build"
5757

5858
[tool.hatch.version]
59-
path = "multigit/__init__.py"
59+
path = "multigit/__main__.py"

src/sphinx/index.rst

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
multigit documentation
7-
======================
6+
.. toctree::
7+
:caption: Contents:
8+
:hidden:
9+
:maxdepth: 2
810

9-
* :ref:`genindex`
10-
* **Classes:**
11-
* :ref:`Subrepos<subrepos>`: *"main"* entrypoint for the full process.
12-
* :ref:`Gitrepo<gitrepo>`: manages a single git repository as per the requested configuration.
11+
genindex
12+
Class Subrepos <subrepos>
13+
Class Gitrepo <gitrepo>
1314

14-
----
15+
multigit documentation
16+
======================
17+
multigit script and module.
1518

16-
.. automodule:: multigit
19+
.. automodule:: multigit.__main__
1720
:noindex:
1821
:members:
1922
:undoc-members:
2023
:show-inheritance:
2124

25+
.. command-output:: multigit --help
26+
2227
----
2328

24-
*"Main"* entry point is the `multigit` script:
29+
**multigit** can also be used as an imported module.
2530

26-
.. command-output:: multigit --help
27-
28-
.. toctree::
29-
:caption: Contents:
30-
:hidden:
31-
:maxdepth: 2
32-
33-
Class Subrepos <subrepos>
34-
Class Gitrepo <gitrepo>
31+
**Classes:**
32+
* :ref:`Subrepos<subrepos>`: processes a full subrepos' configuration.
33+
* :ref:`Gitrepo<gitrepo>`: manages a single git repository as per the requested configuration.

0 commit comments

Comments
 (0)