Skip to content

Commit

Permalink
Merge pull request #98 from GEOS-ESM/develop
Browse files Browse the repository at this point in the history
Merge Develop into Main for release
  • Loading branch information
mathomp4 committed Aug 19, 2020
2 parents b4561be + d07229e commit 64fda06
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 16 deletions.
40 changes: 40 additions & 0 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"license": "other-open",
"upload_type": "software",
"creators": [
{
"name": "Thompson, Matthew",
"affiliation": "GMAO SSAI",
"orcid": "0000-0001-6222-6863"
},
{
"name": "Chakraborty, Purnendu"
},
{
"name": "Clune, Tom",
"affiliation": "NASA",
"orcid": "0000-0003-3320-0204"
}
],
"contributors": [
{
"name": "Thompson, Matthew",
"affiliation": "GMAO SSAI",
"orcid": "0000-0001-6222-6863",
"type": "ContactPerson"
},
{
"name": "Clune, Tom",
"affiliation": "NASA",
"orcid": "0000-0003-3320-0204",
"type": "ProjectLeader"
}
],
"keywords": [
"git",
"repository",
"python",
"yaml"
],
"access_right": "open"
}
10 changes: 8 additions & 2 deletions mepo.d/cmdline/branch_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ def __init__(self, branch):
def __list(self):
brlist = self.branch_subparsers.add_parser(
'list',
description = 'List local branches of all components')
description = 'List local branches.'
'If no component is specified, runs over all components')
brlist.add_argument(
'-a', '--all',
action = 'store_true',
help = 'list all (local+remote) branches of all components')
help = 'list all (local+remote) branches')
brlist.add_argument(
'comp_name',
metavar = 'comp-name',
nargs = '*',
help = 'Component to list branches in')

def __create(self):
create = self.branch_subparsers.add_parser(
Expand Down
22 changes: 18 additions & 4 deletions mepo.d/cmdline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from cmdline.branch_parser import MepoBranchArgParser
from cmdline.stash_parser import MepoStashArgParser
from cmdline.tag_parser import MepoTagArgParser

class MepoArgParser(object):

Expand All @@ -27,6 +28,7 @@ def parse(self):
self.__checkout()
self.__checkout_if_exists()
self.__branch()
self.__tag()
self.__stash()
self.__develop()
self.__pull()
Expand Down Expand Up @@ -137,15 +139,23 @@ def __fetch_all(self):
fetch_all.add_argument('--tags','-t', action = 'store_true', help = 'Fetch tags.')

def __branch(self):
branch = self.subparsers.add_parser('branch')
branch = self.subparsers.add_parser(
'branch',
description = "Runs branch commands.")
MepoBranchArgParser(branch)

def __stash(self):
stash = self.subparsers.add_parser(
'stash',
description = "Runs stash commands.")
'stash',
description = "Runs stash commands.")
MepoStashArgParser(stash)

def __tag(self):
tag = self.subparsers.add_parser(
'tag',
description = "Runs tag commands.")
MepoTagArgParser(tag)

def __develop(self):
develop = self.subparsers.add_parser(
'develop',
Expand Down Expand Up @@ -216,7 +226,11 @@ def __commit(self):
def __push(self):
push = self.subparsers.add_parser(
'push',
description = 'Push local commits to remote')
description = 'Push local commits or tags to remote')
push.add_argument(
'--tags',
action = 'store_true',
help = 'push tags')
push.add_argument(
'comp_name',
metavar = 'comp-name',
Expand Down
10 changes: 5 additions & 5 deletions mepo.d/cmdline/stash_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def __push(self):
'push',
description = 'Push (create) stash in component <comp-name>')
stpush.add_argument(
'-m', '--message',
type=str,
metavar = 'message',
default=None,
help = 'Message for the stash')
'-m', '--message',
type=str,
metavar = 'message',
default=None,
help = 'Message for the stash')
stpush.add_argument(
'comp_name',
metavar = 'comp-name',
Expand Down
56 changes: 56 additions & 0 deletions mepo.d/cmdline/tag_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import argparse

class MepoTagArgParser(object):

def __init__(self, tag):
self.tag = tag.add_subparsers()
self.tag.title = 'mepo tag sub-commands'
self.tag.dest = 'mepo_tag_cmd'
self.tag.required = True
self.__list()
self.__create()
self.__delete()

def __list(self):
tglist = self.tag.add_parser(
'list',
description = 'List tags.'
'If no component is specified, runs over all components')
tglist.add_argument(
'comp_name',
metavar = 'comp-name',
nargs = '*',
help = 'Component to list tags in')

def __create(self):
create = self.tag.add_parser(
'create',
description = 'Create tag <tag-name> in component <comp-name>')
create.add_argument('tag_name', metavar = 'tag-name')
create.add_argument(
'-a', '--annotate',
action = 'store_true',
help = "Make an annotated tag")
create.add_argument(
'-m', '--message',
type=str,
metavar = 'message',
default = None,
help = "Message for the tag"
)
create.add_argument(
'comp_name',
metavar = 'comp-name',
nargs = '+',
help = 'Component to create tags in')

def __delete(self):
delete = self.tag.add_parser(
'delete',
description = 'Delete tag <tag-name> in component <comp-name>')
delete.add_argument('tag_name', metavar = 'tag-name')
delete.add_argument(
'comp_name',
metavar = 'comp-name',
nargs = '+',
help = 'Component to delete tags in')
13 changes: 11 additions & 2 deletions mepo.d/command/branch/brlist/brlist.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from state.state import MepoState
from utilities import verify
from repository.git import GitRepository

def run(args):
allcomps = MepoState.read_state()
max_namelen = len(max([x.name for x in allcomps], key=len))
comps2list = _get_comps_to_list(args.comp_name, allcomps)
max_namelen = len(max([x.name for x in comps2list], key=len))
FMT = '{:<%s.%ss} | {:<s}' % (max_namelen, max_namelen)
for comp in allcomps:
for comp in comps2list:
git = GitRepository(comp.remote, comp.local)
output = git.list_branch(args.all).rstrip().split('\n')
print(FMT.format(comp.name, output[0]))
for line in output[1:]:
print(FMT.format('', line))

def _get_comps_to_list(specified_comps, allcomps):
comps_to_list = allcomps
if specified_comps:
verify.valid_components(specified_comps, allcomps)
comps_to_list = [x for x in allcomps if x.name in specified_comps]
return comps_to_list
2 changes: 1 addition & 1 deletion mepo.d/command/push/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def run(args):
comps2push = [x for x in allcomps if x.name in args.comp_name]
for comp in comps2push:
git = GitRepository(comp.remote, comp.local)
output = git.push()
output = git.push(args.tags)
print('----------\nPushed: {}\n----------'.format(comp.name))
print(output)
62 changes: 62 additions & 0 deletions mepo.d/command/tag/create/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from state.state import MepoState
from utilities import verify
from repository.git import GitRepository

# Popping up an EDITOR is based on https://stackoverflow.com/a/39989442
import os, tempfile, subprocess

def run(args):
allcomps = MepoState.read_state()
verify.valid_components(args.comp_name, allcomps)
comps2crttg = [x for x in allcomps if x.name in args.comp_name]

tf_file = None

if args.annotate:
create_annotated_tag = True
elif args.message:
create_annotated_tag = True
else:
create_annotated_tag = False

if create_annotated_tag:
# Pop up an editor if a message is not provided
if not args.message:
EDITOR = git_var('GIT_EDITOR')
initial_message = b"" # set up the file

# Use delete=False to keep the file around as we send the file name to git commit -F
tf = tempfile.NamedTemporaryFile(delete=False)
tf_file = tf.name
tf.write(initial_message)
tf.flush()
subprocess.call([EDITOR, tf.name])

for comp in comps2crttg:
git = GitRepository(comp.remote, comp.local)
git.create_tag(args.tag_name,create_annotated_tag,args.message,tf_file)
print('+ {}: {}'.format(comp.name, args.tag_name))

if create_annotated_tag:
# Now close and by-hand delete the temp file
if not args.message:
tf.close()
os.unlink(tf.name)

def git_var(what):
'''
return GIT_EDITOR or GIT_PAGER, for instance
Found at https://stackoverflow.com/a/44174750/1876449
'''
proc = subprocess.Popen(['git', 'var', what], shell=False,
stdout=subprocess.PIPE)
output = proc.stdout.read()
status = proc.wait()
if status != 0:
raise Exception("git_var failed with [%]" % what)
output = output.rstrip(b'\n')
output = output.decode('utf8', errors='ignore') # or similar for py3k
return output

12 changes: 12 additions & 0 deletions mepo.d/command/tag/delete/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from state.state import MepoState
from utilities import verify
from repository.git import GitRepository

def run(args):
allcomps = MepoState.read_state()
verify.valid_components(args.comp_name, allcomps)
comps2deltg = [x for x in allcomps if x.name in args.comp_name]
for comp in comps2deltg:
git = GitRepository(comp.remote, comp.local)
git.delete_tag(args.tag_name)
print('- {}: {}'.format(comp.name, args.tag_name))
15 changes: 15 additions & 0 deletions mepo.d/command/tag/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import subprocess as sp

from state.state import MepoState

from command.tag.tglist import tglist
from command.tag.create import create
from command.tag.delete import delete

def run(args):
d = {
'list': tglist,
'create': create,
'delete': delete
}
d[args.mepo_tag_cmd].run(args)
22 changes: 22 additions & 0 deletions mepo.d/command/tag/tglist/tglist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from state.state import MepoState
from utilities import verify
from repository.git import GitRepository

def run(args):
allcomps = MepoState.read_state()
comps2list = _get_comps_to_list(args.comp_name, allcomps)
max_namelen = len(max([x.name for x in comps2list], key=len))
FMT = '{:<%s.%ss} | {:<s}' % (max_namelen, max_namelen)
for comp in comps2list:
git = GitRepository(comp.remote, comp.local)
output = git.list_tags().rstrip().split('\n')
print(FMT.format(comp.name, output[0]))
for line in output[1:]:
print(FMT.format('', line))

def _get_comps_to_list(specified_comps, allcomps):
comps_to_list = allcomps
if specified_comps:
verify.valid_components(specified_comps, allcomps)
comps_to_list = [x for x in allcomps if x.name in specified_comps]
return comps_to_list
27 changes: 25 additions & 2 deletions mepo.d/repository/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def list_branch(self, all=False):
cmd += ' -a'
return shellcmd.run(cmd.split(), output=True)

def list_tags(self):
cmd = self.__git + ' tag'
return shellcmd.run(cmd.split(), output=True)

def list_stash(self):
cmd = self.__git + ' stash list'
return shellcmd.run(cmd.split(), output=True)
Expand Down Expand Up @@ -100,13 +104,29 @@ def create_branch(self, branch_name):
cmd = self.__git + ' branch {}'.format(branch_name)
shellcmd.run(cmd.split())

def create_tag(self, tag_name, annotate, message, tf_file=None):
if annotate:
if tf_file:
cmd = ['git', '-C', self.__local, 'tag', '-a', '-F', tf_file, tag_name]
elif message:
cmd = ['git', '-C', self.__local, 'tag', '-a', '-m', message, tag_name]
else:
raise Exception("This should not happen")
else:
cmd = ['git', '-C', self.__local, 'tag', tag_name]
shellcmd.run(cmd)

def delete_branch(self, branch_name, force):
delete = '-d'
if force:
delete = '-D'
cmd = self.__git + ' branch {} {}'.format(delete, branch_name)
shellcmd.run(cmd.split())

def delete_tag(self, tag_name):
cmd = self.__git + ' tag -d {}'.format(tag_name)
shellcmd.run(cmd.split())

def verify_branch(self, branch_name):
cmd = self.__git + ' show-branch remotes/origin/{}'.format(branch_name)
status = shellcmd.run(cmd.split(),status=True)
Expand Down Expand Up @@ -226,8 +246,11 @@ def commit_files(self, message, tf_file=None):
raise Exception("This should not happen")
shellcmd.run(cmd)

def push(self):
cmd = self.__git + ' push -u {}'.format(self.__remote)
def push(self,tags):
if tags:
cmd = self.__git + ' push --tags {}'.format(self.__remote)
else:
cmd = self.__git + ' push -u {}'.format(self.__remote)
return shellcmd.run(cmd.split(), output=True).strip()

def get_remote_latest_commit_id(self, branch):
Expand Down

0 comments on commit 64fda06

Please sign in to comment.