示例#1
0
def test_utils():
    '''script to test all utils functions'''
    from pox import pattern, getvars, expandvars, convert, replace, \
                    index_join, findpackage, remote, parse_remote, \
                    select, selectdict, env, homedir, username

   #print('testing pattern...')
    assert pattern(['PYTHON*','DEVELOPER']) == 'PYTHON*;DEVELOPER'
    assert pattern([]) == ''

   #print('testing getvars...')
    bogusdict = {'QAZWERFDSXCV_STUFF':'${DV_DIR}/pythia-${QAZWERFDSXCV_VERSION}/stuff',
                 'MIKE_VERSION':'1.0','MIKE_DIR':'${HOME}/junk',
                 'DUMMY_VERSION':'6.9','DUMMY_STUFF':'/a/b',
                 'DV_DIR':'${HOME}/dev', 'QAZWERFDSXCV_VERSION':'0.0.1'}
    home = homedir()
    if 'HOME' not in os.environ:
        os.environ['HOME'] = home
    assert getvars(home) == {}
    d1 = {'DV_DIR': '${HOME}/dev', 'QAZWERFDSXCV_VERSION': '0.0.1'}
    d2 = {'MIKE_DIR': '${HOME}/junk'}
    assert getvars('${DV_DIR}/pythia-${QAZWERFDSXCV_VERSION}/stuff',bogusdict,'/') == d1
    assert getvars('${MIKE_DIR}/stuff',bogusdict,'/') == d2
    _home = 'HOME'
    assert getvars('${%s}/stuff' % _home, sep='/') == {_home: homedir()}

   #print('testing expandvars...')
    assert expandvars(home) == homedir()
    x = '${ASDFQWEGQVQEGQERGQEVQEEEVCQERGWEGWEFGW}/stuff'
    assert expandvars(x) == x
    x = '${HOME}/junk/${HOME}/dev/stuff'
    assert expandvars('${MIKE_DIR}/${DV_DIR}/stuff',bogusdict) == x
    assert expandvars('${DV_DIR}/${QAZWERFDSXCV_VERSION}',secondref=bogusdict) == \
           expandvars('${DV_DIR}/${QAZWERFDSXCV_VERSION}',bogusdict,os.environ)
    assert expandvars('${%s}/stuff' % _home) == ''.join([homedir(), '/stuff'])

   #print('testing convert...')
    source = 'test.txt'
    f = open(source,'w')
    f.write('this is a test file.'+os.linesep)
    f.close()
    assert convert(source,'mac',verbose=False) == convert(source,verbose=False)
    assert convert(source,'foo',verbose=False) > 0

   #print('testing replace...')
    replace(source,{' is ':' was '})
    replace(source,{'\\sfile.\\s':'.'})
    f = open(source,'r')
    assert f.read().rstrip() == 'this was a test.'
    f.close()
    os.remove(source)

   #print('testing index_join...')
    fl = ['begin ','hello ','world ','string ']
    assert index_join(fl,'hello ','world ') == 'hello world '

   #print('testing findpackage...')
    assert not findpackage('python','aoskvaosvoaskvoak',all=True,verbose=False,recurse=False)
    p = findpackage('lib/python*',env('HOME',all=False),all=False,verbose=False,recurse=1)
    if p: assert 'lib/python' in p

   #print('testing remote...')
    myhost = 'login.cacr.caltech.edu'
    assert remote('~/dev') == '~/dev'
    assert 'localhost' in remote('~/dev',loopback=True)
    thing = '@login.cacr.caltech.edu:~/dev'
    assert remote('~/dev',host=myhost,user=username()).endswith(thing)

   #print('testing parse_remote...')
    destination = 'danse@%s:~/dev' % myhost
    x = ('-l danse', 'login.cacr.caltech.edu', '~/dev')
    assert parse_remote(destination,login_flag=True) == x
    destination = 'danse@%s:' % myhost
    assert parse_remote(destination) == ('danse', 'login.cacr.caltech.edu', '')
    destination = '%s:' % myhost
    x = ('', 'login.cacr.caltech.edu', '')
    assert parse_remote(destination,login_flag=True) == x
    destination = 'test.txt'
    x = ('', 'localhost', 'test.txt')
    assert parse_remote(destination,loopback=True) == x

   #print('testing select...')
    test = ['zero','one','two','three','4','five','six','seven','8','9/81']
    assert select(test) == ['three', 'seven']
    assert select(test,minimum=True) == ['4', '8']
    assert select(test,reverse=True,all=False) == 'seven'
    assert select(test,counter='/',all=False) == '9/81'
    test = [[1,2,3],[4,5,6],[1,3,5]]
    assert select(test) == test
    assert select(test,counter=3) == [test[0], test[-1]]
    assert select(test,counter=3,minimum=True) == [test[1]]

   #print('testing selectdict...')
    x = {'MIKE_VERSION': '1.0', 'DUMMY_VERSION': '6.9'}
    assert selectdict(bogusdict,minimum=True) == x
    x = {'DUMMY_STUFF': '/a/b', 'QAZWERFDSXCV_STUFF': '${DV_DIR}/pythia-${QAZWERFDSXCV_VERSION}/stuff'}
    assert selectdict(bogusdict,counter='/') == x
    assert len(selectdict(bogusdict,counter='/',all=False)) == 1
    return
示例#2
0
def test_shutils():
    '''script to test all shutils functions'''
    from pox import shelltype, homedir, rootdir, sep, mkdir, walk, where, env, \
                    username, minpath, which, which_python, find, shellsub, \
                    expandvars, __version__ as version

    #print('testing shelltype...')
    shell = shelltype()
    try:
        assert shell in [
            'bash', 'sh', 'csh', 'zsh', 'tcsh', 'ksh', 'rc', 'es', 'cmd'
        ]
    except AssertionError:
        if shell:
            print("Warning: non-standard shell type")
            assert isinstance(shell, str)
        else:
            print("Warning: could not determine shell type")
            assert shell is None

#print('testing username...')
#print(username())

#print('testing homedir...')
#print(homedir())
    assert homedir().rstrip(sep()).endswith(username())

    #print('testing rootdir...')
    #print(rootdir())
    assert homedir().startswith(rootdir())

    #print('testing sep...')
    #print(sep())
    #print(sep('ext'))
    #   print(sep('foo'))

    #print('testing mkdir...')
    newdir = sep().join(['xxxtest', 'testxxx'])
    assert mkdir(newdir).rstrip(sep()).endswith(newdir)
    #print('cleaning up...')
    os.removedirs(newdir)

    #print('testing walk...')
    #print(walk('/usr/local','*',recurse=False,folders=True,files=False))
    folders = walk(rootdir(), '*', recurse=False, folders=True, files=False)
    assert len(folders) > 0
    ### assert all(not os.path.isfile(folder) for folder in folders)
    home = walk(homedir() + sep() + os.pardir, username(), False, True)[0]
    assert home == homedir()

    #print('testing where...')
    shells = walk(home, '.bashrc', recurse=0)
    bashrc = where('.bashrc', home)
    if bashrc:
        assert bashrc in shells
    else:
        assert not shells

#print(bashrc)

#print('testing minpath...')
#print(minpath(os.path.expandvars('$PATH')))
    path = expandvars('$PATH')
    assert minpath(path).count(sep('path')) <= path.count(sep('path'))

    #print('testing env...')
    assert env('ACSDAGHQSBFCASDCOMAOCMQOMCQWMOCQOMCOMQRCVOMQOCMQORMCQ') == {}
    if 'HOME' not in os.environ:
        os.environ['HOME'] = homedir()
    assert env('HOME', all=False) or env('USERPROFILE', all=False) == homedir()
    pathdict = env('*PATH*', minimal=True)
    assert len(pathdict) > 0
    assert all('PATH' in key for key in pathdict)

    #print('testing which...')
    assert which('python').endswith(('python', 'python.exe'))
    assert which('python') in which('python', all=True)

    #print('testing find...')
    #print(find('python','/usr/local',type='l'))
    #print(find('*py;*txt'))
    x = os.path.dirname(__file__)
    if not x:  # this file is not found
        x = which('pox;pox_launcher.py')
        if x:  # if executable found, then navigate to the test directory
            p = which_python(fullpath=False, version=True)
            x = os.sep.join((x.rsplit(os.sep, 2)[0], 'lib', p))
            x = [
                p for p in find('test_shutils.py', x, True, 'f')
                if version in p
            ]
            x = x[0] if x else ''
    if x:
        assert set(find('__init__*;__main__*;test_*', x, False,
                        'f')) == set(find('*py;*pyc', x, recurse=False))

    #print('testing shellsub...')
    command = '${HOME}/bin/which foo("bar")'
    #print(repr(command))
    #print(repr(shellsub(command)))
    assert shellsub(command) == '\\${HOME}/bin/which foo\\(\\"bar\\"\\)'

    return
示例#3
0
def test_utils():
    '''script to test all utils functions'''
    from pox import pattern, getvars, expandvars, convert, replace, \
                    index_join, findpackage, remote, parse_remote, \
                    select, selectdict, env, homedir, username

   #print('testing pattern...')
    assert pattern(['PYTHON*','DEVELOPER']) == 'PYTHON*;DEVELOPER'
    assert pattern([]) == ''

   #print('testing getvars...')
    bogusdict = {'QAZWERFDSXCV_STUFF':'${DV_DIR}/pythia-${QAZWERFDSXCV_VERSION}/stuff',
                 'MIKE_VERSION':'1.0','MIKE_DIR':'${HOME}/junk',
                 'DUMMY_VERSION':'6.9','DUMMY_STUFF':'/a/b',
                 'DV_DIR':'${HOME}/dev', 'QAZWERFDSXCV_VERSION':'0.0.1'}
    home = homedir()
    if 'HOME' not in os.environ:
        os.environ['HOME'] = home
    assert getvars(home) == {}
    d1 = {'DV_DIR': '${HOME}/dev', 'QAZWERFDSXCV_VERSION': '0.0.1'}
    d2 = {'MIKE_DIR': '${HOME}/junk'}
    assert getvars('${DV_DIR}/pythia-${QAZWERFDSXCV_VERSION}/stuff',bogusdict,'/') == d1
    assert getvars('${MIKE_DIR}/stuff',bogusdict,'/') == d2
    _home = 'HOME'
    assert getvars('${%s}/stuff' % _home, sep='/') == {_home: homedir()}

   #print('testing expandvars...')
    assert expandvars(home) == homedir()
    x = '${ASDFQWEGQVQEGQERGQEVQEEEVCQERGWEGWEFGW}/stuff'
    assert expandvars(x) == x
    x = '${HOME}/junk/${HOME}/dev/stuff'
    assert expandvars('${MIKE_DIR}/${DV_DIR}/stuff',bogusdict) == x
    assert expandvars('${DV_DIR}/${QAZWERFDSXCV_VERSION}',secondref=bogusdict) == \
           expandvars('${DV_DIR}/${QAZWERFDSXCV_VERSION}',bogusdict,os.environ)
    assert expandvars('${%s}/stuff' % _home) == ''.join([homedir(), '/stuff'])

   #print('testing convert...')
    source = 'test.txt'
    f = open(source,'w')
    f.write('this is a test file.'+os.linesep)
    f.close()
    assert convert(source,'mac',verbose=False) == convert(source,verbose=False)
    assert convert(source,'foo',verbose=False) > 0

   #print('testing replace...')
    replace(source,{' is ':' was '})
    replace(source,{'\sfile.\s':'.'})
    f = open(source,'r')
    assert f.read().rstrip() == 'this was a test.'
    f.close()
    os.remove(source)

   #print('testing index_join...')
    fl = ['begin ','hello ','world ','string ']
    assert index_join(fl,'hello ','world ') == 'hello world '

   #print('testing findpackage...')
    assert not findpackage('python','aoskvaosvoaskvoak',all=True,verbose=False,recurse=False)
    p = findpackage('lib/python*',env('HOME',all=False),all=False,verbose=False,recurse=1)
    if p: assert 'lib/python' in p

   #print('testing remote...')
    myhost = 'login.cacr.caltech.edu'
    assert remote('~/dev') == '~/dev'
    assert 'localhost' in remote('~/dev',loopback=True)
    thing = '@login.cacr.caltech.edu:~/dev'
    assert remote('~/dev',host=myhost,user=username()).endswith(thing)

   #print('testing parse_remote...')
    destination = 'danse@%s:~/dev' % myhost
    x = ('-l danse', 'login.cacr.caltech.edu', '~/dev')
    assert parse_remote(destination,login_flag=True) == x
    destination = 'danse@%s:' % myhost
    assert parse_remote(destination) == ('danse', 'login.cacr.caltech.edu', '')
    destination = '%s:' % myhost
    x = ('', 'login.cacr.caltech.edu', '')
    assert parse_remote(destination,login_flag=True) == x
    destination = 'test.txt'
    x = ('', 'localhost', 'test.txt')
    assert parse_remote(destination,loopback=True) == x

   #print('testing select...')
    test = ['zero','one','two','three','4','five','six','seven','8','9/81']
    assert select(test) == ['three', 'seven']
    assert select(test,minimum=True) == ['4', '8']
    assert select(test,reverse=True,all=False) == 'seven'
    assert select(test,counter='/',all=False) == '9/81'
    test = [[1,2,3],[4,5,6],[1,3,5]]
    assert select(test) == test
    assert select(test,counter=3) == [test[0], test[-1]]
    assert select(test,counter=3,minimum=True) == [test[1]]

   #print('testing selectdict...')
    x = {'MIKE_VERSION': '1.0', 'DUMMY_VERSION': '6.9'}
    assert selectdict(bogusdict,minimum=True) == x
    x = {'DUMMY_STUFF': '/a/b', 'QAZWERFDSXCV_STUFF': '${DV_DIR}/pythia-${QAZWERFDSXCV_VERSION}/stuff'}
    assert selectdict(bogusdict,counter='/') == x
    assert len(selectdict(bogusdict,counter='/',all=False)) == 1
    return
示例#4
0
def test():
    '''test(); script to test all functions'''
    from pox import shelltype, homedir, rootdir, sep, mkdir, walk, where, \
                    username, minpath, env, which, find, shellsub, expandvars

   #print('testing shelltype...')
    shell = shelltype()
    try:
        assert shell in ['bash','sh','csh','zsh','tcsh','ksh','rc','es','cmd']
    except AssertionError:
        print("Warning: non-standard shell type")
        assert isinstance(shell, str)

   #print('testing username...')
   #print(username())

   #print('testing homedir...')
   #print(homedir())
    assert homedir().rstrip(sep()).endswith(username())

   #print('testing rootdir...')
   #print(rootdir())
    assert homedir().startswith(rootdir())

   #print('testing sep...')
   #print(sep())
   #print(sep('ext'))
#   print(sep('foo'))

   #print('testing mkdir...')
    newdir = sep().join(['xxxtest','testxxx'])
    assert mkdir(newdir).rstrip(sep()).endswith(newdir)
   #print('cleaning up...')
    os.removedirs(newdir)

   #print('testing walk...')
   #print(walk('/usr/local','*',recurse=False,folders=True,files=False))
    folders = walk(rootdir(),'*',recurse=False,folders=True,files=False)
    assert len(folders) > 0
    assert all(not os.path.isfile(folder) for folder in folders)
    home = walk(homedir()+sep()+os.pardir, username(), False, True)[0]
    assert home == homedir()

   #print('testing where...')
    shells = walk(home,'.bashrc',recurse=0)
    bashrc = where('.bashrc',home)
    if bashrc:
        assert bashrc in shells
    else:
        assert not shells
   #print(bashrc)

   #print('testing minpath...')
   #print(minpath(os.path.expandvars('$PATH')))
    path = expandvars('$PATH')
    assert minpath(path).count(sep('path')) <= path.count(sep('path'))

   #print('testing env...')
    assert env('ACSDAGHQSBFCASDCOMAOCMQOMCQWMOCQOMCOMQRCVOMQOCMQORMCQ') == {}
    assert env('HOME',all=False) == homedir()
    pathdict = env('*PATH*',minimal=True)
    assert len(pathdict) > 0
    assert all('PATH' in key for key in pathdict)

   #print('testing which...')
    assert which('python').endswith('python')
    assert which('python') in which('python',all=True)

   #print('testing find...')
   #print(find('python','/usr/local',type='l'))
   #print(find('*py;*txt'))
    assert find('test_*','.',False,'f') == find('*py;*txt',recurse=False)

   #print('testing shellsub...')
    command = '${HOME}/bin/which foo("bar")'
   #print(repr(command))
   #print(repr(shellsub(command)))
    assert shellsub(command) == '\\${HOME}/bin/which foo\\(\\"bar\\"\\)'

    return
示例#5
0
def test_shutils():
    """script to test all shutils functions"""
    from pox import (
        shelltype,
        homedir,
        rootdir,
        sep,
        mkdir,
        walk,
        where,
        username,
        minpath,
        env,
        which,
        find,
        shellsub,
        expandvars,
    )

    # print('testing shelltype...')
    shell = shelltype()
    try:
        assert shell in ["bash", "sh", "csh", "zsh", "tcsh", "ksh", "rc", "es", "cmd"]
    except AssertionError:
        if shell:
            print("Warning: non-standard shell type")
            assert isinstance(shell, str)
        else:
            print("Warning: could not determine shell type")
            assert shell is None

    # print('testing username...')
    # print(username())

    # print('testing homedir...')
    # print(homedir())
    assert homedir().rstrip(sep()).endswith(username())

    # print('testing rootdir...')
    # print(rootdir())
    assert homedir().startswith(rootdir())

    # print('testing sep...')
    # print(sep())
    # print(sep('ext'))
    #   print(sep('foo'))

    # print('testing mkdir...')
    newdir = sep().join(["xxxtest", "testxxx"])
    assert mkdir(newdir).rstrip(sep()).endswith(newdir)
    # print('cleaning up...')
    os.removedirs(newdir)

    # print('testing walk...')
    # print(walk('/usr/local','*',recurse=False,folders=True,files=False))
    folders = walk(rootdir(), "*", recurse=False, folders=True, files=False)
    assert len(folders) > 0
    assert all(not os.path.isfile(folder) for folder in folders)
    home = walk(homedir() + sep() + os.pardir, username(), False, True)[0]
    assert home == homedir()

    # print('testing where...')
    shells = walk(home, ".bashrc", recurse=0)
    bashrc = where(".bashrc", home)
    if bashrc:
        assert bashrc in shells
    else:
        assert not shells
    # print(bashrc)

    # print('testing minpath...')
    # print(minpath(os.path.expandvars('$PATH')))
    path = expandvars("$PATH")
    assert minpath(path).count(sep("path")) <= path.count(sep("path"))

    # print('testing env...')
    assert env("ACSDAGHQSBFCASDCOMAOCMQOMCQWMOCQOMCOMQRCVOMQOCMQORMCQ") == {}
    if "HOME" not in os.environ:
        os.environ["HOME"] = homedir()
    assert env("HOME", all=False) or env("USERPROFILE", all=False) == homedir()
    pathdict = env("*PATH*", minimal=True)
    assert len(pathdict) > 0
    assert all("PATH" in key for key in pathdict)

    # print('testing which...')
    assert which("python").endswith(("python", "python.exe"))
    assert which("python") in which("python", all=True)

    # print('testing find...')
    # print(find('python','/usr/local',type='l'))
    # print(find('*py;*txt'))
    x = "tests" if find("setup.py", recurse=False) else "."
    assert find("__init__*;test_*", x, False, "f") == find("*py", x, recurse=False)

    # print('testing shellsub...')
    command = '${HOME}/bin/which foo("bar")'
    # print(repr(command))
    # print(repr(shellsub(command)))
    assert shellsub(command) == '\\${HOME}/bin/which foo\\(\\"bar\\"\\)'

    return
示例#6
0
def test():
    '''test(); script to test all functions'''
    from pox import shelltype, homedir, rootdir, sep, mkdir, walk, where, \
                    username, minpath, env, which, find, shellsub, expandvars

    #print('testing shelltype...')
    shell = shelltype()
    try:
        assert shell in [
            'bash', 'sh', 'csh', 'zsh', 'tcsh', 'ksh', 'rc', 'es', 'cmd'
        ]
    except AssertionError:
        print("Warning: non-standard shell type")
        assert isinstance(shell, str)

#print('testing username...')
#print(username())

#print('testing homedir...')
#print(homedir())
    assert homedir().rstrip(sep()).endswith(username())

    #print('testing rootdir...')
    #print(rootdir())
    assert homedir().startswith(rootdir())

    #print('testing sep...')
    #print(sep())
    #print(sep('ext'))
    #   print(sep('foo'))

    #print('testing mkdir...')
    newdir = sep().join(['xxxtest', 'testxxx'])
    assert mkdir(newdir).rstrip(sep()).endswith(newdir)
    #print('cleaning up...')
    os.removedirs(newdir)

    #print('testing walk...')
    #print(walk('/usr/local','*',recurse=False,folders=True,files=False))
    folders = walk(rootdir(), '*', recurse=False, folders=True, files=False)
    assert len(folders) > 0
    assert all(not os.path.isfile(folder) for folder in folders)
    home = walk(homedir() + sep() + os.pardir, username(), False, True)[0]
    assert home == homedir()

    #print('testing where...')
    shells = walk(home, '.bashrc', recurse=0)
    bashrc = where('.bashrc', home)
    if bashrc:
        assert bashrc in shells
    else:
        assert not shells

#print(bashrc)

#print('testing minpath...')
#print(minpath(os.path.expandvars('$PATH')))
    path = expandvars('$PATH')
    assert minpath(path).count(sep('path')) <= path.count(sep('path'))

    #print('testing env...')
    assert env('ACSDAGHQSBFCASDCOMAOCMQOMCQWMOCQOMCOMQRCVOMQOCMQORMCQ') == {}
    assert env('HOME', all=False) or env('USERPROFILE', all=False) == homedir()
    pathdict = env('*PATH*', minimal=True)
    assert len(pathdict) > 0
    assert all('PATH' in key for key in pathdict)

    #print('testing which...')
    assert which('python').endswith(('python', 'python.exe'))
    assert which('python') in which('python', all=True)

    #print('testing find...')
    #print(find('python','/usr/local',type='l'))
    #print(find('*py;*txt'))
    assert find('test_*', '.', False, 'f') == find('*py;*txt', recurse=False)

    #print('testing shellsub...')
    command = '${HOME}/bin/which foo("bar")'
    #print(repr(command))
    #print(repr(shellsub(command)))
    assert shellsub(command) == '\\${HOME}/bin/which foo\\(\\"bar\\"\\)'

    return