From d7b472903d169e5ab6cfd85cd8142db4ec37fbe1 Mon Sep 17 00:00:00 2001 From: Timo Furrer Date: Sat, 28 Jan 2017 11:21:12 +0100 Subject: [PATCH] Move mkdir_p function to utils module --- pipenv/project.py | 21 +-------------------- pipenv/utils.py | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index 57358e49..703d270b 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -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): diff --git a/pipenv/utils.py b/pipenv/utils.py index a64fe7e2..ae43160d 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -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) + +