Skip to content

Commit

Permalink
add filepaths_in_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
escapewindow committed Aug 19, 2016
1 parent a63c8b3 commit 3f11636
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 13 additions & 0 deletions scriptworker/utils.py
Expand Up @@ -127,3 +127,16 @@ async def raise_future_exceptions(tasks):
exc = task.exception()
if exc is not None:
raise exc


def filepaths_in_dir(path):
"""Given a directory path, find all files in that directory, and return
the relative paths to those files.
"""
filepaths = []
for root, directories, filenames in os.walk(path):
for filename in filenames:
filepath = os.path.join(root, filename)
filepath = filepath.replace(path, '').lstrip('/')
filepaths.append(filepath)
return filepaths
18 changes: 17 additions & 1 deletion tests/test_utils.py
Expand Up @@ -6,10 +6,11 @@
import mock
import os
import pytest
import tempfile
from scriptworker.context import Context
from scriptworker.exceptions import ScriptWorkerException, ScriptWorkerRetryException
import scriptworker.utils as utils
from . import fake_session, fake_session_500, FakeResponse
from . import fake_session, fake_session_500, FakeResponse, touch

assert fake_session, fake_session_500 # silence flake8

Expand Down Expand Up @@ -198,3 +199,18 @@ async def two():
tasks = [asyncio.ensure_future(one()), asyncio.ensure_future(two())]
with pytest.raises(IOError):
await utils.raise_future_exceptions(tasks)


def test_filepaths_in_dir():
filepaths = sorted([
"asdfasdf/lwekrjweoi/lsldkfjs",
"lkdsjf/werew/sdlkfds",
"lsdkjf/sdlkfds",
"lkdlkf/lsldkfjs",
])
with tempfile.TemporaryDirectory() as tmp:
for path in filepaths:
parent_dir = os.path.join(tmp, os.path.dirname(path))
os.makedirs(parent_dir)
touch(os.path.join(tmp, path))
assert sorted(utils.filepaths_in_dir(tmp)) == filepaths

0 comments on commit 3f11636

Please sign in to comment.