1import re 2 3from paver.easy import task, path as Path 4import pip 5 6 7def remove_all(paths): 8 for path in paths: 9 path.rmtree() if path.isdir() else path.remove() 10 11 12@task 13def update_vendored(): 14 update_pkg_resources() 15 update_setuptools() 16 17 18def rewrite_packaging(pkg_files, new_root): 19 """ 20 Rewrite imports in packaging to redirect to vendored copies. 21 """ 22 for file in pkg_files.glob('*.py'): 23 text = file.text() 24 text = re.sub(r' (pyparsing|six)', rf' {new_root}.\1', text) 25 file.write_text(text) 26 27 28def clean(vendor): 29 """ 30 Remove all files out of the vendor directory except the meta 31 data (as pip uninstall doesn't support -t). 32 """ 33 remove_all( 34 path 35 for path in vendor.glob('*') 36 if path.basename() != 'vendored.txt' 37 ) 38 39 40def install(vendor): 41 clean(vendor) 42 install_args = [ 43 'install', 44 '-r', str(vendor / 'vendored.txt'), 45 '-t', str(vendor), 46 ] 47 pip.main(install_args) 48 remove_all(vendor.glob('*.dist-info')) 49 remove_all(vendor.glob('*.egg-info')) 50 (vendor / '__init__.py').write_text('') 51 52 53def update_pkg_resources(): 54 vendor = Path('pkg_resources/_vendor') 55 install(vendor) 56 rewrite_packaging(vendor / 'packaging', 'pkg_resources.extern') 57 58 59def update_setuptools(): 60 vendor = Path('setuptools/_vendor') 61 install(vendor) 62 rewrite_packaging(vendor / 'packaging', 'setuptools.extern') 63