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