Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
sub2_main = ub.touch(join(sub2, '__main__.py'))
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
with PythonPathContext(dpath):
subpaths = sorted(static.package_modpaths(root, with_pkg=True))
# should only return files not directories
assert root_init in subpaths
assert sub1_init in subpaths
assert sub2_init in subpaths
assert root not in subpaths
assert sub1 not in subpaths
assert sub2 not in subpaths
assert root_main in subpaths
def make_dummy_package(dpath, pkgname='mkinit_dummy_module'):
"""
Creates a dummy package structure with or without __init__ files
"""
root = ub.ensuredir(join(dpath, pkgname))
ub.delete(root)
ub.ensuredir(root)
paths = {
'root': root,
'submod1': ub.touch(join(root, 'submod1.py')),
'submod2': ub.touch(join(root, 'submod2.py')),
'subdir1': ub.ensuredir(join(root, 'subdir1')),
'subdir2': ub.ensuredir(join(root, 'subdir2')),
}
paths['subdir1_init'] = ub.touch(join(paths['subdir1'], '__init__.py'))
paths['subdir2_init'] = ub.touch(join(paths['subdir2'], '__init__.py'))
paths['root_init'] = ub.touch(join(paths['root'], '__init__.py'))
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
simple_subattr2 = "hello world"
_private_attr = "hello world"
'''))
ub.writeto(paths['subdir2_init'], ub.codeblock(
'''
__all__ = ['public_attr']
def test_ensuredir_recreate():
base = ub.ensure_app_cache_dir('ubelt/tests')
folder = join(base, 'foo')
member = join(folder, 'bar')
ub.ensuredir(folder, recreate=True)
ub.ensuredir(member)
assert exists(member)
ub.ensuredir(folder, recreate=True)
assert not exists(member)
def make_dummy_package(dpath, pkgname='mkinit_dummy_module'):
"""
Creates a dummy package structure with or without __init__ files
"""
root = ub.ensuredir(join(dpath, pkgname))
ub.delete(root)
ub.ensuredir(root)
paths = {
'root': root,
'submod1': ub.touch(join(root, 'submod1.py')),
'submod2': ub.touch(join(root, 'submod2.py')),
'subdir1': ub.ensuredir(join(root, 'subdir1')),
'subdir2': ub.ensuredir(join(root, 'subdir2')),
}
paths['subdir1_init'] = ub.touch(join(paths['subdir1'], '__init__.py'))
paths['subdir2_init'] = ub.touch(join(paths['subdir2'], '__init__.py'))
paths['root_init'] = ub.touch(join(paths['root'], '__init__.py'))
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
def test_import_modpath_package():
assert '_tmproot373.sub1.sub2.testmod' not in sys.modules
temp = ub.TempDir().start()
# with ub.TempDir() as temp:
if True:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot373'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
ub.touch(join(root, '__init__.py'))
ub.touch(join(sub1, '__init__.py'))
ub.touch(join(sub2, '__init__.py'))
modpath = join(sub2, 'testmod.py')
text = ub.codeblock(
'''
a = 'value'
''')
ub.writeto(modpath, text)
assert temp.dpath not in sys.path
module = ub.import_module_from_path(modpath)
assert temp.dpath not in sys.path, 'pythonpath should remain clean'
assert module.a == 'value'
assert module.__file__ == modpath
def make_dummy_package(dpath, pkgname='mkinit_dummy_module'):
"""
Creates a dummy package structure with or without __init__ files
"""
root = ub.ensuredir(join(dpath, pkgname))
ub.delete(root)
ub.ensuredir(root)
paths = {
'root': root,
'submod1': ub.touch(join(root, 'submod1.py')),
'submod2': ub.touch(join(root, 'submod2.py')),
'subdir1': ub.ensuredir(join(root, 'subdir1')),
'subdir2': ub.ensuredir(join(root, 'subdir2')),
}
paths['subdir1_init'] = ub.touch(join(paths['subdir1'], '__init__.py'))
paths['subdir2_init'] = ub.touch(join(paths['subdir2'], '__init__.py'))
paths['root_init'] = ub.touch(join(paths['root'], '__init__.py'))
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
simple_subattr2 = "hello world"
_private_attr = "hello world"
def test_modpath_to_modname():
"""
CommandLine:
pytest testing/test_static.py::test_modpath_to_modname -s
python testing/test_static.py test_modpath_to_modname
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
sub2_main = ub.touch(join(sub2, '__main__.py'))
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
sub2_main = ub.touch(join(sub2, '__main__.py'))
bad1 = ub.ensuredir((root, 'bad1'))
bad2 = ub.ensuredir((sub1, 'bad2'))
b0 = ub.touch(join(bad1, 'b0.py'))
b1 = ub.touch(join(bad2, 'b1.py'))
with PythonPathContext(dpath):
subpaths = sorted(static.package_modpaths(root, with_pkg=True))
# should only return files not directories
assert root_init in subpaths
assert sub1_init in subpaths
assert sub2_init in subpaths
assert root not in subpaths
assert sub1 not in subpaths
assert sub2 not in subpaths
assert root_main in subpaths
assert sub2_main in subpaths
def test_ensuredir_verbosity():
base = ub.ensure_app_cache_dir('ubelt/tests')
with ub.CaptureStdout() as cap:
ub.ensuredir(join(base, 'foo'), verbose=0)
assert cap.text == ''
# None defaults to verbose=0
with ub.CaptureStdout() as cap:
ub.ensuredir((base, 'foo'), verbose=None)
assert cap.text == ''
ub.delete(join(base, 'foo'))
with ub.CaptureStdout() as cap:
ub.ensuredir(join(base, 'foo'), verbose=1)
assert 'creating' in cap.text
with ub.CaptureStdout() as cap:
ub.ensuredir(join(base, 'foo'), verbose=1)
assert 'existing' in cap.text