Skip to content

Commit baf679b

Browse files
committed
Make a hacked PTH file instead of installing in place.
1 parent 039f1cc commit baf679b

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

datastore/make_pth.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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)

datastore/tox.ini

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
[tox]
2+
skipsdist=True
23
envlist =
34
py27,py34,py35,cover
45

56
[testing]
6-
localdeps =
7-
pip install --upgrade {toxinidir}/../core
87
deps =
98
pytest
9+
httplib2 >= 0.9.1
10+
googleapis-common-protos >= 1.3.4
11+
oauth2client >= 3.0.0, < 4.0.0dev
12+
protobuf >= 3.0.0
13+
six
14+
grpcio >= 1.0.0, < 2.0dev
15+
installcmd =
16+
python {toxinidir}/make_pth.py --site-packages {envsitepackagesdir}
1017
covercmd =
1118
py.test --quiet \
1219
--cov=google.cloud.datastore \
@@ -16,7 +23,7 @@ covercmd =
1623

1724
[testenv]
1825
commands =
19-
{[testing]localdeps}
26+
{[testing]installcmd}
2027
py.test --quiet {posargs} unit_tests
2128
deps =
2229
{[testing]deps}
@@ -25,7 +32,7 @@ deps =
2532
basepython =
2633
python2.7
2734
commands =
28-
{[testing]localdeps}
35+
{[testing]installcmd}
2936
{[testing]covercmd}
3037
deps =
3138
{[testenv]deps}

0 commit comments

Comments
 (0)