python的deploy的代码
2011-06-09 09:21 ubunoon 阅读(564) 评论(0) 编辑 收藏 举报昨天本来想在本地windows上搭建一下python的lighttpd(1.4.28.1)服务器,可以一直是fastcgi启动code.py的错误,错误代码为2,也一直没有找到原因。期待高手给说个配置方式?
写了一个类似的deploy代码,用来进行代码发布到lighttpd服务上,代码还不是很全,只有简单的拷贝与更新,没有服务器上无用代码的删除功能。
#! /usr/bin/env python
#coding=utf-8
# Deploy code to server
import time
import stat
import os
import shutil
__version__ = '0.0.1a'
__author__ = "Damon"
__email__ = "netubu#gmail.com" # change # to @
# Default folder with project and deploy server
server_folder = 'C:\\lighttpd\\htdocs'
project_folder = os.path.dirname( os.path.abspath( __file__ ) )
def has_modify(src_file, dst_file):
'''Compare src_file modify time and dst_file modify time, which
is the newest file. Return True if dst_file is newer. Off couse, dst_file
does not exist also is an modify file
'''
if not os.path.exists( dst_file ):
return True
src_stat = os.stat( src_file )
dst_stat = os.stat( dst_file )
#print 'time',src_file, dst_file, src_stat[ stat.ST_MTIME ] , dst_stat[ stat.ST_MTIME ]
return src_stat[ stat.ST_MTIME ] > dst_stat[ stat.ST_MTIME ]
def get_folder_without_project_folder(folder, project_folder):
folders = []
#print project_folder, folder
while project_folder != folder:
basename = os.path.basename(folder)
folders.append( basename)
folder = os.path.dirname( folder )
return os.sep.join( folders )
def deploy_ignore_wrapper(project_folder, deploy_folder):
def deploy_ignore(path, names):
''' Return a list which doest not copy to dest folder'''
new_names = []
for name in names:
if name.endswith('.pyc') or name.endswith('.pyo') or \
name == os.path.basename( __file__ ):
new_names.append( name )
continue
src_filename = os.path.join( path, name )
folder = get_folder_without_project_folder( path, project_folder )
dest_filename = os.path.join( deploy_folder, folder, name)
if not has_modify(src_filename, dest_filename):
new_names.append( name )
return new_names
return deploy_ignore
def deploy(src, dst, symlinks=False, ignore=None):
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
try:
os.makedirs(dst)
except WindowsError: # folder has already exists does not need to create
pass
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
deploy(srcname, dstname, symlinks, ignore)
else:
shutil.copy2(srcname, dstname)
print '%s --> %s' %(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
try:
shutil.copystat(src, dst)
except WindowsError:
# can't copy file access times on Windows
pass
except OSError, why:
errors.extend((src, dst, str(why)))
if errors:
raise Error, errors
if __name__ == '__main__':
project_name = os.path.basename( project_folder )
deploy_folder = os.path.join( server_folder, project_name )
deploy( project_folder, deploy_folder,
ignore=deploy_ignore_wrapper(project_folder, deploy_folder) )
代码基本上是拷贝了shutil中的copytree进行修改的。
/*
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */