Skip to content

Commit 4b0893d

Browse files
authored
CI: export env details (#16)
* export env details * ci integrate * minor format
1 parent b7aeeb0 commit 4b0893d

File tree

9 files changed

+68
-21
lines changed

9 files changed

+68
-21
lines changed

.actions/helpers.py

+50-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import glob
22
import os
33
import shutil
4+
from datetime import datetime
45
from pprint import pprint
56

67
import fire
78
import tqdm
89
import yaml
10+
from pip._internal.operations import freeze
911

12+
PATH_HERE = os.path.dirname(__file__)
13+
PATH_ROOT = os.path.dirname(PATH_HERE)
14+
PATH_REQ_DEFAULT = os.path.join(PATH_ROOT, "requirements", "default.txt")
1015
REPO_NAME = "lightning-tutorials"
1116
DEFAULT_BRANCH = "main"
1217
TEMPLATE_HEADER = f"""
@@ -64,15 +69,20 @@
6469
class HelperCLI:
6570

6671
SKIP_DIRS = (
67-
"docs",
6872
".actions",
6973
".github",
74+
".notebooks",
75+
"docs",
7076
)
7177
META_FILE = ".meta.yml"
7278
REQUIREMENTS_FILE = "requirements.txt"
7379

7480
@staticmethod
75-
def expand_script(fpath: str):
81+
def enrich_script(fpath: str):
82+
"""Add template header and footer to the python base script.
83+
Args:
84+
fpath: path to python script
85+
"""
7686
with open(fpath, "r") as fp:
7787
py_file = fp.readlines()
7888
fpath_meta = os.path.join(os.path.dirname(fpath), HelperCLI.META_FILE)
@@ -94,7 +104,6 @@ def group_folders(
94104
fpath_drop_folders: str = "dropped-folders.txt",
95105
) -> None:
96106
"""Group changes by folders
97-
98107
Args:
99108
fpath_gitdiff: raw git changes
100109
@@ -125,8 +134,8 @@ def group_folders(
125134
@staticmethod
126135
def parse_requirements(dir_path: str):
127136
"""Parse standard requirements from meta file
128-
129-
:param dir_path: path to the folder
137+
Args:
138+
dir_path: path to the folder
130139
"""
131140
fpath = os.path.join(dir_path, HelperCLI.META_FILE)
132141
assert os.path.isfile(fpath)
@@ -141,6 +150,11 @@ def parse_requirements(dir_path: str):
141150

142151
@staticmethod
143152
def copy_notebooks(path_root: str, path_docs_ipynb: str = "docs/source/notebooks"):
153+
"""Copy all notebooks from a folder to doc folder.
154+
Args:
155+
path_root: source path to the project root in this tutorials
156+
path_docs_ipynb: destination path to the notebooks location
157+
"""
144158
ls_ipynb = []
145159
for sub in (['*.ipynb'], ['**', '*.ipynb']):
146160
ls_ipynb += glob.glob(os.path.join(path_root, '.notebooks', *sub))
@@ -159,8 +173,8 @@ def copy_notebooks(path_root: str, path_docs_ipynb: str = "docs/source/notebooks
159173
@staticmethod
160174
def valid_accelerator(dir_path: str):
161175
"""Parse standard requirements from meta file
162-
163-
:param dir_path: path to the folder
176+
Args:
177+
dir_path: path to the folder
164178
"""
165179
fpath = os.path.join(dir_path, HelperCLI.META_FILE)
166180
assert os.path.isfile(fpath)
@@ -170,6 +184,35 @@ def valid_accelerator(dir_path: str):
170184
os_acc = os.environ.get("ACCLERATOR", 'cpu')
171185
return int(os_acc in accels)
172186

187+
@staticmethod
188+
def update_env_details(dir_path: str):
189+
"""Export the actial packages used in runtime
190+
Args:
191+
dir_path: path to the folder
192+
"""
193+
fpath = os.path.join(dir_path, HelperCLI.META_FILE)
194+
assert os.path.isfile(fpath)
195+
meta = yaml.safe_load(open(fpath))
196+
# default is COU runtime
197+
with open(PATH_REQ_DEFAULT) as fp:
198+
req = fp.readlines()
199+
req += meta.get('requirements', [])
200+
req = [r.strip() for r in req]
201+
202+
def _parse(pkg: str, keys: str = " <=>") -> str:
203+
"""Parsing just the package name"""
204+
if any(c in pkg for c in keys):
205+
ix = min([pkg.index(c) for c in keys if c in pkg])
206+
pkg = pkg[:ix]
207+
return pkg
208+
209+
require = set([_parse(r) for r in req if r])
210+
env = {_parse(p): p for p in freeze.freeze()}
211+
meta['environment'] = [env[r] for r in require]
212+
meta['updated'] = datetime.now().isoformat()
213+
214+
yaml.safe_dump(meta, stream=open(fpath, 'w'))
215+
173216

174217
if __name__ == '__main__':
175218
fire.Fire(HelperCLI)

.actions/ipynb-generate.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ python -c "import os ; assert(os.path.isfile(os.path.join('$1', '.meta.yml')))"
99
py_file=( $(ls "$1"/*.py) )
1010
echo $py_file
1111

12-
python .actions/helpers.py expand_script $py_file
12+
python .actions/helpers.py enrich-script $py_file
1313

1414
jupytext --set-formats "ipynb,py:percent" $py_file

.actions/ipynb-render.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ pip install --quiet --requirement requirements.txt
1717
cat "$1/requirements.txt"
1818
pip install --requirement "$1/requirements.txt"
1919

20-
accel=$(python .actions/helpers.py valid_accelerator $1 2>&1)
20+
accel=$(python .actions/helpers.py valid-accelerator $1 2>&1)
2121
if [ $accel -eq 1 ]
2222
then
2323
papermill $ipynb_file $pub_file
24+
python .actions/helpers.py update-env-details $1
2425
else
2526
echo "WARNING: not valid accelerator so no outputs will be generated"
2627
cp $ipynb_file $pub_file
2728
fi
2829

30+
git add "$1/.meta.yml"
2931
git add $pub_file

.actions/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Fire
22
tqdm
33
PyYAML
4+
pip

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ repos:
1313
- id: end-of-file-fixer
1414
- id: trailing-whitespace
1515
- id: check-yaml
16-
- id: pretty-format-json
1716

1817
- repo: https://github.com/PyCQA/isort
1918
rev: 5.8.0

requirements.txt

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
virtualenv
2-
ipython[notebook]
3-
jupytext # converting
4-
treon # testing
5-
papermill # render
6-
yapf
7-
flake8
8-
isort
9-
1+
-r requirements/devel.txt
102
-r .actions/requirements.txt
113

124
# default for all examples
13-
pytorch-lightning>=1.2
5+
-r requirements/default.txt

requirements/default.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytorch-lightning>=1.2
2+
torch

requirements/devel.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
virtualenv
2+
ipython[notebook]
3+
jupytext # converting
4+
treon # testing
5+
papermill # render
6+
yapf
7+
flake8
8+
isort

starters/datamodules/.meta.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
title: PyTorch Lightning DataModules
22
author: PL team
33
created: 2021-01-31
4-
updated: 2021-02-22
4+
updated: 2021-02-21
55
license: CC
66
description: |
77
This notebook will walk you through how to start using Datamodules.

0 commit comments

Comments
 (0)