Skip to content

Commit

Permalink
added "cm checkout repo mlcommons@ck --branch=dev"
Browse files Browse the repository at this point in the history
  • Loading branch information
gfursin authored and pgmpablo157321 committed Mar 20, 2024
1 parent 0c25fc9 commit 702ef5d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
46 changes: 39 additions & 7 deletions cm/cmind/repo/automation/repo/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def pull(self, i):
(pat) (str): Personal Access Token (if supported and url=='')
(branch) (str): Git branch
(checkout) (str): Git checkout
(checkout_only) (bool): only checkout existing repo
(depth) (int): Git depth
(desc) (str): brief repository description (1 line)
(prefix) (str): extra directory to keep CM artifacts
Expand All @@ -48,6 +49,8 @@ def pull(self, i):
prefix = i.get('prefix','')
pat = i.get('pat','')

checkout_only = i.get('checkout_only', False)

if url == '':
if alias != '':
url = self.cmind.cfg['repo_url_prefix']
Expand Down Expand Up @@ -80,7 +83,7 @@ def pull(self, i):

if url == '':
pull_repos = []

for repo in sorted(self.cmind.repos.lst, key = lambda x: x.meta.get('alias','')):
meta = repo.meta

Expand All @@ -89,7 +92,7 @@ def pull(self, i):
# Pick it up from the path

repo_path = repo.path

pull_repos.append({'alias': os.path.basename(repo_path),
'path_to_repo': repo_path})
else:
Expand Down Expand Up @@ -127,15 +130,23 @@ def pull(self, i):

# Prepare path to repo
repos = self.cmind.repos

r = repos.pull(alias = alias, url = url, branch = branch, checkout = checkout, console = console,
desc=desc, prefix=prefix, depth=depth, path_to_repo=path_to_repo)

r = repos.pull(alias = alias,
url = url,
branch = branch,
checkout = checkout,
console = console,
desc=desc,
prefix=prefix,
depth=depth,
path_to_repo=path_to_repo,
checkout_only=checkout_only)
if r['return']>0: return r

repo_meta = r['meta']

repo_metas[alias] = repo_meta

if len(pull_repos)>0 and self.cmind.use_index:
if console:
print (self.cmind.cfg['line'])
Expand All @@ -145,6 +156,28 @@ def pull(self, i):

return {'return':0, 'meta':repo_meta, 'metas': repo_metas}



############################################################
def checkout(self, i):
"""
Checkout repository
Args:
(branch) (str): branch name
(checkout) (str): checkout
See "pull" action
Returns:
See "pull" action
"""

i['checkout_only'] = True

return self.pull(i)


############################################################
def show(self, i):
"""
Expand All @@ -162,7 +195,6 @@ def show(self, i):
return self.search(i)



############################################################
def search(self, i):
"""
Expand Down
63 changes: 34 additions & 29 deletions cm/cmind/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def process(self, repo_path, mode='add'):
return {'return':0}

############################################################
def pull(self, alias, url = '', branch = '', checkout = '', console = False, desc = '', prefix = '', depth = None, path_to_repo = None):
def pull(self, alias, url = '', branch = '', checkout = '', console = False, desc = '', prefix = '', depth = None, path_to_repo = None, checkout_only = False):
"""
Clone or pull CM repository
Expand All @@ -261,6 +261,7 @@ def pull(self, alias, url = '', branch = '', checkout = '', console = False, des
(url) (str): Git repository URL
(branch) (str): Git repository branch
(checkout) (str): Git repository checkout
(checkout_only) (bool): only checkout existing repo
(depth) (int): Git repository depth
(console) (bool): if True, print some info to console
(desc) (str): optional repository description
Expand Down Expand Up @@ -310,47 +311,48 @@ def pull(self, alias, url = '', branch = '', checkout = '', console = False, des

download=True if url.find('.zip')>0 else False

if download:
# If CM repo already exists
if os.path.isdir(path_to_repo):
return {'return':1, 'error':'repository is already installed'}
if not checkout_only:
if download:
# If CM repo already exists
if os.path.isdir(path_to_repo):
return {'return':1, 'error':'repository is already installed'}

os.makedirs(path_to_repo)

os.chdir(path_to_repo)

cmd = 'wget --no-check-certificate "'+url+'" -O '+alias
os.makedirs(path_to_repo)

else:
if os.path.isdir(path_to_repo):
# Attempt to update
os.chdir(path_to_repo)

cmd = 'git pull'
cmd = 'wget --no-check-certificate "'+url+'" -O '+alias

else:
# Attempt to clone
clone = True
if os.path.isdir(path_to_repo):
# Attempt to update
os.chdir(path_to_repo)

os.chdir(self.full_path_to_repos)
cmd = 'git pull'
else:
# Attempt to clone
clone = True

cmd = 'git clone '+url+' '+alias
os.chdir(self.full_path_to_repos)

# Check if depth is set
if depth!=None and depth!='':
cmd+=' --depth '+str(depth)
cmd = 'git clone '+url+' '+alias

if console:
print (cmd)
print ('')
# Check if depth is set
if depth!=None and depth!='':
cmd+=' --depth '+str(depth)

r = os.system(cmd)
if console:
print (cmd)
print ('')

if clone and not os.path.isdir(path_to_repo):
return {'return':1, 'error':'repository was not cloned'}
r = os.system(cmd)

if clone and not os.path.isdir(path_to_repo):
return {'return':1, 'error':'repository was not cloned'}

os.chdir(path_to_repo)

if download:
if download and not checkout_only:
import zipfile

pack_file = os.path.join(path_to_repo, alias)
Expand Down Expand Up @@ -396,8 +398,11 @@ def pull(self, alias, url = '', branch = '', checkout = '', console = False, des
if branch != '' or checkout != '':
cmd = 'git checkout'

# When checkout only, we do not need -b for branch
extra_flag = ' ' if checkout_only else ' -b '

if branch != '':
cmd += ' -b ' + branch
cmd += extra_flag + branch

if checkout!='':
cmd += ' ' + checkout
Expand Down

0 comments on commit 702ef5d

Please sign in to comment.