<p>我喜欢现在的答案。还有。。。我觉得在“现实世界”里有一种更干净的方式来做这件事。如果要执行任何大小或规模的项目,尤其是在“多个”环境中,则必须使用节头功能。我想把它与格式良好的可复制代码放在这里,使用一个健壮的现实世界的例子。这是在Ubuntu 14中运行的,但是可以跨平台工作:</p>
<h2>现实世界的简单例子</h2>
<p><b><i>基于环境的配置</b>
<br/>
设置示例(终端):</p>
<blockquote>
<blockquote>
<p>cd ~/my/cool/project
touch local.properties
touch environ.properties
ls -la ~/my/cool/project
-rwx------ 1 www-data www-data 0 Jan 24 23:37 local.properties
-rwx------ 1 www-data www-data 0 Jan 24 23:37 environ.properties</p>
</blockquote>
</blockquote>
<p>设置好权限</p>
<pre><code>>> chmod 644 local.properties
>> chmod 644 env.properties
>> ls -la
-rwxr--r-- 1 www-data www-data 0 Jan 24 23:37 local.properties
-rwxr--r-- 1 www-data www-data 0 Jan 24 23:37 environ.properties
</code></pre>
<p>编辑属性文件。<br/></p>
<h3>文件1:local.properties</h3>
<p><i>这是您的属性文件,位于您的计算机和工作区的本地,包含敏感数据,不要推送到版本控制!!!</i></p>
<pre><code>[global]
relPath=local/path/to/images
filefilters=(.jpg)|(.png)
[dev.mysql]
dbPwd=localpwd
dbUser=localrootuser
[prod.mysql]
dbPwd=5tR0ngpwD!@#
dbUser=serverRootUser
[branch]
# change this to point the script at a specific environment
env=dev
</code></pre>
<h3>文件2:环境属性</h3>
<p><i>此属性文件由所有人共享,更改将推送到版本控制</i></p>
<pre><code>#----------------------------------------------------
# Dev Environment
#----------------------------------------------------
[dev.mysql]
dbUrl=localhost
dbName=db
[dev.ftp]
site=localhost
uploaddir=http://localhost/www/public/images
[dev.cdn]
url=http://localhost/cdn/www/images
#----------------------------------------------------
# Prod Environment
#----------------------------------------------------
[prod.mysql]
dbUrl=http://yoursite.com:80
dbName=db
[prod.ftp]
site=ftp.yoursite.com:22
uploaddir=/www/public/
[prod.cdn]
url=http://s3.amazon.com/your/images/
</code></pre>
<h3>Python文件:readCfg.py</h3>
<p><i>此脚本是一个可重用的片段,用于加载配置文件列表</i>
导入配置分析器
导入操作系统</p>
<pre><code># a simple function to read an array of configuration files into a config object
def read_config(cfg_files):
if(cfg_files != None):
config = ConfigParser.RawConfigParser()
# merges all files into a single config
for i, cfg_file in enumerate(cfg_files):
if(os.path.exists(cfg_file)):
config.read(cfg_file)
return config
</code></pre>
<h3>Python文件:yourCoolProgram.py</h3>
<p><i>此程序将导入上面的文件,并调用“read_config”方法</i></p>
<pre><code>from readCfg import read_config
#merge all into one config dictionary
config = read_config(['local.properties', 'environ.properties'])
if(config == None):
return
# get the current branch (from local.properties)
env = config.get('branch','env')
# proceed to point everything at the 'branched' resources
dbUrl = config.get(env+'.mysql','dbUrl')
dbUser = config.get(env+'.mysql','dbUser')
dbPwd = config.get(env+'.mysql','dbPwd')
dbName = config.get(env+'.mysql','dbName')
# global values
relPath = config.get('global','relPath')
filefilterList = config.get('global','filefilters').split('|')
print "files are: ", fileFilterList, "relative dir is: ", relPath
print "branch is: ", env, " sensitive data: ", dbUser, dbPwd
</code></pre>
<h3>结论</h3>
<p>根据上述配置,现在可以有一个脚本,通过更改“local.properties”中的[branch]env值来完全更改环境。这都是基于良好的配置原则!耶!</p>