python setup.py 安装测试

本文介绍了如何使用Python的setuptools创建可安装的模块,并通过pip进行安装与卸载的过程。详细展示了配置setup.py文件的方法,模块内部实现细节,以及如何进行单元测试确保模块功能正确。

1.setup.py

root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# cat setup.py
#coding:utf-8

try:
    #从setuptools模块中导入setup进行安装.
    from setuptools import setup
except ImportError:
    from distutils.core import setup


#软件作者填写的软件信息.
config = {
    'description': 'My Project ....',
    'author': 'My Name TPlinux',
    'url': 'URL to get it at like tplinux.cn',
    'download_url': 'Where to download it. tplinux.cn/downloads/xxx.tar.gz',
    'author_email': 'My email. tplinux@163.com',
    'version': '0.1',
    'install_requires': ['hyhrm'],     #安装我的软件需要的环境
    'packages': ['hyhrm'],
    'package_dir': {'hyhrm':'src/hyhrm'},
    'package_data': {'hyhrm':['data/*.dat']},
    'scripts': ['Scripts/hyhrm'],
    'name': 'hyhrm'
}

#调用setup函数进行安装.
setup(**config)
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton#

 

2.------------------------------------------src

root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# cat src/hyhrm/myrm.py
#!/usr/bin/python
#coding:utf-8

import time
import sys
import os
import shutil


PATH_TRASH = '/home/tp/.trash/'
def main():
    '''
    myrm file1 file2 file3
    myrm dir1
    argv[0]: myrm
    argv[1]: file1
    argv[2]: file2
    argv[3]: file3
    ...
    os.path: 处理目录相关操作
    time.time(): 获取系统时间
    '''
    #print('test:',sys.argv)
    #print('[1:]:',sys.argv[1:])
    if len(sys.argv) < 2:
        print('this tool like rm')
        print(sys.argv[0], ' file1 ')

    for arg_file in sys.argv[1:]:
        #print('arg_file:{}'.format(arg_file))
        filename = str(time.time()).split('.')[0] + arg_file
        #print('newfile:{}'.format(os.path.join(PATH_TRASH, filename)))
        shutil.move(arg_file, os.path.join(PATH_TRASH, filename))
    

if __name__ == '__main__':
    if not os.path.exists(PATH_TRASH):
        os.mkdir(PATH_TRASH)

    main()
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton

3.对《笨办法学python》第46个练习进行测试。

 

root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# cat logs/log.txt
1. 文件目录树
2.执行nosetests 进程测试
tp@VM-103-136-ubuntu:~/stu_python/projects/skeleton$ nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.003s    #执行了一个测试
OK
tp@VM-103-136-ubuntu:~/stu_python/projects/skeleton$
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# tree
.
├── bin
├── logs
│   └── log.txt
├── Scripts
│   ├── hyhrm
│   ├── install.py
│   └── remove.sh
├── setup.py
├── src
│   └── hyhrm
│       ├── data
│       │   ├── myrm.py
│       │   └── remove.sh
│       ├── __init__.py
│       ├── __init__.pyc
│       └── myrm.py
└── tests
    ├── hyhrm_tests.py
        └── __init__.py

        7 directories, 12 files
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# nosetests
.
----------------------------------------------------------------------
Ran 1 test in 0.003s

OK
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# tree
.
    ├── bin
    ├── logs
    │   └── log.txt
    ├── Scripts
    │   ├── hyhrm
    │   ├── install.py
    │   └── remove.sh
    ├── setup.py
    ├── src
    │   └── hyhrm
    │       ├── data
    │       │   ├── myrm.py
    │       │   └── remove.sh
    │       ├── __init__.py
    │       ├── __init__.pyc
    │       └── myrm.py
    └── tests
        ├── hyhrm_tests.py
            ├── hyhrm_tests.pyc
                ├── __init__.py
                    └── __init__.pyc

7 directories, 14 files
多了pyc

 

3.使用pip 安装自己的模块
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# python setup.py install
running install
running bdist_egg
running egg_info
writing requirements to hyhrm.egg-info/requires.txt
writing hyhrm.egg-info/PKG-INFO
writing top-level names to hyhrm.egg-info/top_level.txt
writing dependency_links to hyhrm.egg-info/dependency_links.txt
reading manifest file 'hyhrm.egg-info/SOURCES.txt'
writing manifest file 'hyhrm.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/hyhrm
copying build/lib.linux-x86_64-2.7/hyhrm/myrm.py -> build/bdist.linux-x86_64/egg/hyhrm
copying build/lib.linux-x86_64-2.7/hyhrm/__init__.py -> build/bdist.linux-x86_64/egg/hyhrm
byte-compiling build/bdist.linux-x86_64/egg/hyhrm/myrm.py to myrm.pyc
byte-compiling build/bdist.linux-x86_64/egg/hyhrm/__init__.py to __init__.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
installing scripts to build/bdist.linux-x86_64/egg/EGG-INFO/scripts
running install_scripts
running build_scripts
creating build/bdist.linux-x86_64/egg/EGG-INFO/scripts
copying build/scripts-2.7/hyhrm -> build/bdist.linux-x86_64/egg/EGG-INFO/scripts
copying build/scripts-2.7/install.py -> build/bdist.linux-x86_64/egg/EGG-INFO/scripts
changing mode of build/bdist.linux-x86_64/egg/EGG-INFO/scripts/hyhrm to 755
changing mode of build/bdist.linux-x86_64/egg/EGG-INFO/scripts/install.py to 755
copying hyhrm.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying hyhrm.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying hyhrm.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying hyhrm.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying hyhrm.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist/hyhrm-0.1-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing hyhrm-0.1-py2.7.egg
Copying hyhrm-0.1-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adding hyhrm 0.1 to easy-install.pth file
Installing hyhrm script to /usr/local/bin
Installing install.py script to /usr/local/bin

Installed /usr/local/lib/python2.7/dist-packages/hyhrm-0.1-py2.7.egg
Processing dependencies for hyhrm==0.1
Finished processing dependencies for hyhrm==0.1

可以看到Script/下的文件被安装到/usr/local/bin/目录下
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# ll /usr/local/bin/install.py
-rwxr-xr-x 1 root root 169 Nov 30 00:08 /usr/local/bin/install.py*
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# ll /usr/local/bin/hyhrm
-rwxr-xr-x 1 root root 159 Nov 30 00:08 /usr/local/bin/hyhrm*
测试hyhrm
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# echo "test" >> test.c
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# hyhrm test.c
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# which hyhrm
/usr/local/bin/hyhrm

测试导入
4.import hyhrm
In [1]: import hyhrm

In [2]: dir(hyhrm)
        Out[2]:
        ['__builtins__',
        '__doc__',
        '__file__',
        '__loader__',
        '__name__',
        '__package__',
        '__path__']

        In [3]: hyhrm.__name__
        Out[3]: 'hyhrm'

In [4]: type(hyhrm)
        Out[4]: module

In [5]: print hyhrm
        <module 'hyhrm' from '/usr/local/lib/python2.7/dist-packages/hyhrm-0.1-py2.7.egg/hyhrm/__init__.pyc'>


5.测试卸载
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton# pip uninstall hyhrm
Uninstalling hyhrm:
/usr/local/lib/python2.7/dist-packages/hyhrm-0.1-py2.7.egg
Proceed (y/n)? y
Successfully uninstalled hyhrm
root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton#

root@VM-103-136-ubuntu:/home/tp/stu_python/projects/skeleton#

---------------------------------------

主要可以使用pip 进程卸载。

调用原理:

ubuntu@VM-103-136-ubuntu:~$ cat /usr/local/bin/hyhrm
#!/usr/bin/python
# EASY-INSTALL-SCRIPT: 'hyhrm==0.1','hyhrm'
__requires__ = 'hyhrm==0.1'
import pkg_resources
pkg_resources.run_script('hyhrm==0.1', 'hyhrm')
                  redis-check-rdb            virtualenv-clone           
ubuntu@VM-103-136-ubuntu:~$ cat /usr/local/bin/install.py
#!/usr/bin/python
# EASY-INSTALL-SCRIPT: 'hyhrm==0.1','install.py'
__requires__ = 'hyhrm==0.1'
import pkg_resources
pkg_resources.run_script('hyhrm==0.1', 'install.py')
ubuntu@VM-103-136-ubuntu:~$

 

bu

转载于:https://my.oschina.net/tplinuxhyh/blog/796482

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值