Commit 39a69009 by fwkz

Covering case when the file already exist.

parent bf5a036e
...@@ -448,22 +448,28 @@ def create_resource(name, content=(), python_package=False): ...@@ -448,22 +448,28 @@ def create_resource(name, content=(), python_package=False):
if python_package: if python_package:
open(os.path.join(root_path, "__init__.py"), "a").close() open(os.path.join(root_path, "__init__.py"), "a").close()
print_success("__init__.py successfully created.")
for name, template_path, context in content: for name, template_path, context in content:
if os.path.splitext(name)[-1] == "": # Checking if resource has extension if not it's directory if os.path.splitext(name)[-1] == "": # Checking if resource has extension if not it's directory
os.mkdir(os.path.join(root_path, name)) mkdir_p(os.path.join(root_path, name))
print_success("Sub-directory /{name} successfully created.".format(name=name))
else: else:
try: try:
with open(template_path, "rb") as template_file: with open(template_path, "rb") as template_file:
template = string.Template(template_file.read()) template = string.Template(template_file.read())
except (IOError, TypeError): except (IOError, TypeError):
template = string.Template("") template = string.Template("")
finally:
with open(os.path.join(root_path, name), "wb") as target_file: try:
file_handle = os.open(os.path.join(root_path, name), os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except OSError as e:
if e.errno == errno.EEXIST:
print_status("{} already exist.".format(name))
else:
raise
else:
with os.fdopen(file_handle, 'w') as target_file:
target_file.write(template.substitute(**context)) target_file.write(template.substitute(**context))
print_success("{file} successfully created.".format(file=name)) print_success("{} successfully created.".format(name))
def mkdir_p(path): def mkdir_p(path):
...@@ -474,9 +480,9 @@ def mkdir_p(path): ...@@ -474,9 +480,9 @@ def mkdir_p(path):
""" """
try: try:
os.makedirs(path) os.makedirs(path)
print_success("Directory {path} sucessfully created.".format(path=path)) print_success("Directory {path} successfully created.".format(path=path))
except OSError as exc: except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path): if exc.errno == errno.EEXIST and os.path.isdir(path):
print_success("Directory {path}".format(path=path)) print_success("Directory {path}".format(path=path))
else: else:
raise raise
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment