2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Shim script for the SDK updater, to allow automatic updating.
8 The purpose of this script is to be a shim which automatically updates
9 sdk_tools (the bundle containing the updater scripts) whenever this script is
12 When the sdk_tools bundle has been updated to the most recent version, this
13 script forwards its arguments to sdk_updater_main.py.
18 from sdk_update_common
import RenameDir
, RemoveDir
, Error
23 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
24 SDK_UPDATE_MAIN
= os
.path
.join(SCRIPT_DIR
, 'sdk_update_main.py')
25 SDK_ROOT_DIR
= os
.path
.dirname(SCRIPT_DIR
)
26 NACLSDK_SHELL_SCRIPT
= os
.path
.join(SDK_ROOT_DIR
, 'naclsdk')
27 if sys
.platform
.startswith('win'):
28 NACLSDK_SHELL_SCRIPT
+= '.bat'
29 SDK_TOOLS_DIR
= os
.path
.join(SDK_ROOT_DIR
, 'sdk_tools')
30 SDK_TOOLS_UPDATE_DIR
= os
.path
.join(SDK_ROOT_DIR
, 'sdk_tools_update')
33 def MakeSdkUpdateMainCmd(args
):
34 """Returns a list of command line arguments to run sdk_update_main.
37 args: A list of arguments to pass to sdk_update_main.py
39 A new list that can be passed to subprocess.call, subprocess.Popen, etc.
41 return [sys
.executable
, SDK_UPDATE_MAIN
] + args
44 def UpdateSDKTools(args
):
45 """Run sdk_update_main to update sdk_tools bundle. Return True if it is
49 args: The arguments to pass to sdk_update_main.py. We need to keep this to
50 ensure sdk_update_main is called correctly; some parameters specify
51 URLS or directories to use.
53 True if the sdk_tools bundle was updated.
55 cmd
= MakeSdkUpdateMainCmd(args
+ ['--update-sdk-tools'])
56 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
57 stdout
, _
= process
.communicate()
58 if process
.returncode
== 0:
59 return stdout
.find('Updating bundle sdk_tools to version') != -1
61 # Updating sdk_tools could fail for any number of reasons. Regardless, it
62 # should be safe to try to run the user's command.
66 def RenameSdkToolsDirectory():
67 """Rename sdk_tools_update to sdk_tools."""
68 # If there is no update directory, bail.
69 if not os
.path
.isdir(SDK_TOOLS_UPDATE_DIR
):
73 tempdir
= tempfile
.mkdtemp()
74 temp_sdktools
= os
.path
.join(tempdir
, 'sdk_tools')
76 RenameDir(SDK_TOOLS_DIR
, temp_sdktools
)
78 # The user is probably on Windows, and the directory is locked.
79 sys
.stderr
.write('Cannot rename directory "%s". Make sure no programs are'
80 ' viewing or accessing this directory and try again.\n' % (
85 RenameDir(SDK_TOOLS_UPDATE_DIR
, SDK_TOOLS_DIR
)
87 # Failed for some reason, move the old dir back.
89 RenameDir(temp_sdktools
, SDK_TOOLS_DIR
)
91 # Not much to do here. sdk_tools won't exist, but sdk_tools_update
92 # should. Hopefully running the batch script again will move
93 # sdk_tools_update -> sdk_tools and it will work this time...
94 sys
.stderr
.write('Unable to restore directory "%s" while auto-updating.'
95 'Make sure no programs are viewing or accessing this directory and'
96 'try again.\n' % (SDK_TOOLS_DIR
,))
104 def CheckRuntimeRequirements():
105 if sys
.platform
.startswith('linux'):
106 if not os
.path
.exists('/lib/ld-linux.so.2'):
107 sys
.stderr
.write("""32-bit runtime environment was not found on this
108 system. Specifically the 32-bit dynamic loader which is needed by the NaCl
109 compilers was not found ('/lib/ld-linux.so.2'). On modern debian/ubuntu
110 systems this is included in the 'libc6:i386' package.\n""")
116 CheckRuntimeRequirements()
117 if UpdateSDKTools(args
) and RenameSdkToolsDirectory():
118 # Call the shell script, just in case this script was updated in the next
119 # version of sdk_tools
120 return subprocess
.call([NACLSDK_SHELL_SCRIPT
] + args
)
122 return subprocess
.call(MakeSdkUpdateMainCmd(args
))
125 if __name__
== '__main__':
128 except KeyboardInterrupt: