Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
screamer
Date:
Fri Apr 01 14:50:03 2016 +0100
Revision:
73:f8ed8e727640
Parent:
71:ddc08f2d5697
Parent:
72:08d69d476038
Child:
74:09b1f16aea24
Merge

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