Python基础学习:svn导出差异文件脚本

简介:

由于是刚接触python不久,所以很多都不是很熟练,只能是用到什么查点什么。所以如果有什么bug或者不严谨的语法或其他,希望各位看客指正。

鉴于公司的平台研发部门需求想直接把svn中的差异代码导出并打包自动上传到指定的服务器上,然后在从指定的服务器上进行一个发布更新。由于我们开发和发布服务器的环境很特殊,中间牵扯到很多网络代理。所以才这么麻烦。

要求如下:

1、自动导出指定版本之间的差异文件

2、根据给定的选项过滤出指定的文件夹以及文件;例如给定选项 a ,那就导出的文件中只保留admin的内容

3、自动打包这些内容并按照当前的时间命名

4、以FTP模式上传到指定的服务器


主要还是在windows下操作这些,实在想不出什么好的方法,于是网络搜索求助。网络真是个神奇的东西,当然我还是没有搜到任何结果。于是加了一些脚本的群,随机的找一个管理员问下有没有相关的脚本或思路。真是天无绝人之路。第一个请教的哥们就给了我一个回答。python可以搞定(当然给的指导肯定不止这些)。

于是当下又在学习python,顺便就用这个来实现(其实是不知道用什么来操作的)


在多次google、baidu之后。写了以下的脚本,目前测试是能满足基本需求的:

python的环境需求:py32-pysvn, python-3.2

pysvn下载官网:http://pysvn.tigris.org/

python的官网就不用提供了吧。

使用方法:

    在windows下dos里切换到脚本存放目录,然后使用脚本内给的用法进行脚本导出。导出的文件夹是相对脚本存放路径来的。

下面贴出代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#-*- coding: utf-8 -*-
#!/usr/bin/env python
 
# ====================================================================
#
# svnchanged_export.py
#
# Export Files in a revision Range
# Usage: python SCRIPT_NAME.py -r beginRev:endRev [ --username user --password passwd ] svnurl site_version(a | s | p)
# site_version: a [admin] s [static] p [platform]
#
# ====================================================================
 
import  pysvn  # http://pysvn.tigris.org/
import  getopt, time, string, sys, shutil
import  os, urllib, tarfile, getpass
import  unicodedata
from  urllib.parse  import  urlparse
from  ftplib  import  FTP
 
# Options by default
date_folder = time.strftime(r "%Y%m%d%H%M%S" , time.localtime())
#site_version="p"
#targetPath = "."    # Current directory
export_dir = "xxxx"    # Change into a folder you want to export, The store path relative to the script
username  =  ""
password  =  ""
url  =  ""
ftp_host = "xxx.xxx.xxx.xxx"
ftp_port = xxx
ftp_user = 'xxxx'
ftp_pass = 'xxxx'
 
revision_min  =  pysvn.Revision( pysvn.opt_revision_kind.number,  0  )
revision_max  =  pysvn.Revision( pysvn.opt_revision_kind.head )
hasRevision  =  False
 
current_dir  =  os.getcwd()
os.chdir(r '%s/%s'  % (os.getcwd(),export_dir))
os.makedirs(r '%s'  % (date_folder))
os.chdir( '../' )
targetPath = (r "%s\%s" %  (export_dir,date_folder)
 
try :
     optlist, args  =  getopt.getopt (sys.argv[ 1 :],  "r:u:p:" ,
                                    [ "revision=" "username=" "password=" ])
     if  len (args)  = =  1  or  len (args)  = =  2 :
         url  =  args[ 0 ]
         if  len (args)  = =  2 :
             #targetPath = args[1]
             site_version  =  args[ 1 ]
     else :
         raise  Exception ( "Input URL [site_version]" )
         
     for  option, value  in  optlist:
         if  option  = =  "--username"  or  option  = =  "-u" :
             username  =  value            
         elif  option  = =  "--password"  or  option  = =  "-p" :
             password  =  value
         elif  option  = =  "--revision"  or  option  = =  "-r" :
             revision  =  value
             if  str .find(value,  ":" ) > =  0 :
                 (revision_min0, revision_max0)  =  str .split(value,  ":" )
                 revision_min  =  pysvn.Revision( pysvn.opt_revision_kind.number,  int (revision_min0) )
                 if  revision_max0 ! =  "HEAD" :
                     revision_max  =  pysvn.Revision( pysvn.opt_revision_kind.number,  int (revision_max0) )
                 hasRevision  =  True
             else :
                 raise  Exception ( "Please Input revision range "  +  str (option))
         else :
             raise  Exception ( "Unknown option "  +  str (option))
             
     if  hasRevision  = =  False :
         raise  Exception ( "Please Input Revision Range -r min:max" )
         
     #urlObject = urlparse(url)
     #if urlObject.scheme == 'http' or urlObject.scheme == 'https':
     #    url = urlObject.scheme+"://"+urlObject.netloc+urllib.quote(urlObject.path.decode(sys.stdin.encoding).encode('utf8'))
     #else:
         #url = unicode(url, sys.stdin.encoding)
     #print (sys.stdin.encoding)
     # print(url)
     if  not  url.endswith( "/" ):
         url  =  url  +  "/"        
         
except  getopt.error as reason:
  raise  Exception( "Usage: "  +  sys.argv[ 0 +  ": "  +  str (reason))
 
f_list = []
f_list = os.listdir(targetPath)
  
for  in  f_list:
     f_path = os.path.join(targetPath, f)
     if  os.path.isfile(f_path):
         os.remove(f_path)
         print  (f_path + " removed." )
     else :
         shutil.rmtree(f_path)
         print  (f_path +  " removed." )
 
print  (targetPath + " is already empty." )
     
 
def  get_login(realm,user,may_save):
     return  True , username, password,  False
    
print  ( "SVN Path:" + url + '   ' + "Diff file path:" + targetPath)
 
client  =  pysvn.Client()
if  username ! =  " " and password != " ":
     client.callback_get_login  =  get_login
 
summary  =  client.diff_summarize(url, revision_min, url, revision_max)
#print summary
for  changed  in  summary:
     #path, summarize_kind, node_kind, prop_changed
     #for key in changed.iterkeys():
     #    print key 
     
     if  pysvn.diff_summarize_kind.delete  = =  changed[ 'summarize_kind' ]:
       fullPath  =  targetPath + "/" + changed[ 'path' ]   
       if  os.path.exists(fullPath):
         os.remove(fullPath)
     
     if  pysvn.diff_summarize_kind.added  = =  changed[ 'summarize_kind' or  pysvn.diff_summarize_kind.modified  = =  changed[ 'summarize_kind' ]:
         print  (changed[ 'summarize_kind' ], changed[ 'path' ])
 
         if  changed[ 'node_kind' = =  pysvn.node_kind. file :
             
             #uniPath = changed['path'].decode('utf8').encode()
             file_text  =  client.cat(url + urllib.parse.quote(changed[ 'path' ].encode( 'utf8' )), revision_max)
             
             fullPath  =  targetPath + "/" + changed[ 'path' ]    
             dirPath  =  fullPath[ 0 :fullPath.rfind( "/" )]
             if  not  os.path.exists(dirPath):
                 os.makedirs(dirPath)
                         
             =  open (fullPath, 'wb' )
             f.write(file_text)
             f.close
         #f = open(fullPath,'wb')
         #f.write(file_text)
             #f.close
 
#f_tar="./"+os.path.basename(targetPath)+".tar"
#if os.path.exists(f_tar):
#    os.remove(f_tar)
#    print (os.path.basename(f_tar)+" is removed.")
#else:
#    print (os.path.basename(f_tar)+" is not exists.")
 
 
# Folder filter regulation
os.chdir((r "%s" %  targetPath)
p_list  =  a_list  =  s_list  =  os.listdir(os.getcwd())
p_outer_list  =  list ( filter ( lambda  x:x ! =  "website"  and  x ! =  "framework" , p_list))
a_outer_list  =  list ( filter ( lambda  x:x ! =  "website"  and  x ! =  "framework"  and  x ! =  "service" , a_list))
s_outer_list  =  list ( filter ( lambda  x:x ! =  "website" , s_list))
 
os.chdir((r "%s\website" %  targetPath)
p_inner_list  =  a_inner_list  =  s_inner_list  =  os.listdir(os.getcwd())
p_inner_list  =  list ( filter ( lambda  x:x ! =  "platform" , p_inner_list))
a_inner_list  =  list ( filter ( lambda  x:x ! =  "admin"  and  x ! =  "union" , a_inner_list))
s_inner_list  =  list ( filter ( lambda  x:x ! =  "static" , s_inner_list))
 
 
def  inner_filter(list_op):
     for  in  list_op:
         shutil.rmtree((r "%s\website\%s" %  (targetPath,i))
     os.chdir((r "%s" %  t_path)
     print  (os.listdir(os.getcwd()))
 
def  filter_site(site_op):
     if  site_version  = =  "p" :
         for  p_o  in  p_outer_list:
             shutil.rmtree((r "%s\%s" %  (targetPath,p_o))
         inner_filter(p_inner_list)
 
     elif  site_version  = =  "a" :
         for  a_o  in  a_outer_list:
             shutil.rmtree((r "%s\%s" %  (targetPath,a_o))
         inner_filter(a_inner_list)
 
     elif  site_version  = =  "s" :
         for  s_o  in  s_outer_list:
             shutil.rmtree((r "%s\%s" %  (targetPath,s_o))
         inner_filter(s_inner_list)
 
     else :
         raise  Exception (( "Unknown site_option: %s" %  site_op)
 
filter_site(site_version)
 
 
print  (( "export file: %s_%s" + '.tar' %  (site_version,date_folder))        
 
def  make_tar(folder_to_tar,dst_folder):
     fold_name  =  os.path.basename(folder_to_tar)
     dst_name  =  "%s_%s.tar"  % (site_version,fold_name)
     dst_path  =  os.path.join(dst_folder, dst_name)   
     tar  =  tarfile.TarFile. open (dst_path,  'w' )
     tar.add(folder_to_tar, fold_name)
     tar.close()
     return  dst_path
     
dst_file  =  make_tar(targetPath, './' )
# print (dst_file)
 
def  upload_file(localfile):
     ftp = FTP()
     ftp.connect(ftp_host,ftp_port)
     ftp.login(ftp_user,ftp_pass)
     ftp.cwd( './' )
     file = open (localfile, 'rb' )
     ftp.storbinary( 'STOR %s'  %  os.path.basename(localfile), file )
     ftp.retrlines( 'LIST' )
     file .close()
     ftp.close()
     ftp.quit
 
upload_file(dst_file)
print  ( 'File Upload Successful.' )


代码就是如上这么多,中间肯定有很多语法的不严谨和bug,大家多多指正。如有需要的可以直接拿去对应的改改基本上也是可以用的。



本文转自Mr_陈 51CTO博客,原文链接:http://blog.51cto.com/chenpipi/1604039,如需转载请自行联系原作者
目录
打赏
0
0
0
0
69
分享
相关文章
|
21天前
|
【01】整体试验思路,如何在有UID的情况下获得用户手机号信息,python开发之理论研究试验,如何通过抖音视频下方的用户的UID获得抖音用户的手机号-本系列文章仅供学习研究-禁止用于任何商业用途-仅供学习交流-优雅草卓伊凡
【01】整体试验思路,如何在有UID的情况下获得用户手机号信息,python开发之理论研究试验,如何通过抖音视频下方的用户的UID获得抖音用户的手机号-本系列文章仅供学习研究-禁止用于任何商业用途-仅供学习交流-优雅草卓伊凡
137 82
|
3月前
|
自动化微信朋友圈:Python脚本实现自动发布动态
本文介绍如何使用Python脚本自动化发布微信朋友圈动态,节省手动输入的时间。主要依赖`pyautogui`、`time`、`pyperclip`等库,通过模拟鼠标和键盘操作实现自动发布。代码涵盖打开微信、定位朋友圈、准备输入框、模拟打字等功能。虽然该方法能提高效率,但需注意可能违反微信使用条款,存在风险。定期更新脚本以适应微信界面变化也很重要。
266 61
【02】整体试验思路,在这之前我们发现sec_uid,sec_uid是什么和uid的关系又是什么?相互如何转换?python开发之理论研究试验,如何通过抖音视频下方的用户的UID获得抖音用户的手机号-本系列文章仅供学习研究-禁止用于任何商业用途-仅供学习交流-优雅草卓伊凡
【02】整体试验思路,在这之前我们发现sec_uid,sec_uid是什么和uid的关系又是什么?相互如何转换?python开发之理论研究试验,如何通过抖音视频下方的用户的UID获得抖音用户的手机号-本系列文章仅供学习研究-禁止用于任何商业用途-仅供学习交流-优雅草卓伊凡
51 6
Python学习:内建属性、内建函数的教程
本文介绍了Python中的内建属性和内建函数。内建属性包括`__init__`、`__new__`、`__class__`等,通过`dir()`函数可以查看类的所有内建属性。内建函数如`range`、`map`、`filter`、`reduce`和`sorted`等,分别用于生成序列、映射操作、过滤操作、累积计算和排序。其中,`reduce`在Python 3中需从`functools`模块导入。示例代码展示了这些特性和函数的具体用法及注意事项。
|
21天前
|
python pandas学习(一)
该代码段展示了四个主要操作:1) 删除指定列名,如商品id;2) 使用正则表达式模糊匹配并删除列,例如匹配订单商品名称1的列;3) 将毫秒级时间戳转换为带有时区调整的日期时间格式,并增加8小时以适应本地时区;4) 将列表转换为DataFrame后保存为Excel文件,文件路径和名称根据变量拼接而成。
24 3
Python学习的自我理解和想法(9)
这是我在B站跟随千锋教育学习Python的第9天,主要学习了赋值、浅拷贝和深拷贝的概念及其底层逻辑。由于开学时间紧张,内容较为简略,但希望能帮助理解这些重要概念。赋值是创建引用,浅拷贝创建新容器但元素仍引用原对象,深拷贝则创建完全独立的新对象。希望对大家有所帮助,欢迎讨论。
利用Python脚本自动备份网络设备配置
通过本文的介绍,我们了解了如何利用Python脚本自动备份网络设备配置。该脚本使用 `paramiko`库通过SSH连接到设备,获取并保存配置文件。通过定时任务调度,可以实现定期自动备份,确保网络设备配置的安全和可用。希望这些内容能够帮助你在实际工作中实现网络设备的自动化备份。
74 14
Python学习的自我理解和想法(10)
这是我在千锋教育B站课程学习Python的第10天笔记,主要学习了函数的相关知识。内容包括函数的定义、组成、命名、参数分类(必须参数、关键字参数、默认参数、不定长参数)及调用注意事项。由于开学时间有限,记录较为简略,望谅解。通过学习,我理解了函数可以封装常用功能,简化代码并便于维护。若有不当之处,欢迎指正。
1.1 学习Python操作Excel的必要性
学习Python操作Excel在当今数据驱动的商业环境中至关重要。Python能处理大规模数据集,突破Excel行数限制;提供丰富的库实现复杂数据分析和自动化任务,显著提高效率。掌握这项技能不仅能提升个人能力,还能为企业带来价值,减少人为错误,提高决策效率。推荐从基础语法、Excel操作库开始学习,逐步进阶到数据可视化和自动化报表系统。通过实际项目巩固知识,关注新技术,为职业发展奠定坚实基础。
让UE自动运行Python脚本:实现与实例解析
本文介绍如何配置Unreal Engine(UE)以自动运行Python脚本,提高开发效率。通过安装Python、配置UE环境及使用第三方插件,实现Python与UE的集成。结合蓝图和C++示例,展示自动化任务处理、关卡生成及数据分析等应用场景。
217 5

热门文章

最新文章

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等