def _DoStartupChecks(): # pylint:disable=g-import-not-at-top from googlecloudsdk.core.util import platforms if not platforms.PythonVersion().IsSupported(): sys.exit(1) if not platforms.Platform.Current().IsSupported(): sys.exit(1)
def RaiseIfNotPython27(): if not platforms.PythonVersion().IsSupported(): raise UnsupportedPythonVersionError(( 'Python 2.7 or greater is required for App Engine commands in gcloud.' '\n\n' 'Your Python location: [{0}]\n\n' 'Please set the CLOUDSDK_PYTHON environment variable to point to a ' 'supported version in order to use this command.').format( sys.executable))
def Run(self, args): holder = info_holder.InfoHolder() python_version = platforms.PythonVersion() if not python_version.IsSupported(): log.warn(( 'Only Python version {0} is supported for the Cloud SDK. Many ' 'commands will work with a previous version, but not all.' ).format(python_version.MinSupportedVersionString())) return holder
def LoadSubElement(self, name, allow_empty=False, release_track_override=None): """Load a specific sub group or command. Args: name: str, The name of the element to load. allow_empty: bool, True to allow creating this group as empty to start with. release_track_override: base.ReleaseTrack, Load the given sub-element under the given track instead of that of the parent. This should only be used when specifically creating the top level release track groups. Returns: _CommandCommon, The loaded sub element, or None if it did not exist. """ # TODO(b/71714857): Remove once all surfaces support py3. if not self._common_type._allow_py3: # pylint: disable=protected-access try: platforms.PythonVersion().IsCompatible( allow_py3=False, raise_exception=True) except platforms.Error: raise exceptions.ToolException( 'This command is not yet compatible with Python 3.\n' + platforms.PythonVersion.ENV_VAR_MESSAGE) name = name.replace('-', '_') # See if this element has already been loaded. existing = self.groups.get(name, None) if not existing: existing = self.commands.get(name, None) if existing: return existing if name in self._unloadable_elements: return None element = None try: if name in self._groups_to_load: element = CommandGroup( self._groups_to_load[name], self._path + [name], release_track_override or self.ReleaseTrack(), self._construction_id, self._cli_generator, self.SubParser(), parent_group=self, allow_empty=allow_empty) self.groups[element.name] = element elif name in self._commands_to_load: element = Command( self._commands_to_load[name], self._path + [name], release_track_override or self.ReleaseTrack(), self._construction_id, self._cli_generator, self.SubParser(), parent_group=self) self.commands[element.name] = element except command_loading.ReleaseTrackNotImplementedException as e: self._unloadable_elements.add(name) log.debug(e) return element
def main(gcloud_cli=None, credential_providers=None): atexit.register(metrics.Shutdown) if not platforms.PythonVersion().IsCompatible(): sys.exit(1) metrics.Started(START_TIME) metrics.Executions( 'gcloud', local_state.InstallationState.VersionForInstalledComponent('core')) if gcloud_cli is None: gcloud_cli = CreateCLI([]) with creds_context_managers.CredentialProvidersManager( credential_providers): try: gcloud_cli.Execute() # Flush stdout so that if we've received a SIGPIPE we handle the broken # pipe within this try block, instead of potentially during interpreter # shutdown. sys.stdout.flush() except IOError as err: # We want to ignore EPIPE IOErrors (as of Python 3.3 these can be caught # specifically with BrokenPipeError, but we do it this way for Python 2 # compatibility). # # By default, Python ignores SIGPIPE (see # http://utcc.utoronto.ca/~cks/space/blog/python/SignalExceptionSurprise # ). # This means that attempting to write any output to a closed pipe (e.g. # in the case of output piped to `head` or `grep -q`) will result in an # IOError, which gets reported as a gcloud crash. We don't want this # behavior, so we ignore EPIPE (it's not a real error; it's a normal # thing to occur). # # Before, we restored the SIGPIPE signal handler, but that caused issues # with scripts/programs that wrapped gcloud. if err.errno == errno.EPIPE: # At this point we've caught the broken pipe, but since Python flushes # standard streams on exit, it's still possible for a broken pipe # error to happen during interpreter shutdown. The interpreter will # catch this but in Python 3 it still prints a warning to stderr # saying that the exception was ignored # (see https://bugs.python.org/issue11380): # # Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' # encoding='UTF-8'> # BrokenPipeError: [Errno 32] Broken pipe # # To prevent this from happening, we redirect any remaining output to # devnull as recommended here: # https://docs.python.org/3/library/signal.html#note-on-sigpipe. devnull = os.open(os.devnull, os.O_WRONLY) os.dup2(devnull, sys.stdout.fileno()) else: raise
def Run(self, args): if args.run_diagnostics: network_diagnostics.NetworkDiagnostic().RunChecks() return holder = info_holder.InfoHolder(anonymizer=info_holder.Anonymizer( ) if args.anonymize else info_holder.NoopAnonymizer()) python_version = platforms.PythonVersion() if not python_version.IsSupported(): log.warning( 'Only Python version {0} is supported for the Cloud SDK. Many ' 'commands will work with a previous version, but not all.'. format(python_version.MinSupportedVersionString())) return holder
def main(gcloud_cli=None, credential_providers=None): if not platforms.PythonVersion().IsCompatible( allow_py3=properties.VALUES.core.allow_py3.GetBool()): sys.exit(1) metrics.Started(START_TIME) # TODO(b/36049857): Put a real version number here metrics.Executions( 'gcloud', local_state.InstallationState.VersionForInstalledComponent('core')) if gcloud_cli is None: gcloud_cli = CreateCLI([]) # Register some other sources for credentials and project. credential_providers = credential_providers or [ creds_store.DevShellCredentialProvider(), creds_store.GceCredentialProvider(), ] for provider in credential_providers: provider.Register() # Register support for service account impersonation. creds_store.IMPERSONATION_TOKEN_PROVIDER = ( iamcred_util.ImpersonationAccessTokenProvider()) try: try: gcloud_cli.Execute() except IOError as err: # We want to ignore EPIPE IOErrors. # By default, Python ignores SIGPIPE (see # http://utcc.utoronto.ca/~cks/space/blog/python/SignalExceptionSurprise). # This means that attempting to write any output to a closed pipe (e.g. in # the case of output piped to `head` or `grep -q`) will result in an # IOError, which gets reported as a gcloud crash. We don't want this # behavior, so we ignore EPIPE (it's not a real error; it's a normal thing # to occur). # Before, we restore the SIGPIPE signal handler, but that caused issues # with scripts/programs that wrapped gcloud. if err.errno != errno.EPIPE: raise except Exception as err: # pylint:disable=broad-except crash_handling.HandleGcloudCrash(err) if properties.VALUES.core.print_unhandled_tracebacks.GetBool(): # We want to see the traceback as normally handled by Python raise else: # This is the case for most non-Cloud SDK developers. They shouldn't see # the full stack trace, but just the nice "gcloud crashed" message. sys.exit(1) finally: for provider in credential_providers: provider.UnRegister()
def Filter(self, unused_context, unused_args): # TODO(b/24169312): remove if not properties.VALUES.app.suppress_change_warning.GetBool(): log.warn(CHANGE_WARNING) properties.PersistProperty( properties.VALUES.app.suppress_change_warning, 'true') if not platforms.PythonVersion().IsSupported(): raise UnsupportedPythonVersionError(( 'Python 2.7 or greater is required for App Engine commands in ' 'gcloud.\n\n' 'Your Python location: [{0}]\n\n' 'Please set the CLOUDSDK_PYTHON environment variable to point to a ' 'supported version in order to use this command.').format( sys.executable))
def testIsCompatible(self, version, is_compatible, error_string): self.assertEqual(is_compatible, platforms.PythonVersion(version).IsCompatible()) if error_string: error_string += """\ If you have a compatible Python interpreter installed, you can use it by setting the CLOUDSDK_PYTHON environment variable to point to it. """ self.AssertErrEquals(error_string) else: self.AssertErrEquals('')
def DisallowPython3(): if not platforms.PythonVersion().IsCompatible(allow_py3=False): sys.exit(1)
def _DoStartupChecks(): if not platforms.PythonVersion().IsCompatible(): sys.exit(1)
def DoAllRequiredChecks(): if not platforms.PythonVersion().IsCompatible(): sys.exit(1)
def DoAllRequiredChecks(): if not platforms.PythonVersion().IsSupported(): sys.exit(1) if not platforms.Platform.Current().IsSupported(): sys.exit(1)
def _DoStartupChecks(): if not platforms.PythonVersion().IsCompatible(): sys.exit(1) if not platforms.Platform.Current().IsSupported(): sys.exit(1)
def testRaise(self): with self.assertRaisesRegex( platforms.Error, 'ERROR: Python 3.0 is not compatible with the Google Cloud SDK.' ' Please use Python version 2.7.x or 3.5 and up.'): platforms.PythonVersion((3, 0)).IsCompatible(raise_exception=True)
def testUnknownVersion(self): version = platforms.PythonVersion() version.version = None self.assertFalse(version.IsCompatible()) self.AssertErrContains('ERROR: Your current version of Python is not ')
def main(gcloud_cli=None, credential_providers=None): if not platforms.PythonVersion().IsCompatible( allow_py3=properties.VALUES.core.allow_py3.GetBool()): sys.exit(1) metrics.Started(START_TIME) # TODO(b/36049857): Put a real version number here metrics.Executions( 'gcloud', local_state.InstallationState.VersionForInstalledComponent('core')) if gcloud_cli is None: gcloud_cli = CreateCLI([]) # Register some other sources for credentials and project. credential_providers = credential_providers or [ creds_store.DevShellCredentialProvider(), creds_store.GceCredentialProvider(), ] for provider in credential_providers: provider.Register() # Register support for service account impersonation. creds_store.IMPERSONATION_TOKEN_PROVIDER = ( iamcred_util.ImpersonationAccessTokenProvider()) try: try: gcloud_cli.Execute() # Flush stdout so that if we've received a SIGPIPE we handle the broken # pipe within this try block, instead of potentially during interpreter # shutdown. sys.stdout.flush() except IOError as err: # We want to ignore EPIPE IOErrors (as of Python 3.3 these can be caught # specifically with BrokenPipeError, but we do it this way for Python 2 # compatibility). # # By default, Python ignores SIGPIPE (see # http://utcc.utoronto.ca/~cks/space/blog/python/SignalExceptionSurprise). # This means that attempting to write any output to a closed pipe (e.g. in # the case of output piped to `head` or `grep -q`) will result in an # IOError, which gets reported as a gcloud crash. We don't want this # behavior, so we ignore EPIPE (it's not a real error; it's a normal thing # to occur). # # Before, we restored the SIGPIPE signal handler, but that caused issues # with scripts/programs that wrapped gcloud. if err.errno == errno.EPIPE: # At this point we've caught the broken pipe, but since Python flushes # standard streams on exit, it's still possible for a broken pipe error # to happen during interpreter shutdown. The interpreter will catch this # but in Python 3 it still prints a warning to stderr saying that the # exception was ignored (see https://bugs.python.org/issue11380): # # Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' # encoding='UTF-8'> # BrokenPipeError: [Errno 32] Broken pipe # # To prevent this from happening, we redirect any remaining output to # devnull as recommended here: # https://docs.python.org/3/library/signal.html#note-on-sigpipe. devnull = os.open(os.devnull, os.O_WRONLY) os.dup2(devnull, sys.stdout.fileno()) else: raise except Exception as err: # pylint:disable=broad-except crash_handling.HandleGcloudCrash(err) if properties.VALUES.core.print_unhandled_tracebacks.GetBool(): # We want to see the traceback as normally handled by Python raise else: # This is the case for most non-Cloud SDK developers. They shouldn't see # the full stack trace, but just the nice "gcloud crashed" message. sys.exit(1) finally: for provider in credential_providers: provider.UnRegister()
def _DoStartupChecks(): from googlecloudsdk.core.util import platforms if not platforms.PythonVersion().IsSupported(): sys.exit(1) if not platforms.Platform.Current().IsSupported(): sys.exit(1)
def DoAllRequiredChecks(): if not platforms.PythonVersion().IsCompatible( properties.VALUES.core.allow_py3.GetBool()): sys.exit(1)