def BuildStartArgs(args, current_os): """Builds the command for starting the pubsub emulator. Args: args: (list of str) The arguments for the pubsub emulator, excluding the program binary. current_os: (platforms.OperatingSystem) Returns: A list of command arguments. """ pubsub_dir = GetPubSubRoot() if current_os is platforms.OperatingSystem.WINDOWS: pubsub_executable = os.path.join(pubsub_dir, r'bin\cloud-pubsub-fake.bat') return execution_utils.ArgsForCMDTool(pubsub_executable, *args) pubsub_executable = os.path.join(pubsub_dir, 'bin/cloud-pubsub-fake') return execution_utils.ArgsForShellTool(pubsub_executable, *args)
def ArgsForGCDEmulator(emulator_args): """Constructs an argument list for calling the GCD emulator. Args: emulator_args: args for the emulator. Returns: An argument list to execute the GCD emulator. """ current_os = platforms.OperatingSystem.Current() if current_os is platforms.OperatingSystem.WINDOWS: cmd = 'cloud_datastore_emulator.cmd' gcd_executable = os.path.join(GetGCDRoot(), cmd) return execution_utils.ArgsForCMDTool(gcd_executable, *emulator_args) else: cmd = 'cloud_datastore_emulator' gcd_executable = os.path.join(GetGCDRoot(), cmd) return execution_utils.ArgsForExecutableTool(gcd_executable, *emulator_args)
def GetArgsForScript(script_name, args): """Returns a list of args for execution of a cloud CLI wrapper. This wrapper must be a sh script on non-windows, or a .cmd file, without the '.cmd' extension (so it has the same script_name as non-windows), on windows. Args: script_name: str, The name of the CLI wrapper. For linux, it must be the whole name. On windows, it is the name except for the trailing '.cmd'. args: [str], The arguments for the script itself. Returns: [str], A list of args for the process executor. """ if _IsOnWindows(): return execution_utils.ArgsForCMDTool(FullPath(script_name + '.cmd'), *args) else: return execution_utils.ArgsForExecutableTool(FullPath(script_name), *args)
def RunGsutilCommand(command_name, command_args=None, run_concurrent=False, out_func=log.file_only_logger.debug, err_func=log.file_only_logger.debug): """Runs the specified gsutil command and returns the command's exit code. This is more reliable than storage_api.StorageClient.CopyFilesToGcs especially for large files. Args: command_name: The gsutil command to run. command_args: List of arguments to pass to the command. run_concurrent: Whether concurrent uploads should be enabled while running the command. out_func: str->None, a function to call with the stdout of the gsutil command. err_func: str->None, a function to call with the stderr of the gsutil command. Returns: The exit code of the call to the gsutil command. """ command_path = _GetGsutilPath() args = ['-m', command_name] if run_concurrent else [command_name] if command_args is not None: args += command_args if platforms.OperatingSystem.Current( ) == platforms.OperatingSystem.WINDOWS: gsutil_args = execution_utils.ArgsForCMDTool(command_path + '.cmd', *args) else: gsutil_args = execution_utils.ArgsForExecutableTool( command_path, *args) log.debug('Running command: [{args}]]'.format(args=' '.join(gsutil_args))) return execution_utils.Exec(gsutil_args, no_exit=True, out_func=out_func, err_func=err_func)
def _CreateBotoConfig(self): gsutil_path = _FindGsutil() if not gsutil_path: log.debug('Unable to find [gsutil]. Not configuring default .boto ' 'file') return boto_path = files.ExpandHomeDir(os.path.join('~', '.boto')) if os.path.exists(boto_path): log.debug('Not configuring default .boto file. File already ' 'exists at [{boto_path}].'.format(boto_path=boto_path)) return # 'gsutil config -n' creates a default .boto file that the user can read and # modify. command_args = ['config', '-n', '-o', boto_path] if platforms.OperatingSystem.Current( ) == platforms.OperatingSystem.WINDOWS: gsutil_args = execution_utils.ArgsForCMDTool( gsutil_path, *command_args) else: gsutil_args = execution_utils.ArgsForExecutableTool( gsutil_path, *command_args) return_code = execution_utils.Exec(gsutil_args, no_exit=True, out_func=log.file_only_logger.debug, err_func=log.file_only_logger.debug) if return_code == 0: log.status.write("""\ Created a default .boto configuration file at [{boto_path}]. See this file and [https://cloud.google.com/storage/docs/gsutil/commands/config] for more information about configuring Google Cloud Storage. """.format(boto_path=boto_path)) else: log.status.write( 'Error creating a default .boto configuration file. ' 'Please run [gsutil config -n] if you would like to ' 'create this file.\n')
def GetArgsForLegacyScript(script_name, args, interpreter=None): """Returns a list of args for execution of the given script. Args: script_name: The name of the script to run. The extension determines which interpreter will be used for execution. Python will be used for .py. If there is no extension it is assumed it is just a binary file and is executed directly. .exe files are executed directly on Windows, or error raised if not on Windows. If it ends with a single dot, it sees what OS it is running on and either executes .cmd or .sh. args: The arguments for the script itself interpreter: an interpreter to use rather than trying to derive it from the extension name. The value is the same as an extension, right now only implemented for 'py'. Returns: a list of args for the process executor Raises: ValueError: if the script type cannot be determined or is invalid for OS """ (_, tail) = os.path.splitext(script_name) # No extension, just try to execute it if not tail and not interpreter: # In our tests, users generally just refer to 'gcloud'. But in windows, # there is no 'gcloud' file, there is gcloud.cmd. This isn't an issue on the # shell because the shell, given <name>, is smart enough to detect that # there is a file <name>.cmd. subprocess.Popen is not smart enough to know # this, however, unless shell=True is given. We'd like to avoid that if # possible, however. if _IsOnWindows() and script_name == 'gcloud': script_name = 'gcloud.cmd' return execution_utils.ArgsForExecutableTool(FullPath(script_name), *args) # Strip the '.' if interpreter: ext = interpreter else: ext = tail[1:] # Python, same across platforms if ext == 'py': return execution_utils.ArgsForPythonTool(FullPath(script_name), *args) # .exe, windows only if ext == 'exe': if not _IsOnWindows(): raise ValueError('Extention for {0} is only valid for WINDOWS'.format( script_name)) return execution_utils.ArgsForExecutableTool(FullPath(script_name), *args) # ending in a '.' if not ext: if _IsOnWindows(): return execution_utils.ArgsForCMDTool( FullPath(script_name + 'cmd'), *args) else: return execution_utils.ArgsForExecutableTool( FullPath(script_name + 'sh'), *args) else: raise ValueError('Unknown script type: {0}'.format(script_name))