|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +from setuptools import namespaces |
| 6 | + |
| 7 | + |
| 8 | +FILENAME = 'danny-hack-nspkg.pth' |
| 9 | +VERSION = '0.20.0' |
| 10 | +CORE_DIST_INFO = 'google_cloud_core-{}.dist-info'.format(VERSION) |
| 11 | +CORE_METADATA = { |
| 12 | + 'metadata_version': '2.0', |
| 13 | + 'name': 'google-cloud-core', |
| 14 | + 'version': VERSION, |
| 15 | +} |
| 16 | +CURR_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 17 | +CORE_DIR = os.path.abspath( |
| 18 | + os.path.join(CURR_DIR, '..', 'core')) |
| 19 | +EXTRA_PATH_TEMPLATE = ( |
| 20 | + 'import sys;' |
| 21 | + 'mp = sys.modules[{!r}].__path__;' |
| 22 | + 'p1 = {!r};' |
| 23 | + '(p1 not in mp) and mp.append(p1);' |
| 24 | + 'p2 = {!r};' |
| 25 | + '(p2 not in mp) and mp.append(p2)\n') |
| 26 | + |
| 27 | + |
| 28 | +def fake_core_dist_info(site_packages): |
| 29 | + """Fake the dist. info of google-cloud-core. |
| 30 | +
|
| 31 | + It needs to be faked since we don't actually install it. |
| 32 | + """ |
| 33 | + dist_info = os.path.join(site_packages, CORE_DIST_INFO) |
| 34 | + if not os.path.isdir(dist_info): |
| 35 | + os.mkdir(dist_info) |
| 36 | + meta = os.path.join(dist_info, 'metadata.json') |
| 37 | + if not os.path.exists(meta): |
| 38 | + with open(meta, 'w') as file_obj: |
| 39 | + json.dump(CORE_METADATA, file_obj) |
| 40 | + |
| 41 | + |
| 42 | +def add_hacked_pth_file(site_packages): |
| 43 | + """Add the hacked .PTH file to the site packages dir.""" |
| 44 | + installer = namespaces.Installer() |
| 45 | + part1 = installer._gen_nspkg_line('google') |
| 46 | + part2 = EXTRA_PATH_TEMPLATE.format( |
| 47 | + 'google', os.path.join(CORE_DIR, 'google'), |
| 48 | + os.path.join(CURR_DIR, 'google')) |
| 49 | + part3 = installer._gen_nspkg_line('google.cloud') |
| 50 | + part4 = EXTRA_PATH_TEMPLATE.format( |
| 51 | + 'google.cloud', os.path.join(CORE_DIR, 'google', 'cloud'), |
| 52 | + os.path.join(CURR_DIR, 'google', 'cloud')) |
| 53 | + |
| 54 | + file_contents = ''.join([part1, part2, part3, part4]) |
| 55 | + full_path = os.path.join(site_packages, FILENAME) |
| 56 | + with open(full_path, 'w') as file_obj: |
| 57 | + file_obj.write(file_contents) |
| 58 | + |
| 59 | + |
| 60 | +def main(site_packages): |
| 61 | + add_hacked_pth_file(site_packages) |
| 62 | + fake_core_dist_info(site_packages) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + parser = argparse.ArgumentParser( |
| 67 | + description='Make a hacked PTH file to emulate "setup.py develop"') |
| 68 | + help_txt = 'Site packages directory where .pth file will be added.' |
| 69 | + parser.add_argument('--site-packages', dest='site_packages', |
| 70 | + required=True, help=help_txt) |
| 71 | + args = parser.parse_args() |
| 72 | + main(args.site_packages) |
0 commit comments