@@ -754,7 +754,7 @@ class Repo(object):
754
754
rev = None
755
755
scm = None
756
756
libs = []
757
- cache = '/tmp/repo_cache'
757
+ cache = None
758
758
759
759
@classmethod
760
760
def fromurl (cls , url , path = None ):
@@ -781,6 +781,8 @@ def fromurl(cls, url, path=None):
781
781
repo .rev = m_repo_url .group (3 )
782
782
else :
783
783
error ('Invalid repository (%s)' % url .strip (), - 1 )
784
+ repo .cache = Global ().get_cfg ('CACHE' )
785
+
784
786
return repo
785
787
786
788
@classmethod
@@ -807,6 +809,7 @@ def fromrepo(cls, path=None):
807
809
808
810
repo .path = os .path .abspath (path )
809
811
repo .name = os .path .basename (repo .path )
812
+ repo .cache = Global ().get_cfg ('CACHE' )
810
813
811
814
repo .sync ()
812
815
@@ -830,7 +833,7 @@ def findparent(cls, path=None):
830
833
path = os .path .abspath (path or os .getcwd ())
831
834
832
835
while cd (path ):
833
- if os .path .isfile (os .path .join (path , Program . config_file )) or Repo .isrepo (path ):
836
+ if os .path .isfile (os .path .join (path , Cfg . file )) or Repo .isrepo (path ):
834
837
return path
835
838
836
839
tpath = path
@@ -1070,9 +1073,8 @@ def check_repo(self, show_warning=None):
1070
1073
return True
1071
1074
1072
1075
1073
- # Program object, used to indicate the root of the code base
1076
+ # Program class, acts code base root
1074
1077
class Program (object ):
1075
- config_file = ".mbed"
1076
1078
path = None
1077
1079
name = None
1078
1080
is_cwd = False
@@ -1085,7 +1087,7 @@ def __init__(self, path=None, print_warning=False):
1085
1087
1086
1088
while cd (path ):
1087
1089
tpath = path
1088
- if os .path .isfile (os .path .join (path , Program . config_file )):
1090
+ if os .path .isfile (os .path .join (path , Cfg . file )):
1089
1091
self .path = path
1090
1092
self .is_cwd = False
1091
1093
break
@@ -1102,45 +1104,17 @@ def __init__(self, path=None, print_warning=False):
1102
1104
"Could not find mbed program in current path \" %s\" .\n "
1103
1105
"You can fix this by calling \" mbed new .\" or \" mbed default root .\" in the root of your program." % self .path )
1104
1106
1105
- # Sets config value
1106
- def set_cfg (self , var , val ):
1107
- fl = os .path .join (self .path , self .config_file )
1108
- try :
1109
- with open (fl ) as f :
1110
- lines = f .read ().splitlines ()
1111
- except IOError :
1112
- lines = []
1113
-
1114
- for line in lines :
1115
- m = re .match (r'^([\w+-]+)\=(.*)?$' , line )
1116
- if m and m .group (1 ) == var :
1117
- lines .remove (line )
1118
-
1119
- lines += [var + "=" + val ]
1120
-
1121
- with open (fl , 'w' ) as f :
1122
- f .write ('\n ' .join (lines ) + '\n ' )
1123
-
1124
- # Gets config value
1125
- def get_cfg (self , var , default_val = None ):
1126
- fl = os .path .join (self .path , self .config_file )
1127
- try :
1128
- with open (fl ) as f :
1129
- lines = f .read ().splitlines ()
1130
- except IOError :
1131
- lines = []
1132
-
1133
- for line in lines :
1134
- m = re .match (r'^([\w+-]+)\=(.*)?$' , line )
1135
- if m and m .group (1 ) == var :
1136
- return m .group (2 )
1137
- return default_val
1107
+ def get_cfg (self , * args , ** kwargs ):
1108
+ return Cfg (self .path ).get (* args , ** kwargs ) or Global ().get_cfg (* args , ** kwargs )
1109
+
1110
+ def set_cfg (self , * args , ** kwargs ):
1111
+ return Cfg (self .path ).set (* args , ** kwargs )
1138
1112
1139
1113
def set_root (self ):
1140
1114
return self .set_cfg ('ROOT' , '.' )
1141
1115
1142
1116
def unset_root (self , path = None ):
1143
- fl = os .path .join (path or self .path , self . config_file )
1117
+ fl = os .path .join (path or self .path , Cfg . file )
1144
1118
if os .path .isfile (fl ):
1145
1119
os .remove (fl )
1146
1120
@@ -1257,6 +1231,63 @@ def get_macros(self):
1257
1231
return macros
1258
1232
1259
1233
1234
+ # Global class, used for global config
1235
+ class Global (object ):
1236
+ def __init__ (self ):
1237
+ self .path = os .path .join (os .path .expanduser ("~" ), '.mbed' )
1238
+ if not os .path .exists (self .path ):
1239
+ os .mkdir (self .path )
1240
+
1241
+ def get_cfg (self , * args , ** kwargs ):
1242
+ return Cfg (self .path ).get (* args , ** kwargs )
1243
+
1244
+ def set_cfg (self , * args , ** kwargs ):
1245
+ return Cfg (self .path ).set (* args , ** kwargs )
1246
+
1247
+
1248
+ class Cfg (object ):
1249
+ path = None
1250
+ file = ".mbed"
1251
+
1252
+ def __init__ (self , path ):
1253
+ self .path = path
1254
+
1255
+ # Sets config value
1256
+ def set (self , var , val ):
1257
+ fl = os .path .join (self .path , self .file )
1258
+ try :
1259
+ with open (fl ) as f :
1260
+ lines = f .read ().splitlines ()
1261
+ except IOError :
1262
+ lines = []
1263
+
1264
+ for line in lines :
1265
+ m = re .match (r'^([\w+-]+)\=(.*)?$' , line )
1266
+ if m and m .group (1 ) == var :
1267
+ lines .remove (line )
1268
+
1269
+ if not val is None :
1270
+ lines += [var + "=" + val ]
1271
+
1272
+ with open (fl , 'w' ) as f :
1273
+ f .write ('\n ' .join (lines ) + '\n ' )
1274
+
1275
+ # Gets config value
1276
+ def get (self , var , default_val = None ):
1277
+ fl = os .path .join (self .path , self .file )
1278
+ try :
1279
+ with open (fl ) as f :
1280
+ lines = f .read ().splitlines ()
1281
+ except IOError :
1282
+ lines = []
1283
+
1284
+ for line in lines :
1285
+ m = re .match (r'^([\w+-]+)\=(.*)?$' , line )
1286
+ if m and m .group (1 ) == var :
1287
+ return m .group (2 )
1288
+ return default_val
1289
+
1290
+
1260
1291
def formaturl (url , format = "default" ):
1261
1292
url = "%s" % url
1262
1293
m = re .match (regex_mbed_url , url )
@@ -1915,19 +1946,42 @@ def toolchain_(name=None):
1915
1946
@subcommand ('default' ,
1916
1947
dict (name = 'name' , help = 'Variable name. E.g. "target", "toolchain", "protocol"' ),
1917
1948
dict (name = 'value' , nargs = '?' , help = 'Value. Will show the currently set default value for a variable if not specified.' ),
1949
+ dict (name = ['-u' , '--unset' ], dest = 'unset' , action = 'store_true' , help = 'Unset the specified variable.' ),
1918
1950
help = 'Set or get program default options.' )
1919
- def default_ (name , value = None ):
1951
+ def default_ (name , value = None , unset = False ):
1920
1952
# Find the root of the program
1921
1953
program = Program (os .getcwd ())
1922
1954
# Change current dir to program root
1923
1955
with cd (program .path ):
1924
1956
var = str (name ).upper ()
1925
- if value is None :
1926
- value = program .get_cfg (var )
1927
- action (( '%s' % value ) if value else 'No default %s set in program "%s"' % (name , program .name ))
1928
- else :
1957
+ if unset :
1958
+ program .set_cfg (var , None )
1959
+ action ('Unset default %s in program "%s"' % (name , program .name ))
1960
+ elif value :
1929
1961
program .set_cfg (var , value )
1930
1962
action ('%s now set as default %s in program "%s"' % (value , name , program .name ))
1963
+ else :
1964
+ value = program .get_cfg (var )
1965
+ action (('%s' % value ) if value else 'No default %s set in program "%s"' % (name , program .name ))
1966
+
1967
+ # Generic config command
1968
+ @subcommand ('global' ,
1969
+ dict (name = 'name' , help = 'Variable name. E.g. "target", "toolchain", "protocol"' ),
1970
+ dict (name = 'value' , nargs = '?' , help = 'Value. Will show the currently set default value for a variable if not specified.' ),
1971
+ dict (name = ['-u' , '--unset' ], dest = 'unset' , action = 'store_true' , help = 'Unset the specified variable.' ),
1972
+ help = 'Set or get global options for all programs. Global options may be overridden by program defaults.' )
1973
+ def default_ (name , value = None , unset = False ):
1974
+ g = Global ()
1975
+ var = str (name ).upper ()
1976
+ if unset :
1977
+ g .set_cfg (var , None )
1978
+ action ('Unset global %s' % name )
1979
+ elif value :
1980
+ g .set_cfg (var , value )
1981
+ action ('%s now set as global %s' % (value , name ))
1982
+ else :
1983
+ value = g .get_cfg (var )
1984
+ action (('%s' % value ) if value else 'No global %s set' % (name ))
1931
1985
1932
1986
# 'config' command (calls into tools/get_config.py)
1933
1987
@subcommand ('config' ,
0 commit comments