<commit_before>#!/usr/bin/env python
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""
import functools
import glob
import os
import re
import sys
import tarfile
import tempfile
import time

from contextlib import closing
from optparse import OptionParser
from xml.dom import minidom

os.environ["PATH"] += os.pathsep + "/var/lib/ambari-agent"
sys.path.append("/usr/lib/ambari-server/lib")

from ambari_server.serverClassPath import JDBC_DRIVER_PATH_PROPERTY
from ambari_server.serverConfiguration import get_value_from_properties, get_ambari_properties

from resource_management.core import File
from resource_management.core import shell
from resource_management.core.environment import Environment
from resource_management.core.logger import Logger
from resource_management.core.resources.system import Directory
from resource_management.core.resources.system import Execute
from resource_management.core.source import StaticFile
from resource_management.libraries import ConfigDictionary
from resource_management.libraries.functions.format import format
from resource_management.libraries.functions.oozie_prepare_war import prepare_war
from resource_management.libraries.resources.hdfs_resource import HdfsResource

DEFAULT_SQL_DRIVER_PATH = get_value_from_properties(get_ambari_properties(), JDBC_DRIVER_PATH_PROPERTY, "/var/lib/ambari-server/resources/sqljdbc41.jar")

with Environment() as env:
  def get_stack_version():
    if not options.hdp_version:
      # Ubuntu returns: "stdin: is not a tty", as subprocess32 output.
      tmpfile = tempfile.NamedTemporaryFile()
      out = None
      with open(tmpfile.name, 'r+') as file:
        get_stack_version_cmd = '/usr/bin/hdp-select status %s > %s' % ('hadoop-mapreduce-historyserver', tmpfile.name)
        code, stdoutdata = shell.call(get_stack_version_cmd)
        out = file.read()
      pass
      if code != 0 or out is None:
        Logger.warning("Could not verify HDP version by calling '%s'. Return Code: %s, Output: %s." %
                       (get_stack_version_cmd, str(code), str(out)))
        return 1

      matches = re.findall(r"([\d\.]+\-\d+)", out)
      stack_version = matches[0] if matches and len(matches) > 0 else None

      if not stack_version:
        Logger.error("Could not parse HDP version from output of hdp-select: %s" % str(out))
        return 1
    else:
      stack_version = options.hdp_version

    return stack_version

  parser = OptionParser()
  parser.add_option("-d", "--database-driver", dest="sql_driver_path", default=DEFAULT_SQL_DRIVER_PATH,
                    help="Path to JDBC driver")
  parser.add_option("-f", "--fs-type", dest="fs_type", default="wasb",
                    help="Expected protocol of fs.defaultFS")
  parser.add_option("-v", "--hdp-version", dest="hdp_version", default="",
                    help="hdp-version used in path of tarballs")
  parser.add_option("-u", "--upgrade", dest="upgrade", action="store_true",
                    help="flag to indicate script is being run for upgrade", default=False)
  (options, args) = parser.parse_args()

  if not os.path.exists(options.sql_driver_path):
    Logger.error("SQL driver file {} does not exist".format(options.sql_driver_path))
    sys.exit(1)

  Logger.info("Using SQL driver from {}".format(options.sql_driver_path))
  sql_driver_filename = os.path.basename(options.sql_driver_path)

  # See if hdfs path prefix is provided on the command line. If yes, use that value, if no
  # use empty string as default.
  hdfs_path_prefix = ""<commit_msg>AMBARI-24843. Make Ambaripreupload.py more configurable - fallback to default driver (#2574)<commit_after>#!/usr/bin/env python
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""
import functools
import glob
import os
import re
import sys
import tarfile
import tempfile
import time

from contextlib import closing
from optparse import OptionParser
from xml.dom import minidom

os.environ["PATH"] += os.pathsep + "/var/lib/ambari-agent"
sys.path.append("/usr/lib/ambari-server/lib")

from ambari_server.serverClassPath import JDBC_DRIVER_PATH_PROPERTY
from ambari_server.serverConfiguration import get_value_from_properties, get_ambari_properties

from resource_management.core import File
from resource_management.core import shell
from resource_management.core.environment import Environment
from resource_management.core.logger import Logger
from resource_management.core.resources.system import Directory
from resource_management.core.resources.system import Execute
from resource_management.core.source import StaticFile
from resource_management.libraries import ConfigDictionary
from resource_management.libraries.functions.format import format
from resource_management.libraries.functions.oozie_prepare_war import prepare_war
from resource_management.libraries.resources.hdfs_resource import HdfsResource

DEFAULT_SQL_DRIVER_PATH = "/var/lib/ambari-server/resources/sqljdbc41.jar"

with Environment() as env:
  def get_stack_version():
    if not options.hdp_version:
      # Ubuntu returns: "stdin: is not a tty", as subprocess32 output.
      tmpfile = tempfile.NamedTemporaryFile()
      out = None
      with open(tmpfile.name, 'r+') as file:
        get_stack_version_cmd = '/usr/bin/hdp-select status %s > %s' % ('hadoop-mapreduce-historyserver', tmpfile.name)
        code, stdoutdata = shell.call(get_stack_version_cmd)
        out = file.read()
      pass
      if code != 0 or out is None:
        Logger.warning("Could not verify HDP version by calling '%s'. Return Code: %s, Output: %s." %
                       (get_stack_version_cmd, str(code), str(out)))
        return 1

      matches = re.findall(r"([\d\.]+\-\d+)", out)
      stack_version = matches[0] if matches and len(matches) > 0 else None

      if not stack_version:
        Logger.error("Could not parse HDP version from output of hdp-select: %s" % str(out))
        return 1
    else:
      stack_version = options.hdp_version

    return stack_version

  parser = OptionParser()
  parser.add_option("-d", "--database-driver", dest="sql_driver_path",
                    default=get_value_from_properties(get_ambari_properties(), JDBC_DRIVER_PATH_PROPERTY, DEFAULT_SQL_DRIVER_PATH),
                    help="Path to JDBC driver")
  parser.add_option("-f", "--fs-type", dest="fs_type", default="wasb",
                    help="Expected protocol of fs.defaultFS")
  parser.add_option("-v", "--hdp-version", dest="hdp_version", default="",
                    help="hdp-version used in path of tarballs")
  parser.add_option("-u", "--upgrade", dest="upgrade", action="store_true",
                    help="flag to indicate script is being run for upgrade", default=False)
  (options, args) = parser.parse_args()

  if not os.path.exists(options.sql_driver_path):
    Logger.error("SQL driver file {} does not exist".format(options.sql_driver_path))
    if os.path.exists(DEFAULT_SQL_DRIVER_PATH):
      Logger.warning("Fallback to SQL driver {}".format(DEFAULT_SQL_DRIVER_PATH))
      options.sql_driver_path = DEFAULT_SQL_DRIVER_PATH
    else:
      sys.exit(1)

  Logger.info("Using SQL driver from {}".format(options.sql_driver_path))
  sql_driver_filename = os.path.basename(options.sql_driver_path)

  # See if hdfs path prefix is provided on the command line. If yes, use that value, if no
  # use empty string as default.
  hdfs_path_prefix = ""
{'fraction_non_alphanumeric': 0.09097103918228279, 'fraction_numerical': 0.004202157864849517, 'mean_word_length': 4.081361800346221, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5215002', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2460, 'n_words': 795}