Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Committer:
Christopher Haster
Date:
Wed Mar 30 17:18:04 2016 -0500
Revision:
49:977ea8d3e661
Parent:
48:6cb5440dd984
Child:
50:3770a8991c11
Child:
52:25da1dfebd7a
Fixed issue with dir missing in os.walk

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 49:977ea8d3e661 255 if file[:-4] in dirs:
Christopher Haster 49:977ea8d3e661 256 dirs.remove(file[:-4])
Christopher Haster 36:5f4546dde73b 257
Christopher Haster 37:bf73ffd98cca 258 def getrepo(self):
Christopher Haster 37:bf73ffd98cca 259 with open(self.lib) as f:
Christopher Haster 37:bf73ffd98cca 260 return Repo.fromurl(f.read()).repo
Christopher Haster 37:bf73ffd98cca 261
Christopher Haster 36:5f4546dde73b 262 def write(self):
Christopher Haster 38:0ca5eea23af9 263 if os.path.isfile(self.lib):
Christopher Haster 38:0ca5eea23af9 264 with open(self.lib) as f:
Christopher Haster 38:0ca5eea23af9 265 if f.read().strip() == self.url.strip():
Christopher Haster 44:5ff277e7f754 266 print self.name, 'unmodified'
Christopher Haster 38:0ca5eea23af9 267 return
Christopher Haster 36:5f4546dde73b 268
Christopher Haster 38:0ca5eea23af9 269 with open(self.lib, 'w') as f:
Christopher Haster 38:0ca5eea23af9 270 f.write(self.url + '\n')
Christopher Haster 36:5f4546dde73b 271
Christopher Haster 44:5ff277e7f754 272 print self.name, '->', self.url
Christopher Haster 44:5ff277e7f754 273
Christopher Haster 15:a6b1f4e65bf4 274 # Clone command
Christopher Haster 15:a6b1f4e65bf4 275 @subcommand('import', 'url', 'name?',
Christopher Haster 15:a6b1f4e65bf4 276 help='recursively import a repository')
Christopher Haster 36:5f4546dde73b 277 def import_(url, path=None):
Christopher Haster 36:5f4546dde73b 278 repo = Repo.fromurl(url, path)
Christopher Haster 15:a6b1f4e65bf4 279
Christopher Haster 35:ffcfa5ace437 280 for scm in scms.values():
Christopher Haster 15:a6b1f4e65bf4 281 try:
Christopher Haster 35:ffcfa5ace437 282 scm.clone(repo.repo, repo.path, repo.hash)
Christopher Haster 15:a6b1f4e65bf4 283 break
Christopher Haster 35:ffcfa5ace437 284 except ProcessException:
Christopher Haster 15:a6b1f4e65bf4 285 pass
Christopher Haster 15:a6b1f4e65bf4 286
Christopher Haster 36:5f4546dde73b 287 repo.synch()
Christopher Haster 15:a6b1f4e65bf4 288
Christopher Haster 15:a6b1f4e65bf4 289 with cd(repo.path):
Christopher Haster 36:5f4546dde73b 290 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 291 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 292 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 15:a6b1f4e65bf4 293
Christopher Haster 19:6ace1080b8bb 294 if (not os.path.isfile('mbed_settings.py') and
Christopher Haster 19:6ace1080b8bb 295 os.path.isfile('mbed-os/tools/settings.py')):
screamer 30:dcc9ff39aee3 296 shutil.copy('mbed-os/tools/default_settings.py', 'mbed_settings.py')
Christopher Haster 19:6ace1080b8bb 297
Christopher Haster 15:a6b1f4e65bf4 298 # Deploy command
Christopher Haster 15:a6b1f4e65bf4 299 @subcommand('deploy',
Christopher Haster 15:a6b1f4e65bf4 300 help='recursively import libraries in current directory')
Christopher Haster 15:a6b1f4e65bf4 301 def deploy():
Christopher Haster 36:5f4546dde73b 302 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 303 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 304 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 305 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 15:a6b1f4e65bf4 306
Christopher Haster 15:a6b1f4e65bf4 307 # Install/uninstall command
Christopher Haster 15:a6b1f4e65bf4 308 @subcommand('add', 'url', 'name?',
Christopher Haster 15:a6b1f4e65bf4 309 help='add a library to the current repository')
Christopher Haster 36:5f4546dde73b 310 def add(url, path=None):
Christopher Haster 36:5f4546dde73b 311 repo = Repo.fromrepo()
Christopher Haster 15:a6b1f4e65bf4 312
Christopher Haster 36:5f4546dde73b 313 lib = Repo.fromurl(url, path)
Christopher Haster 36:5f4546dde73b 314 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 315 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 36:5f4546dde73b 316 lib.synch()
Christopher Haster 15:a6b1f4e65bf4 317
Christopher Haster 36:5f4546dde73b 318 lib.write()
Christopher Haster 36:5f4546dde73b 319 repo.scm.add(lib.lib)
Christopher Haster 15:a6b1f4e65bf4 320
Christopher Haster 36:5f4546dde73b 321 @subcommand('remove', 'path',
Christopher Haster 15:a6b1f4e65bf4 322 help='remove a library from the current repository folder')
Christopher Haster 36:5f4546dde73b 323 def remove(path):
Christopher Haster 36:5f4546dde73b 324 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 325 lib = Repo.fromrepo(path)
Christopher Haster 15:a6b1f4e65bf4 326
Christopher Haster 36:5f4546dde73b 327 repo.scm.remove(lib.lib)
Christopher Haster 36:5f4546dde73b 328 shutil.rmtree(lib.path)
Christopher Haster 42:58b35941ebd0 329 repo.scm.unignore(lib.path[len(repo.path)+1:])
Christopher Haster 15:a6b1f4e65bf4 330
Christopher Haster 15:a6b1f4e65bf4 331 # Publish command
Christopher Haster 15:a6b1f4e65bf4 332 @subcommand('publish',
Christopher Haster 15:a6b1f4e65bf4 333 help='recursively publish changes to remote repositories')
Christopher Haster 18:1b4252106474 334 def publish(always=True):
Christopher Haster 36:5f4546dde73b 335 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 336 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 337 with cd(lib.path):
Christopher Haster 36:5f4546dde73b 338 publish(False)
Christopher Haster 36:5f4546dde73b 339 synch()
Christopher Haster 15:a6b1f4e65bf4 340
Christopher Haster 36:5f4546dde73b 341 dirty = repo.scm.dirty()
Christopher Haster 17:1e487c450f06 342
Christopher Haster 36:5f4546dde73b 343 if dirty:
Christopher Haster 36:5f4546dde73b 344 print 'Uncommitted changes in %s (%s)' % (repo.name, repo.path)
Christopher Haster 24:b29dad301edd 345 raw_input('Press enter to commit and push ')
Christopher Haster 36:5f4546dde73b 346 repo.scm.commit()
Christopher Haster 17:1e487c450f06 347
Christopher Haster 36:5f4546dde73b 348 if dirty or always:
Christopher Haster 18:1b4252106474 349 try:
Christopher Haster 36:5f4546dde73b 350 repo.scm.push()
Christopher Haster 18:1b4252106474 351 except ProcessException as e:
Christopher Haster 18:1b4252106474 352 sys.exit(e[0])
Christopher Haster 15:a6b1f4e65bf4 353
Christopher Haster 20:84d6e18cbc20 354 # Update command
Christopher Haster 20:84d6e18cbc20 355 @subcommand('update', 'ref?',
Christopher Haster 20:84d6e18cbc20 356 help='recursively updates libraries and current repository')
Christopher Haster 20:84d6e18cbc20 357 def update(ref=None):
Christopher Haster 36:5f4546dde73b 358 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 359 repo.scm.pull(ref)
Christopher Haster 36:5f4546dde73b 360
Christopher Haster 36:5f4546dde73b 361 for lib in repo.libs:
Christopher Haster 37:bf73ffd98cca 362 if (not os.path.isfile(lib.lib) or
Christopher Haster 37:bf73ffd98cca 363 lib.repo != Repo.fromrepo(lib.path).repo):
Christopher Haster 36:5f4546dde73b 364 with cd(lib.path):
Christopher Haster 36:5f4546dde73b 365 if lib.cwd.dirty():
Christopher Haster 37:bf73ffd98cca 366 sys.stderr.write('Uncommitted changes in %s (%s)\n'
Christopher Haster 36:5f4546dde73b 367 % (lib.name, lib.path))
Christopher Haster 36:5f4546dde73b 368 sys.exit(1)
Christopher Haster 20:84d6e18cbc20 369
Christopher Haster 36:5f4546dde73b 370 shutil.rmtree(lib.path)
Christopher Haster 42:58b35941ebd0 371 repo.scm.unignore(lib.path[len(repo.path)+1:])
Christopher Haster 36:5f4546dde73b 372
Christopher Haster 36:5f4546dde73b 373 repo.synch()
Christopher Haster 36:5f4546dde73b 374
Christopher Haster 36:5f4546dde73b 375 for lib in repo.libs:
Christopher Haster 45:5a91306f7924 376 if os.path.isdir(lib.path):
Christopher Haster 45:5a91306f7924 377 with cd(lib.path):
Christopher Haster 45:5a91306f7924 378 update(lib.hash)
Christopher Haster 45:5a91306f7924 379 else:
Christopher Haster 42:58b35941ebd0 380 import_(lib.url, lib.path)
Christopher Haster 42:58b35941ebd0 381 repo.scm.ignore(lib.path[len(repo.path)+1:])
Christopher Haster 20:84d6e18cbc20 382
Christopher Haster 15:a6b1f4e65bf4 383 # Synch command
Christopher Haster 15:a6b1f4e65bf4 384 @subcommand('synch',
Christopher Haster 15:a6b1f4e65bf4 385 help='synchronize lib files')
Christopher Haster 15:a6b1f4e65bf4 386 def synch():
Christopher Haster 36:5f4546dde73b 387 repo = Repo.fromrepo()
Christopher Haster 36:5f4546dde73b 388 for lib in repo.libs:
Christopher Haster 36:5f4546dde73b 389 lib.synch()
Christopher Haster 43:8a4a902a0654 390 lib.write()
Christopher Haster 15:a6b1f4e65bf4 391
Christopher Haster 15:a6b1f4e65bf4 392 # Compile command
Christopher Haster 15:a6b1f4e65bf4 393 @subcommand('compile', 'args*',
Christopher Haster 15:a6b1f4e65bf4 394 help='compile project using workspace_tools')
Christopher Haster 15:a6b1f4e65bf4 395 def compile(args):
Christopher Haster 15:a6b1f4e65bf4 396 if not os.path.isdir('mbed-os'):
Christopher Haster 37:bf73ffd98cca 397 sys.stderr.write('Warning! mbed-os not found?\n')
Christopher Haster 15:a6b1f4e65bf4 398 sys.exit(-1)
Christopher Haster 15:a6b1f4e65bf4 399
Christopher Haster 40:2446665dfdf8 400 repo = Repo.fromrepo()
Christopher Haster 40:2446665dfdf8 401
Christopher Haster 21:1a3f920b6f07 402 macros = []
Christopher Haster 15:a6b1f4e65bf4 403 if os.path.isfile('MACROS.txt'):
Christopher Haster 15:a6b1f4e65bf4 404 with open('MACROS.txt') as f:
Christopher Haster 15:a6b1f4e65bf4 405 macros = f.read().splitlines()
Christopher Haster 15:a6b1f4e65bf4 406
Christopher Haster 15:a6b1f4e65bf4 407 env = os.environ.copy()
Christopher Haster 15:a6b1f4e65bf4 408 env['PYTHONPATH'] = '.'
Christopher Haster 15:a6b1f4e65bf4 409 popen(['python', 'mbed-os/tools/make.py']
Christopher Haster 15:a6b1f4e65bf4 410 + list(chain.from_iterable(izip(repeat('-D'), macros)))
Christopher Haster 36:5f4546dde73b 411 + ['--source=%s' % repo.path,
Christopher Haster 36:5f4546dde73b 412 '--build=%s' % os.path.join(repo.path, '.build')]
Christopher Haster 15:a6b1f4e65bf4 413 + args,
Christopher Haster 15:a6b1f4e65bf4 414 env=env)
Christopher Haster 15:a6b1f4e65bf4 415
Christopher Haster 15:a6b1f4e65bf4 416 # Export command
Christopher Haster 15:a6b1f4e65bf4 417 @subcommand('export', 'args*',
Christopher Haster 15:a6b1f4e65bf4 418 help='generate project files')
Christopher Haster 15:a6b1f4e65bf4 419 def export(args):
Christopher Haster 15:a6b1f4e65bf4 420 if not os.path.isdir('mbed-os'):
Christopher Haster 37:bf73ffd98cca 421 sys.stderr.write('Warning! mbed-os not found?\n')
Christopher Haster 15:a6b1f4e65bf4 422 sys.exit(-1)
Christopher Haster 15:a6b1f4e65bf4 423
Christopher Haster 40:2446665dfdf8 424 repo = Repo.fromrepo()
Christopher Haster 40:2446665dfdf8 425
Christopher Haster 41:59e9d808ee05 426 macros = []
Christopher Haster 41:59e9d808ee05 427 if os.path.isfile('MACROS.txt'):
Christopher Haster 41:59e9d808ee05 428 with open('MACROS.txt') as f:
Christopher Haster 41:59e9d808ee05 429 macros = f.read().splitlines()
Christopher Haster 41:59e9d808ee05 430
Christopher Haster 18:1b4252106474 431 env = os.environ.copy()
Christopher Haster 18:1b4252106474 432 env['PYTHONPATH'] = '.'
Christopher Haster 15:a6b1f4e65bf4 433 popen(['python', 'mbed-os/tools/project.py',
Christopher Haster 36:5f4546dde73b 434 '--source=%s' % repo.path]
Christopher Haster 41:59e9d808ee05 435 + list(chain.from_iterable(izip(repeat('-D'), macros)))
Christopher Haster 18:1b4252106474 436 + args,
Christopher Haster 18:1b4252106474 437 env=env)
Christopher Haster 15:a6b1f4e65bf4 438
Christopher Haster 15:a6b1f4e65bf4 439 # Parse/run command
Christopher Haster 15:a6b1f4e65bf4 440 args, remainder = parser.parse_known_args()
Christopher Haster 15:a6b1f4e65bf4 441 status = args.command(args)
Christopher Haster 15:a6b1f4e65bf4 442 sys.exit(status or 0)
Christopher Haster 15:a6b1f4e65bf4 443