简单的python包脚手架实用程序。
simplepkg的Python项目详细描述
simplepkg公司
https://pypi.org/project/simplepkg/
simplepkg创建一个简单的python包
文件包含基本模板,创建的包立即可以通过pip安装,或上载到PyPI。在
请将TestPyPI用于开发或测试目的:https://test.pypi.org/
$ simplepkg demo
demo/
├── demo
│ ├── data
│ │ └── config
│ ├── logs
│ │ └── demo.log
│ ├── modules
│ │ ├── demo_module.py
│ │ └── __init__.py
│ ├── test
│ │ ├── __init__.py
│ │ ├── test_app.py
│ │ ├── test_demo_module.py
│ │ └── test_demo.py
│ ├── tmp
│ │ └── tmp
│ ├── app.py
│ ├── __init__.py
│ └── __main__.py
├── LICENSE
├── Makefile
├── MANIFEST.in
├── README.md
└── setup.py
建议在venv中安装,对于Debian:
^{pr2}$基本用途
$ pip install simplepkg
$ simplepkg <pkg_name> [options]
$ cd <pkg_name>
$ python3 -m <pkg_name>
# use pip to install and run the newly generated package
$ pip install --upgrade setuptools wheel
$ pip install .
$ <pkg_name>
usage: simplepkg <pkg_name> [options]
Simple python package scaffolding utility
positional arguments:
pkg_name name of your new package
optional arguments:
-h, --help show this help message and exit
-g, --git git init this package upon creation (requires git)
-nl, --no-license do _NOT_ include a license, package will not meet official PyPI requirements
--mit include MIT license, default
--apache include Apache license
--bsd2 include BSD2 license
--bsd3 include BSD3 license
--gplv2 include GPLv2 license
--gplv3 include GPLv3 license
--unlicense include Unlicense license
运行单元测试
$ cd <pkg_name>
$ python -m unittest
构建并上传到PyPI
$ python3 -m pip install --upgrade setuptools wheel
$ python3 setup.py sdist bdist_wheel
$ python3 -m pip install --upgrade twine
# Upload to _test_ PyPI - test your packages here: https://test.pypi.org/
$ twine upload --repository testpypi dist/*
# Upload to PyPI
$ twine upload dist/*
使用Makefile构建并上载到PyPI
# Remove all directories generated in previous builds, and all __pycache__ directories
$ make clean
# Build distributable components/wheel
$ make build
# Upload to TestPyPI
$ make testpypi
# Upload to PyPI
$ make pypi
关于默认目录
为什么是数据?
数据非常常见,通常用于config或sqlite。如果您没有数据,或者希望将其保存在不同的位置—主目录中的点文件,$XDG_CONFIG_DIR
,等等,只需删除该目录并更新MANIFEST.in
。在
为什么是日志?
您可能希望将日志移动到另一个位置,如/var/logs
,主目录中的一个点文件,$XDG_CONFIG_DIR
,等等—您可以这样做—只需确保您的项目创建/验证所需的位置,删除logs dir,更新<pkg_name>/<pkg_name>/__init__.py
中的日志文件处理程序,并更新MANIFEST.in
。在
为什么要测试?
test目录简化了运行测试。您可以从根目录运行python3 -m unittest
来运行所有测试。测试目录被显式地排除在setup.py
中,因此它不会包含在安装中。如果您想在包中包含测试,只需更新setup.py
。在
为什么是tmp?
有时您可能需要生成文件并将它们发送到其他地方。您可以在tmp
目录中生成临时文件,发送它们,然后删除tmp
之外的所有文件。请注意,以任何其他方式使用此目录将阻止在卸载包时删除该目录,有关详细信息,请参阅下面的示例。这不会造成任何破坏,但它确实会在python环境中丢弃不必要的文件(卸载时会向用户提供一个警告,指出哪些文件)。如果不需要临时文件或计划使用其他位置存放临时文件,如/tmp
,则可以删除tmp
目录并更新MANIFEST.in
。在
有用的程序包功能
# Installed modules can be called from anywhere, this function allows you
# to interact with local package locations regardless of the user's
# working directory.
def get_path(path=''):
"""
Append your relative path with absolute root, or send empty call to
get project root. Primarily designed for accessing files local to
the app when deployed as a module.
"""
root = Path(__file__).parent
path = Path(f'{root}/{path}')
return path
# If utilizing the tmp directory this function will remove all files in
# in accordance with the tmp guidelines in 'Regarding the default
# directories'.
def shutdown():
"""
Clean up after run. Deletes all directories and files in the tmp folder.
Creates a single file in the tmp folder, required for clean uninstall.
"""
tmp_path = get_path('tmp')
for leaf in reversed(list(tmp_path.rglob('*'))):
try:
if leaf.is_file():
leaf.unlink()
elif leaf.is_dir():
leaf.rmdir()
except Exception:
msg = (f'Error deleting child item: {leaf}')
logging.error(msg)
tmp_uninstaller = Path(f'{tmp_path}/tmp')
tmp_uninstaller.touch()
- 项目
标签: