def _make_archive(self): import tarfile build_dir = os.path.basename(self.build_dir) archive_name = self.package_name + '.tar' current_dir = getcwd() os.chdir(self.build_base) try: # We must convert the archive_name and base_dir from unicode to utf-8 due to a bug in the version of tarfile # that ships with Python 2.7.2, the version of Python used by the app team's build system as of this date: # 12 Sep 2014. tar = tarfile.open(str(archive_name), 'w|gz') try: tar.add(str(build_dir)) finally: tar.close() gzipped_archive_name = archive_name + '.gz' if os.path.exists(gzipped_archive_name): os.remove(gzipped_archive_name) os.rename(archive_name, gzipped_archive_name) finally: os.chdir(current_dir) return
def run(self): import unittest if not self.skip_setup_teardown: try: splunk('search', '| setup environment="{0}"'.format(self.env), '-app', self.test_harness_name, '-uri', self.uri, '-auth', self.auth) splunk_restart(self.uri, self.auth) except CalledProcessError as e: sys.exit(e.returncode) current_directory = os.path.abspath(getcwd()) os.chdir(os.path.join(project_dir, 'tests')) print('') try: suite = unittest.defaultTestLoader.discover('.', pattern=self.pattern) unittest.TextTestRunner(verbosity=2).run( suite) # 1 = show dots, >1 = show all finally: os.chdir(current_directory) if not self.skip_setup_teardown: try: splunk('search', '| teardown', '-app', self.test_harness_name, '-uri', self.uri, '-auth', self.auth) except CalledProcessError as e: sys.exit(e.returncode) return
class File(Validator): """ Validates file option values. """ def __init__(self, mode='rt', buffering=None, directory=None): self.mode = mode self.buffering = buffering self.directory = File._var_run_splunk if directory is None else directory def __call__(self, value): if value is None: return value path = six.text_type(value) if not os.path.isabs(path): path = os.path.join(self.directory, path) try: value = open(path, self.mode) if self.buffering is None else open( path, self.mode, self.buffering) except IOError as error: raise ValueError( 'Cannot open {0} with mode={1} and buffering={2}: {3}'.format( value, self.mode, self.buffering, error)) return value def format(self, value): return None if value is None else value.name _var_run_splunk = os.path.join( os.environ['SPLUNK_HOME'] if 'SPLUNK_HOME' in os.environ else getcwd(), 'var', 'run', 'splunk')
def run(self): import unittest if not self.skip_setup_teardown: try: splunk( 'search', '| setup environment="{0}"'.format(self.env), '-app', self.test_harness_name, '-uri', self.uri, '-auth', self.auth) splunk_restart(self.uri, self.auth) except CalledProcessError as e: sys.exit(e.returncode) current_directory = os.path.abspath(getcwd()) os.chdir(os.path.join(project_dir, 'tests')) print('') try: suite = unittest.defaultTestLoader.discover('.', pattern=self.pattern) unittest.TextTestRunner(verbosity=2).run(suite) # 1 = show dots, >1 = show all finally: os.chdir(current_directory) if not self.skip_setup_teardown: try: splunk('search', '| teardown', '-app', self.test_harness_name, '-uri', self.uri, '-auth', self.auth) except CalledProcessError as e: sys.exit(e.returncode) return
finally: os.chdir(current_directory) if not self.skip_setup_teardown: try: splunk('search', '| teardown', '-app', self.test_harness_name, '-uri', self.uri, '-auth', self.auth) except CalledProcessError as e: sys.exit(e.returncode) return # endregion current_directory = getcwd() os.chdir(project_dir) try: setup(description='Custom Search Command examples', name=os.path.basename(project_dir), version='1.6.4', author='Splunk, Inc.', author_email='*****@*****.**', url='http://github.com/splunk/splunk-sdk-python', license='http://www.apache.org/licenses/LICENSE-2.0', classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Other Environment', 'Intended Audience :: Information Technology', 'License :: Other/Proprietary License',
def configure_logging(logger_name, filename=None): """ Configure logging and return the named logger and the location of the logging configuration file loaded. This function expects a Splunk app directory structure:: <app-root> bin ... default ... local ... This function looks for a logging configuration file at each of these locations, loading the first, if any, logging configuration file that it finds:: local/{name}.logging.conf default/{name}.logging.conf local/logging.conf default/logging.conf The current working directory is set to *<app-root>* before the logging configuration file is loaded. Hence, paths in the logging configuration file are relative to *<app-root>*. The current directory is reset before return. You may short circuit the search for a logging configuration file by providing an alternative file location in `path`. Logging configuration files must be in `ConfigParser format`_. #Arguments: :param logger_name: Logger name :type logger_name: bytes, unicode :param filename: Location of an alternative logging configuration file or `None`. :type filename: bytes, unicode or NoneType :returns: The named logger and the location of the logging configuration file loaded. :rtype: tuple .. _ConfigParser format: https://docs.python.org/2/library/logging.config.html#configuration-file-format """ if filename is None: if logger_name is None: probing_paths = [ path.join('local', 'logging.conf'), path.join('default', 'logging.conf') ] else: probing_paths = [ path.join('local', logger_name + '.logging.conf'), path.join('default', logger_name + '.logging.conf'), path.join('local', 'logging.conf'), path.join('default', 'logging.conf') ] for relative_path in probing_paths: configuration_file = path.join(app_root, relative_path) if path.exists(configuration_file): filename = configuration_file break elif not path.isabs(filename): found = False for conf in 'local', 'default': configuration_file = path.join(app_root, conf, filename) if path.exists(configuration_file): filename = configuration_file found = True break if not found: raise ValueError( 'Logging configuration file "{}" not found in local or default directory' .format(filename)) elif not path.exists(filename): raise ValueError( 'Logging configuration file "{}" not found'.format(filename)) if filename is not None: global _current_logging_configuration_file filename = path.realpath(filename) if filename != _current_logging_configuration_file: working_directory = getcwd() chdir(app_root) try: fileConfig(filename, {'SPLUNK_HOME': splunk_home}) finally: chdir(working_directory) _current_logging_configuration_file = filename if len(root.handlers) == 0: root.addHandler(StreamHandler()) return None if logger_name is None else getLogger(logger_name), filename
global _current_logging_configuration_file filename = path.realpath(filename) if filename != _current_logging_configuration_file: working_directory = getcwd() chdir(app_root) try: fileConfig(filename, {'SPLUNK_HOME': splunk_home}) finally: chdir(working_directory) _current_logging_configuration_file = filename if len(root.handlers) == 0: root.addHandler(StreamHandler()) return None if logger_name is None else getLogger(logger_name), filename _current_logging_configuration_file = None splunk_home = path.abspath(path.join(getcwd(), environ.get('SPLUNK_HOME', ''))) app_file = getattr(sys.modules['__main__'], '__file__', sys.executable) app_root = path.dirname(path.abspath(path.dirname(app_file))) splunklib_logger, logging_configuration = configure_logging('splunklib') __all__ = [ 'app_file', 'app_root', 'logging_configuration', 'splunk_home', 'splunklib_logger' ]
def configure_logging(logger_name, filename=None): """ Configure logging and return the named logger and the location of the logging configuration file loaded. This function expects a Splunk app directory structure:: <app-root> bin ... default ... local ... This function looks for a logging configuration file at each of these locations, loading the first, if any, logging configuration file that it finds:: local/{name}.logging.conf default/{name}.logging.conf local/logging.conf default/logging.conf The current working directory is set to *<app-root>* before the logging configuration file is loaded. Hence, paths in the logging configuration file are relative to *<app-root>*. The current directory is reset before return. You may short circuit the search for a logging configuration file by providing an alternative file location in `path`. Logging configuration files must be in `ConfigParser format`_. #Arguments: :param logger_name: Logger name :type logger_name: bytes, unicode :param filename: Location of an alternative logging configuration file or `None`. :type filename: bytes, unicode or NoneType :returns: The named logger and the location of the logging configuration file loaded. :rtype: tuple .. _ConfigParser format: https://docs.python.org/2/library/logging.config.html#configuration-file-format """ if filename is None: if logger_name is None: probing_paths = [path.join('local', 'logging.conf'), path.join('default', 'logging.conf')] else: probing_paths = [ path.join('local', logger_name + '.logging.conf'), path.join('default', logger_name + '.logging.conf'), path.join('local', 'logging.conf'), path.join('default', 'logging.conf')] for relative_path in probing_paths: configuration_file = path.join(app_root, relative_path) if path.exists(configuration_file): filename = configuration_file break elif not path.isabs(filename): found = False for conf in 'local', 'default': configuration_file = path.join(app_root, conf, filename) if path.exists(configuration_file): filename = configuration_file found = True break if not found: raise ValueError('Logging configuration file "{}" not found in local or default directory'.format(filename)) elif not path.exists(filename): raise ValueError('Logging configuration file "{}" not found'.format(filename)) if filename is not None: global _current_logging_configuration_file filename = path.realpath(filename) if filename != _current_logging_configuration_file: working_directory = getcwd() chdir(app_root) try: fileConfig(filename, {'SPLUNK_HOME': splunk_home}) finally: chdir(working_directory) _current_logging_configuration_file = filename if len(root.handlers) == 0: root.addHandler(StreamHandler()) return None if logger_name is None else getLogger(logger_name), filename
if filename is not None: global _current_logging_configuration_file filename = path.realpath(filename) if filename != _current_logging_configuration_file: working_directory = getcwd() chdir(app_root) try: fileConfig(filename, {'SPLUNK_HOME': splunk_home}) finally: chdir(working_directory) _current_logging_configuration_file = filename if len(root.handlers) == 0: root.addHandler(StreamHandler()) return None if logger_name is None else getLogger(logger_name), filename _current_logging_configuration_file = None splunk_home = path.abspath(path.join(getcwd(), environ.get('SPLUNK_HOME', ''))) app_file = getattr(sys.modules['__main__'], '__file__', sys.executable) app_root = path.dirname(path.abspath(path.dirname(app_file))) splunklib_logger, logging_configuration = configure_logging('splunklib') __all__ = ['app_file', 'app_root', 'logging_configuration', 'splunk_home', 'splunklib_logger']
suite = unittest.defaultTestLoader.discover('.', pattern=self.pattern) unittest.TextTestRunner(verbosity=2).run(suite) # 1 = show dots, >1 = show all finally: os.chdir(current_directory) if not self.skip_setup_teardown: try: splunk('search', '| teardown', '-app', self.test_harness_name, '-uri', self.uri, '-auth', self.auth) except CalledProcessError as e: sys.exit(e.returncode) return # endregion current_directory = getcwd() os.chdir(project_dir) try: setup( description='Custom Search Command examples', name=os.path.basename(project_dir), version='1.6.6', author='Splunk, Inc.', author_email='*****@*****.**', url='http://github.com/splunk/splunk-sdk-python', license='http://www.apache.org/licenses/LICENSE-2.0', classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Other Environment', 'Intended Audience :: Information Technology',