Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
Christopher Haster
Date:
Wed Mar 30 16:10:42 2016 -0500
Revision:
42:58b35941ebd0
Parent:
41:59e9d808ee05
Child:
43:8a4a902a0654
Added hidden ignoring of .lib files in git/hg

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