Skip to content

Commit 96b070f

Browse files
author
huzhonglan
committed
虚拟环境脚本
1 parent 933bf76 commit 96b070f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

create_venv_script.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding=utf-8
2+
3+
"""
4+
virtualenv定制脚本接受3个扩展函数
5+
extend_parser(optparse_parser):添加额外的选项
6+
adjust_options(optioins, args): 改变当前的选项
7+
after_install: 在默认的环境安装好之后,执行其他工作,通过这个函数定制
8+
"""
9+
import os
10+
import logging
11+
import subprocess
12+
import virtualenv
13+
14+
15+
virtualenv_path = subprocess.check_output(['which', 'virtualenv']).strip()
16+
EXPORT_TEXT = ''
17+
ROOT_PATH = '/home/python/venv'
18+
19+
def extend_parse(parser):
20+
parser.add_option('-r', '-req', action='append', type='string', dest='reqs',
21+
help="specify additional required packages", default=[])
22+
23+
def adjust_options(options, args):
24+
if not args:
25+
return
26+
base_dir = args[0]
27+
args[0] = os.path.join(ROOT_PATH, base_dir)
28+
29+
def after_install(options, home_dir):
30+
if not options.reqs:
31+
logging.warning('Warn:You maybe need specify some required packages!')
32+
for req in options.reqs:
33+
subprocess.call(['{} /bin/pip'.format(home_dir), 'install', req])
34+
35+
def main():
36+
text = virtualenv.create_bootstrap_script(EXPORT_TEXT,python_version='3.5')
37+
print('%s' % virtualenv_path)
38+
with open(virtualenv_path, 'w') as f:
39+
f.write(text)
40+
41+
42+
if __name__ == '__main__':
43+
main()
44+
45+
46+
"""
47+
创建一个自动安装了Django, Flask的虚拟环境
48+
>virtualenv -r django -r flask
49+
"""

0 commit comments

Comments
 (0)