From 9e2679310ae62eccf13084efcf7696be51629e87 Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Mon, 17 May 2021 10:20:47 -0400 Subject: [PATCH] Enable python -m stgit The top-level `stg` script is now a simple shell script that invokes StGit as a runnable python module with `python -m stgit`. This approach is aligns StGit with modern Python practices and may help with portability to Windows. Signed-off-by: Peter Grayson --- Makefile | 14 +++++++------- setup.py | 4 ++-- stg | 44 +++----------------------------------------- stgit/__main__.py | 18 ++++++++++++++++++ stgit/main.py | 19 ++++++++++--------- stgit/version.py | 2 +- 6 files changed, 41 insertions(+), 60 deletions(-) rewrite stg (99%) create mode 100644 stgit/__main__.py diff --git a/Makefile b/Makefile index 8e2c840..6729c31 100644 --- a/Makefile +++ b/Makefile @@ -35,13 +35,13 @@ install-html: lint: lint-black lint-isort lint-flake8 lint-t lint-black: - $(PYTHON) -m black --check --quiet --diff . stg + $(PYTHON) -m black --check --quiet --diff . lint-isort: - $(PYTHON) -m isort --check-only --quiet --diff . stg + $(PYTHON) -m isort --check-only --quiet --diff . lint-flake8: - $(PYTHON) -m flake8 . stg + $(PYTHON) -m flake8 . lint-t: $(MAKE) -C t test-lint @@ -49,15 +49,15 @@ lint-t: .PHONY: lint lint-black lint-isort lint-flake8 lint-t format: - $(PYTHON) -m black . stg - $(PYTHON) -m isort --quiet . stg + $(PYTHON) -m black . + $(PYTHON) -m isort --quiet . test: build $(MAKE) -C t all test-patches: - for patch in $$(stg series --noprefix $(TEST_PATCHES)); do \ - stg goto $$patch && $(MAKE) test || break; \ + for patch in $$($(PYTHON) -m stgit.main series --noprefix $(TEST_PATCHES)); do \ + $(PYTHON) -m stgit.main goto $$patch && $(MAKE) test || break; \ done .PHONY: format test test-patches diff --git a/setup.py b/setup.py index 341cf24..a26f5b7 100755 --- a/setup.py +++ b/setup.py @@ -30,8 +30,8 @@ def __check_min_version(min_ver, ver): def __check_python_version(): """Check the minimum Python version""" - pyver = '.'.join(map(str, sys.version_info)) - if not __check_min_version(version.python_min_ver, pyver): + pyver = sys.version_info[: len(version.python_min_ver)] + if pyver < version.python_min_ver: print( 'Python version %s or newer required. Found %s' % (version.python_min_ver, pyver), diff --git a/stg b/stg dissimilarity index 99% index a2d760b..1fbf064 100755 --- a/stg +++ b/stg @@ -1,41 +1,3 @@ -#!/usr/bin/env python3 -import os -import sys - -__copyright__ = """ -Copyright (C) 2005, Catalin Marinas - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License version 2 as -published by the Free Software Foundation. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, see http://www.gnu.org/licenses/. -""" - -if __name__ == '__main__': - if sys.version_info[:2] < (3, 5): - sys.stderr.write('StGit requires Python >= 3.5\n') - sys.exit(1) - - if os.environ.get('COVERAGE_PROCESS_START'): - import sys - - import coverage - - if len(sys.argv) < 2 or sys.argv[1].startswith('-'): - context = 'stg' - else: - context = 'stg-' + sys.argv[1] - - cov = coverage.process_startup() - cov.switch_context(context) - - from stgit.main import main - - main() +#!/bin/sh + +exec "${PYTHON:-python3}" -m stgit "$@" diff --git a/stgit/__main__.py b/stgit/__main__.py new file mode 100644 index 0000000..72a49ee --- /dev/null +++ b/stgit/__main__.py @@ -0,0 +1,18 @@ +if __name__ == '__main__': # pragma: no cover + import os + import sys + + if os.environ.get('COVERAGE_PROCESS_START'): + import coverage + + if len(sys.argv) < 2 or sys.argv[1].startswith('-'): + context = 'stg' + else: + context = 'stg-' + sys.argv[1] + + cov = coverage.process_startup() + cov.switch_context(context) + + from stgit.main import main + + main() diff --git a/stgit/main.py b/stgit/main.py index 7a94993..01fb821 100644 --- a/stgit/main.py +++ b/stgit/main.py @@ -5,6 +5,7 @@ import sys import traceback import stgit.commands +import stgit.version from stgit import argparse, run, utils from stgit.compat import environ_get, fsdecode_utf8 from stgit.config import config @@ -53,9 +54,6 @@ def append_alias_commands(cmd_list): cmd_list.append((name, CommandAlias(name, command), 'Alias commands', command)) -# -# The commands map -# class Commands(dict): """Commands class. It performs on-demand module loading""" @@ -66,7 +64,7 @@ class Commands(dict): if not candidates: out.error( 'Unknown command: %s' % key, - 'Try "%s help" for a list of supported commands' % prog, + 'Try "%s help" for a list of supported commands' % sys.argv[0], ) sys.exit(utils.STGIT_GENERAL_ERROR) elif len(candidates) == 1: @@ -105,11 +103,8 @@ def print_help(): def _main(): - global prog - - sys.argv = list(map(fsdecode_utf8, sys.argv)) - - prog = os.path.basename(sys.argv[0]) + sys.argv[:] = list(map(fsdecode_utf8, sys.argv)) + prog = sys.argv[0] = 'stg' if len(sys.argv) < 2: print('usage: %s ' % prog, file=sys.stderr) @@ -206,6 +201,12 @@ def _main(): def main(): + python_version = sys.version_info[: len(stgit.version.python_min_ver)] + if python_version < stgit.version.python_min_ver: + ver_str = '.'.join(map(str, stgit.version.python_min_ver)) + sys.stderr.write('StGit requires Python >= %s\n' % ver_str) + sys.exit(utils.STGIT_GENERAL_ERROR) + try: _main() finally: diff --git a/stgit/version.py b/stgit/version.py index f7013ab..66b6e9c 100644 --- a/stgit/version.py +++ b/stgit/version.py @@ -76,4 +76,4 @@ def get_version(): # minimum version requirements git_min_ver = '2.2.0' -python_min_ver = '3.5' +python_min_ver = (3, 5) -- 2.11.4.GIT