From c5279540b8de63f93c64968aeb46f17ddc294715 Mon Sep 17 00:00:00 2001 From: Boruch Baum Date: Wed, 18 Jan 2023 19:38:10 -0500 Subject: [PATCH] Simplify pip packaging and remove cruft files + file setup.py had been placing the following files and directories in improper places as follows: + installed directory doc to ~/.local/lib/python3.10/site-packages/ + installed executable nerd-dictation to ~/.local/lib/python3.10/site-packages/ + installed directory nerd-dictation to ~/.local/lib/ + installed executable and readme.rst to ~/.local/nerd-dictation/ + file setup.py had NOT been performing the following: + installing directory doc to ~/.local/share/doc/nerd-dictation + installing documentation beyond readme.rst + installing example nerd-dictation.py files + installing executable nerd-dictation to ~/.local/bin + file setup.py suffered from needless complexity due to being placed in a sub folder instead of the based directory as is standard and expected practice + file pyproject.toml did not contain the minimum content as prescribed by package setuptools' documentation + this file exists only for the purpose of setuptools, so it was incongruous that it existed as it should, in the package root directory, even though the setup.py script to actually run setuptools was hidden in a sub-sub-folder. + a config file nerd-dictation.py is described as default, but had not been installed by default, so it wasn't really the default. --- package/python/readme.rst | 11 ------- package/python/setup.py | 68 --------------------------------------- package/readme.rst | 7 +++- pyproject.toml | 4 +++ setup.py | 57 ++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 80 deletions(-) delete mode 100644 package/python/readme.rst delete mode 100644 package/python/setup.py create mode 100644 setup.py diff --git a/package/python/readme.rst b/package/python/readme.rst deleted file mode 100644 index 6037536..0000000 --- a/package/python/readme.rst +++ /dev/null @@ -1,11 +0,0 @@ -################## -Packaging with PIP -################## - -To install nerd-dictaiton as a package from pip, run: - -.. code-block:: sh - - pip install package/python - -On a typical Linux system this will install nerd-dictation to ``~/.local/bin``. diff --git a/package/python/setup.py b/package/python/setup.py deleted file mode 100644 index 697d2b6..0000000 --- a/package/python/setup.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later - -import os -import setuptools -import shutil -import tempfile - - -def read_file_as_utf8(filepath): - with open(filepath, encoding="utf-8") as fh: - return fh.read() - - -def main(base_dir): - root_dir = os.path.normpath(os.path.join(base_dir, "..", "..")) - - if not os.path.exists(os.path.join(root_dir, "nerd-dictation")): - root_dir = base_dir - CWD = os.getcwd() - - NERD_DICTATION_DST = os.path.join(base_dir, "nerd-dictation") - README_DST = os.path.join(base_dir, "doc", "readme.rst") - os.makedirs(os.path.join(base_dir, "doc"), exist_ok=True) - - # Path pairs to copy. - COPY_PATH_PAIRS = ( - # As this is a single, self contained script, this is all that's needed. - (os.path.join(root_dir, "nerd-dictation"), NERD_DICTATION_DST), - (os.path.join(root_dir, "readme.rst"), README_DST), - ) - - if root_dir != base_dir: - for src, dst in COPY_PATH_PAIRS: - shutil.copy2(src, dst) - - # `setuptools` expects relative paths. - NERD_DICTATION_DST = os.path.relpath(NERD_DICTATION_DST, CWD) - README_DST = os.path.relpath(README_DST, CWD) - - setuptools.setup( - name="nerd-dictation", - version="0.0.0", - maintainer="Campbell Barton", - maintainer_email="ideasman42@gmail.com", - description="Offline Speech to Text for Desktop", - long_description=read_file_as_utf8(README_DST), - long_description_content_type="text/x-rst", - url="https://github.com/ideasman42/nerd-dictation", - classifiers=[ - "Programming Language :: Python :: 3", - "Topic :: Office :: Productivity", - "Topic :: Accessibility :: Voice", - "Operating System :: POSIX :: Linux", - "License :: OSI Approved :: GPL v3.0", - ], - scripts=[NERD_DICTATION_DST], - data_files=[("nerd-dictation", [NERD_DICTATION_DST, README_DST])], - packages=[""], - package_data={"": [NERD_DICTATION_DST, README_DST]}, - include_package_data=True, - install_requires=["vosk"], - python_requires=">=3.8", - ) - - -if __name__ == "__main__": - main(os.path.abspath(os.path.dirname(__file__))) diff --git a/package/readme.rst b/package/readme.rst index bf74ee7..c7a1c39 100644 --- a/package/readme.rst +++ b/package/readme.rst @@ -5,5 +5,10 @@ Packaging While nerd-dictation is intended to run without the need for a build/packaging step, some users may find it convenient. -- `Python's pip (setup-tools) `_. +- Python's pip (setup-tools) + +From the parent of the project directory, run: + + pip3 install ./nerd-dictation + - `GNU Guix `_. diff --git a/pyproject.toml b/pyproject.toml index c228223..950aff6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,6 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + [tool.black] line-length = 119 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0926a52 --- /dev/null +++ b/setup.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later + +import setuptools +import os +import re +import glob +import shutil + +def read_file_as_utf8(filepath): + with open(filepath, encoding="utf-8") as fh: + return fh.read() + + +def main(base_dir): + + doc_dir="share/doc/nerd-dictation" + fn="nerd-dictation.py" + doc_list=[(doc_dir,list(filter(re.compile(r'.*rst').match, os.listdir("./"))))] + for ex in glob.glob("examples/*/"): + doc_list.append((os.path.join(doc_dir,ex), [os.path.join(ex,fn)])) + + if os.geteuid()!=0 and os.name=="posix": + config_dir=os.path.join(os.path.expanduser("~"),".config/nerd-dictation") + if not os.path.isdir(config_dir): + os.makedirs(config_dir) + shutil.copy("examples/default/nerd-dictation.py",config_dir) + # For cross-platform assistance, see: (pypi: appdirs) (github: platformdirs) + # For root install, first support /etc/nerd-dictation + + setuptools.setup( + name="nerd-dictation", + version="0.0.0", + maintainer="Campbell Barton", + maintainer_email="ideasman42@gmail.com", + description="Offline Speech to Text for Desktop", + long_description=read_file_as_utf8("readme.rst"), + long_description_content_type="text/x-rst", + url="https://github.com/ideasman42/nerd-dictation", + classifiers=[ + "Programming Language :: Python :: 3", + "Topic :: Office :: Productivity", + "Topic :: Accessibility :: Voice", + "Operating System :: POSIX :: Linux", + "License :: OSI Approved :: GPL v3.0", + ], + scripts=["nerd-dictation"], + data_files=doc_list, + packages=[""], + package_data={"nerd-dictation": [""]}, + install_requires=["vosk"], + python_requires=">=3.8", + ) + + +if __name__ == "__main__": + main(os.path.abspath(os.path.dirname(__file__)))