在Linux下运行的源码备份的Python程序:
- #!/usr/bin/python
- # Filename : srcbackup.py
- # 2008-10-08 13:40 by daineng@taobao
- import os
- import sys
- import time
- BACK_FENDS = ['.py', '.cpp', '.c']
- BACK_FBGNS = ['makefile']
- EXCLUDE_DENDS = ['data']
- ################################################################################
- # Arguments
- if len(sys.argv) < 2 :
- print >> sys.stderr, 'Usage: %s <backup_dir> [prefix]' % sys.argv[0]
- sys.exit(1)
- prefix = ''
- if len(sys.argv) > 2 :
- prefix = sys.argv[2]
- if prefix : prefix += '.'
- back_dir = sys.argv[1]
- if not os.path.isdir(back_dir) :
- print >> sys.stderr, 'Error: [%s] is not a directory!' % back_dir
- sys.exit(1)
- ################################################################################
- # Functions
- def check_fback(e) :
- e = e.lower()
- for s in BACK_FENDS :
- if e.endswith(s) :
- return True
- for s in BACK_FBGNS :
- if e.startswith(s) :
- return True
- return False
- def check_dback(e) :
- e = e.lower()
- for s in EXCLUDE_DENDS :
- if e.endswith(s) :
- return False
- return True
- def wfunc(arg, dirname, fnames) :
- #print >> sys.stderr, 'ENTER [%s]' % dirname
- removes = []
- for e in fnames :
- if check_fback(e) :
- path = os.path.join(dirname, e)
- #print >> sys.stderr, 'Backup [%s]' % path
- arg.append(path)
- if not check_dback(e) :
- removes.append(e)
- for e in removes :
- fnames.remove(e)
- ################################################################################
- # Backup
- backs = []
- os.path.walk(back_dir, wfunc, backs)
- if back_dir.endswith(os.sep) : back_dir = back_dir[:-len(os.sep)]
- backs.insert(0, 'tar czvf %s%s.%s.tgz' % (prefix, back_dir, time.strftime('%y%m%d.%H%M%S')))
- os.system(' '.join(backs))