127127
128128# stores current working directory for recursive operations
129129cwd_root = ""
130-
130+ _cwd = os . environ [ 'PWD' ] or os . getcwd ()
131131
132132# Logging and output
133133def log (msg ):
@@ -175,7 +175,7 @@ class ProcessException(Exception):
175175
176176def popen (command , stdin = None , ** kwargs ):
177177 # print for debugging
178- info ('Exec "' + ' ' .join (command )+ '" in ' + os . getcwd ())
178+ info ('Exec "' + ' ' .join (command )+ '" in ' + getcwd ())
179179 try :
180180 proc = subprocess .Popen (command , ** kwargs )
181181 except OSError as e :
@@ -187,11 +187,11 @@ def popen(command, stdin=None, **kwargs):
187187 raise e
188188
189189 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 ())
191191
192192def pquery (command , stdin = None , ** kwargs ):
193193 if very_verbose :
194- info ('Query "' + ' ' .join (command )+ '" in ' + os . getcwd ())
194+ info ('Query "' + ' ' .join (command )+ '" in ' + getcwd ())
195195 try :
196196 proc = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , ** kwargs )
197197 except OSError as e :
@@ -208,7 +208,7 @@ def pquery(command, stdin=None, **kwargs):
208208 log (str (stdout ).strip ()+ "\n " )
209209
210210 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 ())
212212
213213 return stdout
214214
@@ -225,12 +225,19 @@ def remove_readonly(func, path, _):
225225# Directory navigation
226226@contextlib .contextmanager
227227def cd (newdir ):
228- prevdir = os .getcwd ()
228+ global _cwd
229+ prevdir = getcwd ()
229230 os .chdir (newdir )
231+ _cwd = newdir
230232 try :
231233 yield
232234 finally :
233235 os .chdir (prevdir )
236+ _cwd = prevdir
237+
238+ def getcwd ():
239+ global _cwd
240+ return _cwd
234241
235242def relpath (root , path ):
236243 return path [len (root )+ 1 :]
@@ -309,12 +316,12 @@ def unpack_rev(rev):
309316 rev_file = os .path .join ('.' + Bld .name , '.rev-' + rev + '.zip' )
310317 try :
311318 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 ()))
313320 zf .extractall ('.' )
314321 except :
315322 if os .path .isfile (rev_file ):
316323 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 ()))
318325
319326 def checkout (rev , clean = False ):
320327 url = Bld .geturl ()
@@ -331,7 +338,7 @@ def checkout(rev, clean=False):
331338 if rev != Bld .getrev () or clean :
332339 Bld .cleanup ()
333340
334- info ("Checkout \" %s\" in %s" % (rev , os .path .basename (os . getcwd ())))
341+ info ("Checkout \" %s\" in %s" % (rev , os .path .basename (getcwd ())))
335342 try :
336343 Bld .unpack_rev (rev )
337344 Bld .seturl (url + '/' + rev )
@@ -345,7 +352,7 @@ def untracked():
345352 return ""
346353
347354 def seturl (url ):
348- info ("Setting url to \" %s\" in %s" % (url , os . getcwd ()))
355+ info ("Setting url to \" %s\" in %s" % (url , getcwd ()))
349356 if not os .path .exists ('.' + Bld .name ):
350357 os .mkdir ('.' + Bld .name )
351358
@@ -417,15 +424,15 @@ def publish(all_refs=None):
417424 popen ([hg_cmd , 'push' ] + (['--new-branch' ] if all_refs else []) + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
418425
419426 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 ()))
421428 popen ([hg_cmd , 'pull' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
422429
423430 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 ()))
425432 popen ([hg_cmd , 'update' , '-C' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
426433
427434 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 ())))
429436 if clean_files :
430437 files = pquery ([hg_cmd , 'status' , '--no-status' , '-ui' ]).splitlines ()
431438 for f in files :
@@ -457,7 +464,7 @@ def outgoing():
457464 return 0
458465
459466 def seturl (url ):
460- info ("Setting url to \" %s\" in %s" % (url , os . getcwd ()))
467+ info ("Setting url to \" %s\" in %s" % (url , getcwd ()))
461468 hgrc = os .path .join ('.hg' , 'hgrc' )
462469 tagpaths = '[paths]'
463470 remote = 'default'
@@ -540,7 +547,7 @@ def ignores():
540547 with open (Hg .ignore_file , 'w' ) as f :
541548 f .write ("syntax: glob\n " + '\n ' .join (ignores )+ '\n ' )
542549 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 )
544551
545552 def ignore (dest ):
546553 Hg .hgrc ()
@@ -555,7 +562,7 @@ def ignore(dest):
555562 with open (Hg .ignore_file , 'a' ) as f :
556563 f .write (dest + '\n ' )
557564 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 )
559566
560567 def unignore (dest ):
561568 Hg .ignore_file = os .path .join ('.hg' , 'hgignore' )
@@ -571,7 +578,7 @@ def unignore(dest):
571578 with open (Hg .ignore_file , 'w' ) as f :
572579 f .write ('\n ' .join (lines ) + '\n ' )
573580 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 )
575582
576583# pylint: disable=no-self-argument, no-method-argument, no-member, no-self-use, unused-argument
577584@scm ('git' )
@@ -634,30 +641,30 @@ def publish(all_refs=None):
634641 if remote and branch :
635642 popen ([git_cmd , 'push' , remote , branch ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
636643 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 ())
638645 if not remote :
639646 error (err + "The local repository is not associated with a remote one." , 1 )
640647 if not branch :
641648 error (err + "Working set is not on a branch." , 1 )
642649
643650 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 ()))
645652 popen ([git_cmd , 'fetch' , '--all' , '--tags' ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
646653
647654 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 ()))
649656 pquery ([git_cmd , 'reset' , 'HEAD' ] + ([] if very_verbose else ['-q' ])) # unmarks files for commit
650657 pquery ([git_cmd , 'checkout' , '.' ] + ([] if very_verbose else ['-q' ])) # undo modified files
651658 pquery ([git_cmd , 'clean' , '-fd' ] + (['-x' ] if clean_files else []) + (['-q' ] if very_verbose else ['-q' ])) # cleans up untracked files and folders
652659
653660 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 ))
655662 popen ([git_cmd , 'merge' , dest ] + (['-v' ] if very_verbose else ([] if verbose else ['-q' ])))
656663
657664 def checkout (rev , clean = False ):
658665 if not rev :
659666 return
660- info ("Checkout \" %s\" in %s" % (rev , os .path .basename (os . getcwd ())))
667+ info ("Checkout \" %s\" in %s" % (rev , os .path .basename (getcwd ())))
661668 branch = None
662669 refs = Git .getrefs (rev )
663670 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):
692699 except ProcessException :
693700 pass
694701 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 ())
696703 if not remote :
697704 info (err + " The local repository is not associated with a remote one." )
698705 if not branch :
@@ -753,7 +760,7 @@ def getremotes(rtype='fetch'):
753760 return result
754761
755762 def seturl (url ):
756- info ("Setting url to \" %s\" in %s" % (url , os . getcwd ()))
763+ info ("Setting url to \" %s\" in %s" % (url , getcwd ()))
757764 return pquery ([git_cmd , 'remote' , 'set-url' , 'origin' , url ]).strip ()
758765
759766 def geturl ():
@@ -807,7 +814,7 @@ def ignores():
807814 with open (Git .ignore_file , 'w' ) as f :
808815 f .write ('\n ' .join (ignores )+ '\n ' )
809816 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 )
811818
812819 def ignore (dest ):
813820 try :
@@ -825,7 +832,7 @@ def ignore(dest):
825832 with open (Git .ignore_file , 'a' ) as f :
826833 f .write (dest .replace ("\\ " , "/" ) + '\n ' )
827834 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 )
829836 def unignore (dest ):
830837 try :
831838 with open (Git .ignore_file ) as f :
@@ -843,7 +850,7 @@ def unignore(dest):
843850 with open (Git .ignore_file , 'w' ) as f :
844851 f .write ('\n ' .join (lines ) + '\n ' )
845852 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 )
847854
848855# Repository object
849856class Repo (object ):
@@ -865,19 +872,19 @@ def fromurl(cls, url, path=None):
865872 m_bld_url = re .match (regex_build_url , url .strip ().replace ('\\ ' , '/' ))
866873 if m_local :
867874 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 )))
869876 repo .url = m_local .group (1 )
870877 repo .rev = m_local .group (2 )
871878 repo .is_local = True
872879 elif m_bld_url :
873880 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 ))
875882 repo .url = m_bld_url .group (1 )+ '/builds'
876883 repo .rev = m_bld_url .group (8 )
877884 repo .is_build = True
878885 elif m_repo_url :
879886 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 ))
881888 repo .url = formaturl (m_repo_url .group (1 ))
882889 repo .rev = m_repo_url .group (3 )
883890 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):
911918 def fromrepo (cls , path = None ):
912919 repo = cls ()
913920 if path is None :
914- path = Repo .findparent (os . getcwd ())
921+ path = Repo .findparent (getcwd ())
915922 if path is None :
916923 error (
917924 "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 ())
919926
920927 repo .path = os .path .abspath (path )
921928 repo .name = os .path .basename (repo .path )
@@ -944,7 +951,7 @@ def isrepo(cls, path=None):
944951
945952 @classmethod
946953 def findparent (cls , path = None ):
947- path = os .path .abspath (path or os . getcwd ())
954+ path = os .path .abspath (path or getcwd ())
948955
949956 while cd (path ):
950957 if os .path .isfile (os .path .join (path , Cfg .file )) or Repo .isrepo (path ):
@@ -959,7 +966,7 @@ def findparent(cls, path=None):
959966
960967 @classmethod
961968 def pathtype (cls , path = None ):
962- path = os .path .abspath (path or os . getcwd ())
969+ path = os .path .abspath (path or getcwd ())
963970
964971 depth = 0
965972 while cd (path ):
@@ -1220,7 +1227,7 @@ class Program(object):
12201227 build_dir = "BUILD"
12211228
12221229 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 ())
12241231 self .path = path
12251232 self .is_cwd = True
12261233
@@ -1675,7 +1682,7 @@ def thunk(parsed_args):
16751682def new (name , scm = 'git' , program = False , library = False , mbedlib = False , create_only = False , depth = None , protocol = None ):
16761683 global cwd_root
16771684
1678- d_path = os .path .abspath (name or os . getcwd ())
1685+ d_path = os .path .abspath (name or getcwd ())
16791686 p_path = os .path .dirname (d_path )
16801687 if program and library :
16811688 error ("Cannot use both --program and --library options." , 1 )
@@ -2179,10 +2186,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
21792186 # Gather remaining arguments
21802187 args = remainder
21812188 # Find the root of the program
2182- program = Program (os . getcwd (), True )
2189+ program = Program (getcwd (), True )
21832190 program .check_requirements (True )
21842191 # 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 ()
21862193
21872194 with cd (program .path ):
21882195 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
22992306 # Gather remaining arguments
23002307 args = remainder
23012308 # Find the root of the program
2302- program = Program (os . getcwd (), True )
2309+ program = Program (getcwd (), True )
23032310 program .check_requirements (True )
23042311 # Save original working directory
2305- orig_path = os . getcwd ()
2312+ orig_path = getcwd ()
23062313
23072314 target = program .get_target (target )
23082315 tchain = program .get_toolchain (toolchain )
@@ -2394,10 +2401,10 @@ def export(ide=None, target=None, source=False, clean=False, supported=False, ap
23942401 # Gather remaining arguments
23952402 args = remainder
23962403 # Find the root of the program
2397- program = Program (os . getcwd (), True )
2404+ program = Program (getcwd (), True )
23982405 program .check_requirements (True )
23992406 # 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 ()
24012408 # Change directories to the program root to use mbed OS tools
24022409 with cd (program .path ):
24032410 tools_dir = program .get_tools ()
@@ -2445,7 +2452,7 @@ def detect():
24452452 # Gather remaining arguments
24462453 args = remainder
24472454 # Find the root of the program
2448- program = Program (os . getcwd (), False )
2455+ program = Program (getcwd (), False )
24492456 program .check_requirements (True )
24502457 # Change directories to the program root to use mbed OS tools
24512458 with cd (program .path ):
@@ -2494,7 +2501,7 @@ def config_(var=None, value=None, global_cfg=False, unset=False, list_config=Fal
24942501 log ("No global configuration is set\n " )
24952502 log ("\n " )
24962503
2497- p = Program (os . getcwd ())
2504+ p = Program (getcwd ())
24982505 action ("Local config (%s):" % p .path )
24992506 if not p .is_cwd :
25002507 p_vars = p .list_cfg ().items ()
@@ -2521,7 +2528,7 @@ def config_(var=None, value=None, global_cfg=False, unset=False, list_config=Fal
25212528 action (('%s' % value ) if value else 'No global %s set' % (name ))
25222529 else :
25232530 # Find the root of the program
2524- program = Program (os . getcwd ())
2531+ program = Program (getcwd ())
25252532 if program .is_cwd and not var == 'ROOT' :
25262533 error (
25272534 "Could not find mbed program in current path \" %s\" .\n "
@@ -2577,7 +2584,7 @@ def main():
25772584 global verbose , very_verbose , remainder , cwd_root
25782585
25792586 # Help messages adapt based on current dir
2580- cwd_root = os . getcwd ()
2587+ cwd_root = getcwd ()
25812588
25822589 if sys .version_info [0 ] != 2 or sys .version_info [1 ] < 7 :
25832590 error (
@@ -2599,7 +2606,7 @@ def main():
25992606 try :
26002607 very_verbose = pargs .very_verbose
26012608 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 )))
26032610 status = pargs .command (pargs )
26042611 except ProcessException as e :
26052612 error (
0 commit comments