Skip to content

Commit a206068

Browse files
authored
Prevent Index creation during import (mlcommons#205)
1 parent 1cf476e commit a206068

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

mlc/action.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ def load_repos(self):
165165
logger.error(f"Error reading file: {e}")
166166
return None
167167

168+
def get_index(self):
169+
if self._index is None:
170+
self._index = Index(self.repos_path, self.repos)
171+
return self._index
168172

169173
def __init__(self):
170174
setup_logging(log_path=os.getcwd(), log_file='.mlc-log.txt')
@@ -200,7 +204,7 @@ def __init__(self):
200204

201205
self.repos = self.load_repos_and_meta()
202206
#logger.info(f"In Action class: {self.repos_path}")
203-
self.index = Index(self.repos_path, self.repos)
207+
self._index = None
204208

205209

206210
def add(self, i):
@@ -380,7 +384,7 @@ def rm(self, i):
380384

381385
logger.info(f"{target_name} item: {item_path} has been successfully removed")
382386

383-
self.index.rm(item_meta, target_name, item_path)
387+
self.get_index().rm(item_meta, target_name, item_path)
384388

385389
return {
386390
"return": 0,
@@ -413,7 +417,7 @@ def save_new_meta(self, i, item_id, item_name, target_name, item_path, repo):
413417
if save_result["return"] > 0:
414418
return save_result
415419

416-
self.index.add(item_meta, target_name, item_path, repo)
420+
self.get_index().add(item_meta, target_name, item_path, repo)
417421
return {'return': 0}
418422

419423
def update(self, i):
@@ -482,7 +486,7 @@ def update(self, i):
482486
# Save the updated meta back to the item
483487
item.meta = meta
484488
save_result = utils.save_json(item_meta_path, meta=meta)
485-
self.index.update(meta, target_name, item.path, item.repo)
489+
self.get_index().update(meta, target_name, item.path, item.repo)
486490

487491
return {'return': 0, 'message': f"Tags updated successfully for {len(found_items)} item(s).", 'list': found_items }
488492

@@ -641,13 +645,13 @@ def mv(self, run_args):
641645
#Put the src uid to the destination path
642646
dest.meta['uid'] = src.meta['uid']
643647
dest._save_meta()
644-
self.index.update(dest.meta, target_name, dest.path, dest.repo)
648+
self.get_index().update(dest.meta, target_name, dest.path, dest.repo)
645649
logger.info(f"""Item with uid {dest.meta['uid']} successfully moved from {src.path} to {dest.path}""")
646650

647651
return {'return': 0, 'src': src, 'dest': dest}
648652

649653
def search(self, i):
650-
indices = self.index.indices
654+
indices = self.get_index().indices
651655
target = i.get('target_name', self.action_type)
652656
target_index = indices.get(target)
653657
result = []

mlc/index.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,10 @@ def rm(self, meta, folder_type, path):
131131
self._save_indices()
132132

133133
def get_item_mtime(self,file):
134-
# logger.debug(f"Getting latest modified time for file: {file}")
135134
latest = 0
136135
t = os.path.getmtime(file)
137136
if t > latest:
138137
latest = t
139-
logger.debug(f"Latest modified time updated to: {latest}")
140-
# logger.debug("No changes in modified time detected.")
141138
return latest
142139

143140
def build_index(self):
@@ -203,7 +200,7 @@ def build_index(self):
203200
repo_path = repo.path #os.path.join(self.repos_path, repo)
204201
if not os.path.isdir(repo_path):
205202
continue
206-
logger.debug(f"Checking repository: {repo_path}")
203+
logger.debug(f"------------Checking repository: {repo_path}---------------")
207204
# Filter for relevant directories in the repo
208205
for folder_type in ["script", "cache", "experiment"]:
209206
logger.debug(f"Checking folder type: {folder_type}")
@@ -230,12 +227,15 @@ def build_index(self):
230227
config_path = json_path
231228
else:
232229
logger.debug(f"No config file found in {automation_path}, skipping")
230+
delete_flag = False
233231
if automation_dir in self.modified_times:
234232
del self.modified_times[automation_dir]
235233
if any(automation_dir in item["path"] for item in self.indices[folder_type]):
236234
logger.debug(f"Removed index entry (if it exists) for {folder_type} : {automation_dir}")
235+
delete_flag = True
237236
self._remove_index_entry(automation_path)
238-
self._save_indices()
237+
if delete_flag:
238+
self._save_indices()
239239
continue
240240
current_item_keys.add(config_path)
241241
mtime = self.get_item_mtime(config_path)

mlc/logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ColoredFormatter(logging.Formatter):
99
"""Custom formatter class to add colors to log levels"""
1010
COLORS = {
1111
'INFO': Fore.GREEN,
12+
'DEBUG': Fore.CYAN + Style.DIM,
1213
'WARNING': Fore.YELLOW,
1314
'ERROR': Fore.RED
1415
}

mlc/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
import inspect
66
import shlex
7+
import unicodedata
78
from . import utils
89

910
from .action import Action, default_parent
@@ -43,7 +44,7 @@ def _load_meta(self):
4344
logger.info(f"No meta file found in {self.path}")
4445

4546
def search(self, i):
46-
indices = self.action_object.index.indices
47+
indices = self.action_object.get_index().indices
4748
target_index = indices.get(self.automation_type)
4849
result = []
4950
if target_index:
@@ -178,7 +179,8 @@ def configure_logging(args):
178179
args.extra[:] = [log_flag_aliases.get(a, a) for a in args.extra]
179180
for flag, level in log_levels.items():
180181
if flag in args.extra:
181-
logger.setLevel(level)
182+
if not logger.isEnabledFor(level):
183+
logger.setLevel(level)
182184
args.extra.remove(flag)
183185

184186

@@ -219,6 +221,7 @@ def is_quoted(arg):
219221
return (arg.startswith("'") and arg.endswith("'")) or \
220222
(arg.startswith('"') and arg.endswith('"'))
221223

224+
222225
def check_raw_arguments_for_non_ascii():
223226
bad_args = []
224227

0 commit comments

Comments
 (0)