Exemple #1
0
def merge_resource(origin_file,
                   overlay_file,
                   target_location,
                   params={},
                   action=PRINT_NONE,
                   json_file=None,
                   root_dir=None,
                   translation_dir="resources",
                   output_file_name=None):
    mergeresource = GritFile(translation_dir=translation_dir)
    mergeresource.Load(overlay_file, params, output_file_name=output_file_name)

    extra_translations = mergeresource.GetTranslationsCopy()

    mainresource = GritFile(translation_dir=translation_dir,
                            keep_sequence=True)
    mainresource.Load(origin_file,
                      params,
                      output_file_name=output_file_name,
                      extra_languages=extra_translations)
    if action in [WRITE_GRD_FILES, PRINT_ALL]:
        mainresource.ReplaceGoogle(exceptions=REPLACE_GOOGLE_EXCEPTIONS)

    mainresource.Update(mergeresource)

    new_resource = mainresource.GritNode()

    resource_target_file = mainresource.filename_base + ".grd"
    resource_target_dir = os.path.join(target_location, translation_dir)

    try:
        os.makedirs(resource_target_dir)
    except:
        pass  # Just ignore the errors here. Most likely the dir exists,
        # otherwise next step will fail instead
    translations = mainresource.Translations()
    for lang, translation in translations.iteritems():
        if lang == "en" or lang == "x-P-pseudo":
            continue  # found in the grd file
        if action == PRINT_ALL:
            print
            print "Writing generated language file", mainresource.GetLanguageFileName(
                lang)
            print "*" * 40
        xtb_filename = os.path.join(target_location,
                                    mainresource.GetLanguageFileName(lang))
        xtbfile = cStringIO.StringIO()
        xtbfile.write("""<?xml version="1.0" encoding="utf-8" ?>\n"""
                      """<!DOCTYPE translationbundle>\n"""
                      """<translationbundle lang="%s">""" % lang)
        for msgid, text in sorted(translation.iteritems()):
            try:
                xtbfile.write((u"""<translation id="%s">%s</translation>\n""" %
                               (msgid, text)).encode("utf8"))
            except:
                print >> sys.stderr, repr(msgid), "|", repr(text), "|", repr(
                    text.encode("utf8"))
                raise
        xtbfile.write("</translationbundle>\n")
        refresh_file.conditional_refresh_file(xtb_filename, xtbfile.getvalue())
        xtbfile.close()

    if action == PRINT_ALL:
        print
        print "Writing generated file", resource_target_file
        print "*" * 40
    content = new_resource.FormatXml("  ", False)
    grd_filename = os.path.join(target_location, resource_target_file)
    grdfile = cStringIO.StringIO()
    grdfile.write("""<?xml version="1.0" encoding="utf-8"?>\n""")
    grdfile.write(content.encode("utf8"))
    refresh_file.conditional_refresh_file(grd_filename, grdfile.getvalue())
    grdfile.close()

    special_update_sources = []
    special_update_targets = []
    special_copy_sources = []
    special_copy_targets = []
    if json_file and root_dir:
        import update_file

        target_name = os.path.basename(target_location)
        update_config = update_file.UpdateConfig(json_file, target_name,
                                                 target_location, root_dir)

        special_update_sources = update_config.get_update_sources()
        special_update_targets = update_config.get_update_targets()
        special_copy_sources = update_config.get_copy_sources()
        special_copy_targets = update_config.get_copy_targets()

        update_config.update_files(force=True)
        update_config.copy_files(force=True)

        mainresource.RemoveSpecialUpdateSources(special_update_sources +
                                                special_copy_sources)

    sources = {
        PRINT_MAIN_SOURCES:
        mainresource.GetAllMainSources(),
        PRINT_SECONDARY_SOURCES:
        mergeresource.GetAllMainSources(),
        PRINT_MAIN_RESOURCES:
        mainresource.GetActiveSources(),
        PRINT_SECONDARY_RESOURCES:
        mainresource.GetActiveOtherSources(),
        PRINT_TARGET_SOURCES: [
            os.path.normpath(os.path.join(target_location,
                                          x)).replace("\\", "/")
            for x in mainresource.GetTargetFiles()
        ],
        PRINT_TARGET_RESOURCES: [
            os.path.normpath(os.path.join(target_location,
                                          x)).replace("\\", "/")
            for x in mainresource.GetMigrateTargetFiles()
        ],
        PRINT_SPECIAL_UPDATE_SOURCES:
        special_update_sources + special_copy_sources,
        PRINT_SPECIAL_UPDATE_TARGETS:
        special_update_targets + special_copy_targets,
    }

    source_dir = os.path.dirname(origin_file)
    # initial copy, to please HTML flattening by grit
    for x in sources[PRINT_MAIN_RESOURCES]:
        try:
            try:
                os.makedirs(os.path.join(target_location, os.path.dirname(x)))
            except:
                pass
            refresh_file.conditional_copy(os.path.join(source_dir, x),
                                          os.path.join(target_location, x))
            #print >>sys.stderr, "Copied", x, "to", target_location
        except shutil.Error:
            continue  # ignore, same file
        except:
            print >> sys.stderr, "Copying", x, "failed"
            raise
            pass

    source_dir = os.path.dirname(overlay_file)
    # initial copy, to please HTML flattening by grit
    for x in sources[PRINT_SECONDARY_RESOURCES]:
        try:
            try:
                os.makedirs(os.path.join(target_location, os.path.dirname(x)))
            except:
                pass
            refresh_file.conditional_copy(os.path.join(source_dir, x),
                                          os.path.join(target_location, x))
            #print >>sys.stderr, "Copied", x, "to", target_location
        except shutil.Error:
            continue  # ignore, same file
        except:
            print >> sys.stderr, "Copying", x, "failed"
            raise
            pass

    json.dump(sources,
              open(os.path.join(target_location, "target_info.json"), "w"))

    return write_targets(action, sources, os.path.dirname(origin_file),
                         os.path.dirname(overlay_file))
 def perform_copy(source_file, target_file, config):
   try:
     os.makedirs(os.path.dirname(target_file))
   except:
     pass
   refresh_file.conditional_copy(source_file, target_file)
Exemple #3
0
 def perform_copy(source_file, target_file, config):
   try:
     os.makedirs(os.path.dirname(target_file))
   except:
     pass
   refresh_file.conditional_copy(source_file, target_file)
Exemple #4
0
def merge_resource(origin_file,
                   overlay_file,
                   target_location,
                   params={},
                   action=PRINT_NONE,
                   json_file=None,
                   root_dir=None,
                   translation_dir="resources",
                   output_file_name=None,
                   translation_stamp=None):
    util.PathSearcher.Configure([
        os.path.dirname(target_location),
        os.path.dirname(overlay_file),
        os.path.dirname(origin_file),
    ])
    mergeresource = GritFile(translation_dir=translation_dir)
    mergeresource.Load(overlay_file,
                       params,
                       output_file_name=output_file_name,
                       load_translations=True)

    extra_translations = mergeresource.GetTranslationsCopy()

    mainresource = GritFile(translation_dir=translation_dir,
                            keep_sequence=True)
    mainresource.Load(origin_file,
                      params,
                      output_file_name=output_file_name,
                      extra_languages=extra_translations,
                      load_translations=True)
    if action in [WRITE_GRD_FILES, PRINT_ALL]:
        mainresource.ReplaceGoogle(exceptions=REPLACE_GOOGLE_EXCEPTIONS)

    mainresource.Update(mergeresource, load_translations=True)

    resource_target_file = mainresource.filename_base + ".grd"
    resource_target_dir = os.path.join(target_location, translation_dir)

    try:
        os.makedirs(resource_target_dir)
    except:
        pass  # Just ignore the errors here. Most likely the dir exists,
        # otherwise next step will fail instead
    if action in [WRITE_GRD_FILES, PRINT_ALL]:
        for lang, extra_translation_items in mergeresource.resources[
                "resources"].UberClique().additional_translations_.items():
            xtb_callback = mainresource.resources["resources"].UberClique(
            ).GenerateXtbParserCallback(lang, override_exisiting=True)
            for tid, string in extra_translation_items.items():
                #print "Item", tid, type(string), string
                xtb_callback(tid, string)
                #translations.setdefault(lang, {})[tid] = util.EscapeHtml(string)
        translations = dict(mainresource.Translations())
        for lnode in iter_gritnode(
                mainresource.get("generated_translations",
                                 empty.TranslationsNode()), node_io.FileNode):
            translations.setdefault(lnode.GetLang(), {})
        for lang, translation in translations.items():
            if lang == "en" or lang == "x-P-pseudo":
                continue  # found in the grd file
            if action == PRINT_ALL:
                print()
                print("Writing generated language file",\
                       mainresource.GetLanguageFileName(lang))
                print("*" * 40)
            xtb_filename = os.path.join(target_location,
                                        mainresource.GetLanguageFileName(lang))
            xtbfile = io.StringIO()
            xtbfile.write(u"""<?xml version="1.0" encoding="utf-8" ?>\n"""
                          """<!DOCTYPE translationbundle>\n"""
                          """<translationbundle lang="%s">""" %
                          (lang if lang != "nb" else "no"))
            for msgid, text in sorted(translation.items()):
                try:
                    xtbfile.write(
                        u"""<translation id="%s">%s</translation>\n""" %
                        (msgid, text))
                except:
                    print(repr(msgid), "|", repr(text), "|",\
                          repr(text.encode("utf8")), file=sys.stderr)
                    raise
            xtbfile.write(u"</translationbundle>\n")
            refresh_file.conditional_refresh_file(
                xtb_filename,
                xtbfile.getvalue().encode("utf8"), translation_stamp)
            xtbfile.close()

        if action == PRINT_ALL:
            print()
            print("Writing generated file", resource_target_file)
            print("*" * 40)
        new_resource = mainresource.GritNode()

        content = new_resource.FormatXml("  ", False)
        grd_filename = os.path.join(target_location, resource_target_file)
        grdfile = io.StringIO()
        grdfile.write(unicode("""<?xml version="1.0" encoding="utf-8"?>\n"""))
        grdfile.write(content)
        refresh_file.conditional_refresh_file(
            grd_filename,
            grdfile.getvalue().encode("utf-8"))
        grdfile.close()

        special_update_sources = []
        special_update_targets = []
        special_copy_sources = []
        special_copy_targets = []
    if json_file and root_dir:
        import update_file

        target_name = os.path.basename(target_location)
        update_config = update_file.UpdateConfig(json_file, target_name,
                                                 target_location, root_dir)

        special_update_sources = update_config.get_update_sources()
        special_update_targets = update_config.get_update_targets()
        special_copy_sources = update_config.get_copy_sources()
        special_copy_targets = update_config.get_copy_targets()

        update_config.update_files(force=True)
        update_config.copy_files(force=True)

        mainresource.RemoveSpecialUpdateSources(special_update_sources +
                                                special_copy_sources)

    sources = {
        PRINT_MAIN_SOURCES:
        mainresource.GetAllMainSources(),
        PRINT_SECONDARY_SOURCES:
        mergeresource.GetAllMainSources(),
        PRINT_MAIN_RESOURCES:
        mainresource.GetActiveSources(),
        PRINT_SECONDARY_RESOURCES:
        mainresource.GetActiveOtherSources(),
        PRINT_TARGET_SOURCES: [
            os.path.normpath(os.path.join(target_location,
                                          x)).replace("\\", "/")
            for x in mainresource.GetTargetFiles()
        ],
        PRINT_TARGET_RESOURCES: [
            os.path.normpath(os.path.join(target_location,
                                          x)).replace("\\", "/")
            for x in mainresource.GetMigrateTargetFiles()
        ],
        PRINT_SPECIAL_UPDATE_SOURCES:
        special_update_sources + special_copy_sources,
        PRINT_SPECIAL_UPDATE_TARGETS:
        special_update_targets + special_copy_targets,
    }

    if False and action != SETUP_RESOURCES:
        source_dir = os.path.dirname(origin_file)
        # initial copy, to please HTML flattening by grit
        for x in sources[PRINT_MAIN_RESOURCES]:
            try:
                try:
                    os.makedirs(
                        os.path.join(target_location, os.path.dirname(x)))
                except:
                    pass
                refresh_file.conditional_copy(os.path.join(source_dir, x),
                                              os.path.join(target_location, x))
                #print >>sys.stderr, "Copied", x, "to", target_location
            except shutil.Error:
                continue  # ignore, same file
            except:
                print("Copying", x, "failed", file=sys.stderr)
                raise
                pass

        source_dir = os.path.dirname(overlay_file)
        # initial copy, to please HTML flattening by grit
        for x in sources[PRINT_SECONDARY_RESOURCES]:
            try:
                try:
                    os.makedirs(
                        os.path.join(target_location, os.path.dirname(x)))
                except:
                    pass
                refresh_file.conditional_copy(os.path.join(source_dir, x),
                                              os.path.join(target_location, x))
                #print >>sys.stderr, "Copied", x, "to", target_location
            except shutil.Error:
                continue  # ignore, same file
            except:
                print("Copying", x, "failed", file=sys.stderr)
                raise
                pass

    json.dump(sources,
              open(os.path.join(target_location, "target_info.json"), "w"))

    return write_targets(action, sources, os.path.dirname(origin_file),
                         os.path.dirname(overlay_file))
def merge_resource(origin_file, overlay_file, target_location, params = {},
                   action=PRINT_NONE, json_file=None, root_dir=None,
                   translation_dir="resources", output_file_name=None):
  mergeresource = GritFile(translation_dir=translation_dir)
  mergeresource.Load(overlay_file, params, output_file_name=output_file_name)

  extra_translations = mergeresource.GetTranslationsCopy()

  mainresource = GritFile(translation_dir=translation_dir, keep_sequence=True)
  mainresource.Load(origin_file, params, output_file_name=output_file_name,
                    extra_languages=extra_translations)
  if action in [WRITE_GRD_FILES, PRINT_ALL]:
    mainresource.ReplaceGoogle(exceptions=REPLACE_GOOGLE_EXCEPTIONS)

  mainresource.Update(mergeresource)

  new_resource = mainresource.GritNode()

  resource_target_file = mainresource.filename_base+".grd"
  resource_target_dir = os.path.join(target_location, translation_dir)

  try:
    os.makedirs(resource_target_dir)
  except:
    pass # Just ignore the errors here. Most likely the dir exists,
         # otherwise next step will fail instead
  translations = mainresource.Translations()
  for lang, translation in translations.iteritems():
    if lang == "en" or lang == "x-P-pseudo":
      continue # found in the grd file
    if action == PRINT_ALL:
      print
      print "Writing generated language file", mainresource.GetLanguageFileName(lang)
      print "*"*40
    xtb_filename = os.path.join(target_location,
                                mainresource.GetLanguageFileName(lang))
    xtbfile = cStringIO.StringIO()
    xtbfile.write("""<?xml version="1.0" encoding="utf-8" ?>\n"""
                  """<!DOCTYPE translationbundle>\n"""
                  """<translationbundle lang="%s">""" % lang)
    for msgid, text in sorted(translation.iteritems()):
      try:
        xtbfile.write((u"""<translation id="%s">%s</translation>\n""" %
                        (msgid, text)).encode("utf8"))
      except:
        print >>sys.stderr, repr(msgid), "|", repr(text), "|", repr(text.encode("utf8"))
        raise
    xtbfile.write("</translationbundle>\n")
    refresh_file.conditional_refresh_file(xtb_filename, xtbfile.getvalue())
    xtbfile.close()


  if action == PRINT_ALL:
    print
    print "Writing generated file", resource_target_file
    print "*"*40
  content = new_resource.FormatXml("  ", False)
  grd_filename = os.path.join(target_location, resource_target_file)
  grdfile = cStringIO.StringIO()
  grdfile.write("""<?xml version="1.0" encoding="utf-8"?>\n""")
  grdfile.write(content.encode("utf8"))
  refresh_file.conditional_refresh_file(grd_filename, grdfile.getvalue())
  grdfile.close()

  special_update_sources = []
  special_update_targets = []
  special_copy_sources = []
  special_copy_targets = []
  if json_file and root_dir:
    import update_file

    target_name = os.path.basename(target_location)
    update_config = update_file.UpdateConfig(json_file, target_name,
                                             target_location, root_dir)

    special_update_sources= update_config.get_update_sources()
    special_update_targets= update_config.get_update_targets()
    special_copy_sources= update_config.get_copy_sources()
    special_copy_targets= update_config.get_copy_targets()

    update_config.update_files(force=True)
    update_config.copy_files(force=True)

    mainresource.RemoveSpecialUpdateSources(special_update_sources +
                                            special_copy_sources)

  sources = {
    PRINT_MAIN_SOURCES:        mainresource.GetAllMainSources(),
    PRINT_SECONDARY_SOURCES:   mergeresource.GetAllMainSources(),
    PRINT_MAIN_RESOURCES:      mainresource.GetActiveSources(),
    PRINT_SECONDARY_RESOURCES: mainresource.GetActiveOtherSources(),
    PRINT_TARGET_SOURCES:      [
      os.path.normpath(os.path.join(target_location, x)).replace("\\", "/")
          for x in mainresource.GetTargetFiles()],
    PRINT_TARGET_RESOURCES:    [
      os.path.normpath(os.path.join(target_location, x)).replace("\\", "/")
          for x in mainresource.GetMigrateTargetFiles()],
    PRINT_SPECIAL_UPDATE_SOURCES: special_update_sources+special_copy_sources,
    PRINT_SPECIAL_UPDATE_TARGETS: special_update_targets+special_copy_targets,
  }

  source_dir = os.path.dirname(origin_file)
  # initial copy, to please HTML flattening by grit
  for x in sources[PRINT_MAIN_RESOURCES]:
    try:
      try:
        os.makedirs(os.path.join(target_location, os.path.dirname(x)))
      except:
        pass
      refresh_file.conditional_copy(os.path.join(source_dir,x),
                                    os.path.join(target_location, x))
      #print >>sys.stderr, "Copied", x, "to", target_location
    except shutil.Error:
      continue # ignore, same file
    except:
      print >>sys.stderr, "Copying", x, "failed"
      raise
      pass

  source_dir = os.path.dirname(overlay_file)
  # initial copy, to please HTML flattening by grit
  for x in sources[PRINT_SECONDARY_RESOURCES]:
    try:
      try:
        os.makedirs(os.path.join(target_location, os.path.dirname(x)))
      except:
        pass
      refresh_file.conditional_copy(os.path.join(source_dir,x),
                                    os.path.join(target_location, x))
      #print >>sys.stderr, "Copied", x, "to", target_location
    except shutil.Error:
      continue # ignore, same file
    except:
      print >>sys.stderr, "Copying", x, "failed"
      raise
      pass

  json.dump(sources, open(os.path.join(target_location,
                                      "target_info.json"), "w"))

  return write_targets(action, sources, os.path.dirname(origin_file),
                       os.path.dirname(overlay_file))