1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
"""All the action we need during build"""
4
+ import io
4
5
import json
5
6
import os
6
- import io
7
7
import pathlib
8
8
import re
9
9
import urllib .request as url_lib
10
10
import zipfile
11
- from typing import List
12
11
13
- import nox # pylint: disable=import-error
12
+ import nox # pylint: disable=import-error
13
+
14
14
15
15
def _install_bundle (session : nox .Session ) -> None :
16
16
session .install (
@@ -28,15 +28,6 @@ def _install_bundle(session: nox.Session) -> None:
28
28
_install_package (f"{ os .getcwd ()} /bundled/libs" , "debugpy" )
29
29
30
30
31
- def _check_files (names : List [str ]) -> None :
32
- root_dir = pathlib .Path (__file__ ).parent
33
- for name in names :
34
- file_path = root_dir / name
35
- lines : List [str ] = file_path .read_text ().splitlines ()
36
- if any (line for line in lines if line .startswith ("# TODO:" )):
37
- raise Exception (f"Please update { os .fspath (file_path )} ." )
38
-
39
-
40
31
def _update_pip_packages (session : nox .Session ) -> None :
41
32
session .run ("pip-compile" , "--generate-hashes" , "--upgrade" , "./requirements.in" )
42
33
session .run (
@@ -46,6 +37,26 @@ def _update_pip_packages(session: nox.Session) -> None:
46
37
"./src/test/python_tests/requirements.in" ,
47
38
)
48
39
40
+
41
+ @nox .session ()
42
+ def lint (session : nox .Session ) -> None :
43
+ """Runs linter and formatter checks on python files."""
44
+
45
+ session .install ("flake8" )
46
+ session .run ("flake8" , "noxfile.py" )
47
+
48
+ # check formatting using black
49
+ session .install ("black" )
50
+ session .run ("black" , "--check" , "noxfile.py" )
51
+
52
+ # check import sorting using isort
53
+ session .install ("isort" )
54
+ session .run ("isort" , "--check" , "noxfile.py" )
55
+
56
+ # check typescript code
57
+ session .run ("npm" , "run" , "lint" , external = True )
58
+
59
+
49
60
@nox .session ()
50
61
def tests (session : nox .Session ) -> None :
51
62
"""Runs all the tests for the extension."""
@@ -117,56 +128,27 @@ def setup(session: nox.Session) -> None:
117
128
"""Sets up the extension for development."""
118
129
_setup_template_environment (session )
119
130
120
- @nox .session ()
121
- def validate_readme (session : nox .Session ) -> None :
122
- """Ensures the linter version in 'requirements.txt' matches 'readme.md'."""
123
- requirements_file = pathlib .Path (__file__ ).parent / "requirements.txt"
124
- readme_file = pathlib .Path (__file__ ).parent / "README.md"
125
-
126
- lines = requirements_file .read_text (encoding = "utf-8" ).splitlines (keepends = False )
127
- module = _get_module_name ()
128
- linter_ver = list (line for line in lines if line .startswith (module ))[0 ]
129
- name , version = linter_ver .split (" " )[0 ].split ("==" )
130
-
131
- session .log (f"Looking for { name } ={ version } in README.md" )
132
- content = readme_file .read_text (encoding = "utf-8" )
133
- if f"{ name } ={ version } " not in content :
134
- raise ValueError (f"Linter info { name } ={ version } was not found in README.md." )
135
- session .log (f"FOUND { name } ={ version } in README.md" )
136
-
137
-
138
- def _update_readme () -> None :
139
- requirements_file = pathlib .Path (__file__ ).parent / "requirements.txt"
140
- lines = requirements_file .read_text (encoding = "utf-8" ).splitlines (keepends = False )
141
- module = _get_module_name ()
142
- linter_ver = list (line for line in lines if line .startswith (module ))[0 ]
143
- _ , version = linter_ver .split (" " )[0 ].split ("==" )
144
-
145
- readme_file = pathlib .Path (__file__ ).parent / "README.md"
146
- content = readme_file .read_text (encoding = "utf-8" )
147
- regex = r"\`([a-zA-Z0-9]+)=([0-9]+\.[0-9]+\.[0-9]+)\`"
148
- result = re .sub (regex , f"`{ module } ={ version } `" , content , 0 , re .MULTILINE )
149
- content = readme_file .write_text (result , encoding = "utf-8" )
150
-
151
131
152
132
@nox .session ()
153
133
def update_packages (session : nox .Session ) -> None :
154
134
"""Update pip and npm packages."""
155
135
session .install ("wheel" , "pip-tools" )
156
136
_update_pip_packages (session )
157
137
_update_npm_packages (session )
158
- _update_readme ()
138
+
159
139
160
140
def _contains (s , parts = ()):
161
141
return any (p for p in parts if p in s )
162
142
143
+
163
144
def _get_pypi_package_data (package_name ):
164
145
json_uri = "https://pypi.org/pypi/{0}/json" .format (package_name )
165
146
# Response format: https://warehouse.readthedocs.io/api-reference/json/#project
166
147
# Release metadata format: https://github.com/pypa/interoperability-peps/blob/master/pep-0426-core-metadata.rst
167
148
with url_lib .urlopen (json_uri ) as response :
168
149
return json .loads (response .read ())
169
150
151
+
170
152
def _get_urls (data , version ):
171
153
return list (
172
154
r ["url" ] for r in data ["releases" ][version ] if _contains (r ["url" ], ("cp37" ,))
@@ -185,6 +167,7 @@ def _download_and_extract(root, url, version):
185
167
print ("\t " + zip_info .filename )
186
168
wheel .extract (zip_info .filename , root )
187
169
170
+
188
171
def _install_package (root , package_name , version = "latest" ):
189
172
from packaging .version import parse as version_parser
190
173
@@ -198,6 +181,7 @@ def _install_package(root, package_name, version="latest"):
198
181
for url in _get_urls (data , use_version ):
199
182
_download_and_extract (root , url , use_version )
200
183
184
+
201
185
@nox .session ()
202
186
def update_build_number (session : nox .Session ) -> None :
203
187
"""Updates build number for the extension."""
@@ -218,4 +202,4 @@ def update_build_number(session: nox.Session) -> None:
218
202
219
203
session .log (f"Updating version from { package_json ['version' ]} to { version } " )
220
204
package_json ["version" ] = version
221
- package_json_path .write_text (json .dumps (package_json , indent = 4 ), encoding = "utf-8" )
205
+ package_json_path .write_text (json .dumps (package_json , indent = 4 ), encoding = "utf-8" )
0 commit comments