Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
Christopher Haster
Date:
Wed Mar 30 16:27:19 2016 -0500
Revision:
44:5ff277e7f754
Parent:
43:8a4a902a0654
Child:
45:5a91306f7924
Added proper support support for ignoring nested git repos in 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 44:5ff277e7f754 114 def ignore(file):
Christopher Haster 44:5ff277e7f754 115 file = '^%s/' % file
Christopher Haster 42:58b35941ebd0 116 exclude = '.hg/exclude'
Christopher Haster 42:58b35941ebd0 117 with open(exclude, 'a') as f:
Christopher Haster 44:5ff277e7f754 118 f.write(file + '\n')
Christopher Haster 42:58b35941ebd0 119
Christopher Haster 44:5ff277e7f754 120 def unignore(file):
Christopher Haster 44:5ff277e7f754 121 file = '^%s/' % file
Christopher Haster 42:58b35941ebd0 122 exclude = '.hg/exclude'
Christopher Haster 42:58b35941ebd0 123 if not os.path.isfile(exclude):
Christopher Haster 42:58b35941ebd0 124 return
Christopher Haster 42:58b35941ebd0 125
Christopher Haster 42:58b35941ebd0 126 with open(exclude) as f:
Christopher Haster 42:58b35941ebd0 127 lines = f.read().splitlines()
Christopher Haster 42:58b35941ebd0 128
Christopher Haster 44:5ff277e7f754 129 if file not in lines:
Christopher Haster 42:58b35941ebd0 130 return
Christopher Haster 42:58b35941ebd0 131
Christopher Haster 44:5ff277e7f754 132 lines.remove(file)
Christopher Haster 42:58b35941ebd0 133
Christopher Haster 42:58b35941ebd0 134 with open(exclude, 'w') as f:
Christopher Haster 42:58b35941ebd0 135 f.write('\n'.join(lines) + '\n')
Christopher Haster 42:58b35941ebd0 136
Christopher Haster 35:ffcfa5ace437 137 @scm('git')
Christopher Haster 35:ffcfa5ace437 138 @staticclass
Christopher Haster 35:ffcfa5ace437 139 class Git(object):
Christopher Haster 35:ffcfa5ace437 140 def clone(url, name=None, hash=None):
Christopher Haster 35:ffcfa5ace437 141 popen(['git', 'clone', url, name])
Christopher Haster 35:ffcfa5ace437 142 if hash:
Christopher Haster 39:8d6f31570710 143 with cd(name):
Christopher Haster 39:8d6f31570710 144 popen(['git', 'checkout', '-q', hash])
Christopher Haster 35:ffcfa5ace437 145
Christopher Haster 35:ffcfa5ace437 146 def add(file): popen(['git', 'add', file])
Christopher Haster 35:ffcfa5ace437 147 def remove(file): popen(['git', 'rm', '-f', file])
Christopher Haster 39:8d6f31570710 148
Christopher Haster 35:ffcfa5ace437 149 def commit(): popen(['git', 'commit', '-a'])
Christopher Haster 39:8d6f31570710 150 def push(): popen(['git', 'push', '--all'])
Christopher Haster 39:8d6f31570710 151 def pull(hash=None):
Christopher Haster 39:8d6f31570710 152 popen(['git', 'fetch', 'origin'])
Christopher Haster 39:8d6f31570710 153 popen(['git', 'merge'] + ([hash] if hash else []))
Christopher Haster 35:ffcfa5ace437 154
Christopher Haster 35:ffcfa5ace437 155 def hash(): return pquery(['git', 'rev-parse', '--short', 'HEAD']).strip()
Christopher Haster 36:5f4546dde73b 156 def dirty(): return pquery(['git', 'diff', '--name-only', 'HEAD'])
Christopher Haster 40:2446665dfdf8 157
Christopher Haster 44:5ff277e7f754 158 def ignore(file):
Christopher Haster 42:58b35941ebd0 159 exclude = '.git/info/exclude'
Christopher Haster 42:58b35941ebd0 160 with open(exclude, 'a') as f:
Christopher Haster 44:5ff277e7f754 161 f.write(file + '\n')
Christopher Haster 42:58b35941ebd0 162
Christopher Haster 44:5ff277e7f754 163 def unignore(file):
Christopher Haster 42:58b35941ebd0 164 exclude = '.git/info/exclude'
Christopher Haster 42:58b35941ebd0 165 if not os.path.isfile(exclude):
Christopher Haster 42:58b35941ebd0 166 return
Christopher Haster 42:58b35941ebd0 167
Christopher Haster 42:58b35941ebd0 168 with open(exclude) as f:
Christopher Haster 42:58b35941ebd0 169 lines = f.read().splitlines()
Christopher Haster 42:58b35941ebd0 170
Christopher Haster 44:5ff277e7f754 171 if file not in lines:
Christopher Haster 42:58b35941ebd0 172 return
Christopher Haster 42:58b35941ebd0 173
Christopher Haster 44:5ff277e7f754 174 lines.remove(file)
Christopher Haster 42:58b35941ebd0 175
Christopher Haster 42:58b35941ebd0 176 with open(exclude, 'w') as f:
Christopher Haster 42:58b35941ebd0 177 f.write('\n'.join(lines) + '\n')
Christopher Haster 40:2446665dfdf8 178
Christopher Haster 35:ffcfa5ace437 179
Christopher Haster 35:ffcfa5ace437 180 # Repository object
Christopher Haster 15:a6b1f4e65bf4 181 class Repo(object):
Christopher Haster 36:5f4546dde73b 182 @classmethod
Christopher Haster 36:5f4546dde73b 183 def fromurl(cls, url, path=None):
Christopher Haster 36:5f4546dde73b 184 repo = cls()
Christopher Haster 36:5f4546dde73b 185
Christopher Haster 36:5f4546dde73b 186 m = re.match('^(.*/([+a-zA-Z0-9_-]+)/?)(?:#(.*))?$', url.strip())
Christopher Haster 36:5f4546dde73b 187 repo.name = os.path.basename(path or m.group(2))
Christopher Haster 36:5f4546dde73b 188 repo.path = os.path.abspath(
Christopher Haster 36:5f4546dde73b 189 path or os.path.join(os.getcwd(), repo.name))
Christopher Haster 36:5f4546dde73b 190
Christopher Haster 36:5f4546dde73b 191 repo.repo = m.group(1)
Christopher Haster 36:5f4546dde73b 192 repo.hash = m.group(3)
Christopher Haster 36:5f4546dde73b 193 return repo
Christopher Haster 15:a6b1f4e65bf4 194
Christopher Haster 15:a6b1f4e65bf4 195 @classmethod
Christopher Haster 36:5f4546dde73b 196 def fromlib(cls, lib=None):
Christopher Haster 36:5f4546dde73b 197 assert lib.endswith('.lib')
Christopher Haster 36:5f4546dde73b 198 with open(lib) as f:
Christopher Haster 36:5f4546dde73b 199 return cls.fromurl(f.read(), lib[:-4])
Christopher Haster 15:a6b1f4e65bf4 200
Christopher Haster 36:5f4546dde73b 201 @classmethod
Christopher Haster 36:5f4546dde73b 202 def fromrepo(cls, path=None):
Christopher Haster 36:5f4546dde73b 203 repo = cls()
Christopher Haster 36:5f4546dde73b 204 repo.path = os.path.abspath(path or os.getcwd())
Christopher Haster 36:5f4546dde73b 205 repo.name = os.path.basename(repo.path)
Christopher Haster 15:a6b1f4e65bf4 206
Christopher Haster 36:5f4546dde73b 207 repo.synch()
Christopher Haster 15:a6b1f4e65bf4 208 return repo
Christopher Haster 15:a6b1f4e65bf4 209
Christopher Haster 15:a6b1f4e65bf4 210 @property
Christopher Haster 15:a6b1f4e65bf4 211 def lib(self):
Christopher Haster 15:a6b1f4e65bf4 212 return self.path + '.lib'
Christopher Haster 15:a6b1f4e65bf4 213
Christopher Haster 15:a6b1f4e65bf4 214 @property
Christopher Haster 15:a6b1f4e65bf4 215 def url(self):
Christopher Haster 15:a6b1f4e65bf4 216 if self.repo:
Christopher Haster 36:5f4546dde73b 217 return self.repo + ('#'+self.hash if self.hash else '')
Christopher Haster 15:a6b1f4e65bf4 218
Christopher Haster 36:5f4546dde73b 219 def synch(self):
Christopher Haster 15:a6b1f4e65bf4 220 if os.path.isdir(self.path):
Christopher Haster 39:8d6f31570710 221 try:
Christopher Haster 39:8d6f31570710 222 self.scm = self.getscm()
Christopher Haster 39:8d6f31570710 223 self.hash = self.gethash()
Christopher Haster 39:8d6f31570710 224 self.libs = list(self.getlibs())
Christopher Haster 39:8d6f31570710 225 except ProcessException:
Christopher Haster 39:8d6f31570710 226 pass
Christopher Haster 15:a6b1f4e65bf4 227
Christopher Haster 37:bf73ffd98cca 228 if os.path.isfile(self.lib):
Christopher Haster 39:8d6f31570710 229 try:
Christopher Haster 39:8d6f31570710 230 self.repo = self.getrepo()
Christopher Haster 39:8d6f31570710 231 except ProcessException:
Christopher Haster 39:8d6f31570710 232 pass
Christopher Haster 37:bf73ffd98cca 233
Christopher Haster 35:ffcfa5ace437 234 def getscm(self):
Christopher Haster 36:5f4546dde73b 235 for name, scm in scms.items():
Christopher Haster 36:5f4546dde73b 236 if os.path.isdir(os.path.join(self.path, '.'+name)):
Christopher Haster 36:5f4546dde73b 237 return scm
Christopher Haster 35:ffcfa5ace437 238
Christopher Haster 15:a6b1f4e65bf4 239 def gethash(self):
Christopher Haster 15:a6b1f4e65bf4 240 with cd(self.path):
Christopher Haster 35:ffcfa5ace437 241 return self.scm.hash()
Christopher Haster 15:a6b1f4e65bf4 242
Christopher Haster 36:5f4546dde73b 243 def getlibs(self):
Christopher Haster 36:5f4546dde73b 244 for root, dirs, files in os.walk(self.path):
Christopher Haster 36:5f4546dde73b 245 dirs[:] = [d for d in dirs if not d.startswith('.')]
Christopher Haster 36:5f4546dde73b 246 files[:] = [f for f in files if not f.startswith('.')]
Christopher Haster 36:5f4546dde73b 247
Christopher Haster 36:5f4546dde73b 248 for file in files:
Christopher Haster 36:5f4546dde73b 249 if file.endswith('.lib'):
Christopher Haster 36:5f4546dde73b 250 yield Repo.fromlib(os.path.join(root, file))
Christopher Haster 36:5f4546dde73b 251
Christopher Haster 37:bf73ffd98cca 252 def getrepo(self):
Christopher Haster 37:bf73ffd98cca 253 with open(self.lib) as f:
Christopher Haster 37:bf73ffd98cca 254 return Repo.fromurl(f.read()).repo
Christopher Haster 37:bf73ffd98cca 255
Christopher Haster 36:5f4546dde73b 256 def write(self):
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 44:5ff277e7f754 260 print self.name, 'unmodified'
Christopher Haster 38:0ca5eea23af9 261 return
Christopher Haster 36:5f4546dde73b 262
Christopher Haster 38:0ca5eea23af9 263 with open(self.lib, 'w') as f:
Christopher Haster 38:0ca5eea23af9 264 f.write(self.url + '\n')
Christopher Haster 36:5f4546dde73b 265
Christopher Haster 44:5ff277e7f754 266 print self.name, '->', self.url
Christopher Haster 44:5ff277e7f754 267
Christopher Haster 15:a6b1f4e65bf4 268 # Clone command
Christopher Haster 15:a6b1f4e65bf4 269 @subcommand('import', 'url', 'name?',
Christopher Haster 15:a6b1f4e65bf4 270 help='recursively import a repository')
Christopher Haster 36:5f4546dde73b 271 def import_(url, path=None):
Christopher Haster 36:5f4546dde73b 272 repo = Repo.fromurl(url, path)
Christopher Haster 15:a6b1f4e65bf4 273
Christopher Haster 35:ffcfa5ace437 274 for scm in scms.values():
Christopher Haster 15:a6b1f4e65bf4 275 try:
Christopher Haster 35:ffcfa5ace437 276 scm.clone(repo.repo, repo.path, repo.hash)
Christopher Haster 15:a6b1f4e65bf4 277 break
Christopher Haster 35:ffcfa5ace437 278 except ProcessException:
Christopher Haster 15:a6b1f4e65bf4 279 pass
Christopher Haster 15:a6b1f4e65bf4 280
Christopher Haster 36:5f4546dde73b 281 repo.synch()
Christopher Haster 15:a6b1f4e65bf4 282
Christopher Haster 15:a6b1f4e65bf4 283 with cd(repo.path):
Christopher Haster 36:5f4546dde73b 284 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 285 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 286 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 15:a6b1f4e65bf4 287
Christopher Haster 19:6ace1080b8bb 288 if (not os.path.isfile('mbed_settings.py') and
Christopher Haster 19:6ace1080b8bb 289 os.path.isfile('mbed-os/tools/settings.py')):
screamer 30:dcc9ff39aee3 290 shutil.copy('mbed-os/tools/default_settings.py', 'mbed_settings.py')
Christopher Haster 19:6ace1080b8bb 291
Christopher Haster 15:a6b1f4e65bf4 292 # Deploy command
Christopher Haster 15:a6b1f4e65bf4 293 @subcommand('deploy',
Christopher Haster 15:a6b1f4e65bf4 294 help='recursively import libraries in current directory')
Christopher Haster 15:a6b1f4e65bf4 295 def deploy():
Christopher Haster 36:5f4546dde73b 296 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 297 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 298 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 299 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 15:a6b1f4e65bf4 300
Christopher Haster 15:a6b1f4e65bf4 301 # Install/uninstall command
Christopher Haster 15:a6b1f4e65bf4 302 @subcommand('add', 'url', 'name?',
Christopher Haster 15:a6b1f4e65bf4 303 help='add a library to the current repository')
Christopher Haster 36:5f4546dde73b 304 def add(url, path=None):
Christopher Haster 36:5f4546dde73b 305 repo = Repo.fromrepo()
Christopher Haster 15:a6b1f4e65bf4 306
Christopher Haster 36:5f4546dde73b 307 lib = Repo.fromurl(url, path)
Christopher Haster 36:5f4546dde73b 308 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 309 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 36:5f4546dde73b 310 lib.synch()
Christopher Haster 15:a6b1f4e65bf4 311
Christopher Haster 36:5f4546dde73b 312 lib.write()
Christopher Haster 36:5f4546dde73b 313 repo.scm.add(lib.lib)
Christopher Haster 15:a6b1f4e65bf4 314
Christopher Haster 36:5f4546dde73b 315 @subcommand('remove', 'path',
Christopher Haster 15:a6b1f4e65bf4 316 help='remove a library from the current repository folder')
Christopher Haster 36:5f4546dde73b 317 def remove(path):
Christopher Haster 36:5f4546dde73b 318 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 319 lib = Repo.fromrepo(path)
Christopher Haster 15:a6b1f4e65bf4 320
Christopher Haster 36:5f4546dde73b 321 repo.scm.remove(lib.lib)
Christopher Haster 36:5f4546dde73b 322 shutil.rmtree(lib.path)
Christopher Haster 42:58b35941ebd0 323 repo.scm.unignore(lib.path[len(repo.path)+1:])
Christopher Haster 15:a6b1f4e65bf4 324
Christopher Haster 15:a6b1f4e65bf4 325 # Publish command
Christopher Haster 15:a6b1f4e65bf4 326 @subcommand('publish',
Christopher Haster 15:a6b1f4e65bf4 327 help='recursively publish changes to remote repositories')
Christopher Haster 18:1b4252106474 328 def publish(always=True):
Christopher Haster 36:5f4546dde73b 329 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 330 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 331 with cd(lib.path):
Christopher Haster 36:5f4546dde73b 332 publish(False)
Christopher Haster 36:5f4546dde73b 333 synch()
Christopher Haster 15:a6b1f4e65bf4 334
Christopher Haster 36:5f4546dde73b 335 dirty = repo.scm.dirty()
Christopher Haster 17:1e487c450f06 336
Christopher Haster 36:5f4546dde73b 337 if dirty:
Christopher Haster 36:5f4546dde73b 338 print 'Uncommitted changes in %s (%s)' % (repo.name, repo.path)
Christopher Haster 24:b29dad301edd 339 raw_input('Press enter to commit and push ')
Christopher Haster 36:5f4546dde73b 340 repo.scm.commit()
Christopher Haster 17:1e487c450f06 341
Christopher Haster 36:5f4546dde73b 342 if dirty or always:
Christopher Haster 18:1b4252106474 343 try:
Christopher Haster 36:5f4546dde73b 344 repo.scm.push()
Christopher Haster 18:1b4252106474 345 except ProcessException as e:
Christopher Haster 18:1b4252106474 346 sys.exit(e[0])
Christopher Haster 15:a6b1f4e65bf4 347
Christopher Haster 20:84d6e18cbc20 348 # Update command
Christopher Haster 20:84d6e18cbc20 349 @subcommand('update', 'ref?',
Christopher Haster 20:84d6e18cbc20 350 help='recursively updates libraries and current repository')
Christopher Haster 20:84d6e18cbc20 351 def update(ref=None):
Christopher Haster 36:5f4546dde73b 352 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 353 repo.scm.pull(ref)
Christopher Haster 36:5f4546dde73b 354
Christopher Haster 36:5f4546dde73b 355 for lib in repo.libs:
Christopher Haster 37:bf73ffd98cca 356 if (not os.path.isfile(lib.lib) or
Christopher Haster 37:bf73ffd98cca 357 lib.repo != Repo.fromrepo(lib.path).repo):
Christopher Haster 36:5f4546dde73b 358 with cd(lib.path):
Christopher Haster 36:5f4546dde73b 359 if lib.cwd.dirty():
Christopher Haster 37:bf73ffd98cca 360 sys.stderr.write('Uncommitted changes in %s (%s)\n'
Christopher Haster 36:5f4546dde73b 361 % (lib.name, lib.path))
Christopher Haster 36:5f4546dde73b 362 sys.exit(1)
Christopher Haster 20:84d6e18cbc20 363
Christopher Haster 36:5f4546dde73b 364 shutil.rmtree(lib.path)
Christopher Haster 42:58b35941ebd0 365 repo.scm.unignore(lib.path[len(repo.path)+1:])
Christopher Haster 36:5f4546dde73b 366
Christopher Haster 36:5f4546dde73b 367 repo.synch()
Christopher Haster 36:5f4546dde73b 368
Christopher Haster 36:5f4546dde73b 369 for lib in repo.libs:
Christopher Haster 42:58b35941ebd0 370 if not os.path.isdir(lib.path):
Christopher Haster 42:58b35941ebd0 371 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 372 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 42:58b35941ebd0 373 else:
Christopher Haster 36:5f4546dde73b 374 with cd(lib.path):
Christopher Haster 36:5f4546dde73b 375 update(lib.hash)
Christopher Haster 20:84d6e18cbc20 376
Christopher Haster 15:a6b1f4e65bf4 377 # Synch command
Christopher Haster 15:a6b1f4e65bf4 378 @subcommand('synch',
Christopher Haster 15:a6b1f4e65bf4 379 help='synchronize lib files')
Christopher Haster 15:a6b1f4e65bf4 380 def synch():
Christopher Haster 36:5f4546dde73b 381 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 382 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 383 lib.synch()
Christopher Haster 43:8a4a902a0654 384 lib.write()
Christopher Haster 15:a6b1f4e65bf4 385
Christopher Haster 15:a6b1f4e65bf4 386 # Compile command
Christopher Haster 15:a6b1f4e65bf4 387 @subcommand('compile', 'args*',
Christopher Haster 15:a6b1f4e65bf4 388 help='compile project using workspace_tools')
Christopher Haster 15:a6b1f4e65bf4 389 def compile(args):
Christopher Haster 15:a6b1f4e65bf4 390 if not os.path.isdir('mbed-os'):
Christopher Haster 37:bf73ffd98cca 391 sys.stderr.write('Warning! mbed-os not found?\n')
Christopher Haster 15:a6b1f4e65bf4 392 sys.exit(-1)
Christopher Haster 15:a6b1f4e65bf4 393
Christopher Haster 40:2446665dfdf8 394 repo = Repo.fromrepo()
Christopher Haster 40:2446665dfdf8 395
Christopher Haster 21:1a3f920b6f07 396 macros = []
Christopher Haster 15:a6b1f4e65bf4 397 if os.path.isfile('MACROS.txt'):
Christopher Haster 15:a6b1f4e65bf4 398 with open('MACROS.txt') as f:
Christopher Haster 15:a6b1f4e65bf4 399 macros = f.read().splitlines()
Christopher Haster 15:a6b1f4e65bf4 400
Christopher Haster 15:a6b1f4e65bf4 401 env = os.environ.copy()
Christopher Haster 15:a6b1f4e65bf4 402 env['PYTHONPATH'] = '.'
Christopher Haster 15:a6b1f4e65bf4 403 popen(['python', 'mbed-os/tools/make.py']
Christopher Haster 15:a6b1f4e65bf4 404 + list(chain.from_iterable(izip(repeat('-D'), macros)))
Christopher Haster 36:5f4546dde73b 405 + ['--source=%s' % repo.path,
Christopher Haster 36:5f4546dde73b 406 '--build=%s' % os.path.join(repo.path, '.build')]
Christopher Haster 15:a6b1f4e65bf4 407 + args,
Christopher Haster 15:a6b1f4e65bf4 408 env=env)
Christopher Haster 15:a6b1f4e65bf4 409
Christopher Haster 15:a6b1f4e65bf4 410 # Export command
Christopher Haster 15:a6b1f4e65bf4 411 @subcommand('export', 'args*',
Christopher Haster 15:a6b1f4e65bf4 412 help='generate project files')
Christopher Haster 15:a6b1f4e65bf4 413 def export(args):
Christopher Haster 15:a6b1f4e65bf4 414 if not os.path.isdir('mbed-os'):
Christopher Haster 37:bf73ffd98cca 415 sys.stderr.write('Warning! mbed-os not found?\n')
Christopher Haster 15:a6b1f4e65bf4 416 sys.exit(-1)
Christopher Haster 15:a6b1f4e65bf4 417
Christopher Haster 40:2446665dfdf8 418 repo = Repo.fromrepo()
Christopher Haster 40:2446665dfdf8 419
Christopher Haster 41:59e9d808ee05 420 macros = []
Christopher Haster 41:59e9d808ee05 421 if os.path.isfile('MACROS.txt'):
Christopher Haster 41:59e9d808ee05 422 with open('MACROS.txt') as f:
Christopher Haster 41:59e9d808ee05 423 macros = f.read().splitlines()
Christopher Haster 41:59e9d808ee05 424
Christopher Haster 18:1b4252106474 425 env = os.environ.copy()
Christopher Haster 18:1b4252106474 426 env['PYTHONPATH'] = '.'
Christopher Haster 15:a6b1f4e65bf4 427 popen(['python', 'mbed-os/tools/project.py',
Christopher Haster 36:5f4546dde73b 428 '--source=%s' % repo.path]
Christopher Haster 41:59e9d808ee05 429 + list(chain.from_iterable(izip(repeat('-D'), macros)))
Christopher Haster 18:1b4252106474 430 + args,
Christopher Haster 18:1b4252106474 431 env=env)
Christopher Haster 15:a6b1f4e65bf4 432
Christopher Haster 43:8a4a902a0654 433 ## List command
Christopher Haster 43:8a4a902a0654 434 #@subcommand('list',
Christopher Haster 43:8a4a902a0654 435 # help='list recursive libraries')
Christopher Haster 43:8a4a902a0654 436 #def
Christopher Haster 43:8a4a902a0654 437
Christopher Haster 15:a6b1f4e65bf4 438 # Parse/run command
Christopher Haster 15:a6b1f4e65bf4 439 args, remainder = parser.parse_known_args()
Christopher Haster 15:a6b1f4e65bf4 440 status = args.command(args)
Christopher Haster 15:a6b1f4e65bf4 441 sys.exit(status or 0)
Christopher Haster 15:a6b1f4e65bf4 442