Show More
@@ -1,96 +1,105 b'' | |||
|
1 | 1 | """utilities for checking submodule status""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2013 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING, distributed as part of this software. |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | import os |
|
15 | 15 | import subprocess |
|
16 | 16 | import sys |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Globals |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | pjoin = os.path.join |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Code |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | def ipython_parent(): |
|
29 | 29 | """return IPython's parent (i.e. root if run from git)""" |
|
30 | 30 | from IPython.utils.path import get_ipython_package_dir |
|
31 | 31 | return os.path.abspath(os.path.dirname(get_ipython_package_dir())) |
|
32 | 32 | |
|
33 | 33 | def ipython_submodules(root): |
|
34 | 34 | """return IPython submodules relative to root""" |
|
35 | 35 | return [ |
|
36 | 36 | pjoin(root, 'IPython', 'html', 'static', 'components'), |
|
37 | 37 | ] |
|
38 | 38 | |
|
39 | 39 | def is_repo(d): |
|
40 | 40 | """is d a git repo?""" |
|
41 |
|
|
|
41 | if not os.path.exists(pjoin(d, '.git')): | |
|
42 | return False | |
|
43 | proc = subprocess.Popen('git status', | |
|
44 | stdout=subprocess.PIPE, | |
|
45 | stderr=subprocess.PIPE, | |
|
46 | shell=True, | |
|
47 | cwd=d, | |
|
48 | ) | |
|
49 | status, _ = proc.communicate() | |
|
50 | return status == 0 | |
|
42 | 51 | |
|
43 | 52 | def check_submodule_status(root=None): |
|
44 | 53 | """check submodule status |
|
45 | 54 | |
|
46 | 55 | Has three return values: |
|
47 | 56 | |
|
48 | 57 | 'missing' - submodules are absent |
|
49 | 58 | 'unclean' - submodules have unstaged changes |
|
50 | 59 | 'clean' - all submodules are up to date |
|
51 | 60 | """ |
|
52 | 61 | |
|
53 | 62 | if hasattr(sys, "frozen"): |
|
54 | 63 | # frozen via py2exe or similar, don't bother |
|
55 | 64 | return 'clean' |
|
56 | 65 | |
|
57 | 66 | if not root: |
|
58 | 67 | root = ipython_parent() |
|
59 | 68 | |
|
60 | 69 | if not is_repo(root): |
|
61 | 70 | # not in git, assume clean |
|
62 | 71 | return 'clean' |
|
63 | 72 | |
|
64 | 73 | submodules = ipython_submodules(root) |
|
65 | 74 | |
|
66 | 75 | for submodule in submodules: |
|
67 | 76 | if not os.path.exists(submodule): |
|
68 | 77 | return 'missing' |
|
69 | 78 | |
|
70 | 79 | # Popen can't handle unicode cwd on Windows Python 2 |
|
71 | 80 | if sys.platform == 'win32' and sys.version_info[0] < 3 \ |
|
72 | 81 | and not isinstance(root, bytes): |
|
73 | 82 | root = root.encode(sys.getfilesystemencoding() or 'ascii') |
|
74 | 83 | # check with git submodule status |
|
75 | 84 | proc = subprocess.Popen('git submodule status', |
|
76 | 85 | stdout=subprocess.PIPE, |
|
77 | 86 | stderr=subprocess.PIPE, |
|
78 | 87 | shell=True, |
|
79 | 88 | cwd=root, |
|
80 | 89 | ) |
|
81 | 90 | status, _ = proc.communicate() |
|
82 | 91 | status = status.decode("ascii", "replace") |
|
83 | 92 | |
|
84 | 93 | for line in status.splitlines(): |
|
85 | 94 | if line.startswith('-'): |
|
86 | 95 | return 'missing' |
|
87 | 96 | elif line.startswith('+'): |
|
88 | 97 | return 'unclean' |
|
89 | 98 | |
|
90 | 99 | return 'clean' |
|
91 | 100 | |
|
92 | 101 | def update_submodules(repo_dir): |
|
93 | 102 | """update submodules in a repo""" |
|
94 | 103 | subprocess.check_call("git submodule init", cwd=repo_dir, shell=True) |
|
95 | 104 | subprocess.check_call("git submodule update --recursive", cwd=repo_dir, shell=True) |
|
96 | 105 |
General Comments 0
You need to be logged in to leave comments.
Login now