From d55217be8501909cbe2a10bf9bcd0f8b3df5323a Mon Sep 17 00:00:00 2001 From: Hubert Tournier Date: Sun, 6 Mar 2022 23:23:55 +0100 Subject: [PATCH] Add files via upload --- License | 11 ++ MANPATH.1.md | 96 ++++++++++++++ MANPATH.3.md | 45 +++++++ Makefile | 64 ++++++++++ README.md | 19 ++- man/manpath.1 | 193 ++++++++++++++++++++++++++++ man/manpath.3 | 60 +++++++++ pyproject.toml | 6 + setup.cfg | 62 +++++++++ src/manpath/__init__.py | 2 + src/manpath/main.py | 272 ++++++++++++++++++++++++++++++++++++++++ tests/manpath.xml | 88 +++++++++++++ 12 files changed, 916 insertions(+), 2 deletions(-) create mode 100644 License create mode 100644 MANPATH.1.md create mode 100644 MANPATH.3.md create mode 100644 Makefile create mode 100644 man/manpath.1 create mode 100644 man/manpath.3 create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 src/manpath/__init__.py create mode 100644 src/manpath/main.py create mode 100644 tests/manpath.xml diff --git a/License b/License new file mode 100644 index 0000000..1edc5ea --- /dev/null +++ b/License @@ -0,0 +1,11 @@ +Copyright 2022+ Hubert Tournier + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/MANPATH.1.md b/MANPATH.1.md new file mode 100644 index 0000000..68e608a --- /dev/null +++ b/MANPATH.1.md @@ -0,0 +1,96 @@ +# MANPATH(1) + +## NAME +manpath - display search path for manual pages + +## SYNOPSIS +**manpath** +\[-Ldq\] +\[--debug\] +\[--help|-?\] +\[--version\] +\[--\] + +## DESCRIPTION +The **manpath** utility determines the user's manual search path from the user's PATH, and local configuration files. +This result is echoed to the standard output. + +### OPTIONS +Options | Use +------- | --- +-L|Output manual locales list instead of the manual path. +-d|Print extra debugging information. +-q|Suppresses warning messages. +--debug|Enable debug mode +--help\|-?|Print usage and a short help message and exit +--version|Print version and exit +--|Options processing terminator + +## IMPLEMENTATION NOTES +The **manpath** utility constructs the manual path from three sources: +1. From each component of the user's PATH for the first of: + - pathname/man + - pathname/MAN + - If pathname ends with /bin: pathname/../share/man and pathname/../man +2. From default manpath entries: + - /usr/share/man + - /usr/share/openssl/man + - /usr/local/share/man + - /usr/local/man +3. The configuration files listed in the FILES section for MANPATH entries. + The information from these locations is then concatenated together. + If the *-L* flag is set, the **manpath** utility will search the configuration files listed in the FILES section for MANLOCALE entries. + +## ENVIRONMENT +The following environment variables affect the execution of **manpath**: + +Environment variable | Use +-------------------- | --- +MANLOCALES|If set with the *-L* flag, causes the utility to display a warning and the value, overriding any other configuration found on the system. +MANPATH|If set, causes the utility to display a warning and the value, overriding any other configuration found on the system. +PATH|Influences the manual path as described in the IMPLEMENTATION NOTES. + +The *MANPATH_DEBUG* environment variable can also be set to any value to enable debug mode. + +The *FLAVOUR* or *MANPATH_FLAVOUR* environment variables can be set to one of the following values, to implement only the corresponding options and behaviours: +* bsd | bsd:freebsd : FreeBSD [manpath(1)](https://www.freebsd.org/cgi/man.cgi?query=manpath) + +## FILES +* /etc/man.conf : System configuration file. +* /usr/local/etc/man.d/*.conf : Local configuration files. + +## EXIT STATUS +The **manpath** utility exits 0 on success, and >0 if an error occurs. + +## SEE ALSO +[apropos(1)](https://www.freebsd.org/cgi/man.cgi?query=apropos), +[man(1)](https://www.freebsd.org/cgi/man.cgi?query=man), +[whatis(1)](https://www.freebsd.org/cgi/man.cgi?query=whatis), +[man.conf(5)](https://www.freebsd.org/cgi/man.cgi?query=man.conf), +[manpath(3)](https://github.com/HubTou/manpath/blob/main/MANPATH.3.md) + +## STANDARDS +The **manpath** utility is a standard UNIX command, though not a POSIX one. + +This re-implementation tries to follow the [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guide for [Python](https://www.python.org/) code. + +## PORTABILITY +To be tested under Windows. + +## HISTORY +This re-implementation was made for the [PNU project](https://github.com/HubTou/PNU). + +## LICENSE +It is available under the [3-clause BSD license](https://opensource.org/licenses/BSD-3-Clause). + +## AUTHORS +[Hubert Tournier](https://github.com/HubTou) + +The manpath(1) manual page is mainly based on the one written for [FreeBSD](https://www.freebsd.org/) by [Gordon Tetlow](https://github.com/tetlowgm). + +## BUGS +This re-implementation corrects 3 (minor) bugs or the original command: +* No warning was displayed when MANPATH was set +* Default manpath entries were supplied when PATH was not set, before "Adding default manpath entries" +* A "Skipping duplicate manpath entry" message was incorrectly displayed with MANPATH directives with empty content in config files + diff --git a/MANPATH.3.md b/MANPATH.3.md new file mode 100644 index 0000000..6a284ee --- /dev/null +++ b/MANPATH.3.md @@ -0,0 +1,45 @@ +# MANPATH(3) + +## NAME +manpath - return the search path for manual pages + +## SYNOPSIS +import **manpath** + +String *manpath*.**get_manpath**(Integer *debug_level* = 0, Boolean *quiet_mode* = False) + +String *manpath*.**get_locales**(Integer *debug_level* = 0, Boolean *quiet_mode* = False) + +## DESCRIPTION +The **get_manpath**() function returns a string of colon separated directories containing manual pages. + +The **get_locales**() function returns a string of colon separated locales for which there are localized manual pages. + +For each of the two functions, if the optional *debug_level* is set to 1, 2 or 3 it will also print increasingly verbose debugging messages to the console. + +And if the optional *quiet_mode* argument is set to True it will discard warning messages printed to the console. + +## ENVIRONMENT +The following environment variables affect the execution of **manpath**: + +Environment variable | Use +-------------------- | --- +MANLOCALES|If set, causes the utility to override any other configuration found on the system. +MANPATH|If set, causes the utility to override any other configuration found on the system. +PATH|Influences the manual path as described in the IMPLEMENTATION NOTES. + +## SEE ALSO +[manpath(1)](https://github.com/HubTou/manpath/blob/main/MANPATH.1.md) + +## STANDARDS +The **manpath** library tries to follow the [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guide for [Python](https://www.python.org/) code. + +## HISTORY +This library was made for the [PNU project](https://github.com/HubTou/PNU). + +## LICENSE +It is available under the [3-clause BSD license](https://opensource.org/licenses/BSD-3-Clause). + +## AUTHORS +[Hubert Tournier](https://github.com/HubTou) + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1146da1 --- /dev/null +++ b/Makefile @@ -0,0 +1,64 @@ +NAME=manpath +SECTION=1 +SOURCES=src/${NAME}/__init__.py src/${NAME}/main.py + +# Default action is to show this help message: +.help: + @echo "Possible targets:" + @echo " check-code Verify PEP 8 compliance (lint)" + @echo " check-security Verify security issues (audit)" + @echo " check-unused Find unused code" + @echo " check-version Find required Python version" + @echo " check-sloc Count Single Lines of Code" + @echo " checks Make all the previous tests" + @echo " format Format code" + @echo " package Build package" + @echo " upload-test Upload the package to TestPyPi" + @echo " upload Upload the package to PyPi" + @echo " distclean Remove all generated files" + +check-code: /usr/local/bin/pylint + -pylint ${SOURCES} + +lint: check-code + +check-security: /usr/local/bin/bandit + -bandit -r ${SOURCES} + +audit: check-security + +check-unused: /usr/local/bin/vulture + -vulture --sort-by-size ${SOURCES} + +check-version: /usr/local/bin/vermin + -vermin ${SOURCES} + +check-sloc: /usr/local/bin/pygount + -pygount --format=summary . + +checks: check-code check-security check-unused check-version check-sloc + +format: /usr/local/bin/black + black ${SOURCES} + +love: + @echo "Not war!" + +man/${NAME}.${SECTION}.gz: man/${NAME}.${SECTION} + @gzip -k9c man/${NAME}.${SECTION} > man/${NAME}.${SECTION}.gz + +man/${NAME}.3.gz: man/${NAME}.3 + @gzip -k9c man/${NAME}.3 > man/${NAME}.3.gz + +package: man/${NAME}.${SECTION}.gz man/${NAME}.3.gz + python -m build + +upload-test: + python -m twine upload --repository testpypi dist/* + +upload: + python -m twine upload dist/* + +distclean: + rm -rf build dist src/*.egg-info man/${NAME}.${SECTION}.gz man/${NAME}.3.gz + diff --git a/README.md b/README.md index 9aefe68..af261d3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ -# manpath -display search path for manual pages +# Installation +Depending on if you want only this tool, the full set of PNU tools, or PNU plus a selection of additional third-parties tools, use one of these commands: + +pip install [pnu-manpath](https://pypi.org/project/pnu-manpath/) +
+pip install [PNU](https://pypi.org/project/PNU/) +
+pip install [pytnix](https://pypi.org/project/pytnix/) + +# MANPATH(1), MANPATH(3) +This repository includes a command-line utility: +* [manpath(1)](https://github.com/HubTou/manpath/blob/main/MANPATH.1.md) - display search path for manual pages + +And a Python library: +* [manpath(3)](https://github.com/HubTou/manpath/blob/main/MANPATH.3.md) - return the search path for manual pages + + diff --git a/man/manpath.1 b/man/manpath.1 new file mode 100644 index 0000000..81164b6 --- /dev/null +++ b/man/manpath.1 @@ -0,0 +1,193 @@ +.\"- +.\" Copyright (c) 2010 Gordon Tetlow +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd March 6, 2022 +.Dt MANPATH 1 +.Os +.Sh NAME +.Nm manpath +.Nd display search path for manual pages +.Sh SYNOPSIS +.Nm +.Op Fl Ldq +.Op Fl -debug +.Op Fl -help|-? +.Op Fl -version +.Op Fl - +.Sh DESCRIPTION +The +.Nm +utility determines the user's manual search path from +the user's +.Ev PATH , +and local configuration files. +This result is echoed to the standard output. +.Ss OPTIONS +.Op Fl d +Print extra debugging information. +.Pp +.Op Fl L +Output manual locales list instead of the manual path. +.Pp +.Op Fl q +Suppresses warning messages. +.Pp +.Op Fl -debug +Enable debug mode +.Pp +.Op Fl -help|-? +Print usage and this help message and exit +.Pp +.Op Fl -version +Print version and exit +.Pp +.Op Fl - +Options processing terminator +.Sh IMPLEMENTATION NOTES +The +.Nm +utility constructs the manual path from three sources: +.Bl -enum -compact +.It +From each component of the user's +.Ev PATH +for the first of: +.Bl -dash -compact +.It +.Pa pathname/man +.It +.Pa pathname/MAN +.It +If pathname ends with /bin: +.Pa pathname/../share/man +and +.Pa pathname/../man +.El +.It +From default manpath entries: +.Bl -dash -compact +.It +.Pa /usr/share/man +.It +.Pa /usr/share/openssl/man +.It +.Pa /usr/local/share/man +.It +.Pa /usr/local/man +.El +.It +The configuration files listed in the +.Sx FILES +section for +.Va MANPATH +entries. +.El +The information from these locations is then concatenated together. +.Pp +If the +.Fl L +flag is set, the +.Nm +utility will search the configuration files listed in the +.Sx FILES +section for +.Va MANLOCALE +entries. +.Sh ENVIRONMENT +The following environment variables affect the execution of +.Nm : +.Bl -tag -width ".Ev MANLOCALES" +.It Ev MANLOCALES +If set with the +.Fl L +flag, causes the utility to display a warning and the value, overriding any +other configuration found on the system. +.It Ev MANPATH +If set, causes the utility to display a warning and the value, overriding +any other configuration found on the system. +.It Ev PATH +Influences the manual path as described in the +.Sx IMPLEMENTATION NOTES . +.El +.Pp +The +.Ev MANPATH_DEBUG +environment variable can also be set to any value to enable debug mode. +.Pp +The +.Ev FLAVOUR +or +.Ev MANPATH_FLAVOUR +environment variables can be set to one of the following values, to implement only the corresponding options and behaviours: +.Bl -bullet +.It +bsd | bsd:freebsd : FreeBSD +.Xr manpath 1 +.El +.Sh FILES +.Bl -tag -width indent -compact +.It Pa /etc/man.conf +System configuration file. +.It Pa /usr/local/etc/man.d/*.conf +Local configuration files. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr apropos 1 , +.Xr man 1 , +.Xr whatis 1 , +.Xr man.conf 5 , +.Xr manpath 3 +.Sh STANDARDS +The +.Nm +utility is a standard UNIX command, though not a POSIX one. +.Pp +This re-implementation tries to follow the PEP 8 style guide for Python code. +.Sh PORTABILITY +To be tested under Windows. +.Sh HISTORY +This re-implementation was made for the +.Lk https://github.com/HubTou/PNU PNU project +.Sh LICENSE +It is available under the 3-clause BSD license. +.Sh AUTHORS +.An Hubert Tournier +.Pp +The manpath(1) manual page is mainly based on the one written for FreeBSD by +.An Gordon Tetlow . +.Sh BUGS +This re-implementation corrects 3 (minor) bugs or the original command: +.Bl -dash -compact +.It +No warning was displayed when MANPATH was set +.It +Default manpath entries were supplied when PATH was not set, before "Adding default manpath entries" +.It +A "Skipping duplicate manpath entry" message was incorrectly displayed with MANPATH directives with empty content in config files +.El diff --git a/man/manpath.3 b/man/manpath.3 new file mode 100644 index 0000000..bd40bfd --- /dev/null +++ b/man/manpath.3 @@ -0,0 +1,60 @@ +.Dd March 6, 2022 +.Dt MANPATH 3 +.Os +.Sh NAME +.Nm manpath +.Nd return the search path for manual pages +.Sh SYNOPSIS +.Em import manpath +.Pp +.Ft String +.Fo manpath.get_manpath +.Fa "Integer debug_level = 0" +.Fa "Boolean quiet_mode = False" +.Fc +.Ft String +.Fo manpath.get_locales +.Fa "Integer debug_level = 0" +.Fa "Boolean quiet_mode = False" +.Fc +.Sh DESCRIPTION +The +.Fn get_manpath +function returns a string of colon separated directories containing manual pages. +.Pp +The +.Fn get_locales +function returns a string of colon separated locales for which there are localized manual pages. +.Pp +For each of the two functions, if the optional +.Fa debug_level +is set to 1, 2 or 3 it will also print increasingly verbose debugging messages to the console. +.Pp +And if the optional +.Fa quiet_mode +argument is set to True it will discard warning messages printed to the console. +.Sh ENVIRONMENT +The following environment variables affect the execution of +.Nm : +.Bl -tag -width ".Ev MANLOCALES" +.It Ev MANLOCALES +If set, causes the utility to override any other configuration found on the system. +.It Ev MANPATH +If set, causes the utility to override any other configuration found on the system. +.It Ev PATH +Influences the manual path as described in the +.Sx IMPLEMENTATION NOTES . +.El +.Sh SEE ALSO +.Xr manpath 1 +.Sh STANDARDS +The +.Lb manpath +tries to follow the PEP 8 style guide for Python code. +.Sh HISTORY +This library was made for the +.Lk https://github.com/HubTou/PNU [PNU project] +.Sh LICENSE +It is available under the 3-clause BSD license. +.Sh AUTHORS +.An Hubert Tournier diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..374b58c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[build-system] +requires = [ + "setuptools>=42", + "wheel" +] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3b5406b --- /dev/null +++ b/setup.cfg @@ -0,0 +1,62 @@ +[metadata] +name = pnu_manpath +description = display search path for manual pages +long_description = file: README.md +long_description_content_type = text/markdown +version = 1.0.0 +license = BSD 3-Clause License +license_files = License +author = Hubert Tournier +author_email = nobody@nowhere.invalid +url = https://github.com/HubTou/manpath/ +project_urls = + Bug Tracker = https://github.com/HubTou/manpath/issues +keywords = pnu-project +classifiers = + Development Status :: 5 - Production/Stable + Environment :: Console + Intended Audience :: Developers + Intended Audience :: End Users/Desktop + License :: OSI Approved :: BSD License + Natural Language :: English + Operating System :: OS Independent + Operating System :: POSIX :: BSD :: FreeBSD + Operating System :: Microsoft :: Windows + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.0 + Programming Language :: Python :: 3.1 + Programming Language :: Python :: 3.2 + Programming Language :: Python :: 3.3 + Programming Language :: Python :: 3.4 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Topic :: Software Development :: Libraries :: Python Modules + Topic :: System + Topic :: Utilities + +[options] +package_dir = + = src +packages = find: +python_requires = >=3.0 +install_requires = + pnu-libpnu + pnu-libmanconf + +[options.packages.find] +where = src + +[options.entry_points] +console_scripts = + manpath = manpath:main + +[options.data_files] +man/man1 = + man/manpath.1.gz +man/man3 = + man/manpath.3.gz + diff --git a/src/manpath/__init__.py b/src/manpath/__init__.py new file mode 100644 index 0000000..32c5bca --- /dev/null +++ b/src/manpath/__init__.py @@ -0,0 +1,2 @@ +"""Wrapper for the source code files""" +from .main import * diff --git a/src/manpath/main.py b/src/manpath/main.py new file mode 100644 index 0000000..6b3bfb9 --- /dev/null +++ b/src/manpath/main.py @@ -0,0 +1,272 @@ +#!/usr/bin/env python +""" manpath - display search path for manual pages +License: 3-clause BSD (see https://opensource.org/licenses/BSD-3-Clause) +Author: Hubert Tournier +""" + +import getopt +import logging +import os +import re +import sys + +import libpnu +import libmanconf + +# Version string used by the what(1) and ident(1) commands: +ID = "@(#) $Id: manpath - display search path for manual pages v1.0.0 (March 6, 2022) by Hubert Tournier $" + +# Default parameters. Can be overcome by environment variables, then command line options +parameters = { + "Debug level": 0, + "Locales mode": False, + "Quiet mode": False, + "Command flavour": "PNU", +} + + +################################################################################ +def _display_help(): + """Displays usage and help""" + if parameters["Command flavour"] in ("PNU", "bsd", "bsd:freebsd"): + print("usage: manpath [--debug] [--help|-?] [--version]", file=sys.stderr) + print(" [-L] [-d] [-q]", file=sys.stderr) + print(" [--]", file=sys.stderr) + print( + " --------- -----------------------------------------------------", + file=sys.stderr + ) + print(" -L Output manual locales list instead of the manual path", file=sys.stderr) + print(" -d Print extra debugging information", file=sys.stderr) + print(" -q Suppresses warning messages", file=sys.stderr) + print(" --debug Enable debug mode", file=sys.stderr) + print(" --help|-? Print usage and this help message and exit", file=sys.stderr) + print(" --version Print version and exit", file=sys.stderr) + print(" -- Options processing terminator", file=sys.stderr) + print(file=sys.stderr) + + +################################################################################ +def _handle_interrupts(signal_number, current_stack_frame): + """Prevent SIGINT signals from displaying an ugly stack trace""" + print(" Interrupted!\n", file=sys.stderr) + _display_help() + sys.exit(0) + + +################################################################################ +def _process_environment_variables(): + """Process environment variables""" + # pylint: disable=C0103 + global parameters + # pylint: enable=C0103 + + if "MANPATH_DEBUG" in os.environ: + logging.disable(logging.NOTSET) + + if "FLAVOUR" in os.environ: + parameters["Command flavour"] = os.environ["FLAVOUR"].lower() + if "MANPATH_FLAVOUR" in os.environ: + parameters["Command flavour"] = os.environ["MANPATH_FLAVOUR"].lower() + + # Command variants supported: + if parameters["Command flavour"] not in ("PNU", "bsd", "bsd:freebsd"): + logging.critical("Unimplemented command FLAVOUR: %s", parameters["Command flavour"]) + sys.exit(1) + + logging.debug("_process_environment_variables(): parameters:") + logging.debug(parameters) + + +################################################################################ +def _process_command_line(): + """Process command line options""" + # pylint: disable=C0103 + global parameters + # pylint: enable=C0103 + + # option letters followed by : expect an argument + # same for option strings followed by = + if parameters["Command flavour"] in ("PNU", "bsd", "bsd:freebsd"): + character_options = "Ldq?" + string_options = [ + "debug", + "help", + "version", + ] + + try: + options, remaining_arguments = getopt.getopt( + sys.argv[1:], character_options, string_options + ) + except getopt.GetoptError as error: + logging.critical("Syntax error: %s", error) + _display_help() + sys.exit(1) + + for option, _ in options: + + if option == "-d": + parameters["Debug level"] += 1 + + elif option == "-L": + parameters["Locales mode"] = True + + elif option == "-q": + parameters["Quiet mode"] = True + + elif option == "--debug": + logging.disable(logging.NOTSET) + + elif option in ("--help", "-?"): + _display_help() + sys.exit(0) + + elif option == "--version": + print(ID.replace("@(" + "#)" + " $" + "Id" + ": ", "").replace(" $", "")) + sys.exit(0) + + logging.debug("_process_command_line(): parameters:") + logging.debug(parameters) + logging.debug("_process_command_line(): remaining_arguments:") + logging.debug(remaining_arguments) + + return remaining_arguments + + +################################################################################ +def _add_directory(directory, manpath, debug_level): + """Test if the given directory exist and is not already in the manpath. Adds it if needed""" + found = False + if os.path.isdir(directory): + found = True + if directory in manpath: + if debug_level > 1: + print("-- Skipping duplicate manpath entry %s" % directory, file=sys.stderr) + else: + if debug_level > 0: + print("-- Adding %s to manpath" % directory, file=sys.stderr) + manpath.append(directory) + + return manpath, found + + +################################################################################ +def get_manpath(debug_level=0, quiet_mode=False): + """Perform manpath processing""" + manpath = [] + manual_path = "" + + if "MANPATH" in os.environ: + manual_path = os.environ["MANPATH"] + + if not quiet_mode: + print("(Warning: MANPATH environment variable set)", file=sys.stderr) + else: + if debug_level > 0: + print("-- Searching PATH for man directories", file=sys.stderr) + if "PATH" in os.environ: + for part in os.environ["PATH"].split(os.pathsep): + manpath, found = _add_directory(part + os.sep + "man", manpath, debug_level) + if not found: + manpath, found = _add_directory(part + os.sep + "MAN", manpath, debug_level) + if not found and part.endswith(os.sep + "bin"): + root = re.sub(r".bin", "", part) + manpath, found = _add_directory( + root + os.sep + "share" + os.sep + "man", + manpath, + debug_level + ) + manpath, found = _add_directory( + root + os.sep + "man", + manpath, + debug_level + ) + if not found and os.altsep and part.endswith(os.altsep + "bin"): + root = re.sub(r".bin", "", part) + manpath, found = _add_directory( + root + os.altsep + "share" + os.altsep + "man", + manpath, + debug_level + ) + manpath, found = _add_directory( + root + os.altsep + "man", + manpath, + debug_level + ) + + if debug_level > 0: + print("-- Adding default manpath entries", file=sys.stderr) + for entry in ( + "/usr/share/man", + "/usr/share/openssl/man", + "/usr/local/share/man", + "/usr/local/man" + ): + manpath, found = _add_directory(entry, manpath, debug_level) + + config_manpaths, _, _ = libmanconf.read_man_conf_files( + debug_level, + manpath_so_far=os.pathsep.join(manpath) + ) + + for part in config_manpaths.split(os.pathsep): + manpath.append(part) + + manual_path = os.pathsep.join(manpath) + + if debug_level > 0: + print("-- Using manual path: %s" % manual_path, file=sys.stderr) + + return manual_path + + +################################################################################ +def get_locales(debug_level=0, quiet_mode=False): + """Perform manpath -L processing""" + manual_locales = "" + + if "MANLOCALES" in os.environ: + manual_locales = os.environ["MANLOCALES"] + + if not quiet_mode: + print("(Warning: MANLOCALES environment variable set)", file=sys.stderr) + else: + _, manual_locales, _ = libmanconf.read_man_conf_files(debug_level) + + if debug_level > 0: + print("-- Available manual locales: %s" % manual_locales, file=sys.stderr) + + return manual_locales + + +################################################################################ +def main(): + """The program's main entry point""" + program_name = os.path.basename(sys.argv[0]) + + libpnu.initialize_debugging(program_name) + libpnu.handle_interrupt_signals(_handle_interrupts) + _process_environment_variables() + _ = _process_command_line() + + if parameters["Locales mode"]: + print( + get_locales( + debug_level=parameters["Debug level"], + quiet_mode=parameters["Quiet mode"] + ) + ) + else: + print( + get_manpath( + debug_level=parameters["Debug level"], + quiet_mode=parameters["Quiet mode"] + ) + ) + + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/tests/manpath.xml b/tests/manpath.xml new file mode 100644 index 0000000..a0accd7 --- /dev/null +++ b/tests/manpath.xml @@ -0,0 +1,88 @@ + + + + + + unset MANPATH ; manpath + + + + + + unset MANPATH ; manpath -d + + + + + + unset MANPATH ; manpath -dd + + + + + + unset MANPATH ; manpath -ddd + + + + + + unset PATH ; unset MANPATH ; manpath -dd + + + + + + MANPATH=/usr/share/man manpath -d + + + + + + MANPATH=/usr/share/man manpath -qd + + + + + + unset MANLOCALES ; manpath -L + + + + + + unset MANLOCALES ; manpath -Ld + + + + + + unset MANLOCALES ; manpath -Ldd + + + + + + unset MANLOCALES ; manpath -Lddd + + + + + + MANLOCALES=fr_FR.UTF-8 manpath -Ld + + + + + + MANLOCALES=fr_FR.UTF-8 manpath -Lqd + + + + + + manpath -i + + + +