python的deploy的代碼

來源:互聯網
上載者:User

昨天本來想在本地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進行修改的。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.