127
127
128
128
# stores current working directory for recursive operations
129
129
cwd_root = ""
130
-
130
+ _cwd = os . getcwd ()
131
131
132
132
# Logging and output
133
133
def log (msg ):
@@ -175,7 +175,7 @@ class ProcessException(Exception):
175
175
176
176
def popen (command , stdin = None , ** kwargs ):
177
177
# print for debugging
178
- info ('Exec "' + ' ' .join (command )+ '" in ' + os . getcwd ())
178
+ info ('Exec "' + ' ' .join (command )+ '" in ' + getcwd ())
179
179
try :
180
180
proc = subprocess .Popen (command , ** kwargs )
181
181
except OSError as e :
@@ -187,11 +187,11 @@ def popen(command, stdin=None, **kwargs):
187
187
raise e
188
188
189
189
if proc .wait () != 0 :
190
- raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), os . getcwd ())
190
+ raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), getcwd ())
191
191
192
192
def pquery (command , stdin = None , ** kwargs ):
193
193
if very_verbose :
194
- info ('Query "' + ' ' .join (command )+ '" in ' + os . getcwd ())
194
+ info ('Query "' + ' ' .join (command )+ '" in ' + getcwd ())
195
195
try :
196
196
proc = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , ** kwargs )
197
197
except OSError as e :
@@ -208,7 +208,7 @@ def pquery(command, stdin=None, **kwargs):
208
208
log (str (stdout ).strip ()+ "\n " )
209
209
210
210
if proc .returncode != 0 :
211
- raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), os . getcwd ())
211
+ raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), getcwd ())
212
212
213
213
return stdout
214
214
@@ -225,12 +225,19 @@ def remove_readonly(func, path, _):
225
225
# Directory navigation
226
226
@contextlib .contextmanager
227
227
def cd (newdir ):
228
- prevdir = os .getcwd ()
228
+ global _cwd
229
+ prevdir = getcwd ()
229
230
os .chdir (newdir )
231
+ _cwd = newdir
230
232
try :
231
233
yield
232
234
finally :
233
235
os .chdir (prevdir )
236
+ _cwd = prevdir
237
+
238
+ def getcwd ():
239
+ global _cwd
240
+ return _cwd
234
241
235
242
def relpath (root , path ):
236
243
return path [len (root )+ 1 :]
@@ -309,12 +316,12 @@ def unpack_rev(rev):
309
316
rev_file = os .path .join ('.' + Bld .name , '.rev-' + rev + '.zip' )
310
317
try :
311
318
with zipfile .ZipFile (rev_file ) as zf :
312
- action ("Unpacking library build \" %s\" in \" %s\" " % (rev , os . getcwd ()))
319
+ action ("Unpacking library build \" %s\" in \" %s\" " % (rev , getcwd ()))
313
320
zf .extractall ('.' )
314
321
except :
315
322
if os .path .isfile (rev_file ):
316
323
os .remove (rev_file )
317
- raise Exception (128 , "An error occurred while unpacking library archive \" %s\" in \" %s\" " % (rev_file , os . getcwd ()))
324
+ raise Exception (128 , "An error occurred while unpacking library archive \" %s\" in \" %s\" " % (rev_file , getcwd ()))
318
325
319
326
def checkout (rev , clean = False ):
320
327
url = Bld .geturl ()
@@ -331,7 +338,7 @@ def checkout(rev, clean=False):
331
338
if rev != Bld .getrev () or clean :
332
339
Bld .cleanup ()
333
340
334
- info ("Checkout \" %s\" in %s" % (rev , os .path .basename (os . getcwd ())))
341
+ info ("Checkout \" %s\" in %s" % (rev , os .path .basename (getcwd ())))
335
342
try :
336
343
Bld .unpack_rev (rev )
337
344
Bld .seturl (url + '/' + rev )
@@ -345,7 +352,7 @@ def untracked():
345
352
return ""
346
353
347
354
def seturl (url ):
348
- info ("Setting url to \" %s\" in %s" % (url , os . getcwd ()))
355
+ info ("Setting url to \" %s\" in %s" % (url , getcwd ()))
349
356
if not os .path .exists ('.' + Bld .name ):
350
357
os .mkdir ('.' + Bld .name )
351
358
@@ -417,15 +424,15 @@ def publish(all_refs=None):
417
424
popen ([hg_cmd , 'push' ] + (['--new-branch' ] if all_refs else []) + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
418
425
419
426
def fetch ():
420
- info ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (os . getcwd ()))
427
+ info ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (getcwd ()))
421
428
popen ([hg_cmd , 'pull' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
422
429
423
430
def discard ():
424
- info ("Discarding local changes in \" %s\" " % os .path .basename (os . getcwd ()))
431
+ info ("Discarding local changes in \" %s\" " % os .path .basename (getcwd ()))
425
432
popen ([hg_cmd , 'update' , '-C' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
426
433
427
434
def checkout (rev , clean = False , clean_files = False ):
428
- info ("Checkout \" %s\" in %s" % (rev if rev else "latest" , os .path .basename (os . getcwd ())))
435
+ info ("Checkout \" %s\" in %s" % (rev if rev else "latest" , os .path .basename (getcwd ())))
429
436
if clean_files :
430
437
files = pquery ([hg_cmd , 'status' , '--no-status' , '-ui' ]).splitlines ()
431
438
for f in files :
@@ -457,7 +464,7 @@ def outgoing():
457
464
return 0
458
465
459
466
def seturl (url ):
460
- info ("Setting url to \" %s\" in %s" % (url , os . getcwd ()))
467
+ info ("Setting url to \" %s\" in %s" % (url , getcwd ()))
461
468
hgrc = os .path .join ('.hg' , 'hgrc' )
462
469
tagpaths = '[paths]'
463
470
remote = 'default'
@@ -540,7 +547,7 @@ def ignores():
540
547
with open (Hg .ignore_file , 'w' ) as f :
541
548
f .write ("syntax: glob\n " + '\n ' .join (ignores )+ '\n ' )
542
549
except IOError :
543
- error ("Unable to write ignore file in \" %s\" " % os .path .join (os . getcwd (), Hg .ignore_file ), 1 )
550
+ error ("Unable to write ignore file in \" %s\" " % os .path .join (getcwd (), Hg .ignore_file ), 1 )
544
551
545
552
def ignore (dest ):
546
553
Hg .hgrc ()
@@ -555,7 +562,7 @@ def ignore(dest):
555
562
with open (Hg .ignore_file , 'a' ) as f :
556
563
f .write (dest + '\n ' )
557
564
except IOError :
558
- error ("Unable to write ignore file in \" %s\" " % os .path .join (os . getcwd (), Hg .ignore_file ), 1 )
565
+ error ("Unable to write ignore file in \" %s\" " % os .path .join (getcwd (), Hg .ignore_file ), 1 )
559
566
560
567
def unignore (dest ):
561
568
Hg .ignore_file = os .path .join ('.hg' , 'hgignore' )
@@ -571,7 +578,7 @@ def unignore(dest):
571
578
with open (Hg .ignore_file , 'w' ) as f :
572
579
f .write ('\n ' .join (lines ) + '\n ' )
573
580
except IOError :
574
- error ("Unable to write ignore file in \" %s\" " % os .path .join (os . getcwd (), Hg .ignore_file ), 1 )
581
+ error ("Unable to write ignore file in \" %s\" " % os .path .join (getcwd (), Hg .ignore_file ), 1 )
575
582
576
583
# pylint: disable=no-self-argument, no-method-argument, no-member, no-self-use, unused-argument
577
584
@scm ('git' )
@@ -634,30 +641,30 @@ def publish(all_refs=None):
634
641
if remote and branch :
635
642
popen ([git_cmd , 'push' , remote , branch ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
636
643
else :
637
- err = "Unable to publish outgoing changes for \" %s\" in \" %s\" .\n " % (os .path .basename (os . getcwd ()), os . getcwd ())
644
+ err = "Unable to publish outgoing changes for \" %s\" in \" %s\" .\n " % (os .path .basename (getcwd ()), getcwd ())
638
645
if not remote :
639
646
error (err + "The local repository is not associated with a remote one." , 1 )
640
647
if not branch :
641
648
error (err + "Working set is not on a branch." , 1 )
642
649
643
650
def fetch ():
644
- info ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (os . getcwd ()))
651
+ info ("Fetching revisions from remote repository to \" %s\" " % os .path .basename (getcwd ()))
645
652
popen ([git_cmd , 'fetch' , '--all' , '--tags' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
646
653
647
654
def discard (clean_files = False ):
648
- info ("Discarding local changes in \" %s\" " % os .path .basename (os . getcwd ()))
655
+ info ("Discarding local changes in \" %s\" " % os .path .basename (getcwd ()))
649
656
pquery ([git_cmd , 'reset' , 'HEAD' ] + ([] if very_verbose else ['-q' ])) # unmarks files for commit
650
657
pquery ([git_cmd , 'checkout' , '.' ] + ([] if very_verbose else ['-q' ])) # undo modified files
651
658
pquery ([git_cmd , 'clean' , '-fd' ] + (['-x' ] if clean_files else []) + (['-q' ] if very_verbose else ['-q' ])) # cleans up untracked files and folders
652
659
653
660
def merge (dest ):
654
- info ("Merging \" %s\" with \" %s\" " % (os .path .basename (os . getcwd ()), dest ))
661
+ info ("Merging \" %s\" with \" %s\" " % (os .path .basename (getcwd ()), dest ))
655
662
popen ([git_cmd , 'merge' , dest ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
656
663
657
664
def checkout (rev , clean = False ):
658
665
if not rev :
659
666
return
660
- info ("Checkout \" %s\" in %s" % (rev , os .path .basename (os . getcwd ())))
667
+ info ("Checkout \" %s\" in %s" % (rev , os .path .basename (getcwd ())))
661
668
branch = None
662
669
refs = Git .getrefs (rev )
663
670
for ref in refs : # re-associate with a local or remote branch (rev is the same)
@@ -692,7 +699,7 @@ def update(rev=None, clean=False, clean_files=False, is_local=False):
692
699
except ProcessException :
693
700
pass
694
701
else :
695
- err = "Unable to update \" %s\" in \" %s\" ." % (os .path .basename (os . getcwd ()), os . getcwd ())
702
+ err = "Unable to update \" %s\" in \" %s\" ." % (os .path .basename (getcwd ()), getcwd ())
696
703
if not remote :
697
704
info (err + " The local repository is not associated with a remote one." )
698
705
if not branch :
@@ -753,7 +760,7 @@ def getremotes(rtype='fetch'):
753
760
return result
754
761
755
762
def seturl (url ):
756
- info ("Setting url to \" %s\" in %s" % (url , os . getcwd ()))
763
+ info ("Setting url to \" %s\" in %s" % (url , getcwd ()))
757
764
return pquery ([git_cmd , 'remote' , 'set-url' , 'origin' , url ]).strip ()
758
765
759
766
def geturl ():
@@ -807,7 +814,7 @@ def ignores():
807
814
with open (Git .ignore_file , 'w' ) as f :
808
815
f .write ('\n ' .join (ignores )+ '\n ' )
809
816
except IOError :
810
- error ("Unable to write ignore file in \" %s\" " % os .path .join (os . getcwd (), Git .ignore_file ), 1 )
817
+ error ("Unable to write ignore file in \" %s\" " % os .path .join (getcwd (), Git .ignore_file ), 1 )
811
818
812
819
def ignore (dest ):
813
820
try :
@@ -825,7 +832,7 @@ def ignore(dest):
825
832
with open (Git .ignore_file , 'a' ) as f :
826
833
f .write (dest .replace ("\\ " , "/" ) + '\n ' )
827
834
except IOError :
828
- error ("Unable to write ignore file in \" %s\" " % os .path .join (os . getcwd (), Git .ignore_file ), 1 )
835
+ error ("Unable to write ignore file in \" %s\" " % os .path .join (getcwd (), Git .ignore_file ), 1 )
829
836
def unignore (dest ):
830
837
try :
831
838
with open (Git .ignore_file ) as f :
@@ -843,7 +850,7 @@ def unignore(dest):
843
850
with open (Git .ignore_file , 'w' ) as f :
844
851
f .write ('\n ' .join (lines ) + '\n ' )
845
852
except IOError :
846
- error ("Unable to write ignore file in \" %s\" " % os .path .join (os . getcwd (), Git .ignore_file ), 1 )
853
+ error ("Unable to write ignore file in \" %s\" " % os .path .join (getcwd (), Git .ignore_file ), 1 )
847
854
848
855
# Repository object
849
856
class Repo (object ):
@@ -865,19 +872,19 @@ def fromurl(cls, url, path=None):
865
872
m_bld_url = re .match (regex_build_url , url .strip ().replace ('\\ ' , '/' ))
866
873
if m_local :
867
874
repo .name = os .path .basename (path or m_local .group (1 ))
868
- repo .path = os .path .abspath (path or os .path .join (os . getcwd (), m_local .group (1 )))
875
+ repo .path = os .path .abspath (path or os .path .join (getcwd (), m_local .group (1 )))
869
876
repo .url = m_local .group (1 )
870
877
repo .rev = m_local .group (2 )
871
878
repo .is_local = True
872
879
elif m_bld_url :
873
880
repo .name = os .path .basename (path or m_bld_url .group (7 ))
874
- repo .path = os .path .abspath (path or os .path .join (os . getcwd (), repo .name ))
881
+ repo .path = os .path .abspath (path or os .path .join (getcwd (), repo .name ))
875
882
repo .url = m_bld_url .group (1 )+ '/builds'
876
883
repo .rev = m_bld_url .group (8 )
877
884
repo .is_build = True
878
885
elif m_repo_url :
879
886
repo .name = os .path .basename (path or m_repo_url .group (2 ))
880
- repo .path = os .path .abspath (path or os .path .join (os . getcwd (), repo .name ))
887
+ repo .path = os .path .abspath (path or os .path .join (getcwd (), repo .name ))
881
888
repo .url = formaturl (m_repo_url .group (1 ))
882
889
repo .rev = m_repo_url .group (3 )
883
890
if repo .rev and repo .rev != 'latest' and not re .match (r'^([a-fA-F0-9]{6,40})$' , repo .rev ):
@@ -911,11 +918,11 @@ def fromlib(cls, lib=None):
911
918
def fromrepo (cls , path = None ):
912
919
repo = cls ()
913
920
if path is None :
914
- path = Repo .findparent (os . getcwd ())
921
+ path = Repo .findparent (getcwd ())
915
922
if path is None :
916
923
error (
917
924
"Could not find mbed program in current path \" %s\" .\n "
918
- "You can fix this by calling \" mbed new .\" or \" mbed config root .\" in the root of your program." % os . getcwd ())
925
+ "You can fix this by calling \" mbed new .\" or \" mbed config root .\" in the root of your program." % getcwd ())
919
926
920
927
repo .path = os .path .abspath (path )
921
928
repo .name = os .path .basename (repo .path )
@@ -944,7 +951,7 @@ def isrepo(cls, path=None):
944
951
945
952
@classmethod
946
953
def findparent (cls , path = None ):
947
- path = os .path .abspath (path or os . getcwd ())
954
+ path = os .path .abspath (path or getcwd ())
948
955
949
956
while cd (path ):
950
957
if os .path .isfile (os .path .join (path , Cfg .file )) or Repo .isrepo (path ):
@@ -959,7 +966,7 @@ def findparent(cls, path=None):
959
966
960
967
@classmethod
961
968
def pathtype (cls , path = None ):
962
- path = os .path .abspath (path or os . getcwd ())
969
+ path = os .path .abspath (path or getcwd ())
963
970
964
971
depth = 0
965
972
while cd (path ):
@@ -1220,7 +1227,7 @@ class Program(object):
1220
1227
build_dir = "BUILD"
1221
1228
1222
1229
def __init__ (self , path = None , print_warning = False ):
1223
- path = os .path .abspath (path or os . getcwd ())
1230
+ path = os .path .abspath (path or getcwd ())
1224
1231
self .path = path
1225
1232
self .is_cwd = True
1226
1233
@@ -1675,7 +1682,7 @@ def thunk(parsed_args):
1675
1682
def new (name , scm = 'git' , program = False , library = False , mbedlib = False , create_only = False , depth = None , protocol = None ):
1676
1683
global cwd_root
1677
1684
1678
- d_path = os .path .abspath (name or os . getcwd ())
1685
+ d_path = os .path .abspath (name or getcwd ())
1679
1686
p_path = os .path .dirname (d_path )
1680
1687
if program and library :
1681
1688
error ("Cannot use both --program and --library options." , 1 )
@@ -2179,10 +2186,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2179
2186
# Gather remaining arguments
2180
2187
args = remainder
2181
2188
# Find the root of the program
2182
- program = Program (os . getcwd (), True )
2189
+ program = Program (getcwd (), True )
2183
2190
program .check_requirements (True )
2184
2191
# Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
2185
- orig_path = os . getcwd ()
2192
+ orig_path = getcwd ()
2186
2193
2187
2194
with cd (program .path ):
2188
2195
tools_dir = os .path .abspath (program .get_tools ())
@@ -2299,10 +2306,10 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
2299
2306
# Gather remaining arguments
2300
2307
args = remainder
2301
2308
# Find the root of the program
2302
- program = Program (os . getcwd (), True )
2309
+ program = Program (getcwd (), True )
2303
2310
program .check_requirements (True )
2304
2311
# Save original working directory
2305
- orig_path = os . getcwd ()
2312
+ orig_path = getcwd ()
2306
2313
2307
2314
target = program .get_target (target )
2308
2315
tchain = program .get_toolchain (toolchain )
@@ -2394,10 +2401,10 @@ def export(ide=None, target=None, source=False, clean=False, supported=False, ap
2394
2401
# Gather remaining arguments
2395
2402
args = remainder
2396
2403
# Find the root of the program
2397
- program = Program (os . getcwd (), True )
2404
+ program = Program (getcwd (), True )
2398
2405
program .check_requirements (True )
2399
2406
# Remember the original path. this is needed for compiling only the libraries and tests for the current folder.
2400
- orig_path = os . getcwd ()
2407
+ orig_path = getcwd ()
2401
2408
# Change directories to the program root to use mbed OS tools
2402
2409
with cd (program .path ):
2403
2410
tools_dir = program .get_tools ()
@@ -2445,7 +2452,7 @@ def detect():
2445
2452
# Gather remaining arguments
2446
2453
args = remainder
2447
2454
# Find the root of the program
2448
- program = Program (os . getcwd (), False )
2455
+ program = Program (getcwd (), False )
2449
2456
program .check_requirements (True )
2450
2457
# Change directories to the program root to use mbed OS tools
2451
2458
with cd (program .path ):
@@ -2494,7 +2501,7 @@ def config_(var=None, value=None, global_cfg=False, unset=False, list_config=Fal
2494
2501
log ("No global configuration is set\n " )
2495
2502
log ("\n " )
2496
2503
2497
- p = Program (os . getcwd ())
2504
+ p = Program (getcwd ())
2498
2505
action ("Local config (%s):" % p .path )
2499
2506
if not p .is_cwd :
2500
2507
p_vars = p .list_cfg ().items ()
@@ -2521,7 +2528,7 @@ def config_(var=None, value=None, global_cfg=False, unset=False, list_config=Fal
2521
2528
action (('%s' % value ) if value else 'No global %s set' % (name ))
2522
2529
else :
2523
2530
# Find the root of the program
2524
- program = Program (os . getcwd ())
2531
+ program = Program (getcwd ())
2525
2532
if program .is_cwd and not var == 'ROOT' :
2526
2533
error (
2527
2534
"Could not find mbed program in current path \" %s\" .\n "
@@ -2577,7 +2584,7 @@ def main():
2577
2584
global verbose , very_verbose , remainder , cwd_root
2578
2585
2579
2586
# Help messages adapt based on current dir
2580
- cwd_root = os . getcwd ()
2587
+ cwd_root = getcwd ()
2581
2588
2582
2589
if sys .version_info [0 ] != 2 or sys .version_info [1 ] < 7 :
2583
2590
error (
@@ -2599,7 +2606,7 @@ def main():
2599
2606
try :
2600
2607
very_verbose = pargs .very_verbose
2601
2608
verbose = very_verbose or pargs .verbose
2602
- info ('Working path \" %s\" (%s)' % (os . getcwd (), Repo .pathtype (cwd_root )))
2609
+ info ('Working path \" %s\" (%s)' % (getcwd (), Repo .pathtype (cwd_root )))
2603
2610
status = pargs .command (pargs )
2604
2611
except ProcessException as e :
2605
2612
error (
0 commit comments