ITeye博客

SVN小文件清除脚本

caocao · 2007-02-12 11:32 · Python · Markdown

隐士以前用惯了ClearCase,换到SVN有点不习惯。ClearCase下面的文件都可以直接Copy出来用,SVN下面的就麻烦了,每个目录下面一个.svn子目录,当你要把项目在Check In前拿去别用时,你就体会到你的项目不是一个项目,此刻SVN灵魂附体令你感觉别扭,当然,Check In后可以用SVN的Export来导出一个干净的项目。不爽,隐士整了个Python脚本,清除这些垃圾。

闲话不多,看脚本:

python 代码

  1. #!/usr/bin/python
  2. # -*- coding: utf8 -*-
  3. #################################
  4. #   Written by caocao           #
  5. #   http://www.caocao.mobi      #
  6. #   http://www.caocao.name      #
  7. #################################
  8. import sys, os, stat
  9. def walk(path):
  10. for item in os.listdir(path):
  11. subpath=os.path.join(path, item)
  12. mode=os.stat(subpath)[stat.ST_MODE]
  13. if stat.S_ISDIR(mode):
  14. if item==".svn":
  15. print "Cleaning %s ..." % subpath
  16. print "%d deleted" % purge(subpath)
  17. else:
  18. walk(subpath)
  19. def purge(path):
  20. count=0
  21. for item in os.listdir(path):
  22. subpath=os.path.join(path, item)
  23. mode=os.stat(subpath)[stat.ST_MODE]
  24. if stat.S_ISDIR(mode):
  25. count+=purge(subpath)
  26. else:
  27. os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE)
  28. os.unlink(subpath)
  29. count+=1
  30. os.rmdir(path)
  31. count+=1
  32. return count
  33. if len(sys.argv)!=2:
  34. print "Usage: python SVNClean.py path"
  35. sys.exit(1)
  36. walk(sys.argv[1])

评论(12)

  1. #12 楼 · sutra · 2007-04-04 17:14

    不爽的原因是因为svn export没有使用python来写,是吧?呵呵。

  2. #11 楼 · duduwolf · 2007-03-21 17:40

    唉,用的了这么麻烦吗? 在linxu下用robbin的法子, 在windows下用我的法子

    在项目平级的目录,执行dos命令: xcopy project_dir project_dir_1 /s /i 或者在项目根目录执行以下dos命令 for /r . %%a in (.) do @if exist "%%a.svn" rd /s /q "%%a.svn"

  3. #10 楼 · simohayha · 2007-02-12 18:08

    Arbow writes:

    [quote=robbin]一行shell命令搞定

    [code]find . -type f -iname ".svn" -exec rm -rf {} ; [/code]

    [/quote]

    应该是 -type d 吧,windows下可以用cygwin

    就是 -type f吧.

  4. #9 楼 · Arbow · 2007-02-12 17:54

    robbin writes:

    一行shell命令搞定

    [code]find . -type f -iname ".svn" -exec rm -rf {} ; [/code]

    应该是 -type d 吧,windows下可以用cygwin

  5. #8 楼 · caocao · 2007-02-12 17:47

    robbin writes:

    一行shell命令搞定

    [code]find . -type f -iname ".svn" -exec rm -rf {} ; [/code]

    :idea: 可惜在windows下,莫非cygwin也有类似的玩意?

  6. #7 楼 · robbin · 2007-02-12 17:23

    一行shell命令搞定

    find . -type f -iname ".svn" -exec rm -rf {} \; 
    
  7. #6 楼 · caocao · 2007-02-12 15:56

    simohayha writes:

    这个用shell 写的话,更简便吧.

    一直紧跟python,shell编程涉入不深,只会最简单的 :oops: windows上面貌似shell玩不转啊,要整vbs,不熟。

  8. #5 楼 · simohayha · 2007-02-12 15:40

    这个用shell 写的话,更简便吧.

  9. #4 楼 · caocao · 2007-02-12 15:23

    limodou writes:

    svn有一个export命令,就是导出干净的目录和文件用的。

    所言即是,这个命令不错,有时候并不是项目目录里面的所有文件都加入了version control,此刻该命令有点鸡肋了,索性直接copy,用这个删.svn目录,挺好。

    SVN Document writes:

    Name svn export — Export a clean directory tree.

    Synopsis svn export [-r rev] URL[@pegrev] [path] svn export [-r rev] PATH1[@pegrev] [path2] Description The first form exports a clean directory tree from the repository specified by URL, at revision REV if it is given, otherwise at HEAD, into PATH. If PATH is omitted, the last component of the URL is used for the local directory name.

    The second form exports a clean directory tree from the working copy specified by PATH1 into PATH2. All local changes will be preserved, [b][color=darkred]but files not under version control will not be copied.[/color][/b]

  10. #3 楼 · limodou · 2007-02-12 15:10

    svn有一个export命令,就是导出干净的目录和文件用的。

  11. #2 楼 · caocao · 2007-02-12 15:03

    因为os.walk()不合要求,如果我要清除我所有进入SVN的项目里面的这类目录,文件、目录总量可能是几十万个,os.walk()比我的这个实现要慢一点,且无法进行多线程优化性能。我的这个,可以用多线程遍历和删除,没试过不知道性能能强多少。

    下面是一个小测试,同样遍历我机器上的一个目录,里面有14116个目录,oswalk耗时平均比mywalk多近一秒。

    D:\temp>python mywalk.py 14116 0:00:17.922000

    D:\temp>python oswalk.py 14116 0:00:18.844000

    源码如下:

    oswalk.py

    
    import os
    from datetime import datetime
    
    path="D:\\program"
    t1=datetime.now()
    count=0
    for dirpath, dirnames, filenames in os.walk(path):
    	count+=1
    	#print "%s/%s/%s" %(dirpath, dirnames, filenames)
    print count
    t2=datetime.now()
    print t2-t1
    
    

    mywalk.py

    
    import os, stat
    from datetime import datetime
    
    def walk(path):
    	count=1
    	for item in os.listdir(path):
    		subpath=os.path.join(path, item)
    		mode=os.stat(subpath)[stat.st_mode]
    		if stat.S_ISDIR(mode):
    			count+=walk(subpath)
    	return count
    
    path="D:\\program"
    t1=datetime.now()
    print walk(path)
    t2=datetime.now()
    print t2-t1
    
    
  12. #1 楼 · Arbow · 2007-02-12 14:35

    为何要自己实现一个walk(),而非使用 os.walk() 函数?

评论功能维护中,当前仅展示历史评论。