Пример #1
0
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
Пример #2
0
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
Пример #3
0
    def testZipDirFullWithFilter(self):
        def RegexPredicate(f):
            return not re.match(
                r'empty_dir|full_dir{}.*'.format(re.escape(os.sep)), f)

        with files.TemporaryDirectory() as src_dir:
            os.makedirs(os.path.join(src_dir, 'empty_dir'))
            with open(os.path.join(src_dir, 'sample.txt'), 'a'):
                pass
            full_dir = os.path.join(src_dir, 'full_dir')
            os.makedirs(full_dir)
            with open(os.path.join(full_dir, 'sample1.txt'), 'a'):
                pass
            with open(os.path.join(full_dir, 'sample2.txt'), 'a') as f:
                f.write('Hello')
            full_directory = os.path.join(src_dir, 'full_directory')
            os.makedirs(full_directory)
            with open(os.path.join(full_directory, 'sample1.txt'), 'a'):
                pass
            with open(os.path.join(full_directory, 'sample2.txt'), 'a') as f:
                f.write('Hello')
            with files.TemporaryDirectory() as dst_dir:
                zip_file = os.path.join(dst_dir, 'arch.zip')
                archive.MakeZipFromDir(zip_file, src_dir, RegexPredicate)
                zf = zipfile.ZipFile(zip_file)
                try:
                    self.assertIsNone(zf.testzip())
                    name_list = [(name, zf.read(name))
                                 for name in zf.namelist()]
                finally:
                    zf.close()
        self.assertEqual([('full_dir/', b''), ('full_directory/', b''),
                          ('full_directory/sample1.txt', b''),
                          ('full_directory/sample2.txt', b'Hello'),
                          ('sample.txt', b'')], sorted(name_list))
Пример #4
0
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 _CreateZipFile(self, tmp_dir, args):
   zip_file_name = os.path.join(tmp_dir, 'fun.zip')
   try:
     archive.MakeZipFromDir(zip_file_name, args.source)
   except ValueError as e:
     raise exceptions.FunctionsError('Error creating a ZIP archive'
                                     ' with the source code: ' + str(e))
   return zip_file_name
Пример #6
0
 def _CreateZipFile(self, tmp_dir, args):
     zip_file_name = os.path.join(tmp_dir, 'fun.zip')
     local_path = deploy_util.GetLocalPath(args)
     try:
         archive.MakeZipFromDir(zip_file_name, local_path)
     except ValueError as e:
         raise exceptions.FunctionsError(
             'Error creating a ZIP archive with the source code '
             'for directory {0}: {1}'.format(local_path, str(e)))
     return zip_file_name
Пример #7
0
 def _CreateZipFile(self, tmp_dir, args):
     zip_file_name = os.path.join(tmp_dir, 'fun.zip')
     local_path = deploy_util.GetLocalPath(args)
     try:
         if args.include_node_modules:
             archive.MakeZipFromDir(zip_file_name, local_path)
         else:
             log.info(
                 'Not including node_modules in deployed code. To include '
                 'node_modules in uploaded code use --include-node-modules '
                 'flag.')
             archive.MakeZipFromDir(
                 zip_file_name,
                 local_path,
                 skip_file_regex=re.escape('node_modules' + os.sep))
     except ValueError as e:
         raise exceptions.FunctionsError(
             'Error creating a ZIP archive with the source code '
             'for directory {0}: {1}'.format(local_path, str(e)))
     return zip_file_name
Пример #8
0
 def _MakeZip(self, src_dir, update_date=False):
   with files.TemporaryDirectory() as dst_dir:
     zip_file = os.path.join(dst_dir, 'arch.zip')
     archive.MakeZipFromDir(zip_file, src_dir, update_date=update_date)
     zf = zipfile.ZipFile(zip_file)
     try:
       self.assertIsNone(zf.testzip())
       name_list = [(name, zf.read(name)) for name in zf.namelist()]
     finally:
       zf.close()
   return name_list
Пример #9
0
def _GetSourceLocal(client, messages, region, function_name, source,
                    stage_bucket_arg, ignore_file_arg):
    """Constructs a `Source` message from a local file system path.

  Args:
    client: The GCFv2 API client
    messages: messages module, the GCFv2 message stubs
    region: str, the region to deploy the function to
    function_name: str, the name of the function
    source: str, the path
    stage_bucket_arg: str, the passed in --stage-bucket flag argument
    ignore_file_arg: str, the passed in --ignore-file flag argument

  Returns:
    function_source: cloud.functions.v2main.Source
  """
    with file_utils.TemporaryDirectory() as tmp_dir:
        zip_file_path = os.path.join(tmp_dir, 'fun.zip')
        chooser = gcloudignore.GetFileChooserForDir(
            source,
            default_ignore_file=_DEFAULT_IGNORE_FILE,
            gcloud_ignore_creation_predicate=_GcloudIgnoreCreationPredicate,
            ignore_file=ignore_file_arg)
        archive.MakeZipFromDir(zip_file_path,
                               source,
                               predicate=chooser.IsIncluded)

        if stage_bucket_arg:
            dest_object = _UploadToStageBucket(region, function_name,
                                               zip_file_path, stage_bucket_arg)
            return messages.Source(storageSource=messages.StorageSource(
                bucket=dest_object.bucket, object=dest_object.name))
        else:
            dest = client.projects_locations_functions.GenerateUploadUrl(
                messages.
                CloudfunctionsProjectsLocationsFunctionsGenerateUploadUrlRequest(
                    generateUploadUrlRequest=messages.GenerateUploadUrlRequest(
                    ),
                    parent='projects/%s/locations/%s' %
                    (properties.VALUES.core.project.GetOrFail(), region)))

            _UploadToGeneratedUrl(zip_file_path, dest.uploadUrl)

            return messages.Source(storageSource=dest.storageSource)
Пример #10
0
 def _CreateZipFile(self, tmp_dir, args):
     zip_file_name = os.path.join(tmp_dir, 'fun.zip')
     archive.MakeZipFromDir(zip_file_name, args.source)
     return zip_file_name
Пример #11
0
 def Zip(self):
     """Creates a zip archive of the specified directory."""
     dst_file = os.path.join(self._tmp_dir.path, self._ARCHIVE_FILE_NAME)
     archive.MakeZipFromDir(dst_file, self._src_dir)
     return dst_file
def MakeZip(src_dir, dst_dir, name):
    zip_pkg = os.path.join(dst_dir, name + '.zip')
    archive.MakeZipFromDir(zip_pkg, src_dir)
    return zip_pkg