Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
Christopher Haster
Date:
Wed Mar 30 20:09:05 2016 -0500
Revision:
64:012dffea11fd
Parent:
63:3b00ba456e00
Child:
65:905ba27af203
Added names to scms

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