tools/bisect_repackage/bisect_repackage_utils.py
def MakeZip(output_dir, archive_name, file_list, file_relative_dir, dir_in_zip,
            raise_error=True, remove_archive_directory=True, strip_files=None,
            ignore_sub_folder=False):
  """Packs files into a new zip archive.

  Files are first copied into a directory within the output_dir named for
  the archive_name, which will be created if necessary and emptied if it
  already exists.  The files are then then packed using archive names
  relative to the output_dir.  That is, if the zipfile is unpacked in place,
  it will create a directory identical to the new archive_name directory, in
  the output_dir.  The zip file will be named as the archive_name, plus
  '.zip'.

  Args:
    output_dir: Absolute path to the directory in which the archive is to
      be created.
    archive_dir: Subdirectory of output_dir holding files to be added to
      the new zipfile.
    file_list: List of paths to files or subdirectories, relative to the
      file_relative_dir.
    file_relative_dir: Absolute path to the directory containing the files
      and subdirectories in the file_list.
    dir_in_zip: Directory where the files are archived into.
    raise_error: Whether to raise a PathNotFound error if one of the files in
      the list is not found.
    remove_archive_directory: Whether to remove the archive staging directory
      before copying files over to it.
    strip_files: List of executable files to strip symbols when zipping

  Returns:
    A tuple consisting of (archive_dir, zip_file_path), where archive_dir
    is the full path to the newly created archive_name subdirectory.

  Raises:
    PathNotFound if any of the files in the list is not found, unless
    raise_error is False, in which case the error will be ignored.
  """

  start_time = time.clock()
  # Collect files into the archive directory.
  archive_dir = os.path.join(output_dir, dir_in_zip)
  print('output_dir: %s, archive_name: %s' % (output_dir, archive_name))
  print('archive_dir: %s, remove_archive_directory: %s, exists: %s' %
        (archive_dir, remove_archive_directory, os.path.exists(archive_dir)))
  if remove_archive_directory and os.path.exists(archive_dir):
    # Move it even if it's not a directory as expected. This can happen with
    # FILES.cfg archive creation where we create an archive staging directory
    # that is the same name as the ultimate archive name.
    if not os.path.isdir(archive_dir):
      print('Moving old "%s" file to create same name directory.' % archive_dir)
      previous_archive_file = '%s.old' % archive_dir
      MoveFile(archive_dir, previous_archive_file)
    else:
      print('Removing %s' % archive_dir)
      RemoveDirectory(archive_dir)
      print('Now, os.path.exists(%s): %s' % (archive_dir,
                                             os.path.exists(archive_dir)))
  MaybeMakeDirectory(archive_dir)
  for needed_file in file_list:
    needed_file = needed_file.rstrip()
    # These paths are relative to the file_relative_dir.  We need to copy
    # them over maintaining the relative directories, where applicable.
    src_path = os.path.join(file_relative_dir, needed_file)
    dirname, basename = os.path.split(needed_file)
    try:
      if os.path.isdir(src_path):
        if WIN_LINK_FUNC:
          WIN_LINK_FUNC(src_path, os.path.join(archive_dir, needed_file))
        else:
          shutil.copytree(src_path, os.path.join(archive_dir, needed_file),
                          symlinks=True)
      elif dirname != '' and basename != '':
        dest_dir = os.path.join(archive_dir, dirname)
        MaybeMakeDirectory(dest_dir)
        CopyFileToDir(src_path, dest_dir, basename, link_ok=True)
        if strip_files and basename in strip_files:
          cmd = ['strip', os.path.join(dest_dir, basename)]
          RunCommand(cmd)
      else:
        CopyFileToDir(src_path, archive_dir, basename, link_ok=True)
        if strip_files and basename in strip_files:
          cmd = ['strip', os.path.join(archive_dir, basename)]
          RunCommand(cmd)
    except PathNotFound:
      if raise_error:
        raise
  end_time = time.clock()
  print(
      'Took %f seconds to create archive directory.' % (end_time - start_time))

  # Pack the zip file.
  output_file = os.path.join(output_dir, '%s.zip' % archive_name)
  previous_file = os.path.join(output_dir, '%s_old.zip' % archive_name)
  MoveFile(output_file, previous_file)

  # If we have 7z, use that as it's much faster. See http://crbug.com/418702.
  windows_zip_cmd = None
  if os.path.exists('C:\\Program Files\\7-Zip\\7z.exe'):
    windows_zip_cmd = ['C:\\Program Files\\7-Zip\\7z.exe', 'a', '-y', '-mx1']

  # On Windows we use the python zip module; on Linux and Mac, we use the zip
  # command as it will handle links and file bits (executable).  Which is much
  # easier then trying to do that with ZipInfo options.
  start_time = time.clock()
  if IsWindows() and not windows_zip_cmd:
    print('Creating %s' % output_file)

    def _Addfiles(to_zip_file, dirname, files_to_add):
      for this_file in files_to_add:
        archive_name = this_file
        this_path = os.path.join(dirname, this_file)
        if os.path.isfile(this_path):
          # Store files named relative to the outer output_dir.
          archive_name = this_path.replace(output_dir + os.sep, '')
          if os.path.getsize(this_path) == 0:
            compress_method = zipfile.ZIP_STORED
          else:
            compress_method = zipfile.ZIP_DEFLATED
          to_zip_file.write(this_path, archive_name, compress_method)
          print('Adding %s' % archive_name)

    zip_file = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED,
                               allowZip64=True)
    try:
      os.path.walk(archive_dir, _Addfiles, zip_file)
    finally:
      zip_file.close()
  else:
    if IsMac() or IsLinux():
      zip_cmd = ['zip', '-yr1']
    else:
      zip_cmd = windows_zip_cmd
    if ignore_sub_folder:
      zip_cmd.extend(['-j'])
    saved_dir = os.getcwd()
    os.chdir(os.path.dirname(archive_dir))
    command = zip_cmd + [output_file, os.path.basename(archive_dir)]
    result = RunCommand(command)
    os.chdir(saved_dir)
    if result and raise_error:
      raise ExternalError('zip failed: %s => %s' %
                          (str(command), result))
  end_time = time.clock()
  print('Took %f seconds to create zip.' % (end_time - start_time))
  return (archive_dir, output_file)
Packs files into a new zip archive.

  Files are first copied into a directory within the output_dir named for
  the archive_name, which will be created if necessary and emptied if it
  already exists.  The files are then then packed using archive names
  relative to the output_dir.  That is, if the zipfile is unpacked in place,
  it will create a directory identical to the new archive_name directory, in
  the output_dir.  The zip file will be named as the archive_name, plus
  '.zip'.

  Args:
    output_dir: Absolute path to the directory in which the archive is to
      be created.
    archive_dir: Subdirectory of output_dir holding files to be added to
      the new zipfile.
    file_list: List of paths to files or subdirectories, relative to the
      file_relative_dir.
    file_relative_dir: Absolute path to the directory containing the files
      and subdirectories in the file_list.
    dir_in_zip: Directory where the files are archived into.
    raise_error: Whether to raise a PathNotFound error if one of the files in
      the list is not found.
    remove_archive_directory: Whether to remove the archive staging directory
      before copying files over to it.
    strip_files: List of executable files to strip symbols when zipping

  Returns:
    A tuple consisting of (archive_dir, zip_file_path), where archive_dir
    is the full path to the newly created archive_name subdirectory.

  Raises:
    PathNotFound if any of the files in the list is not found, unless
    raise_error is False, in which case the error will be ignored.
  
Packs files into a new zip archive.
Files are first copied into a directory within the output_dir named for
the archive_name, which will be created if necessary and emptied if it
already exists.  The files are then then packed using archive names
relative to the output_dir.  That is, if the zipfile is unpacked in place,
it will create a directory identical to the new archive_name directory, in
the output_dir.  The zip file will be named as the archive_name, plus
'.zip'.

Absolute path to the directory in which the archive is to
be created.
archive_dir: Subdirectory of output_dir holding files to be added to
the new zipfile.
file_list: List of paths to files or subdirectories, relative to the
file_relative_dir.
file_relative_dir: Absolute path to the directory containing the files
and subdirectories in the file_list.
dir_in_zip: Directory where the files are archived into.
raise_error: Whether to raise a PathNotFound error if one of the files in
the list is not found.
remove_archive_directory: Whether to remove the archive staging directory
before copying files over to it.
strip_files: List of executable files to strip symbols when zipping

A tuple consisting of (archive_dir, zip_file_path), where archive_dir
is the full path to the newly created archive_name subdirectory.

PathNotFound if any of the files in the list is not found, unless
raise_error is False, in which case the error will be ignored.
Packs files into a new zip archive . Files are first copied into a directory within the output_dir named for the archive_name which will be created if necessary and emptied if it already exists . The files are then then packed using archive names relative to the output_dir . That is if the zipfile is unpacked in place it will create a directory identical to the new archive_name directory in the output_dir . The zip file will be named as the archive_name plus ' . zip ' . Absolute path to the directory in which the archive is to be created . archive_dir : Subdirectory of output_dir holding files to be added to the new zipfile . file_list : List of paths to files or subdirectories relative to the file_relative_dir . file_relative_dir : Absolute path to the directory containing the files and subdirectories in the file_list . dir_in_zip : Directory where the files are archived into . raise_error : Whether to raise a PathNotFound error if one of the files in the list is not found . remove_archive_directory : Whether to remove the archive staging directory before copying files over to it . strip_files : List of executable files to strip symbols when zipping A tuple consisting of ( archive_dir zip_file_path ) where archive_dir is the full path to the newly created archive_name subdirectory . PathNotFound if any of the files in the list is not found unless raise_error is False in which case the error will be ignored .
def MakeZip(output_dir, archive_name, file_list, file_relative_dir, dir_in_zip,
            raise_error=True, remove_archive_directory=True, strip_files=None,
            ignore_sub_folder=False):
  start_time = time.clock()
  archive_dir = os.path.join(output_dir, dir_in_zip)
  print('output_dir: %s, archive_name: %s' % (output_dir, archive_name))
  print('archive_dir: %s, remove_archive_directory: %s, exists: %s' %
        (archive_dir, remove_archive_directory, os.path.exists(archive_dir)))
  if remove_archive_directory and os.path.exists(archive_dir):
    if not os.path.isdir(archive_dir):
      print('Moving old "%s" file to create same name directory.' % archive_dir)
      previous_archive_file = '%s.old' % archive_dir
      MoveFile(archive_dir, previous_archive_file)
    else:
      print('Removing %s' % archive_dir)
      RemoveDirectory(archive_dir)
      print('Now, os.path.exists(%s): %s' % (archive_dir,
                                             os.path.exists(archive_dir)))
  MaybeMakeDirectory(archive_dir)
  for needed_file in file_list:
    needed_file = needed_file.rstrip()
    src_path = os.path.join(file_relative_dir, needed_file)
    dirname, basename = os.path.split(needed_file)
    try:
      if os.path.isdir(src_path):
        if WIN_LINK_FUNC:
          WIN_LINK_FUNC(src_path, os.path.join(archive_dir, needed_file))
        else:
          shutil.copytree(src_path, os.path.join(archive_dir, needed_file),
                          symlinks=True)
      elif dirname != '' and basename != '':
        dest_dir = os.path.join(archive_dir, dirname)
        MaybeMakeDirectory(dest_dir)
        CopyFileToDir(src_path, dest_dir, basename, link_ok=True)
        if strip_files and basename in strip_files:
          cmd = ['strip', os.path.join(dest_dir, basename)]
          RunCommand(cmd)
      else:
        CopyFileToDir(src_path, archive_dir, basename, link_ok=True)
        if strip_files and basename in strip_files:
          cmd = ['strip', os.path.join(archive_dir, basename)]
          RunCommand(cmd)
    except PathNotFound:
      if raise_error:
        raise
  end_time = time.clock()
  print(
      'Took %f seconds to create archive directory.' % (end_time - start_time))
  output_file = os.path.join(output_dir, '%s.zip' % archive_name)
  previous_file = os.path.join(output_dir, '%s_old.zip' % archive_name)
  MoveFile(output_file, previous_file)
  windows_zip_cmd = None
  if os.path.exists('C:\\Program Files\\7-Zip\\7z.exe'):
    windows_zip_cmd = ['C:\\Program Files\\7-Zip\\7z.exe', 'a', '-y', '-mx1']
  start_time = time.clock()
  if IsWindows() and not windows_zip_cmd:
    print('Creating %s' % output_file)
    def _Addfiles(to_zip_file, dirname, files_to_add):
      for this_file in files_to_add:
        archive_name = this_file
        this_path = os.path.join(dirname, this_file)
        if os.path.isfile(this_path):
          archive_name = this_path.replace(output_dir + os.sep, '')
          if os.path.getsize(this_path) == 0:
            compress_method = zipfile.ZIP_STORED
          else:
            compress_method = zipfile.ZIP_DEFLATED
          to_zip_file.write(this_path, archive_name, compress_method)
          print('Adding %s' % archive_name)
    zip_file = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED,
                               allowZip64=True)
    try:
      os.path.walk(archive_dir, _Addfiles, zip_file)
    finally:
      zip_file.close()
  else:
    if IsMac() or IsLinux():
      zip_cmd = ['zip', '-yr1']
    else:
      zip_cmd = windows_zip_cmd
    if ignore_sub_folder:
      zip_cmd.extend(['-j'])
    saved_dir = os.getcwd()
    os.chdir(os.path.dirname(archive_dir))
    command = zip_cmd + [output_file, os.path.basename(archive_dir)]
    result = RunCommand(command)
    os.chdir(saved_dir)
    if result and raise_error:
      raise ExternalError('zip failed: %s => %s' %
                          (str(command), result))
  end_time = time.clock()
  print('Took %f seconds to create zip.' % (end_time - start_time))
  return (archive_dir, output_file)
def MakeZip ( output_dir , archive_name , file_list , file_relative_dir , dir_in_zip , raise_error = True , remove_archive_directory = True , strip_files = None , ignore_sub_folder = False ) : start_time = time . clock ( ) archive_dir = os . path . join ( output_dir , dir_in_zip ) print ( 'output_dir: %s, archive_name: %s' % ( output_dir , archive_name ) ) print ( 'archive_dir: %s, remove_archive_directory: %s, exists: %s' % ( archive_dir , remove_archive_directory , os . path . exists ( archive_dir ) ) ) if remove_archive_directory and os . path . exists ( archive_dir ) : if not os . path . isdir ( archive_dir ) : print ( 'Moving old "%s" file to create same name directory.' % archive_dir ) previous_archive_file = '%s.old' % archive_dir MoveFile ( archive_dir , previous_archive_file ) else : print ( 'Removing %s' % archive_dir ) RemoveDirectory ( archive_dir ) print ( 'Now, os.path.exists(%s): %s' % ( archive_dir , os . path . exists ( archive_dir ) ) ) MaybeMakeDirectory ( archive_dir ) for needed_file in file_list : needed_file = needed_file . rstrip ( ) src_path = os . path . join ( file_relative_dir , needed_file ) dirname , basename = os . path . split ( needed_file ) try : if os . path . isdir ( src_path ) : if WIN_LINK_FUNC : WIN_LINK_FUNC ( src_path , os . path . join ( archive_dir , needed_file ) ) else : shutil . copytree ( src_path , os . path . join ( archive_dir , needed_file ) , symlinks = True ) elif dirname != '' and basename != '' : dest_dir = os . path . join ( archive_dir , dirname ) MaybeMakeDirectory ( dest_dir ) CopyFileToDir ( src_path , dest_dir , basename , link_ok = True ) if strip_files and basename in strip_files : cmd = [ 'strip' , os . path . join ( dest_dir , basename ) ] RunCommand ( cmd ) else : CopyFileToDir ( src_path , archive_dir , basename , link_ok = True ) if strip_files and basename in strip_files : cmd = [ 'strip' , os . path . join ( archive_dir , basename ) ] RunCommand ( cmd ) except PathNotFound : if raise_error : raise end_time = time . clock ( ) print ( 'Took %f seconds to create archive directory.' % ( end_time - start_time ) ) output_file = os . path . join ( output_dir , '%s.zip' % archive_name ) previous_file = os . path . join ( output_dir , '%s_old.zip' % archive_name ) MoveFile ( output_file , previous_file ) windows_zip_cmd = None if os . path . exists ( 'C:\\Program Files\\7-Zip\\7z.exe' ) : windows_zip_cmd = [ 'C:\\Program Files\\7-Zip\\7z.exe' , 'a' , '-y' , '-mx1' ] start_time = time . clock ( ) if IsWindows ( ) and not windows_zip_cmd : print ( 'Creating %s' % output_file ) def _Addfiles ( to_zip_file , dirname , files_to_add ) : for this_file in files_to_add : archive_name = this_file this_path = os . path . join ( dirname , this_file ) if os . path . isfile ( this_path ) : archive_name = this_path . replace ( output_dir + os . sep , '' ) if os . path . getsize ( this_path ) == 0 : compress_method = zipfile . ZIP_STORED else : compress_method = zipfile . ZIP_DEFLATED to_zip_file . write ( this_path , archive_name , compress_method ) print ( 'Adding %s' % archive_name ) zip_file = zipfile . ZipFile ( output_file , 'w' , zipfile . ZIP_DEFLATED , allowZip64 = True ) try : os . path . walk ( archive_dir , _Addfiles , zip_file ) finally : zip_file . close ( ) else : if IsMac ( ) or IsLinux ( ) : zip_cmd = [ 'zip' , '-yr1' ] else : zip_cmd = windows_zip_cmd if ignore_sub_folder : zip_cmd . extend ( [ '-j' ] ) saved_dir = os . getcwd ( ) os . chdir ( os . path . dirname ( archive_dir ) ) command = zip_cmd + [ output_file , os . path . basename ( archive_dir ) ] result = RunCommand ( command ) os . chdir ( saved_dir ) if result and raise_error : raise ExternalError ( 'zip failed: %s => %s' % ( str ( command ) , result ) ) end_time = time . clock ( ) print ( 'Took %f seconds to create zip.' % ( end_time - start_time ) ) return ( archive_dir , output_file )
Packs files into a new zip archive.
Packs files into a new zip archive .
"""Packs files into a new zip archive.

  Files are first copied into a directory within the output_dir named for
  the archive_name, which will be created if necessary and emptied if it
  already exists.  The files are then then packed using archive names
  relative to the output_dir.  That is, if the zipfile is unpacked in place,
  it will create a directory identical to the new archive_name directory, in
  the output_dir.  The zip file will be named as the archive_name, plus
  '.zip'.

  Args:
    output_dir: Absolute path to the directory in which the archive is to
      be created.
    archive_dir: Subdirectory of output_dir holding files to be added to
      the new zipfile.
    file_list: List of paths to files or subdirectories, relative to the
      file_relative_dir.
    file_relative_dir: Absolute path to the directory containing the files
      and subdirectories in the file_list.
    dir_in_zip: Directory where the files are archived into.
    raise_error: Whether to raise a PathNotFound error if one of the files in
      the list is not found.
    remove_archive_directory: Whether to remove the archive staging directory
      before copying files over to it.
    strip_files: List of executable files to strip symbols when zipping

  Returns:
    A tuple consisting of (archive_dir, zip_file_path), where archive_dir
    is the full path to the newly created archive_name subdirectory.

  Raises:
    PathNotFound if any of the files in the list is not found, unless
    raise_error is False, in which case the error will be ignored.
  """ # Collect files into the archive directory. # Move it even if it's not a directory as expected. This can happen with # FILES.cfg archive creation where we create an archive staging directory # that is the same name as the ultimate archive name. # These paths are relative to the file_relative_dir.  We need to copy # them over maintaining the relative directories, where applicable. # Pack the zip file. # If we have 7z, use that as it's much faster. See http://crbug.com/418702. # On Windows we use the python zip module; on Linux and Mac, we use the zip # command as it will handle links and file bits (executable).  Which is much # easier then trying to do that with ZipInfo options. # Store files named relative to the outer output_dir.
output_dir
archive_name
file_list
file_relative_dir
dir_in_zip
raise_error
remove_archive_directory
strip_files
ignore_sub_folder