@@ -995,10 +995,9 @@ def fromurl(cls, url, path=None):
995
995
else :
996
996
error ('Invalid repository (%s)' % url .strip (), - 1 )
997
997
998
- cache_cfg = Global ().get_cfg ('CACHE' , '' )
999
- if cache_repositories and cache_cfg and cache_cfg != 'none' and cache_cfg != 'off' and cache_cfg != 'disabled' :
1000
- loc = cache_cfg if (cache_cfg and cache_cfg != 'on' and cache_cfg != 'enabled' ) else None
1001
- repo .cache = loc or os .path .join (tempfile .gettempdir (), 'mbed-repo-cache' )
998
+ cache_cfg = Global ().cache_cfg ()
999
+ if cache_repositories and cache_cfg ['cache' ] == 'enabled' :
1000
+ repo .cache = cache_cfg ['cache_dir' ]
1002
1001
1003
1002
return repo
1004
1003
@@ -1030,10 +1029,9 @@ def fromrepo(cls, path=None):
1030
1029
repo .path = os .path .abspath (path )
1031
1030
repo .name = os .path .basename (repo .path )
1032
1031
1033
- cache_cfg = Global ().get_cfg ('CACHE' , '' )
1034
- if cache_repositories and cache_cfg and cache_cfg != 'none' and cache_cfg != 'off' and cache_cfg != 'disabled' :
1035
- loc = cache_cfg if (cache_cfg and cache_cfg != 'on' and cache_cfg != 'enabled' ) else None
1036
- repo .cache = loc or os .path .join (tempfile .gettempdir (), 'mbed-repo-cache' )
1032
+ cache_cfg = Global ().cache_cfg ()
1033
+ if cache_repositories and cache_cfg ['cache' ] == 'enabled' :
1034
+ repo .cache = cache_cfg ['cache_dir' ]
1037
1035
1038
1036
repo .sync ()
1039
1037
@@ -1636,6 +1634,10 @@ def set_cfg(self, *args, **kwargs):
1636
1634
def list_cfg (self , * args , ** kwargs ):
1637
1635
return Cfg (self .path ).list (* args , ** kwargs )
1638
1636
1637
+ def cache_cfg (self , * args , ** kwargs ):
1638
+ return Cfg (self .path ).cache (* args , ** kwargs )
1639
+
1640
+
1639
1641
# Cfg classed used for handling the config backend
1640
1642
class Cfg (object ):
1641
1643
path = None
@@ -1701,6 +1703,16 @@ def list(self):
1701
1703
vars [m .group (1 )] = m .group (2 )
1702
1704
return vars
1703
1705
1706
+ # Get cache configuration
1707
+ def cache (self ):
1708
+ cache_cfg = self .get ('CACHE' , None )
1709
+ cache_val = 'enabled' if cache_repositories and cache_cfg and cache_cfg != 'none' and cache_cfg != 'off' and cache_cfg != 'disabled' else 'disabled'
1710
+
1711
+ cache_dir_cfg = self .get ('CACHE_DIR' , None )
1712
+ loc = cache_dir_cfg if cache_dir_cfg != 'default' else (cache_cfg if (cache_cfg and cache_cfg != 'on' and cache_cfg != 'off' and cache_cfg != 'none' and cache_cfg != 'enabled' and cache_cfg != 'disabled' ) else None )
1713
+ cache_base = loc or tempfile .gettempdir ()
1714
+ return {'cache' : cache_val , 'cache_base' : cache_base , 'cache_dir' : os .path .join (cache_base , 'mbed-cache' )}
1715
+
1704
1716
1705
1717
def formaturl (url , format = "default" ):
1706
1718
url = "%s" % url
@@ -2746,11 +2758,12 @@ def target_(name=None, global_cfg=False, supported=False):
2746
2758
return compile_ (supported = supported )
2747
2759
return config_ ('target' , name , global_cfg = global_cfg )
2748
2760
2761
+
2749
2762
@subcommand ('toolchain' ,
2750
2763
dict (name = 'name' , nargs = '?' , help = 'Default toolchain name. Example: ARM, GCC_ARM, IAR' ),
2751
2764
dict (name = ['-G' , '--global' ], dest = 'global_cfg' , action = 'store_true' , help = 'Use global settings, not local' ),
2752
2765
dict (name = ['-S' , '--supported' ], dest = 'supported' , action = 'store_true' , help = 'Shows supported matrix of targets and toolchains' ),
2753
- help = 'Set or get default toolchain\n \n ' ,
2766
+ help = 'Set or get default toolchain' ,
2754
2767
description = (
2755
2768
"Set or get default toolchain\n "
2756
2769
"This is an alias to 'mbed config [--global] toolchain [name]'\n " ))
@@ -2759,6 +2772,54 @@ def toolchain_(name=None, global_cfg=False, supported=False):
2759
2772
return compile_ (supported = supported )
2760
2773
return config_ ('toolchain' , name , global_cfg = global_cfg )
2761
2774
2775
+
2776
+ @subcommand ('cache' ,
2777
+ dict (name = 'on' , nargs = '?' , help = 'Turn repository caching on. Will use either a default cache location or the specified cache directory to store repositories.' ),
2778
+ dict (name = 'off' , nargs = '?' , help = 'Turn repository caching off. Note that this doesn\' t purge cached repositories. See "purge".' ),
2779
+ dict (name = 'ls' , nargs = '?' , help = 'List cached repositories' ),
2780
+ dict (name = 'purge' , nargs = '?' , help = 'Purge cached repositories. Note that this doesn\' t turn caching off' ),
2781
+ dict (name = ['-D' , '--dir' ], dest = 'cache_dir' , help = 'Set cache directory. Set to "default" to let mbed CLI determine the cache directory location.' ),
2782
+ help = 'Repository cache management\n \n ' ,
2783
+ description = (
2784
+ "Set or get default toolchain\n " ))
2785
+ def cache_ (on = False , off = False , ls = False , purge = False , cache_dir = None , global_cfg = False ):
2786
+ cmd = str (on ).lower ()
2787
+ argument = off
2788
+ g = Global ()
2789
+
2790
+ if cache_dir :
2791
+ if not os .path .exists (cache_dir ):
2792
+ try :
2793
+ os .makedirs (cache_dir )
2794
+ except (IOError , ImportError , OSError ):
2795
+ error ("Unable to create cache directory \" %s\" " % cache_dir , 128 )
2796
+ elif not os .path .isdir (cache_dir ):
2797
+ error ("The specified location \" %s\" is not a directory" % cache_dir , 128 )
2798
+ elif len (os .listdir (cache_dir )) > 1 :
2799
+ warning ("Directory \" %s\" is not empty." % d_path )
2800
+
2801
+ g .set_cfg ('CACHE_DIR' , cache_dir )
2802
+ action ('Repository cache location set to \" %s\" ' % cache_dir )
2803
+
2804
+ if cmd == 'off' or cmd == 'on' :
2805
+ g .set_cfg ('CACHE' , 'enabled' if cmd == 'on' else 'disabled' );
2806
+ cfg = g .cache_cfg ()
2807
+ action ('Repository cache is now %s.' % str (cfg ['cache' ]).upper ())
2808
+ action ('Cache location \" %s\" ' % cfg ['cache_dir' ])
2809
+ elif cmd == 'ls' :
2810
+ print 'list'
2811
+ elif cmd == 'purge' :
2812
+ cfg = g .cache_cfg ()
2813
+ action ('Purging cached repositories in \" %s\" ' % cfg ['cache_base' ])
2814
+ if os .path .isdir (path ):
2815
+ rmtree_readonly (cfg ['cache_dir' ])
2816
+ action ('Complete' )
2817
+ else :
2818
+ cfg = g .cache_cfg ()
2819
+ action ('Repository cache is %s.' % str (cfg ['cache' ]).upper ())
2820
+ action ('Cache location \" %s\" ' % cfg ['cache_base' ])
2821
+
2822
+
2762
2823
@subcommand ('help' ,
2763
2824
help = 'This help screen' )
2764
2825
def help_ ():
0 commit comments