Python作为Google
的第三大开发语言、Dropbox
的基础语言已经已经越来越得到人们的喜爱,尤其是运维人员,而且很多优秀的运维工具都是Python开发的,引用Python作者的一句话:人生苦短,我用Python。
下面我就和大家简单分享下我在用Python进行开发时使用的一些Python配置方面的小工具。
Python版本管理
我以前使用pythonbrew
来管理我的开发机上面多个Python解析器,现在我使用pyenv
来管理我的Python解析器,github地址,安装和使用直接见github里面的README就行了,用起来还是比较简单的,通过pyenv
可以管理多个Python版本并共存,随时切换想要使用的版本,且这个工具本身不依赖于Python。
环境依赖
根据12Factor规则,应用程序要显示的声明依赖,这里我能想到的一个很好的例子是Ruby
的Gemfile
,这个文件里面声明了一个Ruby
应用程序所依赖的所有依赖,Gemfile
就是对Ruby
程序的依赖声明
,在Python里面对依赖进行声明我想到了pip
。
依赖申明好了,不同的应用程序间如何进行依赖隔离呢,同样Ruby
程序是用bundle exec
,这里为什么要提Ruby
,那时因为我以前配置一个Ruby应用程序感觉非常方便,直接执行script/bootstarp
所有依赖都安装好了,基本直接运行程序就能跑了。
其实Python里面也有一个非常好的依赖隔离的东西virtualenv
,下面简单介绍下基于virtualenv
写了一个简单的Python项目类似Ruby
的bundle
的小工具。
Virtualenv Exec(ve)
源代码如下:
#!/bin/bash
if [[ $# -le 0 ]]; then
echo "Usage: `basename $0` <command> [agrument]..."
fi
if [ -r "$HOME/.verc" ]; then
source "$HOME/.verc"
fi
if [ -z "$VIRTUALENV_DIR" ]; then
VIRTUALENV_DIR=".virtualenv"
fi
DIR=`pwd`
for (( ;; ))
do
if [ -r "$DIR/$VIRTUALENV_DIR/bin/activate" ]; then
source "$DIR/$VIRTUALENV_DIR/bin/activate"
if [ -n "$VIRTUAL_ENV" ]; then
break
fi
fi
if [ "$DIR" == "/" ]; then
break
fi
DIR=`dirname $DIR`
done
if [ -z "$VIRTUAL_ENV" ]; then
echo "virtualenv: not found"
echo
exit 1
else
if [ "$1" == "-v" ]; then
echo "virtualenv: $VIRTUAL_ENV"
echo
shift
fi
exec $@
fi
bootstrap.py
源代码如下:
# -*- coding: utf-8 -*-
if __name__ == '__main__':
exit(1)
import os
import subprocess
import logging
import hashlib
import stat
logger = logging.getLogger(__name__)
class Bootstrap(object):
working_dir = None
bootstrap_dir = None
virtualenv_dir = None
def __init__(self, working_dir=None):
self.working_dir = working_dir or os.getcwd()
self.bootstrap_dir = self._config_dot_dir('BOOTSTRAP_DIR', '.bootstrap')
self.virtualenv_dir = self._config_dot_dir('VIRTUALENV_DIR', '.virtualenv')
if not os.path.isdir(self.bootstrap_dir):
os.makedirs(self.bootstrap_dir)
logger.debug('working_dir: %s', self.working_dir)
logger.debug('bootstrap_dir: %s', self.bootstrap_dir)
logger.debug('virtualenv_dir: %s', self.virtualenv_dir)
self.install_virtualenv()
self.install_ve()
def _config_dot_dir(self, env_name, default_value=None):
value = os.environ.get(env_name)
if not value:
return '%s/%s' % (self.working_dir, default_value)
logger.debug('config %s from envron', env_name)
if os.path.isabs(value):
return value
else:
return '%s/%s' % (self.working_dir, value)
def install_virtualenv(self):
if os.path.isfile('%s/%s' % (self.virtualenv_dir, 'bin/activate')):
return
executable = None
try:
executable = subprocess.check_output(['command', '-v', 'virtualenv-12.0.7/virtualenv.py'])
if not type(executable) is str:
# convert from bytes to str (unicode) under python3
executable = executable.decode()
executable = executable.strip()
except:
pass
if not executable:
virtualenv_tar = '%s/%s' % (self.bootstrap_dir, 'virtualenv.tar.gz')
executable = '%s/%s' % (self.bootstrap_dir, 'virtualenv-12.0.7/virtualenv.py')
self.download('http://mirrors.aliyun.com/pypi/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz',
virtualenv_tar, 'e08796f79d112f3bfa6653cc10840114')
os.system('tar -zxvf "%s" -C "%s"' % (virtualenv_tar, self.bootstrap_dir))
os.chdir(self.bootstrap_dir)
try:
os.system('%s --distribute --no-site-packages "%s"' % (executable, self.virtualenv_dir))
finally:
os.chdir(self.working_dir)
def download(self, source, target, hashing=None):
#if hashing and os.path.isfile(target) and hashing == self.md5sum(target):
#return
if os.system('wget "%s" -O "%s"' % (source, target)) == 0:
return
if os.system('curl "%s" -o "%s"' % (source, target)) == 0:
return
logger.error('Unable to download "%s"' % source)
raise RuntimeError
def mark_executable(self, path):
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def md5sum(self, filename):
return hashlib.md5(open(filename, 'rb').read()).hexdigest()
def install_ve(self):
executable = '%s/%s' % (self.bootstrap_dir, 've')
self.download('https://raw.github.com/baijian/ve/v1.1/ve',
executable, 'fd0f0601c6732ca9a5b3b62e691d68cb')
self.mark_executable(executable)
def ve(self, cmd):
os.system('%s/%s %s' % (self.bootstrap_dir, 've', cmd))
_bootstrap = None
__all_ = ['bootstrap', 've']
def bootstrap(working_dir=None):
global _bootstrap
_bootstrap = Bootstrap(working_dir)
def ve(cmd):
global _bootstrap
if not _bootstrap:
bootstrap()
_bootstrap.ve(cmd)
开始使用
- 创建项目目录
mkdir project && mkdir project/script && cd project
- 编写脚手架
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
source = "https://raw.github.com/baijian/bootstrap.py/v1.2/bootstrap.py"
target = "%s/bootstrap.py" % os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(target):
os.system("wget '%s' -O %s" % (source, target))
from bootstrap import bootstrap, ve
bootstrap(os.path.dirname(os.path.abspath(__file__)) + "/..")
ve("pip install requests==2.4.1")
- 修改依赖申明(可选)
编辑并修改script/bootstrap文件
- 安装项目依赖
script/bootstrap
- 查看项目依赖
ve pip freeze
- 查看项目使用的Python解析器
ve command -v python
其他
如果要使用Python进行web应用的开发,可以选用的框架诸如Flask
、Django
、Tornado
、web.py
,提到web.py
,想到了互联网之子亚伦·斯沃茨
,有兴趣可以看看他的视频~
还有一些有些的优秀的Python运维工具诸如:Ansible
、Saltstack
、Fabric
、Supervisord
等,Python的开源模块的质量非常高,这也是我喜欢Python的一个很重要的原因。