mbed-scli test program

Dependencies:   Scheduler

Revision:
7:9df443d2570b
Parent:
6:7915b5819364
Child:
8:f1c7a11d0670
diff -r 7915b5819364 -r 9df443d2570b mlib.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mlib.py	Sat Mar 04 16:08:32 2017 +0900
@@ -0,0 +1,128 @@
+####  lib.py for mbed ####
+# v0.8 # 2017/03 made by dinau
+#
+# Notice: Unsupported libraries.
+#         These libraries must be specified in Makefile.
+#   mbed-os
+#   mbed-dev
+
+
+# Usage:
+#   $ make lib
+# or
+#   $ python makelib.py
+#
+
+import os,sys
+import commands
+from urlparse import urlparse
+
+HG              = 1
+GIT             = 2
+MBEDOS          = 3
+CMD_HG_CLONE    = 'hg clone -q '
+CMD_GIT_CLONE   = 'git clone -q '
+CMD_GIT_CHECKOUT= 'git checkout '
+
+def hgClone(param):
+    cmd = CMD_HG_CLONE + param
+    return os.system( cmd )
+
+def gitClone(param):
+    cmd = CMD_GIT_CLONE + param
+    return os.system( cmd )
+
+def gitCheckout(param,disp=False):
+    cmd = CMD_GIT_CHECKOUT + param
+    if disp:
+        print cmd
+        sys.stdout.flush()
+    return os.system( cmd )
+
+def getDvcs(url):
+    p = urlparse( url )
+    #print p
+    if ('developer.mbed.org' in url) or ('mbed.org' in url) or ('bitbucke.org' in url):
+        return HG;
+    elif 'mbed-os' in url:
+        return MBEDOS
+    elif 'github.com' in url:
+        return GIT
+    else:
+        return 0
+
+def getLibs(target_dir):
+    savedir = os.getcwd()
+    os.chdir(target_dir)
+    liblist=[]
+    for fname in os.listdir('.'):
+        if os.path.isfile(fname):
+            name, ext = os.path.splitext(fname)
+            if ext == '.lib':
+                if name != 'mbed-os':
+                    liblist.append(fname)
+    if len( liblist )  > 0:
+        #print liblist
+        for libfile in liblist:
+            subdir, ext = os.path.splitext(libfile)
+            if os.path.isdir( subdir ):
+                print "%s :: already exists !" % (subdir)
+                continue
+            print subdir
+            url = open(libfile).read()
+            print url
+            sys.stdout.flush()
+            dvcs = getDvcs(url)
+            if dvcs == HG:
+                hgClone( url )
+                getLibs(subdir)
+            elif dvcs == MBEDOS:
+                # Nothing is downloaded if mbed-os lib be.
+                print 'mbed-os'
+                sys.stdout.flush()
+            elif dvcs == GIT:
+                params = url.split('#')
+                if len(params) == 1:
+                    gitClone( params[0] )
+                    getLibs(subdir)
+                elif len(params) == 2:
+                    gitClone( params[0] )
+                    os.chdir(subdir)
+                    print 'cd %s' % (subdir)
+                    sys.stdout.flush()
+                    gitCheckout( params[1] )
+                    getLibs(subdir)
+    os.chdir(savedir)
+
+###################
+# main program
+###################
+if len(sys.argv) == 1:
+    print '--- Getting libraries ----'
+    getLibs('.')
+    print '--- end ---'
+else:
+    url = param1 = sys.argv[1]
+    try:
+        revNum = int(param1)
+    except:
+        p = urlparse( url )
+        path = p.path
+        if path[-1:] == '/':
+            path = path[:-1]
+        libdir  = os.path.basename(path)
+        libname = libdir + '.lib'
+        if not os.path.isdir( libdir ):
+            with open( libname,'w') as fp:
+                fp.write( url )
+            print '--- Getting libraries ----'
+            getLibs('.')
+            print '--- end ---'
+    else:
+        mbed_root_file ='mbed-root.txt'
+        mbedRoot = open(mbed_root_file).read()
+        os.chdir( mbedRoot.strip() )
+        gitCheckout( 'mbed_lib_rev' + str( revNum ), True )
+
+
+