Skip to content

Commit

Permalink
mkdir is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
duyixian committed Oct 22, 2020
1 parent e4d77f7 commit 64e167e
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ GNU coreutils implementation with Python 3.8

✔ head

✔ mkdir

## INSTALL

```shell
Expand All @@ -24,4 +26,5 @@ pip install -U u_coreutils
u-cat a.txt
u-echo -e -n 'Hello,World!\n'
u-head -n 5 a.txt
u-mkdir -p -v a/b/c/d
```
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "u-coreutils"
readme = "README.md"
repository = "https://github.com/duyixian1234/u-coreutils"
version = "0.1.6"
version = "0.1.7"

[tool.poetry.dependencies]
click = "^7.1.2"
Expand All @@ -25,6 +25,7 @@ rope = "^0.18.0"
u-cat = 'u_coreutils.cat:run'
u-echo = 'u_coreutils.echo:run'
u-head = 'u_coreutils.head:run'
u-mkdir = 'u_coreutils.mkdir:run'

[tool.pylint.master]
job = 0
Expand Down
Empty file added tests/mkdir/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions tests/mkdir/test_mkdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pathlib import Path
from click.testing import CliRunner
from u_coreutils.mkdir import mkdir


def test_mkdir():
runner = CliRunner()
with runner.isolated_filesystem():
path = Path("a")
path.mkdir()

result = runner.invoke(mkdir, ["b"])
assert result.exit_code == 0
assert result.output == ""

result = runner.invoke(mkdir, ["-v", "c"])
assert result.exit_code == 0
assert result.output == "mkdir: created directory 'c'\n"

result = runner.invoke(mkdir, ["-v", "a"])
assert result.exit_code == 1
assert result.output == "mkdir: a: File exists\n"

result = runner.invoke(mkdir, ["a/b"])
assert result.exit_code == 0
assert result.output == ""

result = runner.invoke(mkdir, ["a/b/c/d"])
assert result.exit_code == 1
assert result.output == "mkdir: a/b/c: No such file or directory\n"

result = runner.invoke(mkdir, ["-p", "-v", "b/c/d"])
assert result.exit_code == 0
assert result.output == "mkdir: created directory 'b/c'\nmkdir: created directory 'b/c/d'\n"
2 changes: 1 addition & 1 deletion tests/test_u_coreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "0.1.6"
assert __version__ == "0.1.7"
2 changes: 1 addition & 1 deletion u_coreutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.6"
__version__ = "0.1.7"
51 changes: 51 additions & 0 deletions u_coreutils/mkdir/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from pathlib import Path
import sys
from typing import List
import click


def process(directory: str, mode: int = 0o755, parents: bool = False, verbose: bool = False):
path = Path(directory)
existsList = reversed([(_path.exists(), _path) for _path in list(path.parents)[:-1]])

if path.exists() and not parents:
click.echo(f"mkdir: {directory}: File exists")
sys.exit(1)

for exists, parentPath in existsList:
if not exists:
if parents:
parentPath.mkdir(mode)
if verbose:
click.echo(f"mkdir: created directory '{parentPath}'")
else:
click.echo(f"mkdir: {parentPath}: No such file or directory")
sys.exit(1)

path.mkdir(mode)
if verbose:
click.echo(f"mkdir: created directory '{path}'")


@click.command()
@click.option("-m", "--mode", help="set file mode (as in chmod), not a=rwx - umask", default="755")
@click.option(
"-p",
"--parents",
flag_value=True,
help="no error if existing, make parent directories as needed",
)
@click.option("-v", "--verbose", flag_value=True, help="print a message for each created directory")
@click.argument("DIRECTORY", required=True, nargs=-1, type=str)
def mkdir(directory: List[str], mode: str, parents: bool, verbose: bool):
"""Create the DIRECTORY(ies), if they do not already exist."""
_mode = int(mode, 8)
for _directory in directory:
process(_directory, _mode, parents, verbose)


def run():
mkdir() # pylint: disable=no-value-for-parameter


__all__ = ["run"]
4 changes: 4 additions & 0 deletions u_coreutils/mkdir/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import run

if __name__ == "__main__":
run()

0 comments on commit 64e167e

Please sign in to comment.