Esempio n. 1
0
    def ExtractArchive(self, archive, extract_dir, rename_from_dir,
                       rename_to_dir):
        tar_file = None

        archive_path = os.path.join(self.user_data_dir, archive)
        extract_path = os.path.join(self.install_dir, extract_dir)
        rename_from_path = os.path.join(self.install_dir, rename_from_dir)
        rename_to_path = os.path.join(self.install_dir, rename_to_dir)

        # Extract to extract_dir, usually "<bundle name>_update".
        # This way if the extraction fails, we haven't blown away the old bundle
        # (if it exists).
        sdk_update_common.RemoveDir(extract_path)
        sdk_update_common.MakeDirs(extract_path)
        curpath = os.getcwd()
        tar_file = None

        try:
            try:
                tar_file = cygtar.CygTar(archive_path, 'r', verbose=True)
            except Exception as e:
                raise Error('Can\'t open archive "%s".\n  %s' %
                            (archive_path, e))

            try:
                logging.info('Changing the directory to %s' % (extract_path, ))
                os.chdir(extract_path)
            except Exception as e:
                raise Error('Unable to chdir into "%s".\n  %s' %
                            (extract_path, e))

            logging.info('Extracting to %s' % (extract_path, ))
            tar_file.Extract()

            logging.info('Changing the directory to %s' % (curpath, ))
            os.chdir(curpath)

            logging.info('Renaming %s->%s' %
                         (rename_from_path, rename_to_path))
            sdk_update_common.RenameDir(rename_from_path, rename_to_path)
        finally:
            # Change the directory back so we can remove the update directory.
            os.chdir(curpath)

            # Clean up the ..._update directory.
            try:
                sdk_update_common.RemoveDir(extract_path)
            except Exception as e:
                logging.error('Failed to remove directory \"%s\".  %s' %
                              (extract_path, e))

            if tar_file:
                tar_file.Close()

        # Remove the archive.
        os.remove(archive_path)
Esempio n. 2
0
def WriteConfig(cfg):
  path = os.path.join(USER_DATA_DIR, CONFIG_FILENAME)
  try:
    sdk_update_common.MakeDirs(USER_DATA_DIR)
  except Exception as e:
    raise Error('Unable to create directory "%s".\n  %s' % (USER_DATA_DIR, e))

  cfg_json = cfg.ToJson()

  try:
    with open(path, 'w') as f:
      f.write(cfg_json)
  except IOError as e:
    raise Error('Unable to write config to "%s".\n  %s' % (path, e))
Esempio n. 3
0
def WriteLocalManifest(manifest):
  path = os.path.join(USER_DATA_DIR, MANIFEST_FILENAME)
  try:
    sdk_update_common.MakeDirs(USER_DATA_DIR)
  except Exception as e:
    raise Error('Unable to create directory "%s".\n  %s' % (USER_DATA_DIR, e))

  try:
    manifest_json = manifest.GetDataAsString()
  except Exception as e:
    raise Error('Error encoding manifest "%s" to JSON.\n  %s' % (path, e))

  try:
    with open(path, 'w') as f:
      f.write(manifest_json)
  except IOError as e:
    raise Error('Unable to write manifest to "%s".\n  %s' % (path, e))
Esempio n. 4
0
  def DownloadToFile(self, url, dest_filename):
    dest_path = os.path.join(self.archive_cache, dest_filename)
    sdk_update_common.MakeDirs(os.path.dirname(dest_path))

    out_stream = None
    url_stream = None
    try:
      out_stream = open(dest_path, 'wb')
      url_stream = download.UrlOpen(url)
      content_length = int(url_stream.info()[HTTP_CONTENT_LENGTH])
      progress = download.MakeProgressFunction(content_length)
      sha1, size = download.DownloadAndComputeHash(url_stream, out_stream,
                                                   progress)
      return sha1, size
    except urllib2.URLError as e:
      raise Error('Unable to read from URL "%s".\n  %s' % (url, e))
    except IOError as e:
      raise Error('Unable to write to file "%s".\n  %s' % (dest_filename, e))
    finally:
      if url_stream:
        url_stream.close()
      if out_stream:
        out_stream.close()
Esempio n. 5
0
    def ExtractArchives(self, archives, extract_dir, rename_from_dir,
                        rename_to_dir):
        tar_file = None

        extract_path = os.path.join(self.install_dir, extract_dir)
        rename_from_path = os.path.join(self.install_dir, rename_from_dir)
        rename_to_path = os.path.join(self.install_dir, rename_to_dir)

        # Extract to extract_dir, usually "<bundle name>_update".
        # This way if the extraction fails, we haven't blown away the old bundle
        # (if it exists).
        sdk_update_common.RemoveDir(extract_path)
        sdk_update_common.MakeDirs(extract_path)
        curpath = os.getcwd()
        tar_file = None

        try:
            try:
                logging.info('Changing the directory to %s' % (extract_path, ))
                os.chdir(extract_path)
            except Exception as e:
                raise Error('Unable to chdir into "%s".\n  %s' %
                            (extract_path, e))

            for i, archive in enumerate(archives):
                archive_path = os.path.join(self.archive_cache, archive)

                if len(archives) > 1:
                    print '(file %d/%d - "%s")' % (
                        i + 1, len(archives), os.path.basename(archive_path))
                logging.info('Extracting to %s' % (extract_path, ))

                if sys.platform == 'win32':
                    try:
                        logging.info('Opening file %s (%d/%d).' %
                                     (archive_path, i + 1, len(archives)))
                        try:
                            tar_file = cygtar.CygTar(archive_path,
                                                     'r',
                                                     verbose=True)
                        except Exception as e:
                            raise Error("Can't open archive '%s'.\n  %s" %
                                        (archive_path, e))

                        tar_file.Extract()
                    finally:
                        if tar_file:
                            tar_file.Close()
                else:
                    try:
                        subprocess.check_call(['tar', 'xf', archive_path])
                    except subprocess.CalledProcessError:
                        raise Error('Error extracting archive: %s' %
                                    archive_path)

            logging.info('Changing the directory to %s' % (curpath, ))
            os.chdir(curpath)

            logging.info('Renaming %s->%s' %
                         (rename_from_path, rename_to_path))
            sdk_update_common.RenameDir(rename_from_path, rename_to_path)
        finally:
            # Change the directory back so we can remove the update directory.
            os.chdir(curpath)

            # Clean up the ..._update directory.
            try:
                sdk_update_common.RemoveDir(extract_path)
            except Exception as e:
                logging.error('Failed to remove directory \"%s\".  %s' %
                              (extract_path, e))