MCPcopy Create free account
hub / github.com/apple/ml-pointersect / save_code

Function save_code

cdslib/core/utils/print_and_save.py:146–204  ·  view source on GitHub ↗

Backup the code in a src directory to a dst directory. The function recursively copies the code (determined by their file extensions) in the folder base_src_dir to a target folder dest_dir while preserving the sub-folder structures. This is useful to take snapshot of the codebase.

(src_dir: str, dest_dir: str, excluded_folders=("artifacts", "__MACOSX"))

Source from the content-addressed store, hash-verified

144
145
146def save_code(src_dir: str, dest_dir: str, excluded_folders=("artifacts", "__MACOSX")):
147 """Backup the code in a src directory to a dst directory.
148
149 The function recursively copies the code (determined by their file extensions) in the folder base_src_dir
150 to a target folder dest_dir while preserving the sub-folder structures.
151
152 This is useful to take snapshot of the codebase.
153
154 Args:
155 src_dir:
156 The root folder directory to copy the code.
157 dest_dir:
158 The destination folder.
159 excluded_folders:
160 A list of subfolder names to exclude from the copying. None: no excluded sub-folders.
161
162 """
163 if not os.path.exists(src_dir):
164 assert False, "Source directory : " + src_dir + " does not exist"
165
166 if not os.path.exists(dest_dir):
167 os.makedirs(dest_dir)
168
169 path_queue = [""]
170
171 while len(path_queue) > 0:
172 current_path = path_queue.pop() # last item in the queue
173 if not os.path.isdir(os.path.join(src_dir, current_path)):
174 src_file = os.path.join(src_dir, current_path)
175 if src_file.endswith(
176 (
177 ".py",
178 ".sh",
179 ".txt",
180 ".cpp",
181 ".c",
182 ".h",
183 ".hpp",
184 ".ipynb",
185 ".yaml",
186 ".cu",
187 )
188 ):
189 shutil.copyfile(src_file, os.path.join(dest_dir, current_path))
190 else:
191 if excluded_folders is not None and current_path in excluded_folders:
192 continue
193 subdirs = os.listdir(os.path.join(src_dir, current_path))
194 for subdir in subdirs:
195 if subdir.startswith(".") or subdir.startswith("__"): # ignore hidden directories or files
196 continue
197
198 # create dst dir if src is a dir
199 if os.path.isdir(os.path.join(src_dir, current_path, subdir)):
200 dst_subdir = os.path.join(dest_dir, current_path, subdir)
201 if not os.path.exists(dst_subdir):
202 os.makedirs(dst_subdir)
203

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected