mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Use fnmatch; read ".xxcign" for additional patterns to ignore.
This commit is contained in:
		
							parent
							
								
									dbd83aa4df
								
							
						
					
					
						commit
						bff5bb3db9
					
				
					 1 changed files with 39 additions and 13 deletions
				
			
		|  | @ -7,9 +7,11 @@ | ||||||
| 
 | 
 | ||||||
| import sys | import sys | ||||||
| import posix | import posix | ||||||
| import stat | from stat import * | ||||||
| import path | import path | ||||||
| import commands | import commands | ||||||
|  | import fnmatch | ||||||
|  | import string | ||||||
| 
 | 
 | ||||||
| EXECMAGIC = '\001\140\000\010' | EXECMAGIC = '\001\140\000\010' | ||||||
| 
 | 
 | ||||||
|  | @ -19,34 +21,57 @@ def getargs(): | ||||||
| 	args = sys.argv[1:] | 	args = sys.argv[1:] | ||||||
| 	if args: | 	if args: | ||||||
| 		return args | 		return args | ||||||
| 	print 'No arguments, checking almost *' | 	print 'No arguments, checking almost *, in "ls -t" order' | ||||||
|  | 	list = [] | ||||||
| 	for file in posix.listdir('.'): | 	for file in posix.listdir('.'): | ||||||
| 		if not skipfile(file): | 		if not skipfile(file): | ||||||
| 			args.append(file) | 			list.append((getmtime(file), file)) | ||||||
| 	if not args: | 	list.sort() | ||||||
|  | 	if not list: | ||||||
| 		print 'Nothing to do -- exit 1' | 		print 'Nothing to do -- exit 1' | ||||||
| 		sys.exit(1) | 		sys.exit(1) | ||||||
| 	args.sort() | 	list.sort() | ||||||
|  | 	list.reverse() | ||||||
|  | 	for mtime, file in list: args.append(file) | ||||||
| 	return args | 	return args | ||||||
| 
 | 
 | ||||||
|  | def getmtime(file): | ||||||
|  | 	try: | ||||||
|  | 		st = posix.stat(file) | ||||||
|  | 		return st[ST_MTIME] | ||||||
|  | 	except posix.error: | ||||||
|  | 		return -1 | ||||||
|  | 
 | ||||||
| badnames = ['tags', 'TAGS', 'xyzzy', 'nohup.out', 'core'] | badnames = ['tags', 'TAGS', 'xyzzy', 'nohup.out', 'core'] | ||||||
| badprefixes = ['.', ',', '@', '#', 'o.'] | badprefixes = ['.', ',', '@', '#', 'o.'] | ||||||
| badsuffixes = \ | badsuffixes = \ | ||||||
| 	['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \ | 	['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \ | ||||||
| 	 '.pyc', '.elc'] | 	 '.pyc', '.elc'] | ||||||
| # XXX Should generalize even more to use fnmatch! | 
 | ||||||
|  | def setup(): | ||||||
|  | 	global ignore | ||||||
|  | 	ignore = badnames[:] | ||||||
|  | 	for p in badprefixes: | ||||||
|  | 		ignore.append(p + '*') | ||||||
|  | 	for p in badsuffixes: | ||||||
|  | 		ignore.append('*' + p) | ||||||
|  | 	try: | ||||||
|  | 		f = open('.xxcign', 'r') | ||||||
|  | 	except IOError: | ||||||
|  | 		return | ||||||
|  | 	ignore = ignore + string.split(f.read()) | ||||||
| 
 | 
 | ||||||
| def skipfile(file): | def skipfile(file): | ||||||
| 	if file in badnames or \ | 	for p in ignore: | ||||||
| 		badprefix(file) or badsuffix(file) or \ | 		if fnmatch.fnmatch(file, p): return 1 | ||||||
| 		path.islink(file) or path.isdir(file): |  | ||||||
| 		return 1 |  | ||||||
| 	# Skip huge files -- probably binaries. |  | ||||||
| 	try: | 	try: | ||||||
| 		st = posix.stat(file) | 		st = posix.lstat(file) | ||||||
| 	except posix.error: | 	except posix.error: | ||||||
| 		return 1 # Doesn't exist -- skip it | 		return 1 # Doesn't exist -- skip it | ||||||
| 	if st[stat.ST_SIZE] >= MAXSIZE: return 1 | 	# Skip non-plain files. | ||||||
|  | 	if not S_ISREG(st[ST_MODE]): return 1 | ||||||
|  | 	# Skip huge files -- probably binaries. | ||||||
|  | 	if st[ST_SIZE] >= MAXSIZE: return 1 | ||||||
| 	# Skip executables | 	# Skip executables | ||||||
| 	try: | 	try: | ||||||
| 		data = open(file, 'r').read(len(EXECMAGIC)) | 		data = open(file, 'r').read(len(EXECMAGIC)) | ||||||
|  | @ -86,4 +111,5 @@ def askyesno(prompt): | ||||||
| 	s = raw_input(prompt) | 	s = raw_input(prompt) | ||||||
| 	return s in ['y', 'yes'] | 	return s in ['y', 'yes'] | ||||||
| 
 | 
 | ||||||
|  | setup() | ||||||
| go(getargs()) | go(getargs()) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Guido van Rossum
						Guido van Rossum