Codebase list pypy3 / c23fc90
Allow pypy3clean to target a specific tag Stefano Rivera 5 years ago
1 changed file(s) with 35 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
33 import collections
44 import itertools
55 import os
6 import shutil
76 import subprocess
87 import sys
98
4039 yield os.path.join(dirpath, fn)
4140
4241
43 def clean_modules(modules, verbose):
44 """Remove all .pyc files for every module specified"""
42 def clean_modules(modules, verbose, tag):
43 """Remove all tag.pyc files for every module specified"""
4544 clean = collections.defaultdict(list)
4645 for module in modules:
4746 dir_, basename = os.path.split(module)
5453
5554 empty = True
5655 for fn in os.listdir(pycache):
57 if fn.endswith('.pyc') and fn.rsplit('.', 2)[0] in basenames:
56 parts = fn.rsplit('.', 2)
57 if parts[0] in basenames and parts[1] == tag and parts[2] == 'pyc':
5858 if verbose:
5959 print('Removing %s' % os.path.join(pycache, fn))
6060 os.unlink(os.path.join(pycache, fn))
6767 os.rmdir(pycache)
6868
6969
70 def clean_directories(directories, verbose):
71 """Indiscriminately remove __pycache__ directories"""
70 def clean_directories(directories, verbose, tag):
71 """Remove the specified tag.pyc files."""
72 suffix = '.{}.pyc'.format(tag)
7273 for root in directories:
7374 for dirpath, dirnames, filenames in os.walk(root):
74 for dir_ in dirnames:
75 if dir_ == '__pycache__':
75 if dirpath.endswith('/__pycache__'):
76 empty = True
77 for file_ in filenames:
78 if file_.endswith(suffix):
79 if verbose:
80 print('Removing %s' % os.path.join(dirpath, file_))
81 os.unlink(os.path.join(dirpath, file_))
82 else:
83 empty = False
84
85 if empty:
7686 if verbose:
77 print('Removing %s' % os.path.join(dirpath, dir_))
78 shutil.rmtree(os.path.join(dirpath, dir_))
87 print('Pruning %s' % dirpath)
88 os.rmdir(dirpath)
89
90
91 def pypy3_tag(version):
92 """Return the tag used by the given PyPy3 version"""
93 parts = version.split('.')
94 if len(parts) != 2:
95 raise argparse.ArgumentTypeError('Version must be MAJOR.MINOR')
96 return 'pypy3-{}{}'.format(*parts)
7997
8098
8199 def main():
91109 help='Be more verbose')
92110 parser.add_argument('-q', '--quiet', action='store_true',
93111 help='Be quiet')
112 parser.add_argument('--version', metavar='VERSION',
113 default='{}.{}'.format(*sys.pypy_version_info[:2]),
114 dest='tag', type=pypy3_tag,
115 help='Clean up .pyc files left by the specified '
116 'pypy3 version')
94117 args = parser.parse_args()
95118
96119 if not (args.package or args.directory):
117140 modules.add(fn)
118141 else:
119142 directories.add(fn)
120 clean_directories(directories, args.verbose)
143 clean_directories(directories, args.verbose, args.tag)
121144
122 clean_modules(modules, args.verbose)
145 clean_modules(modules, args.verbose, args.tag)
123146
124147
125148 if __name__ == '__main__':