Move mkdir_p function to utils module

This commit is contained in:
Timo Furrer
2017-01-28 11:21:12 +01:00
parent 67c5204fad
commit d7b472903d
2 changed files with 25 additions and 21 deletions
+1 -20
View File
@@ -5,29 +5,10 @@ import toml
from requests.compat import OrderedDict
from .utils import format_toml, multi_split
from .utils import format_toml, multi_split, mkdir_p
from .utils import convert_deps_from_pip, convert_deps_to_pip
def mkdir_p(newdir):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
From: http://code.activestate.com/recipes/82465-a-friendly-mkdir/
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
mkdir_p(head)
if tail:
os.mkdir(newdir)
class Project(object):
"""docstring for Project"""
def __init__(self):
+24 -1
View File
@@ -1,7 +1,9 @@
import os
import tempfile
import delegator
import click
import requirements
import tempfile
def format_toml(data):
@@ -111,3 +113,24 @@ def convert_deps_to_pip(deps, r=True):
f = tempfile.NamedTemporaryFile(suffix='-requirements.txt', delete=False)
f.write('\n'.join(dependencies).encode('utf-8'))
return f.name
def mkdir_p(newdir):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
From: http://code.activestate.com/recipes/82465-a-friendly-mkdir/
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired dir, '{0}', already exists.".format(newdir))
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
mkdir_p(head)
if tail:
os.mkdir(newdir)