Пример #1
0
def editfile(fpath, verbose=True):  # nocover
    """
    DEPRECATED: This has been ported to xdev, please use that version.

    Opens a file or code corresponding to a live python object in your
    preferred visual editor. This function is mainly useful in an interactive
    IPython session.

    The visual editor is determined by the `VISUAL` environment variable.  If
    this is not specified it defaults to gvim.

    Args:
        fpath (PathLike): a file path or python module / function
        verbose (int): verbosity

    Example:
        >>> # xdoctest: +SKIP
        >>> # This test interacts with a GUI frontend, not sure how to test.
        >>> import ubelt as ub
        >>> ub.editfile(ub.util_platform.__file__)
        >>> ub.editfile(ub)
        >>> ub.editfile(ub.editfile)
    """
    from six import types
    import ubelt as ub
    import warnings
    schedule_deprecation(**DEP_SCHEDULE_1)
    warnings.warn('Please use xdev.editfile instead', DeprecationWarning)
    if not isinstance(fpath, string_types):
        if isinstance(fpath, types.ModuleType):
            fpath = fpath.__file__
        else:
            fpath =  sys.modules[fpath.__module__].__file__
        fpath_py = fpath.replace('.pyc', '.py')
        if exists(fpath_py):
            fpath = fpath_py

    if verbose:
        print('[ubelt] editfile("{}")'.format(fpath))

    editor = os.environ.get('VISUAL', 'gvim')
    if not ub.find_exe(editor):
        warnings.warn('Cannot find visual editor={}'.format(editor), UserWarning)
        # Try and fallback on commonly installed editor
        alt_candidates = [
            'gedit',
            'TextEdit'
            'Notepad',
            # 'todo vscode',
            # 'todo atom',
            # 'todo sublime',
        ]
        for cand in alt_candidates:
            if ub.find_exe(cand):
                editor = cand

    if not exists(fpath):
        raise IOError('Cannot start nonexistent file: %r' % fpath)
    ub.cmd([editor, fpath], fpath, detach=True)
Пример #2
0
def editfile(fpath, verbose=True):
    """
    Opens a file or code corresponding to a live python object in your
    preferred visual editor. This function is mainly useful in an interactive
    IPython session.

    The visual editor is determined by the `VISUAL` environment variable.  If
    this is not specified it defaults to gvim.

    Args:
        fpath (PathLike): a file path or python module / function
        verbose (int): verbosity

    DisableExample:
        >>> # This test interacts with a GUI frontend, not sure how to test.
        >>> import xdev
        >>> ub.editfile(xdev.misc.__file__)
        >>> ub.editfile(xdev)
        >>> ub.editfile(xdev.editfile)
    """
    if not isinstance(fpath, six.string_types):
        if isinstance(fpath, types.ModuleType):
            fpath = fpath.__file__
        else:
            fpath = sys.modules[fpath.__module__].__file__
        fpath_py = fpath.replace('.pyc', '.py')
        if exists(fpath_py):
            fpath = fpath_py

    if verbose:
        print('[xdev] editfile("{}")'.format(fpath))

    editor = os.environ.get('VISUAL', 'gvim')
    if not ub.find_exe(editor):
        import warnings
        warnings.warn('Cannot find visual editor={}'.format(editor),
                      UserWarning)
        # Try and fallback on commonly installed editor
        alt_candidates = [
            'gedit',
            'TextEdit'
            'Notepad',
        ]
        for cand in alt_candidates:
            if ub.find_exe(cand):
                editor = cand

    if not exists(fpath):
        raise IOError('Cannot start nonexistant file: %r' % fpath)
    ub.cmd([editor, fpath], fpath, detach=True)
Пример #3
0
def testme():
    import ubelt as ub
    cmake_exe = ub.find_exe('cmake')
    print('cmake_exe = {!r}'.format(cmake_exe))
    fpath = "build-out.txt"
    echo = False
    with winlog(fpath, echo) as logger:  # NOQA
        print("INSIDE LOGGER")
        # p = subprocess.run("c:/Program Files/CMake/bin/cmake --version")
        p = subprocess.run("{} --version".format(cmake_exe),
                           shell=True)  # NOQA

    import ubelt as ub
    cmake_exe = ub.find_exe('cmake')
    with ub.CaptureStdout() as cap:
        p = subprocess.run([cmake_exe, '--version'])
Пример #4
0
def 查找可执行文件(名称, 匹配所有=False, 路径=None):
    """
    # 查找可执行文件('ls')
    # 查找可执行文件('ping')
    # assert 查找可执行文件('which') == 查找可执行文件(查找可执行文件('which'))
    # 查找可执行文件('which', 匹配所有=True)
    # 查找可执行文件('ping', 匹配所有=True)
    # 查找可执行文件('cmake', 匹配所有=True)
    # 查找可执行文件('nvcc', 匹配所有=True)
    # 查找可执行文件('noexist', 匹配所有=True)
    """
    return ub.find_exe(名称, 匹配所有, 路径)
Пример #5
0
def is_directory_open(dpath):
    # FIXME
    import ubelt as ub  # pip install me! https://github.com/Erotemic/ubelt
    import platform
    from os.path import basename
    import re
    computer_name = platform.node()
    dname = basename(dpath)
    if not ub.find_exe('wmctrl'):
        raise Exception('wmctrl must be installed')

    for line in ub.cmd('wmctrl -lxp')['out'].splitlines():
        parts = re.split(' +', line)
        if len(parts) > 3 and parts[3] == 'nautilus.Nautilus':
            if parts[4] == computer_name:
                # FIXME: Might be a False positive!
                line_dname = ' '.join(parts[5:])
                if line_dname == dname:
                    return True
    # Always correctly returns False
    return False
Пример #6
0
def _benchmark_cog_conversions():
    """
    CommandLine:
        xdoctest -m ~/code/ndsampler/ndsampler/utils/util_gdal.py _benchmark_cog_conversions
    """
    # Benchmark
    # xdoc: +REQUIRES(--bench)
    from ndsampler.utils.validate_cog import validate
    import xdev
    import kwimage
    # Prepare test data
    shape = (8000, 8000, 1)
    print('Test data shape = {!r}'.format(shape))
    data = np.random.randint(0, 255, shape, dtype=np.uint16)
    print('Test data size = {}'.format(
        xdev.byte_str(data.size * data.dtype.itemsize)))

    dpath = ub.ensure_app_cache_dir('ndsampler', 'cog_benchmark')
    src_fpath = join(dpath, 'src.png')
    kwimage.imwrite(src_fpath, data)

    # Benchmark conversions
    dst_api_fpath = join(dpath, 'dst_api.tiff')
    dst_cli_fpath = join(dpath, 'dst_cli.tiff')
    dst_data_fpath = join(dpath, 'dst_data.tiff')

    ti = ub.Timerit(3, bestof=3, verbose=3, unit='s')

    compress = 'RAW'
    compress = 'DEFLATE'
    blocksize = 256

    if 1:

        for timer in ti.reset('cov-convert-data'):
            ub.delete(dst_data_fpath)
            with timer:
                _imwrite_cloud_optimized_geotiff(dst_data_fpath,
                                                 data,
                                                 compress=compress,
                                                 blocksize=blocksize)
        assert not len(validate(dst_data_fpath)[1])

        for timer in ti.reset('cog-convert-api2'):
            ub.delete(dst_api_fpath)
            with timer:
                _api_convert_cloud_optimized_geotiff2(src_fpath,
                                                      dst_api_fpath,
                                                      compress=compress,
                                                      blocksize=blocksize)
        assert not len(validate(dst_api_fpath)[1])

        for timer in ti.reset('cog-convert-api'):
            ub.delete(dst_api_fpath)
            with timer:
                _api_convert_cloud_optimized_geotiff(src_fpath,
                                                     dst_api_fpath,
                                                     compress=compress,
                                                     blocksize=blocksize)
        assert not len(validate(dst_api_fpath)[1])

    for timer in ti.reset('cog-convert-cli'):
        ub.delete(dst_cli_fpath)
        with timer:
            _cli_convert_cloud_optimized_geotiff(src_fpath,
                                                 dst_cli_fpath,
                                                 compress=compress,
                                                 blocksize=blocksize)
    assert not len(validate(dst_data_fpath)[1])

    if ub.find_exe('cog'):
        # requires pip install cogeotiff
        for timer in ti.reset('cogeotiff cli --compress {}'.format(compress)):
            ub.delete(dst_cli_fpath)
            with timer:
                info = ub.cmd(
                    'cog create {} {} --compress {} --block-size {}'.format(
                        src_fpath, dst_cli_fpath, compress, blocksize),
                    verbose=0)
                assert info['ret'] == 0
        assert not len(validate(dst_data_fpath)[1])
Пример #7
0
def test_redirect():
    """
    python ~/misc/tests/python/test_tee.py
    """
    # sys_attr = 'stdout'
    # redirector = PosixRedictSystemStream(sys_attr)
    # with redirector:
    #     import ubelt as ub
    #     cmake_exe = ub.find_exe('cmake')
    #     subprocess.run([cmake_exe, '--version'])
    #     print('hello world')
    # redirector.logfile.seek(0)
    # captured = redirector.logfile.read()
    # print('captured = {!r}'.format(captured))
    # print('---------')

    import tempfile
    import ubelt as ub
    from os.path import join
    dpath = tempfile.mkdtemp()
    # This should probably be the real log file. But it must live on disk!
    fpath = join(dpath, 'stream_queue')
    writer = open(fpath, mode='wb+')
    reader = open(fpath, mode='rb+')

    sys_attr = 'stdout'
    redirector = PosixRedictSystemStream(sys_attr, writer)

    echo_writer = open(os.dup(sys.stdout.fileno()), "w")

    from threading import Thread

    _kill = threading.Event()

    def background_reader(reader, echo_writer, redirector, _kill):
        while True:
            is_killed = _kill.wait(.1)
            if is_killed:
                break
            redirector.flush()
            line = reader.readline()
            while line:
                echo_writer.write('thread write: {}\n'.format(line))
                line = reader.readline()

    cmake_exe = ub.find_exe('cmake')
    with redirector:

        _thread = Thread(target=background_reader,
                         args=(reader, echo_writer, redirector, _kill))
        _thread.daemon = True  # thread dies with the program
        _thread.start()

        print('hello world')

        subprocess.run([cmake_exe, '--version'])

        subprocess.run([cmake_exe, '--version'])
        print('hello world')
    _kill.set()

    print('finished redirection')
    print('finished redirection')
Пример #8
0
def main():
    import ubelt as ub

    # This should probably be the real log file. But it must live on disk!
    # from os.path import join
    # dpath = tempfile.mkdtemp()
    # fpath = join(dpath, 'stream_queue')
    # writer = open(fpath, mode='wb+')
    # reader = open(fpath, mode='rb+')

    fpath = 'a-test-log-file.txt'
    writer = open(fpath, mode='wb+')
    writer.write(b'')
    reader = open(fpath, mode='rb+')

    sys_attr = 'stdout'
    redirector = RedirectSystemStream(sys_attr, writer)

    echo_writer = open(os.dup(sys.stdout.fileno()), "w")

    from threading import Thread

    logged_lines = []

    _kill = threading.Event()

    def background_reader(reader, echo_writer, redirector, _kill):
        while True:
            is_killed = _kill.wait(.1)
            # echo_writer.write('check\n')
            redirector.flush()
            line = reader.readline()
            while line:
                logged_lines.append(line)
                echo_writer.write('thread write: {}\n'.format(line))
                echo_writer.flush()
                line = reader.readline()

            # echo_writer.write('check is_killed={}\n'.format(is_killed))
            if is_killed:
                break

    cmake_exe = ub.find_exe('cmake')
    with redirector:

        _thread = Thread(target=background_reader,
                         args=(reader, echo_writer, redirector, _kill))
        # _thread.daemon = True  # thread dies with the program
        _thread.start()

        print('hello world')

        subprocess.run([cmake_exe, '--version'])

        subprocess.run([cmake_exe, '--version'])
        print('hello world')

        redirector.flush()
        # import time
        # time.sleep(0.1)
        _kill.set()
        _thread.join()

    echo_writer.flush()
    print('logged_lines = {!r}'.format(logged_lines))

    print('finished redirection')
    print('finished redirection')