1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Utility functions for sdk_update.py and sdk_update_main.py."""
16 class Error(Exception):
17 """Generic error/exception for sdk_update module"""
21 def MakeDirs(directory
):
22 if not os
.path
.exists(directory
):
23 logging
.info('Making directory %s' % (directory
,))
24 os
.makedirs(directory
)
27 def RemoveDir(outdir
):
28 """Removes the given directory
30 On Unix systems, this just runs shutil.rmtree, but on Windows, this doesn't
31 work when the directory contains junctions (as does our SDK installer).
32 Therefore, on Windows, it runs rmdir /S /Q as a shell command. This always
33 does the right thing on Windows. If the directory already didn't exist,
34 RemoveDir will return successfully without taking any action.
37 outdir: The directory to delete
40 Error - If this operation fails for any reason.
45 for num_tries
in xrange(max_tries
):
50 if not os
.path
.exists(outdir
):
51 # The directory can't be removed because it doesn't exist.
55 # On Windows this could be an issue with junctions, so try again with
57 if sys
.platform
== 'win32':
59 cmd
= ['rmdir', '/S', '/Q', outdir
]
60 process
= subprocess
.Popen(cmd
, stderr
=subprocess
.PIPE
, shell
=True)
61 _
, stderr
= process
.communicate()
62 if process
.returncode
!= 0:
63 raise Error('\"%s\" failed with code %d. Output:\n %s' % (
64 ' '.join(cmd
), process
.returncode
, stderr
))
66 # Ignore failures, we'll just try again.
67 except subprocess
.CalledProcessError
as e
:
68 # CalledProcessError has no error message, generate one.
69 last_exception
= Error('\"%s\" failed with code %d.' % (
70 ' '.join(e
.cmd
), e
.returncode
))
74 # Didn't work, sleep and try again.
75 time
.sleep(num_tries
+ 1)
78 raise Error('Unable to remove directory "%s"\n %s' % (outdir
,
82 def RenameDir(srcdir
, destdir
):
83 """Renames srcdir to destdir. Removes destdir before doing the
84 rename if it already exists."""
88 for num_tries
in xrange(max_tries
):
91 shutil
.move(srcdir
, destdir
)
93 except OSError as err
:
94 if err
.errno
!= errno
.EACCES
:
96 # If we are here, we didn't exit due to raised exception, so we are
97 # handling a Windows flaky access error. Sleep one second and try
99 time
.sleep(num_tries
+ 1)
101 # end of while loop -- could not RenameDir
102 raise Error('Could not RenameDir %s => %s after %d tries.\n'
103 'Please check that no shells or applications '
104 'are accessing files in %s.'
105 % (srcdir
, destdir
, num_tries
+ 1, destdir
))