Beispiel #1
0
def system_information(handler):
    '''Handler for system info'''
    value, error = {}, {}
    try:
        import psutil
        process = psutil.Process(os.getpid())
        value['system', 'cpu-count'] = psutil.cpu_count()
        value['system', 'cpu-usage'] = psutil.cpu_percent()
        value['system', 'memory-usage'] = psutil.virtual_memory().percent
        value['system', 'disk-usage'] = psutil.disk_usage('/').percent
        value['gramex', 'memory-usage'] = process.memory_info()[0]
        value['gramex', 'open-files'] = len(process.open_files())
    except ImportError:
        app_log.warning('psutil required for system stats')
        error['system', 'cpu-count'] = 'psutil not installed'
        error['system', 'cpu-usage'] = 'psutil not installed'
        error['system', 'memory-usage'] = 'psutil not installed'
        error['system', 'disk-usage'] = 'psutil not installed'
        error['gramex', 'memory-usage'] = 'psutil not installed'
        error['gramex', 'open-files'] = 'psutil not installed'
    try:
        import conda
        value['conda', 'version'] = conda.__version__,
    except ImportError:
        app_log.warning('conda required for conda stats')
        error['conda', 'version'] = 'conda not installed'

    from shutilwhich import which
    value['node', 'path'] = which('node')
    value['git', 'path'] = which('git')

    from gramex.cache import Subprocess
    apps = {
        ('node', 'version'): Subprocess('node --version', shell=True),
        ('npm', 'version'): Subprocess('npm --version', shell=True),
        ('yarn', 'version'): Subprocess('yarn --version', shell=True),
        ('git', 'version'): Subprocess('git --version', shell=True),
    }
    for key, proc in apps.items():
        stdout, stderr = yield proc.wait_for_exit()
        value[key] = stdout.strip()
        if not value[key]:
            error[key] = stderr.strip()

    value['python', 'version'] = '{0}.{1}.{2}'.format(*sys.version_info[:3])
    value['python', 'path'] = sys.executable
    value['gramex', 'version'] = gramex.__version__
    value['gramex', 'path'] = os.path.dirname(gramex.__file__)

    import pandas as pd
    df = pd.DataFrame({'value': value, 'error': error}).reset_index()
    df.columns = ['section', 'key'] + df.columns[2:].tolist()
    df = df[['section', 'key', 'value',
             'error']].sort_values(['section', 'key'])
    df['error'] = df['error'].fillna('')
    data = gramex.data.filter(df, handler.args)
    # TODO: handle _format, _meta, _download, etc just like FormHandler
    raise Return(gramex.data.download(data))
Beispiel #2
0
def subprocess(handler):
    '''Used by test_cache.TestSubprocess to check if gramex.cache.Subprocess works'''
    kwargs = {}
    if handler.args.get('out'):
        kwargs['stream_stdout'] = [handler.write] * len(handler.args['out'])
    if handler.args.get('err'):
        kwargs['stream_stderr'] = [handler.write] * len(handler.args['err'])
    if handler.args.get('buf'):
        buf = handler.args['buf'][0]
        kwargs['buffer_size'] = int(buf) if buf.isdigit() else buf
    if handler.args.get('env'):
        kwargs['env'] = dict(os.environ)
        kwargs['env'][str('GRAMEXTESTENV')] = str('test')   # env keys & values can only by str()
    handler.write('stream: ')
    proc = Subprocess(handler.args['args'], universal_newlines=True, **kwargs)
    stdout, stderr = yield proc.wait_for_exit()
    raise gen.Return(b'return: ' + stdout + stderr)
Beispiel #3
0
    def get(self, *path_args):
        if self.redirects:
            self.save_redirect_page()
        for header_name, header_value in self.headers.items():
            self.set_header(header_name, header_value)

        proc = Subprocess(
            self.cmdargs,
            shell=self.shell,
            cwd=self.cwd,
            stream_stdout=self.stream_stdout,
            stream_stderr=self.stream_stderr,
            buffer_size=self.buffer_size,
        )
        yield proc.wait_for_exit()
        # Wait for process to finish
        proc.proc.wait()
        if self.redirects:
            self.redirect_next()
Beispiel #4
0
    def get(self, *path_args):
        if self.redirects:
            self.save_redirect_page()
        for header_name, header_value in self.headers.items():
            self.set_header(header_name, header_value)

        proc = Subprocess(
            self.cmdargs,
            # shell=True is safe here if app developer explicitly requests it
            shell=self.shell,  # nosec
            cwd=self.cwd,
            stream_stdout=self.stream_stdout,
            stream_stderr=self.stream_stderr,
            buffer_size=self.buffer_size,
        )
        yield proc.wait_for_exit()
        # Wait for process to finish
        proc.proc.wait()
        if self.redirects:
            self.redirect_next()