def Update(self): """Creates or updates the RC file.""" if self.rc_path: if os.path.isfile(self.rc_path): with open(self.rc_path) as rc_file: rc_contents = rc_file.read() original_rc_contents = rc_contents else: rc_contents = '' original_rc_contents = '' if self.path_update: rc_contents = _GetRcContents( '# The next line updates PATH for the Google Cloud SDK.', self.path, rc_contents) if self.completion_update: rc_contents = _GetRcContents( '# The next line enables shell command completion for gcloud.', self.completion, rc_contents, pattern=('# The next line enables [a-z][a-z]*' ' command completion for gcloud.')) if rc_contents == original_rc_contents: _TraceAction( 'No changes necessary for [{rc}].'.format(rc=self.rc_path)) return if os.path.exists(self.rc_path): rc_backup = self.rc_path + '.backup' _TraceAction('Backing up [{rc}] to [{backup}].'.format( rc=self.rc_path, backup=rc_backup)) shutil.copyfile(self.rc_path, rc_backup) with open(self.rc_path, 'w') as rc_file: rc_file.write(rc_contents) _TraceAction( '[{rc_path}] has been updated.'.format(rc_path=self.rc_path)) _TraceAction( console_io.FormatRequiredUserAction( 'Start a new shell for the changes to take effect.')) if not self.completion_update: _TraceAction( console_io.FormatRequiredUserAction( 'Source [{rc}]in your profile to enable shell command completion for ' 'gcloud.'.format(rc=self.completion))) if not self.path_update: _TraceAction( console_io.FormatRequiredUserAction( 'Source [{rc}] in your profile to add the Google Cloud SDK command ' 'line tools to your $PATH.'.format(rc=self.path)))
def Update(self): """Creates or updates the RC file.""" if self.rc_path: # Check whether RC file is a file and store its contents. if os.path.isfile(self.rc_path): with open(self.rc_path) as rc_file: rc_contents = rc_file.read() original_rc_contents = rc_contents elif os.path.exists(self.rc_path): _TraceAction( '[{rc_path}] exists and is not a file, so it cannot be updated.' .format(rc_path=self.rc_path)) return else: rc_contents = '' original_rc_contents = '' if self.path_update: rc_contents = _GetRcContents( '# The next line updates PATH for the Google Cloud SDK.', self.path, rc_contents, source_line=self._GetSourceLine()) # gcloud doesn't (yet?) support completion for Fish, so check whether the # completion file exists if self.completion_update and self._CompletionExists(): rc_contents = _GetRcContents( '# The next line enables shell command completion for gcloud.', self.completion, rc_contents, source_line=self._GetSourceLine(), pattern=('# The next line enables [a-z][a-z]*' ' command completion for gcloud.')) if rc_contents == original_rc_contents: _TraceAction( 'No changes necessary for [{rc}].'.format(rc=self.rc_path)) return if os.path.exists(self.rc_path): rc_backup = self.rc_path + '.backup' _TraceAction('Backing up [{rc}] to [{backup}].'.format( rc=self.rc_path, backup=rc_backup)) shutil.copyfile(self.rc_path, rc_backup) # Update rc file, creating it if it does not exist. rc_dir = os.path.dirname(self.rc_path) try: files.MakeDir(rc_dir) except (files.Error, OSError): _TraceAction( 'Could not create directories for [{rc_path}], so it ' 'cannot be updated.'.format(rc_path=self.rc_path)) return with open(self.rc_path, 'w') as rc_file: rc_file.write(rc_contents) _TraceAction( '[{rc_path}] has been updated.'.format(rc_path=self.rc_path)) _TraceAction( console_io.FormatRequiredUserAction( 'Start a new shell for the changes to take effect.')) if not self.completion_update: _TraceAction( '==> Source [{rc}] in your profile to enable shell command ' 'completion for gcloud.'.format(rc=self.completion)) if not self.path_update: _TraceAction( '==> Source [{rc}] in your profile to add the Google Cloud SDK ' 'command line tools to your $PATH.'.format(rc=self.path))
def Update(self): """Creates or updates the RC file.""" if self.rc_path: # Check whether RC file is a file and store its contents. if os.path.isfile(self.rc_path): rc_contents = files.ReadFileContents(self.rc_path) original_rc_contents = rc_contents elif os.path.exists(self.rc_path): _TraceAction( '[{rc_path}] exists and is not a file, so it cannot be updated.' .format(rc_path=self.rc_path)) return else: rc_contents = '' original_rc_contents = '' if self.path_update: rc_contents = _GetRcContents( '# The next line updates PATH for the Google Cloud SDK.', self.path, rc_contents, self.shell) # gcloud doesn't (yet?) support completion for Fish, so check whether the # completion file exists if self.completion_update and self._CompletionExists(): rc_contents = _GetRcContents( '# The next line enables shell command completion for gcloud.', self.completion, rc_contents, self.shell, pattern=('# The next line enables [a-z][a-z]*' ' command completion for gcloud.')) if rc_contents == original_rc_contents: _TraceAction( 'No changes necessary for [{rc}].'.format(rc=self.rc_path)) return if os.path.exists(self.rc_path): rc_backup = self.rc_path + '.backup' _TraceAction('Backing up [{rc}] to [{backup}].'.format( rc=self.rc_path, backup=rc_backup)) shutil.copyfile(self.rc_path, rc_backup) # Update rc file, creating it if it does not exist. rc_dir = os.path.dirname(self.rc_path) try: files.MakeDir(rc_dir) except (files.Error, IOError, OSError): _TraceAction( 'Could not create directories for [{rc_path}], so it ' 'cannot be updated.'.format(rc_path=self.rc_path)) return try: files.WriteFileContents(self.rc_path, rc_contents) except (files.Error, IOError, OSError): _TraceAction( 'Could not update [{rc_path}]. Ensure you have write access to ' 'this location.'.format(rc_path=self.rc_path)) return _TraceAction( '[{rc_path}] has been updated.'.format(rc_path=self.rc_path)) _TraceAction( console_io.FormatRequiredUserAction( 'Start a new shell for the changes to take effect.')) screen_reader = properties.VALUES.accessibility.screen_reader.GetBool() prefix = '' if screen_reader else '==> ' if not self.completion_update and self._CompletionExists(): _TraceAction( prefix + 'Source [{rc}] in your profile to enable shell command ' 'completion for gcloud.'.format(rc=self.completion)) if not self.path_update: _TraceAction( prefix + 'Source [{rc}] in your profile to add the Google Cloud SDK ' 'command line tools to your $PATH.'.format(rc=self.path))
def UpdateRC(command_completion, path_update, rc_path, bin_path, sdk_root): """Update the system path to include bin_path. Args: command_completion: bool, Whether or not to do command completion. If None, ask. path_update: bool, Whether or not to update PATH. If None, ask. rc_path: str, The path to the rc file to update. If None, ask. bin_path: str, The absolute path to the directory that will contain Cloud SDK binaries. sdk_root: str, The path to the Cloud SDK root. """ host_os = platforms.OperatingSystem.Current() if host_os == platforms.OperatingSystem.WINDOWS: if path_update is None: path_update = console_io.PromptContinue( prompt_string='Update %PATH% to include Cloud SDK binaries?') if path_update: _UpdatePathForWindows(bin_path) return if command_completion is None: if path_update is None: # Ask only one question if both were not set. path_update = console_io.PromptContinue( prompt_string=('\nModify profile to update your $PATH ' 'and enable shell command completion?')) command_completion = path_update else: command_completion = console_io.PromptContinue( prompt_string=('\nModify profile to enable shell command ' 'completion?')) elif path_update is None: path_update = console_io.PromptContinue( prompt_string=('\nModify profile to update your $PATH?')) rc_paths = _GetRcPaths(command_completion, path_update, rc_path, sdk_root, host_os) if rc_paths.rc_path: if os.path.exists(rc_paths.rc_path): with open(rc_paths.rc_path) as rc_file: rc_data = rc_file.read() cached_rc_data = rc_data else: rc_data = '' cached_rc_data = '' if path_update: rc_data = _GetRcData( '# The next line updates PATH for the Google Cloud' ' SDK.', rc_paths.path, rc_data) if command_completion: rc_data = _GetRcData( '# The next line enables shell command completion' ' for gcloud.', rc_paths.completion, rc_data, pattern='# The next line enables [a-z][a-z]*' ' command completion for gcloud.') if cached_rc_data == rc_data: print( 'No changes necessary for [{rc}].'.format(rc=rc_paths.rc_path)) return if os.path.exists(rc_paths.rc_path): rc_backup = rc_paths.rc_path + '.backup' print('Backing up [{rc}] to [{backup}].'.format( rc=rc_paths.rc_path, backup=rc_backup)) shutil.copyfile(rc_paths.rc_path, rc_backup) with open(rc_paths.rc_path, 'w') as rc_file: rc_file.write(rc_data) print('[{rc_path}] has been updated.'.format(rc_path=rc_paths.rc_path)) print( console_io.FormatRequiredUserAction( 'Start a new shell for the changes to take effect.')) if not command_completion: print( console_io.FormatRequiredUserAction( 'Source [{rc}]in your profile to enable shell command completion for ' 'gcloud.'.format(rc=rc_paths.completion))) if not path_update: print( console_io.FormatRequiredUserAction( 'Source [{rc}] in your profile to add the Google Cloud SDK command ' 'line tools to your $PATH.'.format(rc=rc_paths.path)))