From c82f469fa82218659b43459972642a9188e50db3 2013-05-03 22:33:59 From: MinRK Date: 2013-05-03 22:33:59 Subject: [PATCH] add utils.submodule --- diff --git a/IPython/utils/submodule.py b/IPython/utils/submodule.py new file mode 100644 index 0000000..144678c --- /dev/null +++ b/IPython/utils/submodule.py @@ -0,0 +1,92 @@ +"""utilities for checking submodule status""" + +#----------------------------------------------------------------------------- +# Copyright (C) 2013 The IPython Development Team +# +# Distributed under the terms of the BSD License. The full license is in +# the file COPYING, distributed as part of this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + +import os +import subprocess +import sys + +#----------------------------------------------------------------------------- +# Globals +#----------------------------------------------------------------------------- + +pjoin = os.path.join + +#----------------------------------------------------------------------------- +# Code +#----------------------------------------------------------------------------- + +def ipython_parent(): + """return IPython's parent (i.e. root if run from git)""" + from IPython.utils.path import get_ipython_package_dir + return os.path.abspath(os.path.dirname(get_ipython_package_dir())) + +def ipython_submodules(root): + """return IPython submodules relative to root""" + return [ + pjoin(root, 'IPython', 'frontend', 'html', 'notebook', 'static', 'components'), + ] + +def is_repo(d): + """is d a git repo?""" + return os.path.exists(pjoin(d, '.git')) + +def check_submodule_status(root=None): + """check submodule status + + Has three return values: + + 'missing' - submodules are absent + 'unclean' - submodules have unstaged changes + 'clean' - all submodules are up to date + """ + + if hasattr(sys, "frozen"): + # frozen via py2exe or similar, don't bother + return 'clean' + + if not root: + root = ipython_parent() + + submodules = ipython_submodules(root) + + for submodule in submodules: + if not os.path.exists(submodule): + return 'missing' + + if not is_repo(root): + # not in git, assume clean + return 'clean' + + # check with git submodule status + proc = subprocess.Popen('git submodule status', + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True, + cwd=root, + ) + status, _ = proc.communicate() + status = status.decode("ascii") + + for line in status.splitlines(): + if status.startswith('-'): + return 'missing' + elif status.startswith('+'): + return 'unclean' + + return 'clean' + +def update_submodules(repo_dir): + """update submodules in a repo""" + subprocess.Popen("git submodule init", cwd=repo_dir, shell=True) + subprocess.Popen("git submodule update", cwd=repo_dir, shell=True) +