def _CreateSourcesZipFile(zip_dir, source_path, ignore_file=None): """Prepare zip file with source of the function to upload. Args: zip_dir: str, directory in which zip file will be located. Name of the file will be `fun.zip`. source_path: str, directory containing the sources to be zipped. ignore_file: custom ignore_file name. Override .gcloudignore file to customize files to be skipped. Returns: Path to the zip file (str). Raises: FunctionsError """ api_util.ValidateDirectoryExistsOrRaiseFunctionError(source_path) if ignore_file and not os.path.exists( os.path.join(source_path, ignore_file)): raise exceptions.FileNotFoundError( 'File {0} referenced by --ignore-file ' 'does not exist.'.format(ignore_file)) _ValidateUnpackedSourceSize(source_path, ignore_file) zip_file_name = os.path.join(zip_dir, 'fun.zip') try: chooser = _GetChooser(source_path, ignore_file) predicate = chooser.IsIncluded archive.MakeZipFromDir(zip_file_name, source_path, predicate=predicate) except ValueError as e: raise exceptions.FunctionsError( 'Error creating a ZIP archive with the source code ' 'for directory {0}: {1}'.format(source_path, six.text_type(e))) return zip_file_name
def _CheckArgs(self, args): # This function should raise ArgumentParsingError, but: # 1. ArgumentParsingError requires the argument returned from add_argument) # and Args() method is static. So there is no elegant way to save it # to be reused here. # 2. _CheckArgs() is invoked from Run() and ArgumentParsingError thrown # from Run are not caught. if args.source_url is None: if args.source_revision is not None: raise exceptions.FunctionsError( 'argument --source-revision: can be given only if argument ' '--source-url is provided') if args.source_branch is not None: raise exceptions.FunctionsError( 'argument --source-branch: can be given only if argument ' '--source-url is provided') if args.source_tag is not None: raise exceptions.FunctionsError( 'argument --source-tag: can be given only if argument ' '--source-url is provided') if args.source is None: args.source = '.' if args.bucket is None: raise exceptions.FunctionsError( 'argument --bucket: required when the function is deployed from' ' a local directory (when argument --source-url is not provided)') util.ValidateDirectoryExistsOrRaiseFunctionError(args.source) else: if args.source is None: raise exceptions.FunctionsError( 'argument --source: required when argument --source-url is ' 'provided')
def _ValidateSourceArgs(args): """Check if args related to source code to deploy are valid. Args: args: parsed command line arguments. Raises: FunctionsError. """ if args.source_url is None: if args.source_revision is not None: raise exceptions.FunctionsError( 'argument --source-revision: can be given only if argument ' '--source-url is provided') if args.source_branch is not None: raise exceptions.FunctionsError( 'argument --source-branch: can be given only if argument ' '--source-url is provided') if args.source_tag is not None: raise exceptions.FunctionsError( 'argument --source-tag: can be given only if argument ' '--source-url is provided') if args.stage_bucket is None: raise exceptions.FunctionsError( 'argument --stage-bucket: required when the function is deployed ' 'from a local directory (when argument --source-url is not ' 'provided)') util.ValidateDirectoryExistsOrRaiseFunctionError(GetLocalPath(args)) else: if args.source_path is None: raise exceptions.FunctionsError( 'argument --source-path: required when argument --source-url is ' 'provided')
def CreateSourcesZipFile(zip_dir, source_path, include_ignored_files): """Prepare zip file with source of the function to upload. Args: zip_dir: str, directory in which zip file will be located. Name of the file will be `fun.zip`. source_path: str, directory containing the sources to be zipped. include_ignored_files: bool, indicates whether `node_modules` directory and its content will be included in the zip. Returns: Path to the zip file (str). Raises: FunctionsError """ util.ValidateDirectoryExistsOrRaiseFunctionError(source_path) _ValidateUnpackedSourceSize(source_path, include_ignored_files) zip_file_name = os.path.join(zip_dir, 'fun.zip') try: if include_ignored_files: log.info( 'Not including node_modules in deployed code. To include ' 'node_modules in uploaded code use --include-ignored-files ' 'flag.') archive.MakeZipFromDir( zip_file_name, source_path, skip_file_regex=GetIgnoreFilesRegex(include_ignored_files)) except ValueError as e: raise exceptions.FunctionsError( 'Error creating a ZIP archive with the source code ' 'for directory {0}: {1}'.format(source_path, str(e))) return zip_file_name
def CreateSourcesZipFile(zip_dir, source_path, include_ignored_files): """Prepare zip file with source of the function to upload. Args: zip_dir: str, directory in which zip file will be located. Name of the file will be `fun.zip`. source_path: str, directory containing the sources to be zipped. include_ignored_files: bool, indicates whether `node_modules` directory and its content will be included in the zip. Returns: Path to the zip file (str). Raises: FunctionsError """ util.ValidateDirectoryExistsOrRaiseFunctionError(source_path) _ValidateUnpackedSourceSize(source_path, include_ignored_files) zip_file_name = os.path.join(zip_dir, 'fun.zip') try: if include_ignored_files: predicate = None else: chooser = _GetChooser(source_path) predicate = chooser.IsIncluded archive.MakeZipFromDir(zip_file_name, source_path, predicate=predicate) except ValueError as e: raise exceptions.FunctionsError( 'Error creating a ZIP archive with the source code ' 'for directory {0}: {1}'.format(source_path, str(e))) return zip_file_name
def DeduceAndCheckArgs(args): """Check command arguments and deduce information if possible. 0. Check if --source-revision, --source-branch or --source-tag are present when --source-url is not present. (and fail if it is so) 1. Check if --source-bucket is present when --source-url is present. 2. Validate if local-path is a directory. 3. Check if --source-path is present when --source-url is present. 4. Check if --trigger-event, --trigger-resource or --trigger-params are present when --trigger-provider is not present. (and fail if it is so) 5. Check --trigger-* family of flags deducing default values if possible and necessary. Args: args: The argument namespace. Returns: args with all implicit information turned into explicit form. """ # This function should raise ArgumentParsingError, but: # 1. ArgumentParsingError requires the argument returned from add_argument) # and Args() method is static. So there is no elegant way to save it # to be reused here. # 2. _CheckArgs() is invoked from Run() and ArgumentParsingError thrown # from Run are not caught. if args.source_url is None: if args.source_revision is not None: raise exceptions.FunctionsError( 'argument --source-revision: can be given only if argument ' '--source-url is provided') if args.source_branch is not None: raise exceptions.FunctionsError( 'argument --source-branch: can be given only if argument ' '--source-url is provided') if args.source_tag is not None: raise exceptions.FunctionsError( 'argument --source-tag: can be given only if argument ' '--source-url is provided') stage_bucket = args.stage_bucket if stage_bucket is None: raise exceptions.FunctionsError( 'argument --stage-bucket: required when the function is deployed ' 'from a local directory (when argument --source-url is not ' 'provided)') util.ValidateDirectoryExistsOrRaiseFunctionError(GetLocalPath(args)) else: if args.source_path is None: raise exceptions.FunctionsError( 'argument --source-path: required when argument --source-url is ' 'provided') if args.trigger_provider is None and ((args.trigger_event is not None) or (args.trigger_resource is not None) or (args.trigger_params is not None)): raise exceptions.FunctionsError( '--trigger-event, --trigger-resource, and --trigger-params may only ' 'be used with --trigger-provider') if args.trigger_provider is not None: return _CheckTriggerProviderArgs(args)
def _CheckArgs(self, args): # This function should raise ArgumentParsingError, but: # 1. ArgumentParsingError requires the argument returned from add_argument) # and Args() method is static. So there is no elegant way to save it # to be reused here. # 2. _CheckArgs() is invoked from Run() and ArgumentParsingError thrown # from Run are not caught. if args.source_url is None: if args.source_revision is not None: raise exceptions.FunctionsError( 'argument --source-revision: can be given only if argument ' '--source-url is provided') if args.source_branch is not None: raise exceptions.FunctionsError( 'argument --source-branch: can be given only if argument ' '--source-url is provided') if args.source_tag is not None: raise exceptions.FunctionsError( 'argument --source-tag: can be given only if argument ' '--source-url is provided') stage_bucket = args.bucket or args.stage_bucket if stage_bucket is None: raise exceptions.FunctionsError( 'argument --stage-bucket: required when the function is deployed ' 'from a local directory (when argument --source-url is not ' 'provided)') util.ValidateDirectoryExistsOrRaiseFunctionError( self._GetLocalPath(args)) else: if args.source is None and args.source_path is None: raise exceptions.FunctionsError( 'argument --source-path: required when argument --source-url is ' 'provided') if args.bucket is not None: log.warn( '--bucket flag is deprecated. Use --stage-bucket instead.') if args.source is not None: log.warn( '--source flag is deprecated. Use --local-path (for sources on ' 'local file system) or --source-path (for sources in Cloud ' 'Source Repositories) instead.') if args.trigger_gs_uri is not None: log.warn( '--trigger-gs-uri flag is deprecated. Use --trigger-bucket ' 'instead.')
def _ValidateSourceArgs(args): """Check if args related to source code to deploy are valid. Args: args: parsed command line arguments. Raises: FunctionsError. """ if args.source_url is None: if args.source_revision is not None: raise exceptions.FunctionsError( 'argument --source-revision: can be given only if argument ' '--source-url is provided') if args.source_branch is not None: raise exceptions.FunctionsError( 'argument --source-branch: can be given only if argument ' '--source-url is provided') if args.source_tag is not None: raise exceptions.FunctionsError( 'argument --source-tag: can be given only if argument ' '--source-url is provided') stage_bucket = args.bucket or args.stage_bucket if stage_bucket is None: raise exceptions.FunctionsError( 'argument --stage-bucket: required when the function is deployed ' 'from a local directory (when argument --source-url is not ' 'provided)') util.ValidateDirectoryExistsOrRaiseFunctionError(GetLocalPath(args)) else: if args.source is None and args.source_path is None: raise exceptions.FunctionsError( 'argument --source-path: required when argument --source-url is ' 'provided') if args.bucket is not None: log.warn('--bucket flag is deprecated. Use --stage-bucket instead.') if args.source is not None: log.warn( '--source flag is deprecated. Use --local-path (for sources on ' 'local file system) or --source-path (for sources in Cloud ' 'Source Repositories) instead.')