Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
screamer
Date:
Fri Apr 01 00:45:54 2016 +0100
Revision:
48:6cb5440dd984
Parent:
47:386f8c519142
Child:
49:977ea8d3e661
Update default git path

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