Python package project

使用 Python 书写项目打包程序(已经存在 shell 的脚本,经过对比 Python 脚本的执行过程更加的可控人性化实现的功能相同)

  1 #!/usr/bin/env python
  2 # _*_coding:utf-8_*_
  3 #  author:  'Edward.Liu'
  4 # dateTime:  '15/12/9'
  5 #   motto:  'Good memory as bad written'
  6 import datetime, time
  7 import os
  8 import shutil
  9 import subprocess
 10 import tarfile
 11 
 12 
 13 class Packages(object):
 14     def __init__(self):
 15         self.SVN_Checked_Directory = "/install/online/"
 16         self.Project_Directory_F = "%scybershop-front/target" % self.SVN_Checked_Directory
 17         self.Project_Directory_B = "%scybershop-web/target" % self.SVN_Checked_Directory
 18         self.Upload_Directory = "/software/backwar/"
 19         self.Project_Directory_F_Name = "cybershop-front-0.0.1-SNAPSHOT.war"
 20         self.Project_Directory_B_Name = "cybershop-web-0.0.1-SNAPSHOT.war"
 21         self.density_name = ['pro', 'demo', 'ptest']
 22         self.bulid_home = "/install/maven/bin/mvn"
 23         self.date_time = datetime.datetime.now().strftime('%Y-%m-%d-%H')
 24 
 25     def Subervison_Check(self):
 26         global SVN_NUMBER
 27         try:
 28             while True:
 29                 SVN_NUMBER = raw_input("\033[32mPleae Input SVN Update Number:\033[0m").strip()
 30                 if SVN_NUMBER.isdigit():
 31                     SVN_NUMBER = int(SVN_NUMBER)
 32                     if os.path.exists(self.SVN_Checked_Directory):
 33                         print "------------------------------"
 34                         os.chdir(self.SVN_Checked_Directory)
 35                         svn_update = "/usr/bin/svn update -r %s" % SVN_NUMBER
 36                         subprocess.call(svn_update, shell=True)
 37                         break
 38                     else:
 39                         print "++++++++++++++++++++++++++++++"
 40                         os.makedirs(self.SVN_Checked_Directory)
 41                         os.chdir(self.SVN_Checked_Directory)
 42                         svn_update = "/usr/bin/svn update -r %s" % SVN_NUMBER
 43                         subprocess.call(svn_update, shell=True)
 44                         break
 45                 else:
 46                     print "\033[31mPlease SVN Number\033[0m"
 47         except KeyboardInterrupt:
 48             print 'ctrl+d or z'
 49 
 50     def Bulid(self):
 51         global env
 52         # 编译项目(分环境)
 53         # 获取生成项目的文件名-- get
 54         bulided_File_Path_F = "%s/%s" % (self.Project_Directory_F, self.Project_Directory_F_Name)
 55         # ---get end
 56         # 编译环境选择--- select---> Maven
 57         for index, value in enumerate(self.density_name):
 58             print index, "Carrefour" + "---->" + value
 59         try:
 60             while True:
 61                 Chose_ENV = raw_input("\033[32mChose Density Environment:\033[0m")
 62                 if Chose_ENV.isdigit():
 63                     Chose_ENV = int(Chose_ENV)
 64                     env = self.density_name[Chose_ENV]
 65                     try:
 66                         if self.density_name[Chose_ENV] == 'pro':
 67                             os.chdir(self.SVN_Checked_Directory)
 68                             bulid_command = "%s clean install -PcarrefourPro -DskipTests" % self.bulid_home
 69                             subprocess.call(bulid_command, shell=True)
 70                             if os.path.isfile(bulided_File_Path_F):
 71                                 print "\033[32mBulid %s SuccessFul\033[0m" % self.density_name[Chose_ENV]
 72                             print "\033[32m--------------------Create TarFiles--------------------\033[0m"
 73                             self.Files_Handle()
 74                             break
 75                         elif self.density_name[Chose_ENV] == 'demo':
 76                             os.chdir(self.SVN_Checked_Directory)
 77                             bulid_command = "%s clean install -Pcarrefour -DskipTests" % self.bulid_home
 78                             subprocess.call(bulid_command, shell=True)
 79                             if os.path.isfile(bulided_File_Path_F):
 80                                 print "\033[32mBulid %s SuccessFul\033[0m" % self.density_name[Chose_ENV]
 81                             print "\033[32m--------------------Create TarFiles--------------------\033[0m"
 82                             self.Files_Handle()
 83                             break
 84                         elif self.density_name[Chose_ENV] == 'ptest':
 85                             os.chdir(self.SVN_Checked_Directory)
 86                             bulid_command = "%s clean install -PcarrefourPtest -DskipTests" % self.bulid_home
 87                             subprocess.call(bulid_command, shell=True)
 88                             if os.path.isfile(bulided_File_Path_F):
 89                                 print "\033[32mBulid %s SuccessFul\033[0m" % self.density_name[Chose_ENV]
 90                             print "\033[32m--------------------Create TarFiles--------------------\033[0m"
 91                             self.Files_Handle()
 92                             break
 93                     except IndexError:
 94                         print "\033[31mSelect error\033[0m"
 95         except KeyboardInterrupt:
 96             print "\033[32m Quit\033[0m"
 97             # select----Maven--->END
 98 
 99     def Files_Handle(self):
100         # 生成文件处理
101         # 文件压缩----tar
102         Tmp_density_dir = "/software/%s%s-%s" % (env, SVN_NUMBER, self.date_time)
103         os.makedirs(Tmp_density_dir)
104         source_fiels = ["%s/%s" % (self.Project_Directory_F, self.Project_Directory_F_Name),
105                         "%s/%s" % (self.Project_Directory_B, self.Project_Directory_B_Name)]
106         for i in range(2):
107             shutil.move(source_fiels[i], Tmp_density_dir)
108         # 创建压缩包
109         os.chdir("/software")
110         tarfile_name = "%s.tar.gz" % Tmp_density_dir.split('/')[2]
111         tar = tarfile.open(tarfile_name, "w:gz")
112         tar.add(Tmp_density_dir.split('/')[2])
113         tar.close()
114         # 创建压缩包---end
115         if os.path.exists(tarfile_name):
116             print "\033[32m----------Delete Temporary Files%s----------\033[0m" % datetime.datetime.now().strftime(
117                 '%Y-%m-%d %H:%M:%S %f')
118             shutil.rmtree(Tmp_density_dir)
119             shutil.move(tarfile_name, self.Upload_Directory)
120             Upload_Files_Name = "%s%s" % (self.Upload_Directory, tarfile_name)
121             print "\033[32mSuccessful Download address:URL/%s\033[0m" % tarfile_name
122         else:
123             print "\033[31m----------Create archive Is Failed%s----------\033[0m" % datetime.datetime.now().strftime(
124                 '%Y-%m-%d %H:%M:%S %f')
125         # 删除临时文件
126         print "\033[32m---------Remove the compiled file%s----------\033[0m" % datetime.datetime.now().strftime(
127             '%Y-%m-%d %H:%M:%S %f')
128         if os.path.exists(Upload_Files_Name):
129             os.chdir("/software")
130             find_tmp = "find %s  -name target" % self.SVN_Checked_Directory
131             porc = subprocess.Popen(find_tmp, shell=True, stdout=subprocess.PIPE)
132             export, err = porc.communicate()
133             out_files = open("path_list.txt", "w")
134             out_files.write(export)
135             out_files.close()
136             fileHandle = open('path_list.txt')
137             for line in fileHandle.readlines():
138                 print "\033[31mRemove Target\033[0m", line
139                 shutil.rmtree(line.strip('\n'))
140             fileHandle.close()
141             os.remove("path_list.txt")
142             # 删除文件----end
143 
144     def usage(self):
145         script_name = "packages.py"
146         print "\033[31m*****************************************\033[0m"
147         print "\033[31m|------------Packages Useage------------|\033[0m"
148         print "\033[32m|------------./%s--------------|\033[0m" % script_name
149         print "\033[32m|------------<path>/%s---------|\033[0m" % script_name
150         print "\033[32m|----------脚本执行过程2部人工干预------|\033[0m"
151         print "\033[32m|----------1.收到输入 SVN 版本号--------|\033[0m"
152         print "\033[32m|----------2.选择需要打包的环境---------|\033[0m"
153         print "\033[32m|----------3.复制输出下载链接进行下载---|\033[0m"
154         print "\033[31m******************************************\033[0m"
155 
156 
157 if __name__ == '__main__':
158     Run_packages = Packages()
159     Run_packages.usage()
160     Run_packages.Subervison_Check()
161     Run_packages.Bulid()
View Code

 

你可能感兴趣的:(Python package project)