-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
366 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
NAME=libpnu | ||
SECTION=3 | ||
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 | ||
|
||
package: man/${NAME}.${SECTION}.gz # src/${NAME}/po/en/${NAME}.mo src/${NAME}/po/fr/${NAME}.mo | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,58 @@ | ||
# libpnu | ||
# Installation | ||
pip install [pnu-libpnu](https://pypi.org/project/pnu-libpnu/) | ||
|
||
# LIBPNU(3) | ||
|
||
## NAME | ||
libpnu — Common utility functions for the PNU project | ||
|
||
## SYNOPSIS | ||
import **libpnu** | ||
|
||
*libpnu*.**initialize_debugging**(String *program_name*) | ||
|
||
*libpnu*.**handle_interrupt_signals**(Function *handler_function*) | ||
|
||
List *libpnu*.**locate_directory**(String *directory*) | ||
|
||
## DESCRIPTION | ||
The **initialize_debugging()** function sets up the Python logging module with a syslog style kind of console log using *program_name* as the program name. | ||
By default, the logging level is set at WARNING or more, but can be lowered by calling **logging.disable()**, | ||
for example with the *logging.NOTSET* parameter to get DEBUG or more logging level. | ||
|
||
The **handle_interrupt_signals()** function calls the specified *handler_function* to process the SIGINT and SIGPIPE signals, | ||
usually to avoid an ugly trace dump to the console in case of interrupt (Control-C pressed or broken pipe). | ||
|
||
The **locate_directory()** function searches the specified *directory* in a variety of possible other directories, | ||
depending on the operating system used (Unix, Windows) and the fact that a package can be user or system installed. | ||
It is intended to be used when the directory can't be directly found, and returns a list of paths where the directory searched has been found. | ||
|
||
For example, if the argument is "/usr/local/etc/man.d", we'll search for "usr/local/etc/man.d", "local/etc/man.d", "etc/man.d" (more likely) and "man.d" | ||
in a list of user's local Python package directories | ||
("$HOME/.local" on Unix, "$APPDATA/python", "$HOMEPATH/appdata/roaming/python", "$USERPROFILE/appdata/roaming/python" on Windows) | ||
and system wide Python package base directories (given by *sys.base_prefix*: "/usr/local" on Unix, "C:/Program Files/Python3x" on Windows). | ||
|
||
## ENVIRONMENT | ||
The following environment variables can be used to identify the user identity and home directory. | ||
|
||
On Unix: *HOME*, *LOGNAME*, *USER*, *LNAME* and *USERNAME*. | ||
|
||
On Windows: *APPDATA*, *HOMEPATH* and *USERPROFILE*. | ||
|
||
## STANDARDS | ||
The **libpnu** library is not a standard UNIX one. | ||
|
||
It 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 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
.Dd February 28, 2022 | ||
.Dt LIBPNU 3 | ||
.Os | ||
.Sh NAME | ||
.Nm libpnu | ||
.Nd Common utility functions for the PNU project | ||
.Sh SYNOPSIS | ||
.Em import libpnu | ||
.Pp | ||
.Fo libpnu.initialize_debugging | ||
.Fa "String program_name" | ||
.Fc | ||
.Fo libpnu.handle_interrupt_signals | ||
.Fa "Function handler_function" | ||
.Fc | ||
.Ft List | ||
.Fo libpnu.locate_directory | ||
.Fa "String directory" | ||
.Fc | ||
.Sh DESCRIPTION | ||
The | ||
.Fn initialize_debugging | ||
function sets up the Python logging module with a syslog style kind of console log using | ||
.Fa program_name | ||
as the program name. | ||
By default, the logging level is set at WARNING or more, but can be lowered by calling | ||
.Fn logging.disable , | ||
for example with the logging.NOTSET parameter to get DEBUG or more logging level. | ||
.Pp | ||
The | ||
.Fn handle_interrupt_signals | ||
function calls the specified | ||
.Fa handler_function | ||
to process the SIGINT and SIGPIPE signals, | ||
usually to avoid an ugly trace dump to the console | ||
in case of interrupt (Control-C pressed or broken pipe). | ||
.Pp | ||
The | ||
.Fn locate_directory | ||
function searches the specified | ||
.Fa directory | ||
in a variety of possible other directories, | ||
depending on the operating system used (Unix, Windows) | ||
and the fact that a package can be user or system installed. | ||
It is intended to be used when the directory can't be directly found, | ||
and returns a list of paths where the directory searched has been found. | ||
.Pp | ||
For example, if the argument is | ||
.Pa "/usr/local/etc/man.d" , | ||
we'll search for "usr/local/etc/man.d", "local/etc/man.d", "etc/man.d" (more likely) and "man.d" | ||
in a list of user's local Python package directories | ||
("$HOME/.local" on Unix, "$APPDATA/python", "$HOMEPATH/appdata/roaming/python", "$USERPROFILE/appdata/roaming/python" on Windows) | ||
and system wide Python package base directories (given by | ||
.Em sys.base_prefix : | ||
"/usr/local" on Unix, "C:/Program Files/Python3x" on Windows). | ||
.Sh ENVIRONMENT | ||
The following environment variables can be used to identify the user identity and home directory. | ||
.Pp | ||
On Unix: | ||
.Ev HOME , | ||
.Ev LOGNAME , | ||
.Ev USER , | ||
.Ev LNAME | ||
and | ||
.Ev USERNAME . | ||
.Pp | ||
On Windows: | ||
.Ev APPDATA , | ||
.Ev HOMEPATH | ||
and | ||
.Ev USERPROFILE . | ||
.Sh STANDARDS | ||
The | ||
.Nm | ||
library is not a standard UNIX one. | ||
.Pp | ||
It tries to follow the PEP 8 style guide for Python code. | ||
.Sh PORTABILITY | ||
To be tested under Windows. | ||
.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[build-system] | ||
requires = [ | ||
"setuptools>=42", | ||
"wheel" | ||
] | ||
build-backend = "setuptools.build_meta" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[metadata] | ||
name = pnu_libpnu | ||
description = Common utility functions for the PNU project | ||
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 = [email protected] | ||
url = https://github.com/HubTou/libpnu/ | ||
project_urls = | ||
Bug Tracker = https://github.com/HubTou/libpnu/issues | ||
keywords = pnu-project | ||
classifiers = | ||
Development Status :: 5 - Production/Stable | ||
Environment :: Console | ||
Intended Audience :: Developers | ||
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.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 | ||
|
||
[options] | ||
package_dir = | ||
= src | ||
packages = find: | ||
python_requires = >=3.3 | ||
|
||
[options.packages.find] | ||
where = src | ||
|
||
[options.data_files] | ||
man/man3 = | ||
man/libpnu.3.gz | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"""Wrapper for the source code files""" | ||
from .main import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python | ||
""" libpnu - Common utility functions for the PNU project | ||
License: 3-clause BSD (see https://opensource.org/licenses/BSD-3-Clause) | ||
Author: Hubert Tournier | ||
""" | ||
|
||
import getpass | ||
import logging | ||
import os | ||
import pwd | ||
import signal | ||
import sys | ||
|
||
# Version string used by the what(1) and ident(1) commands: | ||
ID = "@(#) $Id: libpnu - Common utility functions for the PNU project v1.0.0 (February 28, 2022) by Hubert Tournier $" | ||
|
||
|
||
################################################################################ | ||
def initialize_debugging(program_name): | ||
"""Set up debugging""" | ||
console_log_format = program_name + ": %(levelname)s: %(message)s" | ||
logging.basicConfig(format=console_log_format, level=logging.DEBUG) | ||
logging.disable(logging.INFO) | ||
|
||
|
||
################################################################################ | ||
def handle_interrupt_signals(handler_function): | ||
"""Process interrupt signals""" | ||
signal.signal(signal.SIGINT, handler_function) | ||
signal.signal(signal.SIGPIPE, handler_function) | ||
|
||
|
||
################################################################################ | ||
def locate_directory(directory): | ||
"""Return a list of paths containing the specified directory""" | ||
directories_list = [] | ||
|
||
parts = [] | ||
if os.sep in directory: | ||
parts = directory.split(os.sep) | ||
elif os.altsep and os.altsep in directory: | ||
parts = directory.split(os.altsep) | ||
else: | ||
parts = [directory] | ||
|
||
# Make absolute paths relative: | ||
if parts and not parts[0]: | ||
parts = parts[1:] | ||
|
||
# For example, if the argument is "/usr/local/etc/man.d", we'll search: | ||
# "usr/local/etc/man.d", "local/etc/man.d", "etc/man.d" and "man.d" | ||
# in a list of user's local Python package directories | ||
# and system wide Python package directories | ||
while parts: | ||
directory = os.sep.join(parts) | ||
|
||
# First searching in the user's local Python packages directory: | ||
if os.name == "posix": | ||
if "HOME" in os.environ: | ||
dir_tested = os.environ["HOME"] + os.sep + ".local" + os.sep + directory | ||
if os.path.isdir(dir_tested): | ||
directories_list.append(dir_tested) | ||
else: | ||
user = getpass.getuser() | ||
if user: | ||
home_directory = pwd.getpwnam(user)[5] | ||
dir_tested = home_directory + os.sep + ".local" + os.sep + directory | ||
if os.path.isdir(dir_tested): | ||
directories_list.append(dir_tested) | ||
|
||
elif os.name == "nt": | ||
last_part = os.sep + "python" + os.sep + directory | ||
mid_part = os.sep + "appdata" + os.sep + "roaming" | ||
if "APPDATA" in os.environ: | ||
dir_tested = os.environ["APPDATA"] + last_part | ||
elif "HOMEPATH" in os.environ: | ||
dir_tested = os.environ["HOMEPATH"] + mid_part + last_part | ||
elif "USERPROFILE" in os.environ: | ||
dir_tested = os.environ["USERPROFILE"] + mid_part + last_part | ||
if os.path.isdir(dir_tested): | ||
directories_list.append(dir_tested) | ||
|
||
# Next trying in Python's base installation prefix: | ||
dir_tested = sys.base_prefix + os.sep + directory | ||
if os.path.isdir(dir_tested): | ||
directories_list.append(dir_tested) | ||
|
||
# Remove the first part: | ||
parts = parts[1:] | ||
|
||
return directories_list | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(0) |