Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ __pycache__/
/.coverage
/.pytest_cache
/.tox
.idea/
venv/
14 changes: 14 additions & 0 deletions flit/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,28 @@ def __init__(self, name, directory='.'):
# It must exist either as a .py file or a directory, but not both
pkg_dir = Path(directory, name)
py_file = Path(directory, name+'.py')
src_dir = Path(directory) / 'src'
src_pkg_dir = Path(src_dir, name)
src_py_file = Path(src_dir, name+'.py')
if pkg_dir.is_dir() and py_file.is_file():
raise ValueError("Both {} and {} exist".format(pkg_dir, py_file))
if src_pkg_dir.is_dir() and src_py_file.is_file():
raise ValueError("Both {} and {} exist".format(src_pkg_dir, src_py_file))
if ((py_file.is_file() or pkg_dir.is_dir()) and
(src_py_file.is_file() or src_pkg_dir.is_dir())):
raise ValueError("Both src and non-src versions of {} exist".format(name))
elif pkg_dir.is_dir():
self.path = pkg_dir
self.is_package = True
elif py_file.is_file():
self.path = py_file
self.is_package = False
elif src_pkg_dir.is_dir():
self.path = src_pkg_dir
self.is_package = True
elif src_py_file.is_file():
self.path = src_py_file
self.is_package = False
else:
raise ValueError("No file/folder found for module {}".format(name))

Expand Down
14 changes: 14 additions & 0 deletions flit/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ def guess_module_name(self):
if p.stem not in {'setup'} and not p.name.startswith('test_'):
modules.append(p.stem)

src_dir = self.directory / 'src'
if src_dir.is_dir():
for p in src_dir.iterdir():
if not p.stem.isidentifier():
continue

if p.is_dir() and (p / '__init__.py').is_file():
if p.name not in {'test', 'tests'}:
packages.append(p.name)

elif p.is_file() and p.suffix == '.py':
if p.stem not in {'setup'} and not p.name.startswith('test_'):
modules.append(p.stem)

if len(packages) == 1:
return packages[0]
elif len(packages) == 0 and len(modules) == 1:
Expand Down
5 changes: 5 additions & 0 deletions tests/samples/module3-pkg.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[metadata]
module=module3
author=Sir Robin
[email protected]
home-page=http://github.com/sirrobin/module3
8 changes: 8 additions & 0 deletions tests/samples/package2/package2-pkg.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[metadata]
module=package2
author=Sir Robin
[email protected]
home-page=http://github.com/sirrobin/package2

[scripts]
pkg_script=package2:main
6 changes: 6 additions & 0 deletions tests/samples/package2/src/package2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""A sample package"""

__version__ = '0.1'

def main():
print("package1 main")
1 change: 1 addition & 0 deletions tests/samples/package2/src/package2/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 1
3 changes: 3 additions & 0 deletions tests/samples/src/module3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Example module"""

__version__ = '0.1'
13 changes: 13 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,23 @@ def test_guess_module_name():
ib = init.IniterBase(td)
assert ib.guess_module_name() == 'baz'

with make_dir(['src/foo.py', 'src/foo-bar.py', 'test_foo.py', 'setup.py'],
['src',]) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() == 'foo'

with make_dir(['src/baz/__init__.py', 'tests/__init__.py'], ['src', 'src/baz', 'tests']) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() == 'baz'

with make_dir(['foo.py', 'bar.py']) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() is None

with make_dir(['src/foo.py', 'src/bar.py'], ['src']) as td:
ib = init.IniterBase(td)
assert ib.guess_module_name() is None

def test_write_license():
with TemporaryDirectory() as td:
ib = init.IniterBase(td)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def clear_samples_dist():
except FileNotFoundError:
pass


def unpack(path):
z = zipfile.ZipFile(str(path))
t = tempfile.TemporaryDirectory()
Expand All @@ -36,6 +37,22 @@ def test_wheel_package():
wheel_main(samples_dir / 'package1-pkg.ini')
assert_isfile(samples_dir / 'dist/package1-0.1-py2.py3-none-any.whl')

def test_wheel_src_module():
clear_samples_dist()
wheel_main(samples_dir / 'module3-pkg.ini')
assert_isfile(samples_dir / 'dist/module3-0.1-py2.py3-none-any.whl')

def clear_package2_dist():
try:
shutil.rmtree(str(samples_dir / 'package2' /'dist'))
except FileNotFoundError:
pass

def test_wheel_src_package():
wheel_main(samples_dir / 'package2' / 'package2-pkg.ini')
assert_isfile(samples_dir / 'package2' / 'dist/package2-0.1-py2.py3-none-any.whl')
clear_package2_dist()

def test_dist_name():
clear_samples_dist()
wheel_main(samples_dir / 'altdistname.ini')
Expand Down