Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-os
neo.py@68:14cc5a81333d, 2016-03-30 (annotated)
- Committer:
- Christopher Haster
- Date:
- Wed Mar 30 20:23:24 2016 -0500
- Revision:
- 68:14cc5a81333d
- Parent:
- 66:bfe0df4ef9f5
- Child:
- 69:eb600c35d6ac
Added status command
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Christopher Haster |
15:a6b1f4e65bf4 | 1 | #!/usr/bin/env python |
| Christopher Haster |
15:a6b1f4e65bf4 | 2 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 3 | import argparse |
| Christopher Haster |
15:a6b1f4e65bf4 | 4 | import sys |
| Christopher Haster |
15:a6b1f4e65bf4 | 5 | import re |
| Christopher Haster |
15:a6b1f4e65bf4 | 6 | import subprocess |
| Christopher Haster |
15:a6b1f4e65bf4 | 7 | import os |
| Christopher Haster |
15:a6b1f4e65bf4 | 8 | import contextlib |
| Christopher Haster |
15:a6b1f4e65bf4 | 9 | import shutil |
| Christopher Haster |
35:ffcfa5ace437 | 10 | from collections import * |
| Christopher Haster |
15:a6b1f4e65bf4 | 11 | from itertools import * |
| Christopher Haster |
15:a6b1f4e65bf4 | 12 | |
| screamer | 47:386f8c519142 | 13 | # Default paths to Mercurial and Git |
| Christopher Haster |
50:3770a8991c11 | 14 | hg_cmd = 'hg' |
| Christopher Haster |
50:3770a8991c11 | 15 | git_cmd = 'git' |
| screamer | 47:386f8c519142 | 16 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 17 | # Subparser handling |
| Christopher Haster |
15:a6b1f4e65bf4 | 18 | parser = argparse.ArgumentParser() |
| Christopher Haster |
15:a6b1f4e65bf4 | 19 | subparsers = parser.add_subparsers() |
| Christopher Haster |
15:a6b1f4e65bf4 | 20 | |
| screamer | 63:3b00ba456e00 | 21 | def log(msg): |
| screamer | 63:3b00ba456e00 | 22 | sys.stderr.write("---\n>>> "+msg+"\n") |
| screamer | 63:3b00ba456e00 | 23 | |
| screamer | 63:3b00ba456e00 | 24 | def error(msg, code): |
| screamer | 63:3b00ba456e00 | 25 | sys.stderr.write("---\n>>> ERROR: "+msg+"---\n") |
| screamer | 63:3b00ba456e00 | 26 | sys.exit(code) |
| screamer | 63:3b00ba456e00 | 27 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 28 | def subcommand(name, *args, **kwargs): |
| Christopher Haster |
15:a6b1f4e65bf4 | 29 | def subcommand(command): |
| Christopher Haster |
15:a6b1f4e65bf4 | 30 | subparser = subparsers.add_parser(name, **kwargs) |
| Christopher Haster |
15:a6b1f4e65bf4 | 31 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 32 | for arg in args: |
| Christopher Haster |
15:a6b1f4e65bf4 | 33 | if arg.endswith('?'): |
| Christopher Haster |
15:a6b1f4e65bf4 | 34 | subparser.add_argument(arg.strip('?'), nargs='?') |
| Christopher Haster |
15:a6b1f4e65bf4 | 35 | elif arg.endswith('*'): |
| Christopher Haster |
15:a6b1f4e65bf4 | 36 | pass |
| Christopher Haster |
15:a6b1f4e65bf4 | 37 | else: |
| Christopher Haster |
15:a6b1f4e65bf4 | 38 | subparser.add_argument(arg) |
| Christopher Haster |
15:a6b1f4e65bf4 | 39 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 40 | def thunk(parsed_args): |
| Christopher Haster |
15:a6b1f4e65bf4 | 41 | ordered_args = [vars(parsed_args)[arg.strip('?*')] |
| Christopher Haster |
15:a6b1f4e65bf4 | 42 | if not arg.endswith('*') else remainder |
| Christopher Haster |
15:a6b1f4e65bf4 | 43 | for arg in args] |
| Christopher Haster |
15:a6b1f4e65bf4 | 44 | return command(*ordered_args) |
| Christopher Haster |
15:a6b1f4e65bf4 | 45 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 46 | subparser.set_defaults(command=thunk) |
| Christopher Haster |
15:a6b1f4e65bf4 | 47 | return command |
| Christopher Haster |
15:a6b1f4e65bf4 | 48 | return subcommand |
| Christopher Haster |
15:a6b1f4e65bf4 | 49 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 50 | # Process execution |
| Christopher Haster |
15:a6b1f4e65bf4 | 51 | class ProcessException(Exception): |
| Christopher Haster |
15:a6b1f4e65bf4 | 52 | pass |
| Christopher Haster |
15:a6b1f4e65bf4 | 53 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 54 | def popen(command, stdin=None, **kwargs): |
| Christopher Haster |
15:a6b1f4e65bf4 | 55 | # print for debugging |
| Christopher Haster |
15:a6b1f4e65bf4 | 56 | print ' '.join(command) |
| Christopher Haster |
15:a6b1f4e65bf4 | 57 | proc = subprocess.Popen(command, **kwargs) |
| Christopher Haster |
15:a6b1f4e65bf4 | 58 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 59 | if proc.wait() != 0: |
| Christopher Haster |
15:a6b1f4e65bf4 | 60 | raise ProcessException(proc.returncode) |
| Christopher Haster |
15:a6b1f4e65bf4 | 61 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 62 | def pquery(command, stdin=None, **kwargs): |
| Christopher Haster |
15:a6b1f4e65bf4 | 63 | proc = subprocess.Popen(command, stdout=subprocess.PIPE, **kwargs) |
| Christopher Haster |
15:a6b1f4e65bf4 | 64 | stdout, _ = proc.communicate(stdin) |
| Christopher Haster |
15:a6b1f4e65bf4 | 65 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 66 | if proc.returncode != 0: |
| Christopher Haster |
15:a6b1f4e65bf4 | 67 | raise ProcessException(proc.returncode) |
| Christopher Haster |
15:a6b1f4e65bf4 | 68 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 69 | return stdout |
| Christopher Haster |
15:a6b1f4e65bf4 | 70 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 71 | # Directory navigation |
| Christopher Haster |
15:a6b1f4e65bf4 | 72 | @contextlib.contextmanager |
| Christopher Haster |
15:a6b1f4e65bf4 | 73 | def cd(newdir): |
| Christopher Haster |
15:a6b1f4e65bf4 | 74 | prevdir = os.getcwd() |
| Christopher Haster |
15:a6b1f4e65bf4 | 75 | os.chdir(newdir) |
| Christopher Haster |
15:a6b1f4e65bf4 | 76 | try: |
| Christopher Haster |
15:a6b1f4e65bf4 | 77 | yield |
| Christopher Haster |
15:a6b1f4e65bf4 | 78 | finally: |
| Christopher Haster |
15:a6b1f4e65bf4 | 79 | os.chdir(prevdir) |
| Christopher Haster |
15:a6b1f4e65bf4 | 80 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 81 | # Handling for multiple version controls |
| Christopher Haster |
35:ffcfa5ace437 | 82 | scms = OrderedDict() |
| Christopher Haster |
35:ffcfa5ace437 | 83 | def scm(name): |
| Christopher Haster |
35:ffcfa5ace437 | 84 | def scm(cls): |
| Christopher Haster |
35:ffcfa5ace437 | 85 | scms[name] = cls() |
| Christopher Haster |
35:ffcfa5ace437 | 86 | return cls |
| Christopher Haster |
35:ffcfa5ace437 | 87 | return scm |
| Christopher Haster |
35:ffcfa5ace437 | 88 | |
| Christopher Haster |
35:ffcfa5ace437 | 89 | def staticclass(cls): |
| Christopher Haster |
35:ffcfa5ace437 | 90 | for k, v in cls.__dict__.items(): |
| Christopher Haster |
36:5f4546dde73b | 91 | if hasattr(v, '__call__') and not k.startswith('__'): |
| Christopher Haster |
35:ffcfa5ace437 | 92 | setattr(cls, k, staticmethod(v)) |
| Christopher Haster |
35:ffcfa5ace437 | 93 | |
| Christopher Haster |
35:ffcfa5ace437 | 94 | return cls |
| Christopher Haster |
35:ffcfa5ace437 | 95 | |
| Christopher Haster |
35:ffcfa5ace437 | 96 | @scm('hg') |
| Christopher Haster |
35:ffcfa5ace437 | 97 | @staticclass |
| Christopher Haster |
35:ffcfa5ace437 | 98 | class Hg(object): |
| Christopher Haster |
64:012dffea11fd | 99 | name = 'hg' |
| Christopher Haster |
64:012dffea11fd | 100 | |
| Christopher Haster |
35:ffcfa5ace437 | 101 | def clone(url, name=None, hash=None): |
| screamer | 63:3b00ba456e00 | 102 | log("Cloning "+name+" from "+url) |
| screamer | 47:386f8c519142 | 103 | popen([hg_cmd, 'clone', url, name] + (['-u', hash] if hash else [])) |
| Christopher Haster |
35:ffcfa5ace437 | 104 | |
| screamer | 63:3b00ba456e00 | 105 | def add(file): |
| screamer | 63:3b00ba456e00 | 106 | log("Adding "+file) |
| screamer | 63:3b00ba456e00 | 107 | popen([hg_cmd, 'add', file]) |
| screamer | 63:3b00ba456e00 | 108 | |
| Christopher Haster |
35:ffcfa5ace437 | 109 | def remove(file): |
| screamer | 63:3b00ba456e00 | 110 | log("Removing "+file) |
| screamer | 47:386f8c519142 | 111 | popen([hg_cmd, 'rm', '-f', file]) |
| Christopher Haster |
35:ffcfa5ace437 | 112 | try: |
| Christopher Haster |
35:ffcfa5ace437 | 113 | os.remove(file) |
| Christopher Haster |
35:ffcfa5ace437 | 114 | except OSError: |
| Christopher Haster |
35:ffcfa5ace437 | 115 | pass |
| Christopher Haster |
35:ffcfa5ace437 | 116 | |
| screamer | 63:3b00ba456e00 | 117 | def commit(): |
| screamer | 63:3b00ba456e00 | 118 | popen([hg_cmd, 'commit']) |
| screamer | 63:3b00ba456e00 | 119 | |
| screamer | 63:3b00ba456e00 | 120 | def push(): |
| screamer | 63:3b00ba456e00 | 121 | log("Pushing to remote repository") |
| screamer | 63:3b00ba456e00 | 122 | popen([hg_cmd, 'push']) |
| screamer | 63:3b00ba456e00 | 123 | |
| Christopher Haster |
35:ffcfa5ace437 | 124 | def pull(hash=None): |
| screamer | 63:3b00ba456e00 | 125 | log("Pulling from remote repository") |
| screamer | 47:386f8c519142 | 126 | popen([hg_cmd, 'pull']) |
| screamer | 47:386f8c519142 | 127 | popen([hg_cmd, 'update'] + (['-r', hash] if hash else [])) |
| Christopher Haster |
35:ffcfa5ace437 | 128 | |
| Christopher Haster |
68:14cc5a81333d | 129 | def status(): |
| Christopher Haster |
68:14cc5a81333d | 130 | popen([hg_cmd, 'status']) |
| Christopher Haster |
68:14cc5a81333d | 131 | |
| screamer | 63:3b00ba456e00 | 132 | def hash(): |
| screamer | 63:3b00ba456e00 | 133 | return pquery([hg_cmd, 'id', '-i']).strip().strip('+') |
| screamer | 63:3b00ba456e00 | 134 | def dirty(): |
| screamer | 63:3b00ba456e00 | 135 | return pquery([hg_cmd, 'status', '-q']) |
| Christopher Haster |
35:ffcfa5ace437 | 136 | |
| Christopher Haster |
44:5ff277e7f754 | 137 | def ignore(file): |
| Christopher Haster |
52:25da1dfebd7a | 138 | hooked = False |
| Christopher Haster |
52:25da1dfebd7a | 139 | hook = 'ignore.local = .hg/hgignore' |
| Christopher Haster |
52:25da1dfebd7a | 140 | with open('.hg/hgrc') as f: |
| Christopher Haster |
52:25da1dfebd7a | 141 | if hook not in f.read().splitlines(): |
| Christopher Haster |
52:25da1dfebd7a | 142 | with open('.hg/hgrc', 'a') as f: |
| Christopher Haster |
52:25da1dfebd7a | 143 | f.write('[ui]\n') |
| Christopher Haster |
52:25da1dfebd7a | 144 | f.write(hook + '\n') |
| Christopher Haster |
52:25da1dfebd7a | 145 | |
| Christopher Haster |
44:5ff277e7f754 | 146 | file = '^%s/' % file |
| screamer | 46:915be4b5a8f0 | 147 | exclude = '.hg/hgignore' |
| Christopher Haster |
42:58b35941ebd0 | 148 | with open(exclude, 'a') as f: |
| Christopher Haster |
44:5ff277e7f754 | 149 | f.write(file + '\n') |
| Christopher Haster |
42:58b35941ebd0 | 150 | |
| Christopher Haster |
44:5ff277e7f754 | 151 | def unignore(file): |
| Christopher Haster |
44:5ff277e7f754 | 152 | file = '^%s/' % file |
| screamer | 46:915be4b5a8f0 | 153 | exclude = '.hg/hgignore' |
| Christopher Haster |
42:58b35941ebd0 | 154 | if not os.path.isfile(exclude): |
| Christopher Haster |
42:58b35941ebd0 | 155 | return |
| Christopher Haster |
42:58b35941ebd0 | 156 | |
| Christopher Haster |
42:58b35941ebd0 | 157 | with open(exclude) as f: |
| Christopher Haster |
42:58b35941ebd0 | 158 | lines = f.read().splitlines() |
| Christopher Haster |
42:58b35941ebd0 | 159 | |
| Christopher Haster |
44:5ff277e7f754 | 160 | if file not in lines: |
| Christopher Haster |
42:58b35941ebd0 | 161 | return |
| Christopher Haster |
42:58b35941ebd0 | 162 | |
| Christopher Haster |
44:5ff277e7f754 | 163 | lines.remove(file) |
| Christopher Haster |
42:58b35941ebd0 | 164 | |
| Christopher Haster |
42:58b35941ebd0 | 165 | with open(exclude, 'w') as f: |
| Christopher Haster |
42:58b35941ebd0 | 166 | f.write('\n'.join(lines) + '\n') |
| Christopher Haster |
42:58b35941ebd0 | 167 | |
| Christopher Haster |
35:ffcfa5ace437 | 168 | @scm('git') |
| Christopher Haster |
35:ffcfa5ace437 | 169 | @staticclass |
| Christopher Haster |
35:ffcfa5ace437 | 170 | class Git(object): |
| Christopher Haster |
64:012dffea11fd | 171 | name = 'git' |
| Christopher Haster |
64:012dffea11fd | 172 | |
| Christopher Haster |
35:ffcfa5ace437 | 173 | def clone(url, name=None, hash=None): |
| screamer | 63:3b00ba456e00 | 174 | log("Cloning "+name+" from "+url) |
| screamer | 47:386f8c519142 | 175 | popen([git_cmd, 'clone', url, name]) |
| Christopher Haster |
35:ffcfa5ace437 | 176 | if hash: |
| Christopher Haster |
39:8d6f31570710 | 177 | with cd(name): |
| screamer | 47:386f8c519142 | 178 | popen([git_cmd, 'checkout', '-q', hash]) |
| Christopher Haster |
35:ffcfa5ace437 | 179 | |
| screamer | 63:3b00ba456e00 | 180 | def add(file): |
| screamer | 63:3b00ba456e00 | 181 | log("Adding "+file) |
| screamer | 63:3b00ba456e00 | 182 | popen([git_cmd, 'add', file]) |
| screamer | 63:3b00ba456e00 | 183 | |
| screamer | 63:3b00ba456e00 | 184 | def remove(file): |
| screamer | 63:3b00ba456e00 | 185 | log("Removing "+file) |
| screamer | 63:3b00ba456e00 | 186 | popen([git_cmd, 'rm', '-f', file]) |
| Christopher Haster |
39:8d6f31570710 | 187 | |
| screamer | 63:3b00ba456e00 | 188 | def commit(): |
| screamer | 63:3b00ba456e00 | 189 | popen([git_cmd, 'commit', '-a']) |
| screamer | 63:3b00ba456e00 | 190 | |
| screamer | 63:3b00ba456e00 | 191 | def push(): |
| screamer | 63:3b00ba456e00 | 192 | log("Pushing to remote repository") |
| screamer | 63:3b00ba456e00 | 193 | popen([git_cmd, 'push', '--all']) |
| screamer | 63:3b00ba456e00 | 194 | |
| Christopher Haster |
39:8d6f31570710 | 195 | def pull(hash=None): |
| screamer | 63:3b00ba456e00 | 196 | log("Pulling from remote repository") |
| screamer | 47:386f8c519142 | 197 | popen([git_cmd, 'fetch', 'origin']) |
| screamer | 47:386f8c519142 | 198 | popen([git_cmd, 'merge'] + ([hash] if hash else [])) |
| Christopher Haster |
35:ffcfa5ace437 | 199 | |
| Christopher Haster |
68:14cc5a81333d | 200 | def status(): |
| Christopher Haster |
68:14cc5a81333d | 201 | popen([git_cmd, 'status', '-s']) |
| Christopher Haster |
68:14cc5a81333d | 202 | |
| screamer | 63:3b00ba456e00 | 203 | def hash(): |
| screamer | 63:3b00ba456e00 | 204 | return pquery([git_cmd, 'rev-parse', '--short', 'HEAD']).strip() |
| screamer | 63:3b00ba456e00 | 205 | |
| screamer | 63:3b00ba456e00 | 206 | def dirty(): |
| screamer | 63:3b00ba456e00 | 207 | return pquery([git_cmd, 'diff', '--name-only', 'HEAD']) |
| Christopher Haster |
40:2446665dfdf8 | 208 | |
| Christopher Haster |
44:5ff277e7f754 | 209 | def ignore(file): |
| Christopher Haster |
42:58b35941ebd0 | 210 | exclude = '.git/info/exclude' |
| Christopher Haster |
42:58b35941ebd0 | 211 | with open(exclude, 'a') as f: |
| Christopher Haster |
44:5ff277e7f754 | 212 | f.write(file + '\n') |
| Christopher Haster |
42:58b35941ebd0 | 213 | |
| Christopher Haster |
44:5ff277e7f754 | 214 | def unignore(file): |
| Christopher Haster |
42:58b35941ebd0 | 215 | exclude = '.git/info/exclude' |
| Christopher Haster |
42:58b35941ebd0 | 216 | if not os.path.isfile(exclude): |
| Christopher Haster |
42:58b35941ebd0 | 217 | return |
| Christopher Haster |
42:58b35941ebd0 | 218 | |
| Christopher Haster |
42:58b35941ebd0 | 219 | with open(exclude) as f: |
| Christopher Haster |
42:58b35941ebd0 | 220 | lines = f.read().splitlines() |
| Christopher Haster |
42:58b35941ebd0 | 221 | |
| Christopher Haster |
44:5ff277e7f754 | 222 | if file not in lines: |
| Christopher Haster |
42:58b35941ebd0 | 223 | return |
| Christopher Haster |
42:58b35941ebd0 | 224 | |
| Christopher Haster |
44:5ff277e7f754 | 225 | lines.remove(file) |
| Christopher Haster |
42:58b35941ebd0 | 226 | |
| Christopher Haster |
42:58b35941ebd0 | 227 | with open(exclude, 'w') as f: |
| Christopher Haster |
42:58b35941ebd0 | 228 | f.write('\n'.join(lines) + '\n') |
| Christopher Haster |
40:2446665dfdf8 | 229 | |
| Christopher Haster |
35:ffcfa5ace437 | 230 | |
| Christopher Haster |
35:ffcfa5ace437 | 231 | # Repository object |
| Christopher Haster |
15:a6b1f4e65bf4 | 232 | class Repo(object): |
| Christopher Haster |
36:5f4546dde73b | 233 | @classmethod |
| Christopher Haster |
36:5f4546dde73b | 234 | def fromurl(cls, url, path=None): |
| Christopher Haster |
36:5f4546dde73b | 235 | repo = cls() |
| Christopher Haster |
36:5f4546dde73b | 236 | |
| Christopher Haster |
54:3cb9f99bbaaa | 237 | m = re.match('^(.*/([\w+-]+)(?:\.\w+)?)/?(?:#(.*))?$', url.strip()) |
| Christopher Haster |
36:5f4546dde73b | 238 | repo.name = os.path.basename(path or m.group(2)) |
| Christopher Haster |
36:5f4546dde73b | 239 | repo.path = os.path.abspath( |
| Christopher Haster |
36:5f4546dde73b | 240 | path or os.path.join(os.getcwd(), repo.name)) |
| Christopher Haster |
36:5f4546dde73b | 241 | |
| Christopher Haster |
36:5f4546dde73b | 242 | repo.repo = m.group(1) |
| Christopher Haster |
36:5f4546dde73b | 243 | repo.hash = m.group(3) |
| Christopher Haster |
36:5f4546dde73b | 244 | return repo |
| Christopher Haster |
15:a6b1f4e65bf4 | 245 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 246 | @classmethod |
| Christopher Haster |
36:5f4546dde73b | 247 | def fromlib(cls, lib=None): |
| Christopher Haster |
36:5f4546dde73b | 248 | assert lib.endswith('.lib') |
| Christopher Haster |
36:5f4546dde73b | 249 | with open(lib) as f: |
| Christopher Haster |
36:5f4546dde73b | 250 | return cls.fromurl(f.read(), lib[:-4]) |
| Christopher Haster |
15:a6b1f4e65bf4 | 251 | |
| Christopher Haster |
36:5f4546dde73b | 252 | @classmethod |
| Christopher Haster |
36:5f4546dde73b | 253 | def fromrepo(cls, path=None): |
| Christopher Haster |
36:5f4546dde73b | 254 | repo = cls() |
| Christopher Haster |
36:5f4546dde73b | 255 | repo.path = os.path.abspath(path or os.getcwd()) |
| Christopher Haster |
36:5f4546dde73b | 256 | repo.name = os.path.basename(repo.path) |
| Christopher Haster |
15:a6b1f4e65bf4 | 257 | |
| Christopher Haster |
36:5f4546dde73b | 258 | repo.synch() |
| Christopher Haster |
15:a6b1f4e65bf4 | 259 | return repo |
| Christopher Haster |
15:a6b1f4e65bf4 | 260 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 261 | @property |
| Christopher Haster |
15:a6b1f4e65bf4 | 262 | def lib(self): |
| Christopher Haster |
15:a6b1f4e65bf4 | 263 | return self.path + '.lib' |
| Christopher Haster |
15:a6b1f4e65bf4 | 264 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 265 | @property |
| Christopher Haster |
15:a6b1f4e65bf4 | 266 | def url(self): |
| Christopher Haster |
15:a6b1f4e65bf4 | 267 | if self.repo: |
| screamer | 60:52dc8fcb07d8 | 268 | return self.repo + '/' + ('#'+self.hash if self.hash else '') |
| Christopher Haster |
15:a6b1f4e65bf4 | 269 | |
| Christopher Haster |
36:5f4546dde73b | 270 | def synch(self): |
| Christopher Haster |
15:a6b1f4e65bf4 | 271 | if os.path.isdir(self.path): |
| Christopher Haster |
39:8d6f31570710 | 272 | try: |
| Christopher Haster |
39:8d6f31570710 | 273 | self.scm = self.getscm() |
| Christopher Haster |
39:8d6f31570710 | 274 | self.hash = self.gethash() |
| Christopher Haster |
39:8d6f31570710 | 275 | self.libs = list(self.getlibs()) |
| Christopher Haster |
39:8d6f31570710 | 276 | except ProcessException: |
| Christopher Haster |
39:8d6f31570710 | 277 | pass |
| Christopher Haster |
15:a6b1f4e65bf4 | 278 | |
| Christopher Haster |
37:bf73ffd98cca | 279 | if os.path.isfile(self.lib): |
| Christopher Haster |
39:8d6f31570710 | 280 | try: |
| Christopher Haster |
39:8d6f31570710 | 281 | self.repo = self.getrepo() |
| Christopher Haster |
39:8d6f31570710 | 282 | except ProcessException: |
| Christopher Haster |
39:8d6f31570710 | 283 | pass |
| Christopher Haster |
37:bf73ffd98cca | 284 | |
| Christopher Haster |
35:ffcfa5ace437 | 285 | def getscm(self): |
| Christopher Haster |
36:5f4546dde73b | 286 | for name, scm in scms.items(): |
| Christopher Haster |
36:5f4546dde73b | 287 | if os.path.isdir(os.path.join(self.path, '.'+name)): |
| Christopher Haster |
36:5f4546dde73b | 288 | return scm |
| Christopher Haster |
35:ffcfa5ace437 | 289 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 290 | def gethash(self): |
| Christopher Haster |
66:bfe0df4ef9f5 | 291 | if self.scm: |
| Christopher Haster |
66:bfe0df4ef9f5 | 292 | with cd(self.path): |
| Christopher Haster |
66:bfe0df4ef9f5 | 293 | return self.scm.hash() |
| Christopher Haster |
15:a6b1f4e65bf4 | 294 | |
| Christopher Haster |
36:5f4546dde73b | 295 | def getlibs(self): |
| Christopher Haster |
36:5f4546dde73b | 296 | for root, dirs, files in os.walk(self.path): |
| Christopher Haster |
36:5f4546dde73b | 297 | dirs[:] = [d for d in dirs if not d.startswith('.')] |
| Christopher Haster |
36:5f4546dde73b | 298 | files[:] = [f for f in files if not f.startswith('.')] |
| Christopher Haster |
36:5f4546dde73b | 299 | |
| Christopher Haster |
36:5f4546dde73b | 300 | for file in files: |
| Christopher Haster |
36:5f4546dde73b | 301 | if file.endswith('.lib'): |
| Christopher Haster |
36:5f4546dde73b | 302 | yield Repo.fromlib(os.path.join(root, file)) |
| Christopher Haster |
49:977ea8d3e661 | 303 | if file[:-4] in dirs: |
| Christopher Haster |
49:977ea8d3e661 | 304 | dirs.remove(file[:-4]) |
| Christopher Haster |
36:5f4546dde73b | 305 | |
| Christopher Haster |
37:bf73ffd98cca | 306 | def getrepo(self): |
| Christopher Haster |
37:bf73ffd98cca | 307 | with open(self.lib) as f: |
| Christopher Haster |
37:bf73ffd98cca | 308 | return Repo.fromurl(f.read()).repo |
| Christopher Haster |
37:bf73ffd98cca | 309 | |
| Christopher Haster |
36:5f4546dde73b | 310 | def write(self): |
| Christopher Haster |
38:0ca5eea23af9 | 311 | if os.path.isfile(self.lib): |
| Christopher Haster |
38:0ca5eea23af9 | 312 | with open(self.lib) as f: |
| Christopher Haster |
38:0ca5eea23af9 | 313 | if f.read().strip() == self.url.strip(): |
| Christopher Haster |
44:5ff277e7f754 | 314 | print self.name, 'unmodified' |
| Christopher Haster |
38:0ca5eea23af9 | 315 | return |
| Christopher Haster |
36:5f4546dde73b | 316 | |
| Christopher Haster |
38:0ca5eea23af9 | 317 | with open(self.lib, 'w') as f: |
| Christopher Haster |
38:0ca5eea23af9 | 318 | f.write(self.url + '\n') |
| Christopher Haster |
36:5f4546dde73b | 319 | |
| Christopher Haster |
44:5ff277e7f754 | 320 | print self.name, '->', self.url |
| Christopher Haster |
44:5ff277e7f754 | 321 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 322 | # Clone command |
| Christopher Haster |
15:a6b1f4e65bf4 | 323 | @subcommand('import', 'url', 'name?', |
| screamer | 63:3b00ba456e00 | 324 | help='Import a program tree') |
| Christopher Haster |
36:5f4546dde73b | 325 | def import_(url, path=None): |
| Christopher Haster |
36:5f4546dde73b | 326 | repo = Repo.fromurl(url, path) |
| Christopher Haster |
15:a6b1f4e65bf4 | 327 | |
| Christopher Haster |
35:ffcfa5ace437 | 328 | for scm in scms.values(): |
| Christopher Haster |
15:a6b1f4e65bf4 | 329 | try: |
| Christopher Haster |
35:ffcfa5ace437 | 330 | scm.clone(repo.repo, repo.path, repo.hash) |
| Christopher Haster |
15:a6b1f4e65bf4 | 331 | break |
| Christopher Haster |
35:ffcfa5ace437 | 332 | except ProcessException: |
| Christopher Haster |
15:a6b1f4e65bf4 | 333 | pass |
| Christopher Haster |
15:a6b1f4e65bf4 | 334 | |
| Christopher Haster |
36:5f4546dde73b | 335 | repo.synch() |
| Christopher Haster |
15:a6b1f4e65bf4 | 336 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 337 | with cd(repo.path): |
| Christopher Haster |
36:5f4546dde73b | 338 | for lib in repo.libs: |
| Christopher Haster |
36:5f4546dde73b | 339 | import_(lib.url, lib.path) |
| Christopher Haster |
42:58b35941ebd0 | 340 | repo.scm.ignore(lib.path[len(repo.path)+1:]) |
| Christopher Haster |
15:a6b1f4e65bf4 | 341 | |
| Christopher Haster |
19:6ace1080b8bb | 342 | if (not os.path.isfile('mbed_settings.py') and |
| Christopher Haster |
19:6ace1080b8bb | 343 | os.path.isfile('mbed-os/tools/settings.py')): |
| screamer | 30:dcc9ff39aee3 | 344 | shutil.copy('mbed-os/tools/default_settings.py', 'mbed_settings.py') |
| Christopher Haster |
19:6ace1080b8bb | 345 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 346 | # Deploy command |
| Christopher Haster |
15:a6b1f4e65bf4 | 347 | @subcommand('deploy', |
| screamer | 63:3b00ba456e00 | 348 | help='Import library in the current program or library') |
| Christopher Haster |
15:a6b1f4e65bf4 | 349 | def deploy(): |
| Christopher Haster |
36:5f4546dde73b | 350 | repo = Repo.fromrepo() |
| Christopher Haster |
36:5f4546dde73b | 351 | for lib in repo.libs: |
| Christopher Haster |
36:5f4546dde73b | 352 | import_(lib.url, lib.path) |
| Christopher Haster |
42:58b35941ebd0 | 353 | repo.scm.ignore(lib.path[len(repo.path)+1:]) |
| Christopher Haster |
15:a6b1f4e65bf4 | 354 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 355 | # Install/uninstall command |
| Christopher Haster |
15:a6b1f4e65bf4 | 356 | @subcommand('add', 'url', 'name?', |
| screamer | 63:3b00ba456e00 | 357 | help='Add a library to the current program or library') |
| Christopher Haster |
36:5f4546dde73b | 358 | def add(url, path=None): |
| Christopher Haster |
36:5f4546dde73b | 359 | repo = Repo.fromrepo() |
| Christopher Haster |
15:a6b1f4e65bf4 | 360 | |
| Christopher Haster |
36:5f4546dde73b | 361 | lib = Repo.fromurl(url, path) |
| Christopher Haster |
36:5f4546dde73b | 362 | import_(lib.url, lib.path) |
| Christopher Haster |
42:58b35941ebd0 | 363 | repo.scm.ignore(lib.path[len(repo.path)+1:]) |
| Christopher Haster |
36:5f4546dde73b | 364 | lib.synch() |
| Christopher Haster |
15:a6b1f4e65bf4 | 365 | |
| Christopher Haster |
36:5f4546dde73b | 366 | lib.write() |
| Christopher Haster |
36:5f4546dde73b | 367 | repo.scm.add(lib.lib) |
| Christopher Haster |
15:a6b1f4e65bf4 | 368 | |
| Christopher Haster |
36:5f4546dde73b | 369 | @subcommand('remove', 'path', |
| screamer | 63:3b00ba456e00 | 370 | help='Remove a library from the current program or library') |
| Christopher Haster |
36:5f4546dde73b | 371 | def remove(path): |
| Christopher Haster |
36:5f4546dde73b | 372 | repo = Repo.fromrepo() |
| Christopher Haster |
36:5f4546dde73b | 373 | lib = Repo.fromrepo(path) |
| Christopher Haster |
15:a6b1f4e65bf4 | 374 | |
| Christopher Haster |
36:5f4546dde73b | 375 | repo.scm.remove(lib.lib) |
| Christopher Haster |
36:5f4546dde73b | 376 | shutil.rmtree(lib.path) |
| Christopher Haster |
42:58b35941ebd0 | 377 | repo.scm.unignore(lib.path[len(repo.path)+1:]) |
| Christopher Haster |
15:a6b1f4e65bf4 | 378 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 379 | # Publish command |
| Christopher Haster |
15:a6b1f4e65bf4 | 380 | @subcommand('publish', |
| screamer | 63:3b00ba456e00 | 381 | help='Publish working tree to remote repositories') |
| Christopher Haster |
18:1b4252106474 | 382 | def publish(always=True): |
| Christopher Haster |
36:5f4546dde73b | 383 | repo = Repo.fromrepo() |
| Christopher Haster |
36:5f4546dde73b | 384 | for lib in repo.libs: |
| Christopher Haster |
36:5f4546dde73b | 385 | with cd(lib.path): |
| Christopher Haster |
36:5f4546dde73b | 386 | publish(False) |
| Christopher Haster |
36:5f4546dde73b | 387 | synch() |
| Christopher Haster |
15:a6b1f4e65bf4 | 388 | |
| Christopher Haster |
36:5f4546dde73b | 389 | dirty = repo.scm.dirty() |
| Christopher Haster |
17:1e487c450f06 | 390 | |
| Christopher Haster |
36:5f4546dde73b | 391 | if dirty: |
| screamer | 63:3b00ba456e00 | 392 | log('Uncommitted changes in %s (%s)' % (repo.name, repo.path)) |
| screamer | 63:3b00ba456e00 | 393 | raw_input('Press enter to commit and push: ') |
| Christopher Haster |
36:5f4546dde73b | 394 | repo.scm.commit() |
| Christopher Haster |
17:1e487c450f06 | 395 | |
| Christopher Haster |
36:5f4546dde73b | 396 | if dirty or always: |
| Christopher Haster |
18:1b4252106474 | 397 | try: |
| Christopher Haster |
36:5f4546dde73b | 398 | repo.scm.push() |
| Christopher Haster |
18:1b4252106474 | 399 | except ProcessException as e: |
| Christopher Haster |
18:1b4252106474 | 400 | sys.exit(e[0]) |
| Christopher Haster |
15:a6b1f4e65bf4 | 401 | |
| Christopher Haster |
20:84d6e18cbc20 | 402 | # Update command |
| Christopher Haster |
20:84d6e18cbc20 | 403 | @subcommand('update', 'ref?', |
| screamer | 63:3b00ba456e00 | 404 | help='Update current program or library and recursively update all libraries') |
| Christopher Haster |
20:84d6e18cbc20 | 405 | def update(ref=None): |
| Christopher Haster |
36:5f4546dde73b | 406 | repo = Repo.fromrepo() |
| Christopher Haster |
36:5f4546dde73b | 407 | repo.scm.pull(ref) |
| Christopher Haster |
36:5f4546dde73b | 408 | |
| Christopher Haster |
36:5f4546dde73b | 409 | for lib in repo.libs: |
| Christopher Haster |
37:bf73ffd98cca | 410 | if (not os.path.isfile(lib.lib) or |
| Christopher Haster |
37:bf73ffd98cca | 411 | lib.repo != Repo.fromrepo(lib.path).repo): |
| Christopher Haster |
36:5f4546dde73b | 412 | with cd(lib.path): |
| Christopher Haster |
36:5f4546dde73b | 413 | if lib.cwd.dirty(): |
| screamer | 63:3b00ba456e00 | 414 | error('Uncommitted changes in %s (%s)\n' |
| screamer | 63:3b00ba456e00 | 415 | % (lib.name, lib.path), 1) |
| Christopher Haster |
20:84d6e18cbc20 | 416 | |
| Christopher Haster |
36:5f4546dde73b | 417 | shutil.rmtree(lib.path) |
| Christopher Haster |
42:58b35941ebd0 | 418 | repo.scm.unignore(lib.path[len(repo.path)+1:]) |
| Christopher Haster |
36:5f4546dde73b | 419 | |
| Christopher Haster |
36:5f4546dde73b | 420 | repo.synch() |
| Christopher Haster |
36:5f4546dde73b | 421 | |
| Christopher Haster |
36:5f4546dde73b | 422 | for lib in repo.libs: |
| Christopher Haster |
45:5a91306f7924 | 423 | if os.path.isdir(lib.path): |
| Christopher Haster |
45:5a91306f7924 | 424 | with cd(lib.path): |
| Christopher Haster |
45:5a91306f7924 | 425 | update(lib.hash) |
| Christopher Haster |
45:5a91306f7924 | 426 | else: |
| Christopher Haster |
42:58b35941ebd0 | 427 | import_(lib.url, lib.path) |
| Christopher Haster |
42:58b35941ebd0 | 428 | repo.scm.ignore(lib.path[len(repo.path)+1:]) |
| Christopher Haster |
20:84d6e18cbc20 | 429 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 430 | # Synch command |
| Christopher Haster |
15:a6b1f4e65bf4 | 431 | @subcommand('synch', |
| screamer | 63:3b00ba456e00 | 432 | help='Synchronize library references (.lib files)') |
| Christopher Haster |
15:a6b1f4e65bf4 | 433 | def synch(): |
| Christopher Haster |
36:5f4546dde73b | 434 | repo = Repo.fromrepo() |
| Christopher Haster |
36:5f4546dde73b | 435 | for lib in repo.libs: |
| Christopher Haster |
36:5f4546dde73b | 436 | lib.synch() |
| Christopher Haster |
43:8a4a902a0654 | 437 | lib.write() |
| Christopher Haster |
15:a6b1f4e65bf4 | 438 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 439 | # Compile command |
| Christopher Haster |
15:a6b1f4e65bf4 | 440 | @subcommand('compile', 'args*', |
| screamer | 63:3b00ba456e00 | 441 | help='Compile project using mbed OS build system') |
| Christopher Haster |
15:a6b1f4e65bf4 | 442 | def compile(args): |
| Christopher Haster |
15:a6b1f4e65bf4 | 443 | if not os.path.isdir('mbed-os'): |
| screamer | 63:3b00ba456e00 | 444 | error('mbed-os not found?\n', -1) |
| Christopher Haster |
15:a6b1f4e65bf4 | 445 | |
| Christopher Haster |
40:2446665dfdf8 | 446 | repo = Repo.fromrepo() |
| Christopher Haster |
40:2446665dfdf8 | 447 | |
| Christopher Haster |
21:1a3f920b6f07 | 448 | macros = [] |
| Christopher Haster |
15:a6b1f4e65bf4 | 449 | if os.path.isfile('MACROS.txt'): |
| Christopher Haster |
15:a6b1f4e65bf4 | 450 | with open('MACROS.txt') as f: |
| Christopher Haster |
15:a6b1f4e65bf4 | 451 | macros = f.read().splitlines() |
| Christopher Haster |
15:a6b1f4e65bf4 | 452 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 453 | env = os.environ.copy() |
| Christopher Haster |
15:a6b1f4e65bf4 | 454 | env['PYTHONPATH'] = '.' |
| Christopher Haster |
15:a6b1f4e65bf4 | 455 | popen(['python', 'mbed-os/tools/make.py'] |
| Christopher Haster |
15:a6b1f4e65bf4 | 456 | + list(chain.from_iterable(izip(repeat('-D'), macros))) |
| Christopher Haster |
36:5f4546dde73b | 457 | + ['--source=%s' % repo.path, |
| Christopher Haster |
36:5f4546dde73b | 458 | '--build=%s' % os.path.join(repo.path, '.build')] |
| Christopher Haster |
15:a6b1f4e65bf4 | 459 | + args, |
| Christopher Haster |
15:a6b1f4e65bf4 | 460 | env=env) |
| Christopher Haster |
15:a6b1f4e65bf4 | 461 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 462 | # Export command |
| Christopher Haster |
15:a6b1f4e65bf4 | 463 | @subcommand('export', 'args*', |
| screamer | 63:3b00ba456e00 | 464 | help='Generate project files for IDE') |
| Christopher Haster |
15:a6b1f4e65bf4 | 465 | def export(args): |
| Christopher Haster |
15:a6b1f4e65bf4 | 466 | if not os.path.isdir('mbed-os'): |
| screamer | 63:3b00ba456e00 | 467 | error('mbed-os not found?\n', -1) |
| Christopher Haster |
15:a6b1f4e65bf4 | 468 | |
| Christopher Haster |
40:2446665dfdf8 | 469 | repo = Repo.fromrepo() |
| Christopher Haster |
40:2446665dfdf8 | 470 | |
| Christopher Haster |
41:59e9d808ee05 | 471 | macros = [] |
| Christopher Haster |
41:59e9d808ee05 | 472 | if os.path.isfile('MACROS.txt'): |
| Christopher Haster |
41:59e9d808ee05 | 473 | with open('MACROS.txt') as f: |
| Christopher Haster |
41:59e9d808ee05 | 474 | macros = f.read().splitlines() |
| Christopher Haster |
41:59e9d808ee05 | 475 | |
| Christopher Haster |
18:1b4252106474 | 476 | env = os.environ.copy() |
| Christopher Haster |
18:1b4252106474 | 477 | env['PYTHONPATH'] = '.' |
| Christopher Haster |
15:a6b1f4e65bf4 | 478 | popen(['python', 'mbed-os/tools/project.py', |
| Christopher Haster |
36:5f4546dde73b | 479 | '--source=%s' % repo.path] |
| Christopher Haster |
41:59e9d808ee05 | 480 | + list(chain.from_iterable(izip(repeat('-D'), macros))) |
| Christopher Haster |
18:1b4252106474 | 481 | + args, |
| Christopher Haster |
18:1b4252106474 | 482 | env=env) |
| Christopher Haster |
15:a6b1f4e65bf4 | 483 | |
| Christopher Haster |
68:14cc5a81333d | 484 | # Helpful status commands |
| Christopher Haster |
65:905ba27af203 | 485 | @subcommand('ls', |
| Christopher Haster |
61:5c3f41de9d97 | 486 | help='list repositories recursively') |
| Christopher Haster |
61:5c3f41de9d97 | 487 | def list_(prefix=''): |
| Christopher Haster |
61:5c3f41de9d97 | 488 | repo = Repo.fromrepo() |
| Christopher Haster |
61:5c3f41de9d97 | 489 | print prefix + repo.name, '(%s)' % repo.hash |
| Christopher Haster |
61:5c3f41de9d97 | 490 | |
| Christopher Haster |
61:5c3f41de9d97 | 491 | for i, lib in enumerate(repo.libs): |
| Christopher Haster |
61:5c3f41de9d97 | 492 | if prefix: |
| Christopher Haster |
61:5c3f41de9d97 | 493 | nprefix = prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ') |
| Christopher Haster |
61:5c3f41de9d97 | 494 | else: |
| Christopher Haster |
61:5c3f41de9d97 | 495 | nprefix = '' |
| Christopher Haster |
61:5c3f41de9d97 | 496 | |
| Christopher Haster |
61:5c3f41de9d97 | 497 | nprefix += '|- ' if i < len(repo.libs)-1 else '`- ' |
| Christopher Haster |
61:5c3f41de9d97 | 498 | |
| Christopher Haster |
61:5c3f41de9d97 | 499 | with cd(lib.path): |
| Christopher Haster |
61:5c3f41de9d97 | 500 | list_(nprefix) |
| Christopher Haster |
61:5c3f41de9d97 | 501 | |
| Christopher Haster |
68:14cc5a81333d | 502 | @subcommand('status', |
| Christopher Haster |
68:14cc5a81333d | 503 | help='show status of nested repositories') |
| Christopher Haster |
68:14cc5a81333d | 504 | def status(): |
| Christopher Haster |
68:14cc5a81333d | 505 | repo = Repo.fromrepo() |
| Christopher Haster |
68:14cc5a81333d | 506 | if repo.scm.dirty(): |
| Christopher Haster |
68:14cc5a81333d | 507 | print '---', repo.name, '---' |
| Christopher Haster |
68:14cc5a81333d | 508 | repo.scm.status() |
| Christopher Haster |
68:14cc5a81333d | 509 | |
| Christopher Haster |
68:14cc5a81333d | 510 | for lib in repo.libs: |
| Christopher Haster |
68:14cc5a81333d | 511 | with cd(lib.path): |
| Christopher Haster |
68:14cc5a81333d | 512 | status() |
| Christopher Haster |
68:14cc5a81333d | 513 | |
| Christopher Haster |
15:a6b1f4e65bf4 | 514 | # Parse/run command |
| Christopher Haster |
15:a6b1f4e65bf4 | 515 | args, remainder = parser.parse_known_args() |
| Christopher Haster |
15:a6b1f4e65bf4 | 516 | status = args.command(args) |
| Christopher Haster |
15:a6b1f4e65bf4 | 517 | sys.exit(status or 0) |
| Christopher Haster |
15:a6b1f4e65bf4 | 518 |