Python os.getenv() Examples

The following are code examples for showing how to use os.getenv(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: flaskit   Author: elston   File: config.py    MIT License 7 votes vote down vote up
def init_app(self, app):
        # ...
        config_name = os.getenv('FLASK_CONFIGURATION', 'base')
        app.config.from_object(self.config_obj[config_name])

        # logging
        # ...
        formatter = logging.Formatter(*app.config['LOGGING_FORMAT'])
        handler = RotatingFileHandler(
            app.config['LOGGING_LOCATION']
            , maxBytes = app.config['LOGGING_SIZE']
            , backupCount = 5)
        # ...
        handler.setLevel(app.config['LOGGING_LEVEL'])
        handler.setFormatter(formatter)    
        app.logger.addHandler(handler)

        # ..templates
        template_folder = app.config.get('TEMPLATE_FOLDER',None)
        if template_folder:
            app.template_folder = template_folder 
Example 2
Project: leapp-repository   Author: oamg   File: component_test.py    Apache License 2.0 6 votes vote down vote up
def destructive_selinux_env():
    for priority, module in TEST_MODULES:
        tests_dir = os.path.join(os.getenv('PYTEST_CURRENT_TEST').rsplit(os.path.sep, 2)[0], testmoduledir)
        _run_cmd(["semodule", "-X", priority, "-i", os.path.join(tests_dir, module + ".cil")],
                 logmsg="Error installing mock module")

    for command in SEMANAGE_COMMANDS:
        _run_cmd(["semanage", command[0], "-a"] + command[1:], logmsg="Error applying selinux customizations")

    yield

    for priority, module in TEST_MODULES + [["400", "permissive_abrt_t"]]:
        _run_cmd(["semodule", "-X", priority, "-r", module])

    for command in SEMANAGE_COMMANDS:
        _run_cmd(["semanage", command[0], "-d"] + command[1:]) 
Example 3
Project: autolims   Author: scottbecker   File: __init__.py    MIT License 6 votes vote down vote up
def setup_logging(
    default_path='logging.json', 
    default_level=logging.INFO,
    env_key='LOG_CFG'):  
    """Setup logging configuration

    """
    path = default_path
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = json.load(f)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level) 
Example 4
Project: incubator-spot   Author: apache   File: kerberos.py    Apache License 2.0 6 votes vote down vote up
def __init__(self):

        self._logger = Util.get_logger('SPOT.COMMON.KERBEROS')
        principal, keytab, sasl_mech, security_proto = config.kerberos()

        if os.getenv('KINITPATH'):
            self._kinit = os.getenv('KINITPATH')
        else:
            self._kinit = "kinit"

        self._kinitopts = os.getenv('KINITOPTS')
        self._keytab = "-kt {0}".format(keytab)
        self._krb_user = principal

        if self._kinit == None or self._keytab == None or self._krb_user == None:
            self._logger.error("Please verify kerberos configuration, some environment variables are missing.")
            sys.exit(1)

        if self._kinitopts is None:
            self._kinit_cmd = "{0} {1} {2}".format(self._kinit, self._keytab, self._krb_user)
        else:
            self._kinit_cmd = "{0} {1} {2} {3}".format(self._kinit, self._kinitopts, self._keytab, self._krb_user) 
Example 5
Project: godot-mono-builds   Author: godotengine   File: os_utils.py    MIT License 6 votes vote down vote up
def get_emsdk_root():
    # Shamelessly copied from Godot's detect.py
    em_config_file = os.getenv('EM_CONFIG') or os.path.expanduser('~/.emscripten')
    if not os.path.exists(em_config_file):
        raise BuildError("Emscripten configuration file '%s' does not exist" % em_config_file)
    with open(em_config_file) as f:
        em_config = {}
        try:
            # Emscripten configuration file is a Python file with simple assignments.
            exec(f.read(), em_config)
        except StandardError as e:
            raise BuildError("Emscripten configuration file '%s' is invalid:\n%s" % (em_config_file, e))
    if 'BINARYEN_ROOT' in em_config and os.path.isdir(os.path.join(em_config.get('BINARYEN_ROOT'), 'emscripten')):
        # New style, emscripten path as a subfolder of BINARYEN_ROOT
        return os.path.join(em_config.get('BINARYEN_ROOT'), 'emscripten')
    elif 'EMSCRIPTEN_ROOT' in em_config:
        # Old style (but can be there as a result from previous activation, so do last)
        return em_config.get('EMSCRIPTEN_ROOT')
    else:
        raise BuildError("'BINARYEN_ROOT' or 'EMSCRIPTEN_ROOT' missing in Emscripten configuration file '%s'" % em_config_file) 
Example 6
Project: alfred-yubikey-otp   Author: robertoriv   File: util.py    MIT License 6 votes vote down vote up
def unset_config(name, bundleid=None):
    """Delete a workflow variable from ``info.plist``.

    .. versionadded:: 1.33

    Args:
        name (str): Name of variable to delete.
        bundleid (str, optional): Bundle ID of workflow variable belongs to.

    """
    if not bundleid:
        bundleid = os.getenv('alfred_workflow_bundleid')

    name = applescriptify(name)
    bundleid = applescriptify(bundleid)

    script = AS_CONFIG_UNSET.format(name=name, bundleid=bundleid)

    run_applescript(script) 
Example 7
Project: client   Author: Scorched-Moon   File: app.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, theme=None, **params):
        """Create a new application given the (optional) theme instance."""
        self.set_global_app()

        if (not theme):
            name = os.getenv("PGU_THEME", "").strip()
            if (name):
                # Use the environment variable defined theme
                self.theme = Theme(name)
            else:
                # Default theme
                self.theme = Theme()
        else:
            # Use the user-supplied theme
            self.theme = theme
        
        params['decorate'] = 'app'
        container.Container.__init__(self,**params)
        self._quit = False
        self.widget = None
        self._chsize = False
        self._repaint = False
        
        self.screen = None
        self.container = None 
Example 8
Project: pyblish-win   Author: pyblish   File: test_import.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_imp_module(self):
        # Verify that the imp module can correctly load and find .py files

        # XXX (ncoghlan): It would be nice to use test_support.CleanImport
        # here, but that breaks because the os module registers some
        # handlers in copy_reg on import. Since CleanImport doesn't
        # revert that registration, the module is left in a broken
        # state after reversion. Reinitialising the module contents
        # and just reverting os.environ to its previous state is an OK
        # workaround
        orig_path = os.path
        orig_getenv = os.getenv
        with EnvironmentVarGuard():
            x = imp.find_module("os")
            new_os = imp.load_module("os", *x)
            self.assertIs(os, new_os)
            self.assertIs(orig_path, new_os.path)
            self.assertIsNot(orig_getenv, new_os.getenv) 
Example 9
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: util.py    MIT License 6 votes vote down vote up
def run_trigger(name, bundleid=None, arg=None):
    """Call an Alfred External Trigger.

    .. versionadded:: 1.31

    If ``bundleid`` is not specified, reads the bundle ID of the current
    workflow from Alfred's environment variables.

    Args:
        name (str): Name of External Trigger to call.
        bundleid (str, optional): Bundle ID of workflow trigger belongs to.
        arg (str, optional): Argument to pass to trigger.
    """
    if not bundleid:
        bundleid = os.getenv('alfred_workflow_bundleid')

    if arg:
        arg = 'with argument "{}"'.format(applescriptify(arg))
    else:
        arg = ''

    script = AS_TRIGGER.format(name=name, bundleid=bundleid,
                               arg=arg)

    run_applescript(script) 
Example 10
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: search.py    MIT License 6 votes vote down vote up
def main(wf):
    query = sys.argv[1]
    baseUrl = os.getenv('baseUrl')
    url = baseUrl + 'user?keyword=' + query
    try:
        result = web.get(url=url)
        result.raise_for_status()
        resp = result.text
        userList = json.loads(resp)
        if len(userList) > 0:
            for item in userList:
                title = item['title']
                subtitle = item['subTitle']
                icon = item['icon']
                userId = item['userId']
                copyText = item['copyText']
                qlurl = item['url']
                wf.add_item(title=title, subtitle=subtitle, icon=icon, largetext=title, copytext=copyText, quicklookurl=qlurl, arg=userId, valid=True)
        else:
            wf.add_item(title='找不到联系人…',subtitle='请重新输入')
    except IOError:
        wf.add_item(title='请先启动微信 & 登录…',subtitle='并确保安装微信小助手')

    wf.send_feedback() 
Example 11
Project: FRIDA   Author: LCAV   File: mkl_fft.py    MIT License 6 votes vote down vote up
def load_libmkl():
    if os.name == 'posix':
        try:
            lib_mkl = os.getenv('LIBMKL')
            return _ctypes.cdll.LoadLibrary(lib_mkl)
        except:
            pass
        try:
            return _ctypes.cdll.LoadLibrary("libmkl_rt.dylib")
        except:
            raise ValueError('MKL Library not found')

    else:
        try:
            return _ctypes.cdll.LoadLibrary("mk2_rt.dll")
        except:
            raise ValueError('MKL Library not found') 
Example 12
Project: mmdetection   Author: open-mmlab   File: setup.py    Apache License 2.0 6 votes vote down vote up
def make_cuda_ext(name, module, sources):

    define_macros = []

    if torch.cuda.is_available() or os.getenv('FORCE_CUDA', '0') == '1':
        define_macros += [("WITH_CUDA", None)]
    else:
        raise EnvironmentError('CUDA is required to compile MMDetection!')

    return CUDAExtension(
        name='{}.{}'.format(module, name),
        sources=[os.path.join(*module.split('.'), p) for p in sources],
        define_macros=define_macros,
        extra_compile_args={
            'cxx': [],
            'nvcc': [
                '-D__CUDA_NO_HALF_OPERATORS__',
                '-D__CUDA_NO_HALF_CONVERSIONS__',
                '-D__CUDA_NO_HALF2_OPERATORS__',
            ]
        }) 
Example 13
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: req_file.py    MIT License 6 votes vote down vote up
def expand_env_variables(lines_enum):
    """Replace all environment variables that can be retrieved via `os.getenv`.

    The only allowed format for environment variables defined in the
    requirement file is `${MY_VARIABLE_1}` to ensure two things:

    1. Strings that contain a `$` aren't accidentally (partially) expanded.
    2. Ensure consistency across platforms for requirement files.

    These points are the result of a discusssion on the `github pull
    request #3514 <https://github.com/pypa/pip/pull/3514>`_.

    Valid characters in variable names follow the `POSIX standard
    <http://pubs.opengroup.org/onlinepubs/9699919799/>`_ and are limited
    to uppercase letter, digits and the `_` (underscore).
    """
    for line_number, line in lines_enum:
        for env_var, var_name in ENV_VAR_RE.findall(line):
            value = os.getenv(var_name)
            if not value:
                continue

            line = line.replace(env_var, value)

        yield line_number, line 
Example 14
Project: spqrel_tools   Author: LCAS   File: tmuxinit.py    MIT License 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--pip", type=str, default=os.getenv('PEPPER_IP'),
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--pport", type=int, default=9559,
                        help="Naoqi port number")

    args = parser.parse_args()
    pip = args.pip
    pport = args.pport

    try:
        connection_url = "tcp://" + pip + ":" + str(pport)
        print "Connecting to ",	connection_url
        app = qi.Application(["Init", "--qi-url=" + connection_url ])
    except RuntimeError:
        print ("Can't connect to Naoqi at ip \"" + pip + "\" on port " + str(pport) +".\n"
               "Please check your script arguments. Run with -h option for help.")
        sys.exit(1)

    app.start()
    session = app.session

    do_init() 
Example 15
Project: pnp   Author: HazardDede   File: pnp.py    MIT License 6 votes vote down vote up
def _setup_logging(*candidates, default_level=logging.INFO, env_key='PNP_LOG_CONF', verbose=False):
    """Setup logging configuration"""
    log_file_path = get_first_existing_file(*candidates)
    env_path = os.getenv(env_key, None)
    if env_path:
        log_file_path = env_path
    if log_file_path and os.path.exists(log_file_path):
        with open(log_file_path, 'rt') as fhandle:
            config = yaml.safe_load(fhandle.read())
        logging.config.dictConfig(config)
        logging.info("Logging loaded from: %s", log_file_path)
        if verbose:
            logging.getLogger().setLevel(logging.DEBUG)
    else:
        logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                            level=logging.DEBUG if verbose else default_level)
        logging.info("Logging loaded with basic configuration") 
Example 16
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: model.py    Apache License 2.0 6 votes vote down vote up
def _update_params_on_kvstore_nccl(param_arrays, grad_arrays, kvstore, param_names):
    """Perform update of param_arrays from grad_arrays on NCCL kvstore."""
    valid_indices = [index for index, grad_list in
                     enumerate(grad_arrays) if grad_list[0] is not None]
    valid_grad_arrays = [grad_arrays[i] for i in valid_indices]
    valid_param_arrays = [param_arrays[i] for i in valid_indices]
    valid_param_names = [param_names[i] for i in valid_indices]
    size = len(valid_grad_arrays)
    start = 0
    # Use aggregation by default only with NCCL
    default_batch = '16'
    batch = int(os.getenv('MXNET_UPDATE_AGGREGATION_SIZE', default_batch))
    while start < size:
        end = start + batch if start + batch < size else size
        # push gradient, priority is negative index
        kvstore.push(valid_param_names[start:end], valid_grad_arrays[start:end], priority=-start)
        # pull back the weights
        kvstore.pull(valid_param_names[start:end], valid_param_arrays[start:end], priority=-start)
        start = end 
Example 17
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def get_target_version():
    return os.getenv('LEAPP_DEVEL_TARGET_RELEASE', None) or CURRENT_TARGET_VERSION 
Example 18
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def skip_check():
    """ Check if an environment variable was used to skip this actor """
    if os.getenv('LEAPP_SKIP_CHECK_OS_RELEASE'):
        reporting.create_report([
            reporting.Title('Skipped OS release check'),
            reporting.Summary('Source RHEL release check skipped via LEAPP_SKIP_CHECK_OS_RELEASE env var.'),
            reporting.Severity(reporting.Severity.HIGH),
            reporting.Tags(COMMON_REPORT_TAGS)
        ] + related)

        return True
    return False 
Example 19
Project: leapp-repository   Author: oamg   File: unit_test.py    Apache License 2.0 5 votes vote down vote up
def _get_tool_path(name):
    for directory in os.getenv('LEAPP_COMMON_TOOLS', '').split(':'):
        full_path = os.path.join(directory, name)
        if os.path.isfile(full_path):
            return full_path
    return None 
Example 20
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def add_boot_entry():
    debug = 'debug' if os.getenv('LEAPP_DEBUG', '0') == '1' else ''

    kernel_dst_path, initram_dst_path = get_boot_file_paths()
    try:
        _remove_old_upgrade_boot_entry(kernel_dst_path)
        run([
            '/usr/sbin/grubby',
            '--add-kernel', '{0}'.format(kernel_dst_path),
            '--initrd', '{0}'.format(initram_dst_path),
            '--title', 'RHEL-Upgrade-Initramfs',
            '--copy-default',
            '--make-default',
            '--args', '{DEBUG} enforcing=0 rd.plymouth=0 plymouth.enable=0'.format(DEBUG=debug)
        ])

        if architecture.matches_architecture(architecture.ARCH_S390X):
            # on s390x we need to call zipl explicitly because of issue in grubby,
            # otherwise the new boot entry will not be set as default
            # See https://bugzilla.redhat.com/show_bug.cgi?id=1764306
            run(['/usr/sbin/zipl'])
    except CalledProcessError as e:
        raise StopActorExecutionError(
           'Cannot configure bootloader.',
           details={'details': '{}: {}'.format(str(e), e.stderr)}
        ) 
Example 21
Project: leapp-repository   Author: oamg   File: rhsm.py    Apache License 2.0 5 votes vote down vote up
def skip_rhsm():
    """ Function to check whether we should skip RHSM related code """
    return os.getenv('LEAPP_DEVEL_SKIP_RHSM', '0') == '1' 
Example 22
Project: leapp-repository   Author: oamg   File: overlaygen.py    Apache License 2.0 5 votes vote down vote up
def _overlay_disk_size():
    """
    Convenient function to retrieve the overlay disk size
    """
    try:
        env_size = os.getenv('LEAPP_OVL_SIZE', default='2048')
        disk_size = int(env_size)
    except ValueError:
        disk_size = 2048
        api.current_logger().warn(
            'Invalid "LEAPP_OVL_SIZE" environment variable "%s". Setting default "%d" value', env_size, disk_size
        )
    return disk_size 
Example 23
Project: clikit   Author: sdispater   File: terminal.py    MIT License 5 votes vote down vote up
def width(self):
        width = os.getenv("COLUMNS", "").strip()
        if width:
            return int(width)

        if self._width is None:
            self._init_dimensions()

        return self._width 
Example 24
Project: clikit   Author: sdispater   File: terminal.py    MIT License 5 votes vote down vote up
def height(self):
        height = os.getenv("LINES", "").strip()
        if height:
            return int(height)

        if self._height is None:
            self._init_dimensions()

        return self._height 
Example 25
Project: alfred-yubikey-otp   Author: robertoriv   File: util.py    MIT License 5 votes vote down vote up
def run_trigger(name, bundleid=None, arg=None):
    """Call an Alfred External Trigger.

    .. versionadded:: 1.31

    If ``bundleid`` is not specified, reads the bundle ID of the current
    workflow from Alfred's environment variables.

    Args:
        name (str): Name of External Trigger to call.
        bundleid (str, optional): Bundle ID of workflow trigger belongs to.
        arg (str, optional): Argument to pass to trigger.

    """
    if not bundleid:
        bundleid = os.getenv('alfred_workflow_bundleid')

    if arg:
        arg = 'with argument "{}"'.format(applescriptify(arg))
    else:
        arg = ''

    script = AS_TRIGGER.format(name=name, bundleid=bundleid,
                               arg=arg)

    run_applescript(script) 
Example 26
Project: alfred-yubikey-otp   Author: robertoriv   File: util.py    MIT License 5 votes vote down vote up
def set_config(name, value, bundleid=None, exportable=False):
    """Set a workflow variable in ``info.plist``.

    .. versionadded:: 1.33

    Args:
        name (str): Name of variable to set.
        value (str): Value to set variable to.
        bundleid (str, optional): Bundle ID of workflow variable belongs to.
        exportable (bool, optional): Whether variable should be marked
            as exportable (Don't Export checkbox).

    """
    if not bundleid:
        bundleid = os.getenv('alfred_workflow_bundleid')

    name = applescriptify(name)
    value = applescriptify(value)
    bundleid = applescriptify(bundleid)

    if exportable:
        export = 'exportable true'
    else:
        export = 'exportable false'

    script = AS_CONFIG_SET.format(name=name, bundleid=bundleid,
                                  value=value, export=export)

    run_applescript(script) 
Example 27
Project: alfred-yubikey-otp   Author: robertoriv   File: workflow3.py    MIT License 5 votes vote down vote up
def __init__(self, **kwargs):
        """Create a new :class:`Workflow3` object.

        See :class:`~workflow.Workflow` for documentation.

        """
        Workflow.__init__(self, **kwargs)
        self.variables = {}
        self._rerun = 0
        # Get session ID from environment if present
        self._session_id = os.getenv('_WF_SESSION_ID') or None
        if self._session_id:
            self.setvar('_WF_SESSION_ID', self._session_id) 
Example 28
Project: pyblish-win   Author: pyblish   File: tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _candidate_tempdir_list():
    """Generate a list of candidate temporary directories which
    _get_default_tempdir will try."""

    dirlist = []

    # First, try the environment.
    for envname in 'TMPDIR', 'TEMP', 'TMP':
        dirname = _os.getenv(envname)
        if dirname: dirlist.append(dirname)

    # Failing that, try OS-specific locations.
    if _os.name == 'riscos':
        dirname = _os.getenv('Wimp$ScrapDir')
        if dirname: dirlist.append(dirname)
    elif _os.name == 'nt':
        dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
    else:
        dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])

    # As a last resort, the current directory.
    try:
        dirlist.append(_os.getcwd())
    except (AttributeError, _os.error):
        dirlist.append(_os.curdir)

    return dirlist 
Example 29
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_wanted_dirs(self):
        # _candidate_tempdir_list contains the expected directories

        # Make sure the interesting environment variables are all set.
        with support.EnvironmentVarGuard() as env:
            for envname in 'TMPDIR', 'TEMP', 'TMP':
                dirname = os.getenv(envname)
                if not dirname:
                    env[envname] = os.path.abspath(envname)

            cand = tempfile._candidate_tempdir_list()

            for envname in 'TMPDIR', 'TEMP', 'TMP':
                dirname = os.getenv(envname)
                if not dirname: raise ValueError
                self.assertIn(dirname, cand)

            try:
                dirname = os.getcwd()
            except (AttributeError, os.error):
                dirname = os.curdir

            self.assertIn(dirname, cand)

            # Not practical to try to verify the presence of OS-specific
            # paths in this list. 
Example 30
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_call_kwargs(self):
        # call() function with keyword args
        newenv = os.environ.copy()
        newenv["FRUIT"] = "banana"
        rc = subprocess.call([sys.executable, "-c",
                              'import sys, os;'
                              'sys.exit(os.getenv("FRUIT")=="banana")'],
                             env=newenv)
        self.assertEqual(rc, 1) 
Example 31
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_env(self):
        newenv = os.environ.copy()
        newenv["FRUIT"] = "orange"
        p = subprocess.Popen([sys.executable, "-c",
                          'import sys,os;'
                          'sys.stdout.write(os.getenv("FRUIT"))'],
                         stdout=subprocess.PIPE,
                         env=newenv)
        self.addCleanup(p.stdout.close)
        self.assertEqual(p.stdout.read(), "orange") 
Example 32
Project: drydock   Author: airshipit   File: conftest.py    Apache License 2.0 5 votes vote down vote up
def input_files(tmpdir_factory, request):
    tmpdir = tmpdir_factory.mktemp('data')
    samples_dir = os.path.dirname(os.getenv('YAMLDIR'))
    samples = os.listdir(samples_dir)

    for f in samples:
        src_file = samples_dir + "/" + f
        dst_file = str(tmpdir) + "/" + f
        shutil.copyfile(src_file, dst_file)

    return tmpdir 
Example 33
Project: azure-cred-manager   Author: gregohardy   File: credentials.py    Apache License 2.0 5 votes vote down vote up
def save_config(creds_dict, agent):
    path = "{0}/.azure/".format(os.getenv("HOME"))
    filename = get_azure_config_file_name(agent)

    if not os.path.isdir(path):
        try:
            os.mkdir(path)
        except OSError as e:
            raise Exception("Failed to create the directory [{0}] to save credentials."
                            " Please either move it or alter the permissions")

    with open(path+filename, 'w') as f:
        f.write(json.dumps(creds_dict))
        f.flush() 
Example 34
Project: azure-cred-manager   Author: gregohardy   File: credentials.py    Apache License 2.0 5 votes vote down vote up
def load_config(agent):
    path = "{0}/.azure/".format(os.getenv("HOME"))
    filename = get_azure_config_file_name(agent)

    if filename is None or not os.path.isfile(path+filename):
        return {}

    with open(path+filename, 'r') as f:
        data = f.read()

    return json.loads(data) 
Example 35
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: convert.py    MIT License 5 votes vote down vote up
def main(wf):
    userId = os.getenv('userId')
    data = wf.stored_data('wechat_search_user_list')
    for item in data:
        if item['userId'] == userId:
            title = 'To:'+item['title']
            subTitle = item['subTitle']
            icon = item['icon']
            wf.add_item(title=title, subtitle=subTitle, icon=icon, valid=True, arg=sys.argv[1])
    wf.send_feedback() 
Example 36
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: userChatLog.py    MIT License 5 votes vote down vote up
def main(wf):
    userId = os.getenv('userId')
    baseUrl = os.getenv('baseUrl')
    url = baseUrl + 'chatlog?userId=' + userId + '&count=45'
    try:
        result = web.get(url=url)
        result.raise_for_status()
        resp = result.text
        userList = json.loads(resp)
        if len(userList) > 0:
            wf.store_data('wechat_send_content',sys.argv[1])
            for item in userList:
                title = item['title']
                subtitle = item['subTitle']
                icon = item['icon']
                userId = item['userId']
                copyText = item['copyText']
                qlurl = item['url']
                srvId = str(item['srvId'])
                titleLen = len(title)
                lineNun = 70
                if titleLen < lineNun:
                    largetext = title
                else:
                    titleArray = []
                    for n in range(titleLen):
                        if n % lineNun == 0:
                            titleArray.append(title[n:n+lineNun])
                    largetext='\n'.join(titleArray)
                wf.add_item(title=title, subtitle=subtitle, icon=icon, valid=True, largetext=largetext, quicklookurl=qlurl, copytext=copyText, arg=srvId)
        else:
            wf.add_item(title='找不到联系人…',subtitle='请重新输入')
    except IOError:
                wf.add_item(title='请先启动微信 & 登录…',subtitle='并确保安装微信小助手')

    wf.send_feedback() 
Example 37
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow3.py    MIT License 5 votes vote down vote up
def __init__(self, **kwargs):
        """Create a new :class:`Workflow3` object.

        See :class:`~workflow.Workflow` for documentation.

        """
        Workflow.__init__(self, **kwargs)
        self.variables = {}
        self._rerun = 0
        # Get session ID from environment if present
        self._session_id = os.getenv('_WF_SESSION_ID') or None
        if self._session_id:
            self.setvar('_WF_SESSION_ID', self._session_id) 
Example 38
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: sendMsg.py    MIT License 5 votes vote down vote up
def main(wf):
    srvId = sys.argv[1]
    userId = os.getenv('userId')
    baseUrl = os.getenv('baseUrl')
    msgContent = wf.stored_data('wechat_send_content')

    url = baseUrl + 'send-message'
    data = {'userId':userId, 'content': msgContent, 'srvId': srvId}
    r = web.post(url=url,data=data)
    r.raise_for_status()
    wf.send_feedback() 
Example 39
Project: earlgrey   Author: icon-project   File: message_queue_connection.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, amqp_target, route_key, username=None, password=None):
        self._amqp_target = amqp_target
        self._route_key = route_key

        self._username = username or os.getenv("AMQP_USERNAME", "guest")
        self._password = password or os.getenv("AMQP_PASSWORD", "guest")

        self._connection: RobustConnection = None
        self._channel: RobustChannel = None

        self._async_info: MessageQueueInfoAsync = None 
Example 40
Project: btrecon   Author: jgamblin   File: btrecon.py    MIT License 5 votes vote down vote up
def color(text, color_code):
    if sys.platform == "win32" and os.getenv("TERM") != "xterm":
        return text

    return '\x1b[%dm%s\x1b[0m' % (color_code, text) 
Example 41
Project: kuaa   Author: rafaelwerneck   File: termcolor.py    GNU General Public License v3.0 5 votes vote down vote up
def colored(text, color=None, on_color=None, attrs=None):
    """Colorize text.

    Available text colors:
        red, green, yellow, blue, magenta, cyan, white.

    Available text highlights:
        on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

    Available attributes:
        bold, dark, underline, blink, reverse, concealed.

    Example:
        colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
        colored('Hello, World!', 'green')
    """
    if os.getenv('ANSI_COLORS_DISABLED') is None:
        fmt_str = '\033[%dm%s'
        if color is not None:
            text = fmt_str % (COLORS[color], text)

        if on_color is not None:
            text = fmt_str % (HIGHLIGHTS[on_color], text)

        if attrs is not None:
            for attr in attrs:
                text = fmt_str % (ATTRIBUTES[attr], text)

        text += RESET
    return text 
Example 42
Project: ups-utils   Author: Ricks-Lab   File: env.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        # Utility Path Definitions
        self.repository_module_path = os.path.dirname(str(Path(__file__).resolve()))
        self.repository_path = os.path.join(self.repository_module_path, '..')
        self.config_dir = os.path.join(os.getenv('HOME'), '.ups-utils/')
        self.dist_share = '/usr/share/ricks-ups-utils/'
        self.dist_icons = os.path.join(self.dist_share, 'icons')
        if os.path.isdir(self.dist_icons):
            self.icon_path = self.dist_icons
        else:
            self.icon_path = os.path.join(self.repository_path, 'icons')

        # Configuration Parameters
        self.ERROR_config = False
        self.ERROR_json = False
        self.UPS_LIST_JSON_FILE = 'config.json'
        # Config.py defaults
        self.DEFAULT_MONITOR_READ_INTERVAL = 10
        self.DEFAULT_DAEMON_READ_INTERVAL = 30
        self.READ_INTERVAL_LIMIT = 5
        # Daemon and Monitor defaults in tuples (critical, warning, limit)
        self.def_threshold_battery_time_rem = (5, 10, 4)
        self.def_threshold_time_on_battery = (5, 3, 1)
        self.def_threshold_battery_load = (90, 80, 10)
        self.def_threshold_battery_capacity = (10, 50, 5)

        # Utility Execution Flags
        self.show_unresponsive = False
        self.DEBUG = False
        self.LOG = False
        self.log_file_ptr = ''
        self.USELTZ = False
        self.LTZ = datetime.utcnow().astimezone().tzinfo
        if self.DEBUG: print('Local TZ: %s' % str(self.LTZ)) 
Example 43
Project: sbo-templates   Author: dslackw   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def __templatesInit(self):
        """Initialiazation templates data
        """
        self.filename = ""
        self.msg = ""
        self.data = []
        self.height = 30
        self.width = 80
        self.app = self.args[0]
        self.handy_ruler = 1
        self.__slackDescComments()
        self.maintainer = '""'
        self.email = '""'
        self.live = ""
        self.editor = "vim"
        self.HOME = os.getenv("HOME") + "/"
        self.filename = "{0}.sbo-maintainer".format(self.HOME)
        self.__maintainerInit()
        self.choises = [
            ("Info", "Edit {0}.info file".format(self.app)),
            ("README", "Edit README file"),
            ("Desktop", "Edit {0}.desktop file".format(self.app)),
            ("Doinst.sh", "Edit doinst.sh script"),
            ("Slack desc", "Edit slack-desc file"),
            ("SlackBuild", "Edit {0}.SlackBuild script".format(
                self.app)),
            ("Chmod", "Permissions -+ {0}.SlackBuild script".format(self.app)),
            ("Download", "Download the sources"),
            ("MD5SUM", "Checksum the sources"),
            ("Maintainer", "Maintainer data"),
            ("Directory", "Change directory"),
            ("Help", "Where to get help"),
            ("Exit", "Exit the program")
        ] 
Example 44
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 5 votes vote down vote up
def user_config_dir(appname=None, appauthor=None, version=None, roaming=False):
    r"""Return full path to the user-specific config dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "roaming" (boolean, default False) can be set True to use the Windows
            roaming appdata directory. That means that for users on a Windows
            network setup for roaming profiles, this user data will be
            sync'd on login. See
            <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
            for a discussion of issues.

    Typical user data directories are:
        Mac OS X:               same as user_data_dir
        Unix:                   ~/.config/<AppName>     # or in $XDG_CONFIG_HOME, if defined
        Win *:                  same as user_data_dir

    For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
    That means, by deafult "~/.config/<AppName>".
    """
    if system in ["win32", "darwin"]:
        path = user_data_dir(appname, appauthor, None, roaming)
    else:
        path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
        if appname:
            path = os.path.join(path, appname)
    if appname and version:
        path = os.path.join(path, version)
    return path 
Example 45
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 5 votes vote down vote up
def user_state_dir(appname=None, appauthor=None, version=None, roaming=False):
    r"""Return full path to the user-specific state dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "roaming" (boolean, default False) can be set True to use the Windows
            roaming appdata directory. That means that for users on a Windows
            network setup for roaming profiles, this user data will be
            sync'd on login. See
            <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
            for a discussion of issues.

    Typical user state directories are:
        Mac OS X:  same as user_data_dir
        Unix:      ~/.local/state/<AppName>   # or in $XDG_STATE_HOME, if defined
        Win *:     same as user_data_dir

    For Unix, we follow this Debian proposal <https://wiki.debian.org/XDGBaseDirectorySpecification#state>
    to extend the XDG spec and support $XDG_STATE_HOME.

    That means, by default "~/.local/state/<AppName>".
    """
    if system in ["win32", "darwin"]:
        path = user_data_dir(appname, appauthor, None, roaming)
    else:
        path = os.getenv('XDG_STATE_HOME', os.path.expanduser("~/.local/state"))
        if appname:
            path = os.path.join(path, appname)
    if appname and version:
        path = os.path.join(path, version)
    return path 
Example 46
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 5 votes vote down vote up
def site_config_dirs(appname):
    r"""Return a list of potential user-shared config dirs for this application.

        "appname" is the name of application.

    Typical user config directories are:
        macOS:      /Library/Application Support/<AppName>/
        Unix:       /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in
                    $XDG_CONFIG_DIRS
        Win XP:     C:\Documents and Settings\All Users\Application ...
                    ...Data\<AppName>\
        Vista:      (Fail! "C:\ProgramData" is a hidden *system* directory
                    on Vista.)
        Win 7:      Hidden, but writeable on Win 7:
                    C:\ProgramData\<AppName>\
    """
    if WINDOWS:
        path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA"))
        pathlist = [os.path.join(path, appname)]
    elif sys.platform == 'darwin':
        pathlist = [os.path.join('/Library/Application Support', appname)]
    else:
        # try looking in $XDG_CONFIG_DIRS
        xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
        if xdg_config_dirs:
            pathlist = [
                os.path.join(expanduser(x), appname)
                for x in xdg_config_dirs.split(os.pathsep)
            ]
        else:
            pathlist = []

        # always look in /etc directly as well
        pathlist.append('/etc')

    return pathlist


# -- Windows support functions -- 
Example 47
Project: securionpay-python   Author: securionpay   File: testcase.py    MIT License 5 votes vote down vote up
def setUp(self):
        api.private_key = os.getenv("PRIVATE_KEY") 
Example 48
Project: interact   Author: dongshengmu   File: util.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.has_sudo = os.getenv('SUDO_COMMAND') is not None
        self.was_user = False 
Example 49
Project: interact   Author: dongshengmu   File: util.py    MIT License 5 votes vote down vote up
def abspath(filepath):
    """Return the absolute pathname for names with '~', '.', '$SRXSRC/obj_sa/bin/flowd'"""
    if '$' in filepath:
        filepath = os.sep.join([os.getenv(x[1:]) if '$' in x else x
                                for x in filepath.split(os.sep)]).replace(os.sep * 2, os.sep)
    if '~' in filepath:
        filepath = os.path.expanduser(filepath)
    return os.path.abspath(filepath) 
Example 50
Project: interact   Author: dongshengmu   File: util.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.has_sudo = os.getenv('SUDO_COMMAND') is not None
        self.was_user = False 
Example 51
Project: interact   Author: dongshengmu   File: util.py    MIT License 5 votes vote down vote up
def abspath(filepath):
    """Return the absolute pathname for names with '~', '.', '$SRXSRC/obj_sa/bin/flowd'"""
    if '$' in filepath:
        filepath = os.sep.join([os.getenv(x[1:]) if '$' in x else x
                                for x in filepath.split(os.sep)]).replace(os.sep * 2, os.sep)
    if '~' in filepath:
        filepath = os.path.expanduser(filepath)
    return os.path.abspath(filepath) 
Example 52
Project: spqrel_tools   Author: LCAS   File: spqrel_ros.py    MIT License 5 votes vote down vote up
def __init__(self, session):
        self.session = session
        self.memory_service = session.service("ALMemory")
        self.veply_q2r = Qi2RosStringTopic(
            self.memory_service, 'Veply', latch=True
        )
        self.person_ana_r2q = Ros2QiStringTopic(
            self.memory_service, '/person_analysis_outcome'
        )
        self.trigger_tobi = Qi2RosStringTopic(
            self.memory_service, 'trigger_person_analysis'
        )

        self.ip = os.getenv("PEPPER_IP", default="127.0.0.1")
        self.website_pub = rospy.Publisher('/qi/webview/url',
                                           String, queue_size=1)

        self.trigger_tobi_sub = rospy.Subscriber(
            '/qi/trigger_person_analysis',
            String, self._trigger_tobi)
        self.trigger_tobi_sub = rospy.Subscriber(
            '/detect_people/fusion',
            String, self._stop_tobi)

        self._image_pub = rospy.Publisher(
            '/spqrel/camera/image_raw',
            Image, queue_size=1)
        self._depth_pub = rospy.Publisher(
            '/spqrel/depth/image_raw',
            Image, queue_size=1)
        self._depth_info_pub = rospy.Publisher(
            '/spqrel/depth/camera_info',
            CameraInfo, queue_size=1)

        self._image_counter = 0
        self._depth_counter = 0
        self._THROTTLE = 5 
Example 53
Project: flasky   Author: RoseOu   File: firefox_binary.py    MIT License 5 votes vote down vote up
def _default_windows_location(self):
        program_files = [os.getenv("PROGRAMFILES", r"C:\Program Files"),
                         os.getenv("PROGRAMFILES(X86)", r"C:\Program Files (x86)")]
        for path in program_files:
            binary_path = os.path.join(path, r"Mozilla Firefox\firefox.exe")
            if os.access(binary_path, os.X_OK):
                return binary_path
        return "" 
Example 54
Project: isolate   Author: itsumma   File: auth-manager.py    MIT License 5 votes vote down vote up
def __init__(self, params):
        self.params = params
        self.action = self.params['action'][0]
        self.redis = Redis(host=os.getenv('ISOLATE_REDIS_HOST', '127.0.0.1'),
                           port=int(os.getenv('ISOLATE_REDIS_PORT', 6379)),
                           password=os.getenv('ISOLATE_REDIS_PASS', None),
                           db=int(os.getenv('ISOLATE_REDIS_DB', 0)))
        self.validate_params()
        self.geoip = IsolateGeoIP() 
Example 55
Project: isolate   Author: itsumma   File: auth-manager.py    MIT License 5 votes vote down vote up
def main():
    arg_parser = argparse.ArgumentParser(prog='auth-manager', epilog='------',
                                         description='Auth management shell helper')
    arg_parser.add_argument('action', type=str, nargs=1, default=[None])
    arg_parser.add_argument('--project', type=str, nargs=1,
                            default=[os.getenv('ISOLATE_DEFAULT_PROJECT', 'main')])
    arg_parser.add_argument('--server-name', type=str, nargs=1, default=[None])
    arg_parser.add_argument('--ip', '--server-ip', type=str, nargs=1, default=[None])

    arg_parser.add_argument('--port', '--server-port', type=int, nargs=1, default=[None])
    arg_parser.add_argument('--user', type=str, nargs=1, default=[None])
    arg_parser.add_argument('--nosudo', action='store_true', default=None)

    arg_parser.add_argument('--proxy-id', type=int, nargs=1,
                            default=[None], help="server_id of proxy")
    arg_parser.add_argument('--server-id', type=int, nargs=1, default=[None],
                            help="server_id (for del-host)")

    arg_parser.add_argument('--debug', action='store_true')
    args = arg_parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S')
    else:
        logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S')

    am = AuthManager(args.__dict__)
    am.process_args() 
Example 56
Project: isolate   Author: itsumma   File: IsolateCore.py    MIT License 5 votes vote down vote up
def __init__(self, **kwargs):
        self.kwargs = kwargs
        self.ASN_DB = os.getenv('ISOLATE_GEOIP_ASN', '/opt/auth/shared/geoip/GeoIPASNum.dat')
        self.asn = GeoIP.open(self.ASN_DB, GeoIP.GEOIP_STANDARD) 
Example 57
Project: isolate   Author: itsumma   File: helper.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.hosts_dump = list()
        self.hosts_dict = dict()
        self.projects = list()
        self.zapi = ZabbixAPI(os.getenv('ISOLATE_ZABBIX_URL'))
        self.zapi.login(os.getenv('ISOLATE_ZABBIX_USER'), os.getenv('ISOLATE_ZABBIX_PASS')) 
Example 58
Project: isolate   Author: itsumma   File: helper.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.projects = list()
        self.hosts_dump = list()
        self.redis = Redis(host=os.getenv('ISOLATE_REDIS_HOST', '127.0.0.1'),
                           port=int(os.getenv('ISOLATE_REDIS_PORT', 6379)),
                           password=os.getenv('ISOLATE_REDIS_PASS', None),
                           db=int(os.getenv('ISOLATE_REDIS_DB', 0))) 
Example 59
Project: cmus-osx   Author: PhilipTrauner   File: util.py    MIT License 5 votes vote down vote up
def locate_editor() -> Optional[Path]:
    for editor in (getenv("VISUAL", None), getenv("EDITOR", None), "nano", "vim", "vi"):
        if editor is not None:
            # Might be absolute path to editor
            editor_path = Path(editor).expanduser()
            # Might also be just the binary name
            editor_which = which(editor)

            if editor_path.is_file():
                return editor_path
            elif editor_which is not None:
                return Path(editor_which)
    return None 
Example 60
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: mxdoc.py    Apache License 2.0 5 votes vote down vote up
def setup(app):

    # If MXNET_DOCS_BUILD_MXNET is set something different than 1
    # Skip the build step
    if os.getenv('MXNET_DOCS_BUILD_MXNET', '1') == '1' or _MXNET_DOCS_BUILD_MXNET:
        print("Building MXNet!")
        app.connect("builder-inited", build_mxnet)
    if _DOXYGEN_DOCS:
        print("Building Doxygen!")
        app.connect("builder-inited", generate_doxygen)
    if _SCALA_DOCS or _CLOJURE_DOCS:
        print("Building Scala!")
        app.connect("builder-inited", build_scala)
    if _SCALA_DOCS:
        print("Building Scala Docs!")
        app.connect("builder-inited", build_scala_docs)
    if _CLOJURE_DOCS:
        print("Building Clojure Docs!")
        app.connect("builder-inited", build_clojure_docs)
    if _R_DOCS:
        print("Building R Docs!")
        app.connect("builder-inited", build_r_docs)
    app.connect('source-read', convert_table)
    app.connect('source-read', add_buttons)
    app.add_config_value('recommonmark_config', {
        'url_resolver': lambda url: 'http://mxnet.io/' + url,
        'enable_eval_rst': True,
    }, True)
    app.add_transform(transform.AutoStructify) 
Example 61
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: base.py    Apache License 2.0 5 votes vote down vote up
def data_dir():
    """

    :return: data directory in the filesystem for storage, for example when downloading models
    """
    return os.getenv('MXNET_HOME', data_dir_default()) 
Example 62
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: common.py    Apache License 2.0 5 votes vote down vote up
def assert_raises_cudnn_disabled():
    def test_helper(orig_test):
        @make_decorator(orig_test)
        def test_new(*args, **kwargs):
            cudnn_disabled = (os.getenv('CUDNN_OFF_TEST_ONLY') == "true")
            if not cudnn_disabled or mx.context.current_context().device_type == 'cpu':
                orig_test(*args, **kwargs)
            else:
                errors = (MXNetError, RuntimeError)
                assert_raises(errors, orig_test, *args, **kwargs)
        return test_new
    return test_helper 
Example 63
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: common.py    Apache License 2.0 5 votes vote down vote up
def assert_raises_cudnn_disabled():
    def test_helper(orig_test):
        @make_decorator(orig_test)
        def test_new(*args, **kwargs):
            cudnn_disabled = (os.getenv('CUDNN_OFF_TEST_ONLY') == "true")
            if not cudnn_disabled or mx.context.current_context().device_type == 'cpu':
                orig_test(*args, **kwargs)
            else:
                errors = (MXNetError, RuntimeError)
                assert_raises(errors, orig_test, *args, **kwargs)
        return test_new
    return test_helper 
Example 64
Project: incubator-spot   Author: apache   File: start_listener.py    Apache License 2.0 4 votes vote down vote up
def main():
    '''
        Main command-line entry point.
    '''
    state = {}

    try:
        args = parse_args()
        conf = json.loads(args.config_file.read())

        # .............................check kerberos authentication
        if os.getenv('KRB_AUTH'):
            kb = Kerberos()
            kb.authenticate()

        state.update(**args.__dict__)

        # .............................add Spark Streaming parameters
        for key in conf['spark-streaming'].keys():
            if conf['spark-streaming'][key] == None:
                continue

            if isinstance(conf['spark-streaming'][key], basestring):
                conf['spark-streaming'][key] = conf['spark-streaming'][key].strip()

                if bool(conf['spark-streaming'][key]):
                    state[key] = conf['spark-streaming'][key]
                continue
            state[key] = conf['spark-streaming'][key]

        # .............................add files to place on the PYTHONPATH
        state['py_files'] = ','.join([os.path.abspath(os.path.join('dist', x)) for x in os.listdir('dist')])

        # .............................add database name
        state['database'] = conf['dbname']

        # .............................add zookeeper's connection string
        state['zkquorum'] = '{0}:{1}'.format(conf['kafka']['zookeper_server'],
                                        conf['kafka']['zookeper_port'])

        spark_job('common/listener.py', **state)

    except SystemExit: raise
    except:
        sys.excepthook(*sys.exc_info())
        sys.exit(1) 
Example 65
Project: incubator-spot   Author: apache   File: collector.py    Apache License 2.0 4 votes vote down vote up
def run(cls):
        '''
            Main command-line entry point.

        :param cls: The class as implicit first argument.
        '''
        try:
            args  = _parse_args()
            conf  = json.loads(args.config_file.read())

            # .........................set up logger
            Util.get_logger('SPOT', args.log_level)

            # .........................check kerberos authentication
            if os.getenv('KRB_AUTH'):
                kb = Kerberos()
                kb.authenticate()

            conf['producer'] = {
                'bootstrap_servers': ['{0}:{1}'
                    .format(conf['kafka']['kafka_server'], conf['kafka']['kafka_port'])]
            }

            conf['file_watcher'] = {
                'path': conf['pipelines'][args.type]['collector_path'],
                'supported_files': conf['pipelines'][args.type]['supported_files'],
                'recursive': True
            }

            # .........................migrate configs
            if not 'local_staging' in conf['pipelines'][args.type].keys():
                conf['pipelines'][args.type]['local_staging'] = '/tmp'

            if 'max_request_size' in conf['kafka'].keys():
                conf['producer']['max_request_size'] = conf['kafka']['max_request_size']

            if not 'process_opt' in conf['pipelines'][args.type].keys():
                conf['pipelines'][args.type]['process_opt'] = ''

            if 'recursive' in conf['pipelines'][args.type].keys():
                conf['file_watcher']['recursive'] = conf['pipelines'][args.type]['recursive']

            collector = cls(args.type, args.topic, args.skip_conversion, **conf)
            collector.start()

        except SystemExit: raise
        except:
            sys.excepthook(*sys.exc_info())
            sys,exit(1) 
Example 66
Project: prediction-constrained-topic-models   Author: dtak   File: pprint_logging.py    MIT License 4 votes vote down vote up
def config_pprint_logging(
        output_path='/tmp/',
        do_write_txtfile=True,
        do_write_stdout=True,
        txtfile='stdout.txt',
        ):
    global RootLog
    RootLog = logging.getLogger('pprint_logging')
    RootLog.handlers = []
    RootLog.setLevel(logging.DEBUG)

    formatter = logging.Formatter('%(message)s')
    # Config logger to save transcript of log messages to plain-text file
    if do_write_txtfile:
        fh = logging.FileHandler(os.path.join(output_path, txtfile))
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        RootLog.addHandler(fh)
    # Config logger that can write to stdout
    if do_write_stdout:
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        RootLog.addHandler(ch)

    # Config null logger, avoids error messages about no handler existing
    if not do_write_txtfile and not do_write_stdout:
        RootLog.addHandler(logging.NullHandler())

    '''
    # Prepare special logs if we are running on the Brown CS grid
    try:
        jobID = int(os.getenv('JOB_ID'))
    except TypeError:
        jobID = 0
    if jobID > 0:
        Log.info('SGE Grid Job ID: %d' % (jobID))

        if 'SGE_STDOUT_PATH' in os.environ:
            # Create symlinks to captured stdout, stdout in output directory
            os.symlink(os.getenv('SGE_STDOUT_PATH'),
                       os.path.join(taskoutpath, 'stdout'))
            os.symlink(os.getenv('SGE_STDERR_PATH'),
                       os.path.join(taskoutpath, 'stderr'))

            with open(os.path.join(taskoutpath, 'GridInfo.txt'), 'w') as f:
                f.write(str(jobID) + "\n")
                f.write(str(taskid) + "\n")
                f.write('stdout: ' + os.getenv('SGE_STDOUT_PATH') + "\n")
                f.write('stderr: ' + os.getenv('SGE_STDERR_PATH') + "\n")
    return jobID
    ''' 
Example 67
Project: iSDX   Author: sdn-ixp   File: gauge.py    Apache License 2.0 4 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(Gauge, self).__init__(*args, **kwargs)

        self.config_file = os.getenv(
            'GAUGE_CONFIG', '/etc/sdx/gauge.conf')
        self.config_dir = os.path.dirname(self.config_file)
        self.exc_logfile = os.getenv(
            'GAUGE_EXCEPTION_LOG', '/var/log/sdx/gauge_exception.log')
        self.logfile = os.getenv('GAUGE_LOG', '/var/log/sdx/gauge.log')

        # Setup logging
        self.logger = logging.getLogger(__name__)
        logger_handler = TimedRotatingFileHandler(
            self.logfile,
            when='midnight')
        log_fmt = '%(asctime)s %(name)-6s %(levelname)-8s %(message)s'
        date_fmt = '%b %d %H:%M:%S'
        default_formatter = logging.Formatter(log_fmt, date_fmt)
        logger_handler.setFormatter(default_formatter)
        self.logger.addHandler(logger_handler)
        self.logger.propagate = 0

        # Set up separate logging for exceptions
        exc_logger = logging.getLogger(self.exc_logname)
        exc_logger_handler = logging.FileHandler(self.exc_logfile)
        exc_logger_handler.setFormatter(
            logging.Formatter(log_fmt, date_fmt))
        exc_logger.addHandler(exc_logger_handler)
        exc_logger.propagate = 1
        exc_logger.setLevel(logging.ERROR)

        # Prevent printing of the 'Starting new HTTP requests' messages
        logging.getLogger("urllib3").setLevel(logging.WARNING)

        self.dps = {}
        with open(self.config_file, 'r') as config_file:
            for dp_conf_file in config_file:
                # config_file should be a list of faucet config filenames
                # separated by linebreaks
                dp = DP.parser(os.path.join(self.config_dir, dp_conf_file.strip()), self.logname)
                try:
                    dp.sanity_check()
                except AssertionError:
                    self.logger.exception(
                        "Error in config file {0}".format(dp_conf_file))
                else:
                    self.dps[dp.dp_id] = dp

        # Create dpset object for querying Ryu's DPSet application
        self.dpset = kwargs['dpset']

        # dict of polling threads:
        # polling threads are indexed by dp_id and then by name
        # eg: self.pollers[0x1]['port_stats']
        self.pollers = {}
        # dict of async event handlers
        self.handlers = {} 
Example 68
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def user_data_dir(appname=None, appauthor=None, version=None, roaming=False):
    r"""Return full path to the user-specific data dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "roaming" (boolean, default False) can be set True to use the Windows
            roaming appdata directory. That means that for users on a Windows
            network setup for roaming profiles, this user data will be
            sync'd on login. See
            <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
            for a discussion of issues.

    Typical user data directories are:
        Mac OS X:               ~/Library/Application Support/<AppName>
        Unix:                   ~/.local/share/<AppName>    # or in $XDG_DATA_HOME, if defined
        Win XP (not roaming):   C:\Documents and Settings\<username>\Application Data\<AppAuthor>\<AppName>
        Win XP (roaming):       C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>
        Win 7  (not roaming):   C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>
        Win 7  (roaming):       C:\Users\<username>\AppData\Roaming\<AppAuthor>\<AppName>

    For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
    That means, by default "~/.local/share/<AppName>".
    """
    if system == "win32":
        if appauthor is None:
            appauthor = appname
        const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
        path = os.path.normpath(_get_win_folder(const))
        if appname:
            if appauthor is not False:
                path = os.path.join(path, appauthor, appname)
            else:
                path = os.path.join(path, appname)
    elif system == 'darwin':
        path = os.path.expanduser('~/Library/Application Support/')
        if appname:
            path = os.path.join(path, appname)
    else:
        path = os.getenv('XDG_DATA_HOME', os.path.expanduser("~/.local/share"))
        if appname:
            path = os.path.join(path, appname)
    if appname and version:
        path = os.path.join(path, version)
    return path 
Example 69
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def site_config_dir(appname=None, appauthor=None, version=None, multipath=False):
    """Return full path to the user-shared data dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "multipath" is an optional parameter only applicable to *nix
            which indicates that the entire list of config dirs should be
            returned. By default, the first item from XDG_CONFIG_DIRS is
            returned, or '/etc/xdg/<AppName>', if XDG_CONFIG_DIRS is not set

    Typical user data directories are:
        Mac OS X:   same as site_data_dir
        Unix:       /etc/xdg/<AppName> or $XDG_CONFIG_DIRS[i]/<AppName> for each value in
                    $XDG_CONFIG_DIRS
        Win *:      same as site_data_dir
        Vista:      (Fail! "C:\ProgramData" is a hidden *system* directory on Vista.)

    For Unix, this is using the $XDG_CONFIG_DIRS[0] default, if multipath=False

    WARNING: Do not use this on Windows. See the Vista-Fail note above for why.
    """
    if system in ["win32", "darwin"]:
        path = site_data_dir(appname, appauthor)
        if appname and version:
            path = os.path.join(path, version)
    else:
        # XDG default for $XDG_CONFIG_DIRS
        # only first, if multipath is False
        path = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
        pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)]
        if appname:
            if version:
                appname = os.path.join(appname, version)
            pathlist = [os.sep.join([x, appname]) for x in pathlist]

        if multipath:
            path = os.pathsep.join(pathlist)
        else:
            path = pathlist[0]
    return path 
Example 70
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True):
    r"""Return full path to the user-specific cache dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "opinion" (boolean) can be False to disable the appending of
            "Cache" to the base app data dir for Windows. See
            discussion below.

    Typical user cache directories are:
        Mac OS X:   ~/Library/Caches/<AppName>
        Unix:       ~/.cache/<AppName> (XDG default)
        Win XP:     C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Cache
        Vista:      C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Cache

    On Windows the only suggestion in the MSDN docs is that local settings go in
    the `CSIDL_LOCAL_APPDATA` directory. This is identical to the non-roaming
    app data dir (the default returned by `user_data_dir` above). Apps typically
    put cache data somewhere *under* the given dir here. Some examples:
        ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
        ...\Acme\SuperApp\Cache\1.0
    OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
    This can be disabled with the `opinion=False` option.
    """
    if system == "win32":
        if appauthor is None:
            appauthor = appname
        path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
        if appname:
            if appauthor is not False:
                path = os.path.join(path, appauthor, appname)
            else:
                path = os.path.join(path, appname)
            if opinion:
                path = os.path.join(path, "Cache")
    elif system == 'darwin':
        path = os.path.expanduser('~/Library/Caches')
        if appname:
            path = os.path.join(path, appname)
    else:
        path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
        if appname:
            path = os.path.join(path, appname)
    if appname and version:
        path = os.path.join(path, version)
    return path 
Example 71
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def user_data_dir(appname=None, appauthor=None, version=None, roaming=False):
    r"""Return full path to the user-specific data dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "roaming" (boolean, default False) can be set True to use the Windows
            roaming appdata directory. That means that for users on a Windows
            network setup for roaming profiles, this user data will be
            sync'd on login. See
            <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
            for a discussion of issues.

    Typical user data directories are:
        Mac OS X:               ~/Library/Application Support/<AppName>
        Unix:                   ~/.local/share/<AppName>    # or in $XDG_DATA_HOME, if defined
        Win XP (not roaming):   C:\Documents and Settings\<username>\Application Data\<AppAuthor>\<AppName>
        Win XP (roaming):       C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>
        Win 7  (not roaming):   C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>
        Win 7  (roaming):       C:\Users\<username>\AppData\Roaming\<AppAuthor>\<AppName>

    For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
    That means, by default "~/.local/share/<AppName>".
    """
    if system == "win32":
        if appauthor is None:
            appauthor = appname
        const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
        path = os.path.normpath(_get_win_folder(const))
        if appname:
            if appauthor is not False:
                path = os.path.join(path, appauthor, appname)
            else:
                path = os.path.join(path, appname)
    elif system == 'darwin':
        path = os.path.expanduser('~/Library/Application Support/')
        if appname:
            path = os.path.join(path, appname)
    else:
        path = os.getenv('XDG_DATA_HOME', os.path.expanduser("~/.local/share"))
        if appname:
            path = os.path.join(path, appname)
    if appname and version:
        path = os.path.join(path, version)
    return path 
Example 72
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def site_config_dir(appname=None, appauthor=None, version=None, multipath=False):
    r"""Return full path to the user-shared data dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "multipath" is an optional parameter only applicable to *nix
            which indicates that the entire list of config dirs should be
            returned. By default, the first item from XDG_CONFIG_DIRS is
            returned, or '/etc/xdg/<AppName>', if XDG_CONFIG_DIRS is not set

    Typical site config directories are:
        Mac OS X:   same as site_data_dir
        Unix:       /etc/xdg/<AppName> or $XDG_CONFIG_DIRS[i]/<AppName> for each value in
                    $XDG_CONFIG_DIRS
        Win *:      same as site_data_dir
        Vista:      (Fail! "C:\ProgramData" is a hidden *system* directory on Vista.)

    For Unix, this is using the $XDG_CONFIG_DIRS[0] default, if multipath=False

    WARNING: Do not use this on Windows. See the Vista-Fail note above for why.
    """
    if system in ["win32", "darwin"]:
        path = site_data_dir(appname, appauthor)
        if appname and version:
            path = os.path.join(path, version)
    else:
        # XDG default for $XDG_CONFIG_DIRS
        # only first, if multipath is False
        path = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
        pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)]
        if appname:
            if version:
                appname = os.path.join(appname, version)
            pathlist = [os.sep.join([x, appname]) for x in pathlist]

        if multipath:
            path = os.pathsep.join(pathlist)
        else:
            path = pathlist[0]
    return path 
Example 73
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True):
    r"""Return full path to the user-specific cache dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "appauthor" (only used on Windows) is the name of the
            appauthor or distributing body for this application. Typically
            it is the owning company name. This falls back to appname. You may
            pass False to disable it.
        "version" is an optional version path element to append to the
            path. You might want to use this if you want multiple versions
            of your app to be able to run independently. If used, this
            would typically be "<major>.<minor>".
            Only applied when appname is present.
        "opinion" (boolean) can be False to disable the appending of
            "Cache" to the base app data dir for Windows. See
            discussion below.

    Typical user cache directories are:
        Mac OS X:   ~/Library/Caches/<AppName>
        Unix:       ~/.cache/<AppName> (XDG default)
        Win XP:     C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Cache
        Vista:      C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Cache

    On Windows the only suggestion in the MSDN docs is that local settings go in
    the `CSIDL_LOCAL_APPDATA` directory. This is identical to the non-roaming
    app data dir (the default returned by `user_data_dir` above). Apps typically
    put cache data somewhere *under* the given dir here. Some examples:
        ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
        ...\Acme\SuperApp\Cache\1.0
    OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
    This can be disabled with the `opinion=False` option.
    """
    if system == "win32":
        if appauthor is None:
            appauthor = appname
        path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
        if appname:
            if appauthor is not False:
                path = os.path.join(path, appauthor, appname)
            else:
                path = os.path.join(path, appname)
            if opinion:
                path = os.path.join(path, "Cache")
    elif system == 'darwin':
        path = os.path.expanduser('~/Library/Caches')
        if appname:
            path = os.path.join(path, appname)
    else:
        path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
        if appname:
            path = os.path.join(path, appname)
    if appname and version:
        path = os.path.join(path, version)
    return path 
Example 74
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def user_cache_dir(appname):
    r"""
    Return full path to the user-specific cache dir for this application.

        "appname" is the name of application.

    Typical user cache directories are:
        macOS:      ~/Library/Caches/<AppName>
        Unix:       ~/.cache/<AppName> (XDG default)
        Windows:    C:\Users\<username>\AppData\Local\<AppName>\Cache

    On Windows the only suggestion in the MSDN docs is that local settings go
    in the `CSIDL_LOCAL_APPDATA` directory. This is identical to the
    non-roaming app data dir (the default returned by `user_data_dir`). Apps
    typically put cache data somewhere *under* the given dir here. Some
    examples:
        ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
        ...\Acme\SuperApp\Cache\1.0

    OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
    """
    if WINDOWS:
        # Get the base path
        path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))

        # When using Python 2, return paths as bytes on Windows like we do on
        # other operating systems. See helper function docs for more details.
        if PY2 and isinstance(path, text_type):
            path = _win_path_to_bytes(path)

        # Add our app name and Cache directory to it
        path = os.path.join(path, appname, "Cache")
    elif sys.platform == "darwin":
        # Get the base path
        path = expanduser("~/Library/Caches")

        # Add our app name to it
        path = os.path.join(path, appname)
    else:
        # Get the base path
        path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache"))

        # Add our app name to it
        path = os.path.join(path, appname)

    return path 
Example 75
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: appdirs.py    MIT License 4 votes vote down vote up
def user_data_dir(appname, roaming=False):
    r"""
    Return full path to the user-specific data dir for this application.

        "appname" is the name of application.
            If None, just the system directory is returned.
        "roaming" (boolean, default False) can be set True to use the Windows
            roaming appdata directory. That means that for users on a Windows
            network setup for roaming profiles, this user data will be
            sync'd on login. See
            <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
            for a discussion of issues.

    Typical user data directories are:
        macOS:                  ~/Library/Application Support/<AppName>
                                if it exists, else ~/.config/<AppName>
        Unix:                   ~/.local/share/<AppName>    # or in
                                $XDG_DATA_HOME, if defined
        Win XP (not roaming):   C:\Documents and Settings\<username>\ ...
                                ...Application Data\<AppName>
        Win XP (roaming):       C:\Documents and Settings\<username>\Local ...
                                ...Settings\Application Data\<AppName>
        Win 7  (not roaming):   C:\\Users\<username>\AppData\Local\<AppName>
        Win 7  (roaming):       C:\\Users\<username>\AppData\Roaming\<AppName>

    For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
    That means, by default "~/.local/share/<AppName>".
    """
    if WINDOWS:
        const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
        path = os.path.join(os.path.normpath(_get_win_folder(const)), appname)
    elif sys.platform == "darwin":
        path = os.path.join(
            expanduser('~/Library/Application Support/'),
            appname,
        ) if os.path.isdir(os.path.join(
            expanduser('~/Library/Application Support/'),
            appname,
        )
        ) else os.path.join(
            expanduser('~/.config/'),
            appname,
        )
    else:
        path = os.path.join(
            os.getenv('XDG_DATA_HOME', expanduser("~/.local/share")),
            appname,
        )

    return path 
Example 76
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: lxc_container.py    MIT License 4 votes vote down vote up
def _create(self):
        """Create a new LXC container.

        This method will build and execute a shell command to build the
        container. It would have been nice to simply use the lxc python library
        however at the time this was written the python library, in both py2
        and py3 didn't support some of the more advanced container create
        processes. These missing processes mainly revolve around backing
        LXC containers with block devices.
        """

        build_command = [
            self.module.get_bin_path('lxc-create', True),
            '--name %s' % self.container_name,
            '--quiet'
        ]

        build_command = self._add_variables(
            variables_dict=self._get_vars(
                variables=LXC_COMMAND_MAP['create']['variables']
            ),
            build_command=build_command
        )

        # Load logging for the instance when creating it.
        if self.module.params.get('container_log') in BOOLEANS_TRUE:
            # Set the logging path to the /var/log/lxc if uid is root. else
            # set it to the home folder of the user executing.
            try:
                if os.getuid() != 0:
                    log_path = os.getenv('HOME')
                else:
                    if not os.path.isdir('/var/log/lxc/'):
                        os.makedirs('/var/log/lxc/')
                    log_path = '/var/log/lxc/'
            except OSError:
                log_path = os.getenv('HOME')

            build_command.extend([
                '--logfile %s' % os.path.join(
                    log_path, 'lxc-%s.log' % self.container_name
                ),
                '--logpriority %s' % self.module.params.get(
                    'container_log_level'
                ).upper()
            ])

        # Add the template commands to the end of the command if there are any
        template_options = self.module.params.get('template_options', None)
        if template_options:
            build_command.append('-- %s' % template_options)

        rc, return_data, err = self._run_command(build_command)
        if rc != 0:
            message = "Failed executing lxc-create."
            self.failure(
                err=err, rc=rc, msg=message, command=' '.join(build_command)
            )
        else:
            self.state_change = True 
Example 77
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: java_cert.py    MIT License 4 votes vote down vote up
def import_cert_url(module, executable, url, port, keystore_path, keystore_pass, alias):
    ''' Import certificate from URL into keystore located at keystore_path '''
    import re

    https_proxy = os.getenv("https_proxy")
    no_proxy = os.getenv("no_proxy")

    proxy_opts = ''
    if https_proxy is not None:
        (proxy_host, proxy_port) = https_proxy.split(':')
        proxy_opts = ("-J-Dhttps.proxyHost=%s -J-Dhttps.proxyPort=%s") % (proxy_host, proxy_port)

        if no_proxy is not None:
            # For Java's nonProxyHosts property, items are separated by '|',
            # and patterns have to start with "*".
            non_proxy_hosts = no_proxy.replace(',', '|')
            non_proxy_hosts = re.sub(r'(^|\|)\.', r'\1*.', non_proxy_hosts)

            # The property name is http.nonProxyHosts, there is no
            # separate setting for HTTPS.
            proxy_opts += (" -J-Dhttp.nonProxyHosts='%s'") % (non_proxy_hosts)

    fetch_cmd = ("%s -printcert -rfc -sslserver %s %s:%d") % (executable, proxy_opts, url, port)

    import_cmd = ("%s -importcert -noprompt -keystore '%s' "
                  "-storepass '%s' -alias '%s'") % (executable, keystore_path,
                                                    keystore_pass, alias)

    if module.check_mode:
        module.exit_json(changed=True)

    # Fetch SSL certificate from remote host.
    (_, fetch_out, _) = module.run_command(fetch_cmd, check_rc=True)

    # Use remote certificate from remote host and import it to a java keystore
    (import_rc, import_out, import_err) = module.run_command(import_cmd,
                                                             data=fetch_out,
                                                             check_rc=False)
    diff = {'before': '\n', 'after': '%s\n' % alias}
    if import_rc == 0:
        return module.exit_json(changed=True, msg=import_out,
                                rc=import_rc, cmd=import_cmd, stdout=import_out,
                                diff=diff)
    else:
        return module.fail_json(msg=import_out, rc=import_rc, cmd=import_cmd,
                                error=import_err) 
Example 78
Project: microservice-junos-deployer   Author: network-automate   File: juniper_junos_common.py    GNU General Public License v3.0 4 votes vote down vote up
def run(self, tmp=None, task_vars=None):
        # The new connection arguments based on fallback/defaults.
        new_connection_args = dict()

        # Get the current connection args from either provider or the top-level
        if 'provider' in self._task.args:
            connection_args = self._task.args['provider']
        else:
            connection_args = self._task.args

        # The environment variables used by Ansible Tower
        if 'user' not in connection_args:
            net_user = os.getenv('ANSIBLE_NET_USERNAME')
            if net_user is not None:
                new_connection_args['user'] = net_user
                connection_args['user'] = net_user
        if 'passwd' not in connection_args:
            net_passwd = os.getenv('ANSIBLE_NET_PASSWORD')
            if net_passwd is not None:
                new_connection_args['passwd'] = net_passwd
                connection_args['passwd'] = net_passwd
        if 'ssh_private_key_file' not in connection_args:
            net_key = os.getenv('ANSIBLE_NET_SSH_KEYFILE')
            if net_key is not None:
                new_connection_args['ssh_private_key_file'] = net_key
                connection_args['ssh_private_key_file'] = net_key

        # The values set by Ansible command line arguments, configuration
        # settings, or environment variables.
        for key in connection_spec_fallbacks:
            if key not in connection_args:
                for task_var_key in connection_spec_fallbacks[key]:
                    if task_var_key in task_vars:
                        new_connection_args[key] = task_vars[task_var_key]
                        break

        # Backwards compatible behavior to fallback to USER env. variable.
        if 'user' not in connection_args and 'user' not in new_connection_args:
            user = os.getenv('USER')
            if user is not None:
                new_connection_args['user'] = user

        # Copy the new connection arguments back into either top-level or
        # the provider dictionary.
        if 'provider' in self._task.args:
            self._task.args['provider'].update(new_connection_args)
        else:
            self._task.args.update(new_connection_args)

        # Pass the hidden _module_utils_path option
        module_utils_path = os.path.normpath(os.path.dirname(__file__))
        self._task.args['_module_utils_path'] = module_utils_path
        # Pass the hidden _module_name option
        self._task.args['_module_name'] = self._task.action

        # Call the parent action module.
        return super(JuniperJunosActionModule, self).run(tmp, task_vars) 
Example 79
Project: spqrel_tools   Author: LCAS   File: init.py    MIT License 4 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--pip", type=str, default=os.getenv('PEPPER_IP'),
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--pport", type=int, default=9559,
                        help="Naoqi port number")

    args = parser.parse_args()
    pip = args.pip
    pport = args.pport

    try:
        connection_url = "tcp://" + pip + ":" + str(pport)
        print "Connecting to ",	connection_url
        app = qi.Application(["Init", "--qi-url=" + connection_url ])
    except RuntimeError:
        print ("Can't connect to Naoqi at ip \"" + pip + "\" on port " + str(pport) +".\n"
               "Please check your script arguments. Run with -h option for help.")
        sys.exit(1)

    app.start()
    session = app.session

    tts_service = session.service("ALTextToSpeech")
    tts_service.say("Initialization started.")
    tts_service.setParameter("speed", 80)

    time.sleep(1)
    #webinit.do_init(session)
    #time.sleep(1)
    postureinit.do_init(session)
    time.sleep(3)

    manage_BG_behaviors.stop_behavior(session, 'boot-config'):

    time.sleep(3)

    manage_BG_behaviors.start_behaviors(session,pip,pport)

    tts_service.say("Initialization completed.")

    tmuxinit.do_init()

    tts_service.say("I am ready.") 
Example 80
Project: Splunk_CBER_App   Author: MHaggis   File: external_search_command.py    MIT License 4 votes vote down vote up
def _execute(path, argv=None, environ=None):
            """ Executes an external search command.

            :param path: Path to the external search command.
            :type path: unicode

            :param argv: Argument list.
            :type argv: list or tuple
            The arguments to the child process should start with the name of the command being run, but this is not
            enforced. A value of :const:`None` specifies that the base name of path name :param:`path` should be used.

            :param environ: A mapping which is used to define the environment variables for the new process.
            :type environ: dict or None.
            This mapping is used instead of the current process’s environment. A value of :const:`None` specifies that
            the :data:`os.environ` mapping should be used.

            :return: None

            """
            search_path = os.getenv('PATH') if environ is None else environ.get('PATH')
            found = ExternalSearchCommand._search_path(path, search_path)

            if found is None:
                raise ValueError('Cannot find command on path: {}'.format(path))

            path = found
            logger.debug('starting command="%s", arguments=%s', path, argv)

            def terminate(signal_number, frame):
                sys.exit('External search command is terminating on receipt of signal={}.'.format(signal_number))

            def terminate_child():
                if p.pid is not None and p.returncode is None:
                    logger.debug('terminating command="%s", arguments=%d, pid=%d', path, argv, p.pid)
                    os.kill(p.pid, CTRL_BREAK_EVENT)

            p = Popen(argv, executable=path, env=environ, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
            atexit.register(terminate_child)
            signal(SIGBREAK, terminate)
            signal(SIGINT, terminate)
            signal(SIGTERM, terminate)

            logger.debug('started command="%s", arguments=%s, pid=%d', path, argv, p.pid)
            p.wait()

            logger.debug('finished command="%s", arguments=%s, pid=%d, returncode=%d', path, argv, p.pid, p.returncode)
            sys.exit(p.returncode)