Morpheus / Mbed OS mbed-Client-Morpheus-hg

Dependencies:   mbed-os

Revision:
35:ffcfa5ace437
Parent:
34:8d47baea4754
Child:
36:5f4546dde73b
diff -r 8d47baea4754 -r ffcfa5ace437 neo.py
--- a/neo.py	Wed Mar 30 08:23:05 2016 -0500
+++ b/neo.py	Wed Mar 30 10:51:38 2016 -0500
@@ -6,8 +6,8 @@
 import subprocess
 import os
 import contextlib
-import collections
 import shutil
+from collections import *
 from itertools import *
 
 # Subparser handling
@@ -90,6 +90,60 @@
 
 
 # Handling for multiple version controls
+scms = OrderedDict()
+def scm(name):
+    def scm(cls):
+        scms[name] = cls()
+        return cls
+    return scm
+
+def staticclass(cls):
+    for k, v in cls.__dict__.items():
+        if not k.startswith('__'):
+            setattr(cls, k, staticmethod(v))
+
+    return cls
+
+@scm('hg')
+@staticclass
+class Hg(object):
+    def clone(url, name=None, hash=None):
+        popen(['hg', 'clone', url, name] + (['-u', hash] if hash else []))
+
+    def add(file): popen(['hg', 'add', file])
+    def remove(file):
+        popen(['hg', 'rm', '-f', file])
+        try:
+            os.remove(file)
+        except OSError:
+            pass
+
+    def commit(): popen(['hg', 'commit'])
+    def push(): popen(['hg', 'push'])
+    def pull(hash=None):
+        popen(['hg', 'pull'])
+        popen(['hg', 'update'] + (['-r', hash] if hash else []))
+
+    def hash(): return pquery(['hg', 'id', '-i']).strip().strip('+')
+    def modified(): return pquery(['hg', 'status', '-q'])
+
+@scm('git')
+@staticclass
+class Git(object):
+    def clone(url, name=None, hash=None):
+        popen(['git', 'clone', url, name])
+        if hash:
+            popen(['git', 'checkout', hash])
+
+    def add(file): popen(['git', 'add', file])
+    def remove(file): popen(['git', 'rm', '-f', file])
+    def commit(): popen(['git', 'commit', '-a'])
+
+    def hash(): return pquery(['git', 'rev-parse', '--short', 'HEAD']).strip()
+    def modified(): return pquery(['git', 'diff', '--name-only', 'HEAD'])
+    
+
+# Repository object
 class Repo(object):
     def __init__(self, path=None):
         self.path = path or os.getcwd()
@@ -124,6 +178,7 @@
     def update(self):
         if os.path.isdir(self.path):
             self.type = self.gettype()
+            self.scm  = self.getscm()
             self.hash = self.gethash()
 
         if os.path.isfile(self.lib):
@@ -136,12 +191,12 @@
             if os.path.isdir(os.path.join(self.path, dir)):
                 return type
 
+    def getscm(self):
+        return scms[self.type]
+
     def gethash(self):
         with cd(self.path):
-            if self.type == 'git':
-                return pquery(['git', 'rev-parse', '--short', 'HEAD']).strip()
-            elif self.type == 'hg':
-                return pquery(['hg', 'id', '-i']).strip().strip('+')
+            return self.scm.hash()
 
 # Clone command
 @subcommand('import', 'url', 'name?',
@@ -149,19 +204,13 @@
 def import_(url, name=None):
     repo = Repo.fromurl(url, name)
 
-    for type in ['hg', 'git']:
+    for scm in scms.values():
         try:
-            popen([type, 'clone', repo.repo, repo.path])
+            scm.clone(repo.repo, repo.path, repo.hash)
             break
-        except:
+        except ProcessException:
             pass
 
-    repo.type = repo.gettype()
-
-    if repo.hash:
-        with cd(repo.path):
-            popen([repo.type, 'checkout', repo.hash])
-
     repo.update()
 
     with cd(repo.path):
@@ -191,7 +240,7 @@
     repo.update()
     savelib(repo)
 
-    popen([cwd.type, 'add', repo.lib])
+    repo.scm.add(repo.lib)
 
 @subcommand('remove', 'library',
     help='remove a library from the current repository folder')
@@ -199,15 +248,8 @@
     cwd = Repo()
     repo = Repo(library)
 
-    popen([cwd.type, 'rm', '-f', repo.lib])
-
-    try:
-        if cwd.type == 'hg':
-            os.remove(repo.lib)
-
-        shutil.rmtree(repo.path)
-    except OSError:
-        pass
+    cwd.scm.remove(repo.lib)
+    shutil.rmtree(repo.path)
 
 # Publish command
 @subcommand('publish',
@@ -221,22 +263,18 @@
                 publish(False)
 
     synch()
-
-    if cwd.type == 'hg':
-        modified = pquery(['hg', 'status', '-q'])
-    elif cwd.type == 'git':
-        modified = pquery(['git', 'diff', '--name-only', 'HEAD'])
+    modified = cwd.scm.modified()
 
     if modified:
         print cwd.path
         print 'Uncommitted changes in %s' % cwd.name
         raw_input('Press enter to commit and push ')
 
-        popen([cwd.type, 'commit'] + (['-a'] if cwd.type == 'git' else []))
+        cwd.scm.commit()
 
     if modified or always:
         try:
-            popen([cwd.type, 'push'] + (['-a'] if cwd.type == 'git' else []))
+            cwd.scm.push()
         except ProcessException as e:
             sys.exit(e[0])
 
@@ -245,13 +283,7 @@
     help='recursively updates libraries and current repository')
 def update(ref=None):
     cwd = Repo()
-
-    popen([cwd.type, 'pull'])
-    if cwd.type == 'hg':
-        popen(['hg', 'update'])
-
-    if ref:
-        popen([cwd.type, 'checkout', ref])
+    cwd.scm.pull(ref)
 
     for url, lib in iterlibs():
         repo = Repo.fromurl(url, lib)
@@ -275,9 +307,6 @@
 
         savelib(repo)
 
-        if cwd.type == 'git':
-            popen([cwd.type, 'add', repo.lib])
-
 # Compile command
 @subcommand('compile', 'args*',
     help='compile project using workspace_tools')