def ProcessOutputParameters(output_file=None, output_dir=None): """Helper function for generating output file and directory.""" output_file = output_file.strip() if output_file else None output_dir = (os.path.abspath(output_dir.strip()) if output_dir else None) dest_file = None dest_dir = None if output_file: if os.path.isfile(output_file): overwrite_prompt = ('{} already exists.'.format(output_file)) console_io.PromptContinue( overwrite_prompt, prompt_string='Do you want to overwrite?', default=True, cancel_string='Aborted script generation.', cancel_on_no=True) dest_file = os.path.basename(output_file) dest_dir = os.path.dirname(output_file) or files.GetCWD() if os.path.isdir(dest_dir) and not files.HasWriteAccessInDir(dest_dir): raise TerraformGenerationError( 'Error writing output file: {} is not writable'.format(dest_dir)) # Output directory. if output_dir: if (os.path.isdir(output_dir) and not files.HasWriteAccessInDir(output_dir)): raise ValueError('Cannot write output to directory {}. ' 'Please check permissions.'.format(output_dir)) dest_file = None dest_dir = output_dir return dest_file, dest_dir
def _CopyIfNotWritable(source_dir, temp_dir): """Returns a writable directory with the same contents as source_dir. If source_dir is writable, it is used. Otherwise, a directory 'dest' inside of temp_dir is used. Args: source_dir: str, the directory to (potentially) copy temp_dir: str, the path to a writable temporary directory in which to store any copied code. Returns: str, the path to a writable directory with the same contents as source_dir (i.e. source_dir, if it's writable, or a copy otherwise). Raises: UploadFailureError: if the command exits non-zero. """ if files.HasWriteAccessInDir(source_dir): return source_dir if files.IsDirAncestorOf(source_dir, temp_dir): raise UncopyablePackageError( 'Cannot copy directory since working directory [{}] is inside of ' 'source directory [{}].'.format(temp_dir, source_dir)) dest_dir = os.path.join(temp_dir, 'dest') log.debug('Copying local source tree from [%s] to [%s]', source_dir, dest_dir) try: files.CopyTree(source_dir, dest_dir) except OSError: raise UncopyablePackageError( 'Cannot write to working location [{}]'.format(dest_dir)) return dest_dir
def _TryCreateOutputPath(self, path): """Try to create output directory if it doesnt exists.""" directory = os.path.abspath(path.strip()) try: if os.path.isdir(directory) and files.HasWriteAccessInDir(directory): return if files.HasWriteAccessInDir(os.path.dirname(directory)): console_io.PromptContinue( 'Path {} does not exists. Do you want to create it?'.format(path), default=True, cancel_on_no=True, cancel_string='Export aborted. No files written.') files.MakeDir(path) else: raise OSError(errno.EACCES) # e.g. ../Path Is not writeable except ValueError: raise ExportPathException('Can not export to path. [{}] is not a ' # pylint: disable=raise-missing-from 'directory.'.format(path)) except OSError: raise ExportPathException('Can not export to path [{}]. ' # pylint: disable=raise-missing-from 'Ensure that enclosing path ' 'exists and is writeable.'.format(path))
def EnsureSDKWriteAccess(sdk_root_override=None): """Error if the current user does not have write access to the sdk root. Args: sdk_root_override: str, The full path to the sdk root to use instead of using config.Paths().sdk_root. Raises: exceptions.RequiresAdminRightsError: If the sdk root is defined and the user does not have write access. """ sdk_root = sdk_root_override or Paths().sdk_root if sdk_root and not file_utils.HasWriteAccessInDir(sdk_root): raise exceptions.RequiresAdminRightsError(sdk_root)