Python deploy code

Source: Internet
Author: User

Yesterday I was trying to build a python Lighttpd (1.4.28.1) server on a local Windows server. It can always be a FastCGI error in starting code. py.CodeIs 2, and no reason is found. Are you looking forward to giving me a configuration?

A similar deploy code is written to publish the code to the Lighttpd service. The code is not complete yet. Only simple copies and updates are required, and no useless code deletion function is provided on the server.

 #  ! /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 # @

# 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 ))

The Code basically copies the copytree in shutil for modification.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.