def prepare_pom(root_dir, name, version, out_dir):
    """
    Writes the pom file using the existing pom (if one exists) in the root directory.
    Changes are made to the groupId, artifactId, and version as necessary. Also, if python has
    spark-package-deps.txt, those are added to the pom as well.
    """
    group_id, artifact_id = name.strip().split("/")
    # In case people want to use their own pom.xml. sbt-spark-package plugin does everything that
    # zip does in this tool. This is useful for purely python packages.
    existing_pom_path = os.path.join(root_dir, 'pom.xml')
    if os.path.isfile(existing_pom_path):
        Xml.register_namespace('', "http://maven.apache.org/POM/4.0.0")
        old_pom = Xml.parse(existing_pom_path)
        project = old_pom.getroot()
        # The XML parser returns a prefix {http://maven.apache.org/POM/4.0.0} to all nodes. The
        # following code gets that prefix, rather than hard-coding it, in case there isn't one.
        prefix_length = project.tag.find('project')
        assert prefix_length > -1, \
            "The root node of the pom file should be <project>, not: %s" % project.tag
        prefix = project.tag[:prefix_length]
    else:
        # Write a pom file from scratch
        attributes = {
            "xmlns": "http://maven.apache.org/POM/4.0.0",
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xsi:schemaLocation": "http://maven.apache.org/POM/4.0.0 "
                                  "http://maven.apache.org/xsd/maven-4.0.0.xsd"
        }
        project = Xml.Element('project', attributes)
        prefix = ""
    # modify groupId to the github repo owner
    pom_add_or_modify_tag(project, prefix + 'groupId', group_id, 1)
    # modify artifactId to the github repo name
    pom_add_or_modify_tag(project, prefix + 'artifactId', artifact_id, 2)
    # modify version to the release version supplied as argument
    pom_add_or_modify_tag(project, prefix + 'version', version, 3)
    # if the project has spark-package dependencies in python (spark-package-deps.txt), add them to
    # the pom
    sp_deps_path = os.path.join(root_dir, 'python', 'spark-package-deps.txt')
    if os.path.isfile(sp_deps_path):
        sp_deps = open(sp_deps_path, 'r')
        for line in sp_deps.readlines():
            line = line.strip()
            if not line.startswith('#'):
                dep_group_id, dep_artifact_id, dep_version = validate_and_return_sp_dep(line)
                dep = {"groupId": dep_group_id,
                       "artifactId": dep_artifact_id,
                       "version": dep_version}
                keys = ["groupId", "artifactId"] # keys to compare on
                pom_add_element(project, prefix, "dependencies", "dependency", dep, keys,
                                ["groupId", "artifactId", "version"])
    repo = {"id": "SparkPackagesRepo",
           "name": "Spark Packages Repository",
           "url": "http://dl.bintray.com/spark-packages/maven/",
           "layout": "default"}
    pom_add_element(project, prefix, "repositories", "repository", repo, ["url"],
                    ["id", "name", "url", "layout"])
    with open(os.path.join(out_dir, "%s-%s.pom" % (artifact_id, version)), 'w') as new_pom:
        new_pom.write(pom_pretty_print(Xml.tostring(project, encoding='UTF-8')))
dataset/ETHPy150Open databricks/spark-package-cmd-tool/python/spark_package/spark_package.py/prepare_pom/original