Skip to content

Commit e2ae6de

Browse files
authored
Switch to Nox, add build_dist script
1 parent 041a433 commit e2ae6de

10 files changed

+217
-131
lines changed

.github/workflows/ci.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ jobs:
3535
python-version: 3.7
3636
- name: Install dependencies
3737
run: |
38-
python3.7 -m pip install tox
38+
python3.7 -m pip install nox
3939
- name: Lint the code
40-
run: tox -e lint
40+
run: nox -s lint
4141

4242
docs:
4343
runs-on: ubuntu-latest
@@ -50,9 +50,9 @@ jobs:
5050
python-version: 3.7
5151
- name: Install dependencies
5252
run: |
53-
python3.7 -m pip install tox
53+
python3.7 -m pip install nox
5454
- name: Build the docs
55-
run: tox -e docs
55+
run: nox -s docs
5656

5757
test-linux:
5858
runs-on: ubuntu-latest

CONTRIBUTING.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ To run the code generation make sure you have pre-requisites installed:
3030
Then you should be able to run the code generation by invoking:
3131

3232
```
33-
python utils/generate_api.py
33+
$ python utils/generate_api.py
3434
```
3535

3636

@@ -45,10 +45,17 @@ The process for contributing to any of the Elasticsearch repositories is similar
4545
assure our users of the origin and continuing existence of the code. You only
4646
need to sign the CLA once.
4747

48-
2. Run the test suite to ensure your changes do not break existing code:
48+
2. Run the linter and test suite to ensure your changes do not break existing code:
4949

5050
````
51-
python setup.py test
51+
# Install Nox for task management
52+
$ python -m pip install nox
53+
54+
# Auto-format and lint your changes
55+
$ nox -s blacken
56+
57+
# Run the test suite
58+
$ python setup.py test
5259
````
5360
5461
See the README file in `test_elasticsearch` directory for more information on
@@ -68,4 +75,3 @@ The process for contributing to any of the Elasticsearch repositories is similar
6875
Then sit back and wait. There will probably be a discussion about the pull
6976
request and, if any changes are needed, we would love to work with you to get
7077
your pull request merged into elasticsearch-py.
71-

noxfile.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Licensed to Elasticsearch B.V under one or more agreements.
2+
# Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information
4+
5+
import nox
6+
7+
8+
SOURCE_FILES = (
9+
"setup.py",
10+
"noxfile.py",
11+
"elasticsearch/",
12+
"test_elasticsearch/",
13+
"utils/",
14+
)
15+
16+
17+
@nox.session(python=["2.7", "3.4", "3.5", "3.6", "3.7", "3.8"])
18+
def test(session):
19+
session.install(".")
20+
session.install("-r", "dev-requirements.txt")
21+
22+
session.run("python", "setup.py", "test")
23+
24+
25+
@nox.session()
26+
def blacken(session):
27+
session.install("black")
28+
29+
session.run("black", "--target-version=py27", *SOURCE_FILES)
30+
session.run("python", "utils/license_headers.py", "fix", *SOURCE_FILES)
31+
32+
lint(session)
33+
34+
35+
@nox.session()
36+
def lint(session):
37+
session.install("flake8", "black")
38+
39+
session.run("black", "--target-version=py27", "--check", *SOURCE_FILES)
40+
session.run("flake8", *SOURCE_FILES)
41+
session.run("python", "utils/license_headers.py", "check", *SOURCE_FILES)
42+
43+
44+
@nox.session()
45+
def docs(session):
46+
session.install(".")
47+
session.install("-rdev-requirements.txt", "sphinx-rtd-theme")
48+
49+
session.run("sphinx-build", "docs/", "docs/_build", "-b", "html")

tox.ini

-57
This file was deleted.

utils/Dockerfile

-12
This file was deleted.

utils/build_dists.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Licensed to Elasticsearch B.V under one or more agreements.
2+
# Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information
4+
5+
"""A command line tool for building and verifying releases
6+
Can be used for building both 'elasticsearch' and 'elasticsearchX' dists.
7+
Only requires 'name' in 'setup.py' and the directory to be changed.
8+
"""
9+
10+
import tempfile
11+
import os
12+
import shlex
13+
import re
14+
import contextlib
15+
import shutil
16+
17+
18+
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19+
tmp_dir = None
20+
21+
22+
@contextlib.contextmanager
23+
def set_tmp_dir():
24+
global tmp_dir
25+
tmp_dir = tempfile.mkdtemp()
26+
yield tmp_dir
27+
shutil.rmtree(tmp_dir)
28+
tmp_dir = None
29+
30+
31+
def run(*argv, expect_exit_code=0):
32+
global tmp_dir
33+
if tmp_dir is None:
34+
os.chdir(base_dir)
35+
else:
36+
os.chdir(tmp_dir)
37+
38+
cmd = " ".join(shlex.quote(x) for x in argv)
39+
print("$ " + cmd)
40+
exit_code = os.system(cmd)
41+
if exit_code != expect_exit_code:
42+
print(
43+
"Command exited incorrectly: should have been %d was %d"
44+
% (expect_exit_code, exit_code)
45+
)
46+
exit(exit_code or 1)
47+
48+
49+
def test_dist(dist):
50+
with set_tmp_dir() as tmp_dir:
51+
dist_name = re.match(r"^(elasticsearch\d*)-", os.path.basename(dist)).group(1)
52+
53+
# Build the venv and install the dist
54+
run("python", "-m", "venv", os.path.join(tmp_dir, "venv"))
55+
venv_python = os.path.join(tmp_dir, "venv/bin/python")
56+
run(venv_python, "-m", "pip", "install", "-U", "pip")
57+
run(venv_python, "-m", "pip", "install", dist)
58+
59+
# Test the sync namespaces
60+
run(venv_python, "-c", f"from {dist_name} import Elasticsearch")
61+
run(
62+
venv_python,
63+
"-c",
64+
f"from {dist_name}.helpers import scan, bulk, streaming_bulk, reindex",
65+
)
66+
run(venv_python, "-c", f"from {dist_name} import Elasticsearch")
67+
run(
68+
venv_python,
69+
"-c",
70+
f"from {dist_name}.helpers import scan, bulk, streaming_bulk, reindex",
71+
)
72+
73+
# Ensure that async is not available yet
74+
run(
75+
venv_python,
76+
"-c",
77+
f"from {dist_name} import AsyncElasticsearch",
78+
expect_exit_code=256,
79+
)
80+
run(
81+
venv_python,
82+
"-c",
83+
f"from {dist_name}.helpers import async_scan, async_bulk, async_streaming_bulk, async_reindex",
84+
expect_exit_code=256,
85+
)
86+
87+
# Install aiohttp and see that async is now available
88+
run(venv_python, "-m", "pip", "install", "aiohttp")
89+
run(venv_python, "-c", f"from {dist_name} import AsyncElasticsearch")
90+
91+
# Ensure that the namespaces are correct for the dist
92+
for suffix in ("", "1", "2", "5", "6", "7", "8", "9", "10"):
93+
distx_name = f"elasticsearch{suffix}"
94+
run(
95+
venv_python,
96+
"-c",
97+
f"import {distx_name}",
98+
expect_exit_code=256 if distx_name != dist_name else 0,
99+
)
100+
101+
# Uninstall the dist, see that we can't import things anymore
102+
run(venv_python, "-m", "pip", "uninstall", "--yes", dist_name)
103+
run(
104+
venv_python,
105+
"-c",
106+
f"from {dist_name} import Elasticsearch",
107+
expect_exit_code=256,
108+
)
109+
110+
111+
def main():
112+
run("rm", "-rf", "build/", "dist/", "*.egg-info", ".eggs")
113+
run("python", "setup.py", "sdist", "bdist_wheel")
114+
115+
for dist in os.listdir(os.path.join(base_dir, "dist")):
116+
test_dist(os.path.join(base_dir, "dist", dist))
117+
118+
# After this run 'python -m twine upload dist/*'
119+
print(
120+
"\n\n"
121+
"===============================\n\n"
122+
" * Releases are ready! *\n\n"
123+
"$ python -m twine upload dist/*\n\n"
124+
"==============================="
125+
)
126+
127+
128+
if __name__ == "__main__":
129+
main()

utils/docker-compose.yml

-38
This file was deleted.

utils/generate_api.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ def parse_orig(self):
9797
break
9898
self.header = "\n".join(header_lines)
9999
self.orders = re.findall(
100-
r'\n (?:async )?def ([a-z_]+)\(',
101-
content,
102-
re.MULTILINE
100+
r"\n (?:async )?def ([a-z_]+)\(", content, re.MULTILINE
103101
)
104102

105103
def _position(self, api):
@@ -154,7 +152,11 @@ def __init__(self, namespace, name, definition):
154152

155153
# Try setting doc refs like 'current' and 'master' to our branches ref.
156154
if BRANCH_NAME is not None:
157-
revised_url = re.sub("/elasticsearch/reference/[^/]+/", f"/elasticsearch/reference/{BRANCH_NAME}/", self.doc_url)
155+
revised_url = re.sub(
156+
"/elasticsearch/reference/[^/]+/",
157+
f"/elasticsearch/reference/{BRANCH_NAME}/",
158+
self.doc_url,
159+
)
158160
if is_valid_url(revised_url):
159161
self.doc_url = revised_url
160162
else:
@@ -193,9 +195,11 @@ def params(self):
193195
return chain(
194196
((p, parts[p]) for p in parts if parts[p]["required"]),
195197
(("body", self.body),) if self.body else (),
196-
((p, parts[p]) for p in parts
197-
if not parts[p]["required"]
198-
and p not in params),
198+
(
199+
(p, parts[p])
200+
for p in parts
201+
if not parts[p]["required"] and p not in params
202+
),
199203
sorted(params.items(), key=lambda x: (x[0] not in parts, x[0])),
200204
)
201205

@@ -314,7 +318,7 @@ def dump_modules(modules):
314318
unasync.Rule(
315319
fromdir="/elasticsearch/_async/client/",
316320
todir="/elasticsearch/client/",
317-
additional_replacements=additional_replacements
321+
additional_replacements=additional_replacements,
318322
),
319323
]
320324

0 commit comments

Comments
 (0)