-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
duyixian
committed
Oct 22, 2020
1 parent
e4d77f7
commit 64e167e
Showing
8 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
def test_version(): | ||
assert __version__ == "0.1.6" | ||
assert __version__ == "0.1.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.6" | ||
__version__ = "0.1.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from . import run | ||
|
||
if __name__ == "__main__": | ||
run() |