Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
screamer
Date:
Fri Apr 01 14:24:17 2016 +0100
Revision:
67:c3b033f16424
Parent:
66:bfe0df4ef9f5
Child:
69:eb600c35d6ac
Add check whether a command is executed within repo context

Who changed what in which revision?

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