1
1
import glob
2
2
import os
3
3
import shutil
4
+ from datetime import datetime
4
5
from pprint import pprint
5
6
6
7
import fire
7
8
import tqdm
8
9
import yaml
10
+ from pip ._internal .operations import freeze
9
11
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" )
10
15
REPO_NAME = "lightning-tutorials"
11
16
DEFAULT_BRANCH = "main"
12
17
TEMPLATE_HEADER = f"""
64
69
class HelperCLI :
65
70
66
71
SKIP_DIRS = (
67
- "docs" ,
68
72
".actions" ,
69
73
".github" ,
74
+ ".notebooks" ,
75
+ "docs" ,
70
76
)
71
77
META_FILE = ".meta.yml"
72
78
REQUIREMENTS_FILE = "requirements.txt"
73
79
74
80
@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
+ """
76
86
with open (fpath , "r" ) as fp :
77
87
py_file = fp .readlines ()
78
88
fpath_meta = os .path .join (os .path .dirname (fpath ), HelperCLI .META_FILE )
@@ -94,7 +104,6 @@ def group_folders(
94
104
fpath_drop_folders : str = "dropped-folders.txt" ,
95
105
) -> None :
96
106
"""Group changes by folders
97
-
98
107
Args:
99
108
fpath_gitdiff: raw git changes
100
109
@@ -125,8 +134,8 @@ def group_folders(
125
134
@staticmethod
126
135
def parse_requirements (dir_path : str ):
127
136
"""Parse standard requirements from meta file
128
-
129
- :param dir_path: path to the folder
137
+ Args:
138
+ dir_path: path to the folder
130
139
"""
131
140
fpath = os .path .join (dir_path , HelperCLI .META_FILE )
132
141
assert os .path .isfile (fpath )
@@ -141,6 +150,11 @@ def parse_requirements(dir_path: str):
141
150
142
151
@staticmethod
143
152
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
+ """
144
158
ls_ipynb = []
145
159
for sub in (['*.ipynb' ], ['**' , '*.ipynb' ]):
146
160
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
159
173
@staticmethod
160
174
def valid_accelerator (dir_path : str ):
161
175
"""Parse standard requirements from meta file
162
-
163
- :param dir_path: path to the folder
176
+ Args:
177
+ dir_path: path to the folder
164
178
"""
165
179
fpath = os .path .join (dir_path , HelperCLI .META_FILE )
166
180
assert os .path .isfile (fpath )
@@ -170,6 +184,35 @@ def valid_accelerator(dir_path: str):
170
184
os_acc = os .environ .get ("ACCLERATOR" , 'cpu' )
171
185
return int (os_acc in accels )
172
186
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
+
173
216
174
217
if __name__ == '__main__' :
175
218
fire .Fire (HelperCLI )
0 commit comments