Skip to content

Commit 8df286b

Browse files
committed
Prevent conflicting repos to be added via mlc add repo
1 parent a206068 commit 8df286b

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

mlc/repo_action.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def add(self, run_args):
7777

7878
repo_path = os.path.join(self.repos_path, repo_folder_name)
7979

80+
r = self.find(run_args)
81+
82+
if r['return'] == 0 and len(r['list']) > 0:
83+
return {'return': 1, "error": f"""Repo already exists at {r['list'][0]}"""}
84+
8085
for repo in self.repos:
8186
if repo.path == i_repo_path:
8287
return {'return': 1, "error": f"""Repo already exists at {repo.path}"""}
@@ -95,7 +100,6 @@ def add(self, run_args):
95100
os.makedirs(repo_path)
96101
else:
97102
repo_path = os.path.abspath(i_repo_path)
98-
logger.info(f"""New repo path: {repo_path}""")
99103

100104
#check if it has MLC meta
101105
meta_file = os.path.join(repo_path, "meta.yaml")
@@ -107,7 +111,8 @@ def add(self, run_args):
107111
utils.save_yaml(meta_file, meta)
108112
else:
109113
meta = utils.read_yaml(meta_file)
110-
self.register_repo(repo_path, meta)
114+
115+
self.register_repo(repo_path, meta, run_args.get('ignore_on_conflict'))
111116

112117
return {'return': 0}
113118

@@ -116,13 +121,32 @@ def conflicting_repo(self, repo_meta):
116121
if repo_object.meta.get('uid', '') == '':
117122
return {"return": 1, "error": f"UID is not present in file 'meta.yaml' in the repo path {repo_object.path}"}
118123
if repo_meta["uid"] == repo_object.meta.get('uid', ''):
119-
if repo_meta['path'] == repo_object.path:
124+
if repo_meta.get('path', '') == repo_object.path:
120125
return {"return": 1, "error": f"Same repo is already registered"}
121126
else:
122127
return {"return": 1, "error": f"Conflicting with repo in the path {repo_object.path}", "conflicting_path": repo_object.path}
123128
return {"return": 0}
124129

125-
def register_repo(self, repo_path, repo_meta):
130+
def register_repo(self, repo_path, repo_meta, ignore_on_conflict=False):
131+
132+
# Check UID conflicts
133+
is_conflict = self.conflicting_repo(repo_meta)
134+
if is_conflict['return'] > 0:
135+
if "UID not present" in is_conflict['error']:
136+
logger.warning(f"UID not found in meta.yaml at {repo_path}. Repo can not be registered in MLC repos. Skipping...")
137+
return {"return": 0}
138+
elif "already registered" in is_conflict["error"]: #at same path
139+
#logger.warning(is_conflict["error"])
140+
logger.debug("No changes made to repos.json.")
141+
return {"return": 0}
142+
else:
143+
logger.warning(f"The repo to be registered has conflict with the repo already in the path: {is_conflict['conflicting_path']}")
144+
if ignore_on_conflict:
145+
logger.warning(f"Ignoring reister as ignore_on_conflict is set")
146+
return {"return": 0, 'conflict': True}
147+
148+
self.unregister_repo(is_conflict['conflicting_path'])
149+
logger.warning(f"{is_conflict['conflicting_path']} is unregistered.")
126150

127151
if repo_meta.get('deps'):
128152
for dep in repo_meta['deps']:
@@ -345,39 +369,18 @@ def pull_repo(self, repo_url, branch=None, checkout = None, tag = None, pat = No
345369
# check the meta file to obtain uids
346370
meta_file_path = os.path.join(repo_path, 'meta.yaml')
347371
if not os.path.exists(meta_file_path):
348-
logger.warning(f"meta.yaml not found in {repo_path}. Repo pulled but not register in mlc repos. Skipping...")
372+
logger.warning(f"meta.yaml not found in {repo_path}. Repo pulled but not registered in MLC repos. Skipping...")
349373
return {"return": 0}
350374

351375
with open(meta_file_path, 'r') as meta_file:
352376
meta_data = yaml.safe_load(meta_file)
353377
meta_data["path"] = repo_path
354378

355-
# Check UID conflicts
356-
is_conflict = self.conflicting_repo(meta_data)
357-
if is_conflict['return'] > 0:
358-
if "UID not present" in is_conflict['error']:
359-
logger.warning(f"UID not found in meta.yaml at {repo_path}. Repo pulled but can not register in mlc repos. Skipping...")
360-
return {"return": 0}
361-
elif "already registered" in is_conflict["error"]:
362-
#logger.warning(is_conflict["error"])
363-
logger.debug("No changes made to repos.json.")
364-
return {"return": 0}
365-
else:
366-
if ignore_on_conflict:
367-
logger.debug("Repo alias existing. Ignoring the repo pull")
368-
return {"return": 0}
369-
370-
logger.warning(f"The repo to be cloned has conflict with the repo already in the path: {is_conflict['conflicting_path']}")
371-
self.unregister_repo(is_conflict['conflicting_path'])
372-
self.register_repo(repo_path, meta_data)
373-
logger.warning(f"{repo_path} is registered in repos.json and {is_conflict['conflicting_path']} is unregistered.")
374-
return {"return": 0}
375-
else:
376-
r = self.register_repo(repo_path, meta_data)
377-
if r['return'] > 0:
378-
return r
379+
r = self.register_repo(repo_path, meta_data, ignore_on_conflict)
380+
if r['return'] > 0:
381+
return r
379382

380-
return {"return": 0}
383+
return {"return": 0}
381384

382385
except subprocess.CalledProcessError as e:
383386
return {'return': 1, 'error': f"Git command failed: {e}"}

0 commit comments

Comments
 (0)