mi mi / mbed-scli-test

Dependencies:   Scheduler

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mlib.py Source File

mlib.py

00001 ####  mlib.py for mbed ####
00002 # v0.9 # 2017/03 made by dinau
00003 #
00004 # Notice: Unsupported libraries.
00005 #         These libraries must be specified in Makefile.
00006 #   mbed-os
00007 #   mbed-dev
00008 
00009 
00010 # Usage:
00011 # Download the dependency libraries from web.
00012 #   $ make lib
00013 # or
00014 #   $ python mlib.py
00015 # Specify URL of a particular library to get it.
00016 #   $ python mlib.py http://...
00017 # Checkout mbed library with specified revision number.
00018 #   $ python mlib.py 136
00019 #
00020 
00021 import os,sys
00022 import commands
00023 from urlparse import urlparse
00024 
00025 HG              = 1
00026 GIT             = 2
00027 MBEDOS          = 3
00028 CMD_HG_CLONE    = 'hg clone -q '
00029 CMD_GIT_CLONE   = 'git clone -q '
00030 CMD_GIT_CHECKOUT= 'git checkout '
00031 
00032 def hgClone(param):
00033     cmd = CMD_HG_CLONE + param
00034     return os.system( cmd )
00035 
00036 def gitClone(param):
00037     cmd = CMD_GIT_CLONE + param
00038     return os.system( cmd )
00039 
00040 def gitCheckout(param,disp=False):
00041     cmd = CMD_GIT_CHECKOUT + param
00042     if disp:
00043         print cmd
00044         sys.stdout.flush()
00045     return os.system( cmd )
00046 
00047 def getDvcs(url):
00048     p = urlparse( url )
00049     #print p
00050     if ('developer.mbed.org' in url) or ('mbed.org' in url) or ('bitbucke.org' in url):
00051         return HG;
00052     elif 'mbed-os' in url:
00053         return MBEDOS
00054     elif 'github.com' in url:
00055         return GIT
00056     else:
00057         return 0
00058 
00059 def getLibs(target_dir):
00060     savedir = os.getcwd()
00061     os.chdir(target_dir)
00062     liblist=[]
00063     for fname in os.listdir('.'):
00064         if os.path.isfile(fname):
00065             name, ext = os.path.splitext(fname)
00066             if ext == '.lib':
00067                 if name != 'mbed-os':
00068                     liblist.append(fname)
00069     if len( liblist )  > 0:
00070         #print liblist
00071         for libfile in liblist:
00072             subdir, ext = os.path.splitext(libfile)
00073             if os.path.isdir( subdir ):
00074                 print "%s :: already exists !" % (subdir)
00075                 continue
00076             print subdir
00077             url = open(libfile).read()
00078             print url
00079             sys.stdout.flush()
00080             dvcs = getDvcs(url)
00081             if dvcs == HG:
00082                 hgClone( url )
00083                 getLibs(subdir)
00084             elif dvcs == MBEDOS:
00085                 # Nothing is downloaded if mbed-os lib be.
00086                 print 'mbed-os'
00087                 sys.stdout.flush()
00088             elif dvcs == GIT:
00089                 params = url.split('#')
00090                 if len(params) == 1:
00091                     gitClone( params[0] )
00092                     getLibs(subdir)
00093                 elif len(params) == 2:
00094                     gitClone( params[0] )
00095                     os.chdir(subdir)
00096                     print 'cd %s' % (subdir)
00097                     sys.stdout.flush()
00098                     gitCheckout( params[1] )
00099                     getLibs(subdir)
00100     os.chdir(savedir)
00101 
00102 ###################
00103 # main program
00104 ###################
00105 if len(sys.argv) == 1:
00106     print '--- Getting libraries ----'
00107     getLibs('.')
00108     print '--- end ---'
00109 else:
00110     url = param1 = sys.argv[1]
00111     try:
00112         # Number or URL
00113         revNum = int(param1)
00114     except:
00115         # if URL of string
00116         # Down load mbed library from URL
00117         p = urlparse( url )
00118         if not("http" in p.scheme ) and not("https" in p.scheme ):
00119             print "URL error !"
00120         else:
00121             path = p.path
00122             if path[-1:] == '/':
00123                 path = path[:-1]
00124             libdir  = os.path.basename(path)
00125             libname = libdir + '.lib'
00126             if not os.path.isdir( libdir ):
00127                 with open( libname,'w') as fp:
00128                     fp.write( url )
00129                 print '--- Getting libraries ----'
00130                 getLibs('.')
00131                 print '--- end ---'
00132     else:
00133         # if Number of string
00134         # get mbed library of specified revsion
00135         mbed_root_file ='mbed-root.txt'
00136         try:
00137             mbedRoot = open(mbed_root_file).read()
00138             os.chdir( mbedRoot.strip() )
00139             gitCheckout( 'mbed_lib_rev' + str( revNum ), True )
00140         except:
00141             print  "First, at least execute 'make' command !"
00142 
00143 
00144 
00145