Compare commits

...

3 Commits

Author SHA1 Message Date
perro tuerto a016ba6c2a Ejercicio 12c 2023-03-07 10:57:06 -08:00
perro tuerto 6d37344f7c Ejercicio 12b 2023-03-07 10:35:36 -08:00
perro tuerto 675fd8ad69 Ejercicio 12a 2023-03-07 10:23:28 -08:00
3 changed files with 91 additions and 0 deletions

34
exercises/ex12/ls Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python
# Exercise 12a
# ls command
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("paths", default=Path().cwd(), nargs="*", type=Path)
parser.add_argument("-a", "--all", action="store_true")
args = parser.parse_args()
result = ""
def get_items(path, with_hidden):
items = path.glob("*")
if not with_hidden:
items = list(filter(lambda i: i.name[0] != ".", items))
return list(sorted(set(items)))
if isinstance(args.paths, list):
roots = list(map(lambda p: get_items(p, args.all), args.paths))
else:
roots = [get_items(args.paths, args.all)]
for items in roots:
if len(items) > 0:
root = items[0].parent
items = "\n".join(list(map(lambda i: i.name, items)))
result += items if len(roots) == 1 else f"{root}:\n{items}\n\n"
if result != "":
print(result.strip())

26
exercises/ex12/rm Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env python
# Exercise 12b
# rm command
import sys
import shutil
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="+", type=Path)
parser.add_argument("-r", "--recursive", action="store_true")
args = parser.parse_args()
error = False
for path in args.paths:
if path.is_dir() and args.recursive is False:
print(f"rm: no se puede borrar '{path}': Es un directorio")
error = True
elif path.is_file():
path.unlink()
else:
shutil.rmtree(path)
if error:
sys.exit(1)

31
exercises/ex12/rmdir Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
# Exercise 12c
# rmdir command
import sys
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="+", type=Path)
parser.add_argument("-p", "--parents", action="store_true")
args = parser.parse_args()
error = False
for path in args.paths:
if path.is_file():
print(f"rmdir: fallo al borrar '{path}': No es un directorio")
error = True
elif len(list(path.glob("*"))) > 0:
print(f"rmdir: fallo al borrar '{path}': El directorio no está vacío")
error = True
else:
if args.parents is True:
while len(path.parts) > 0:
path.rmdir()
path = path.parent
else:
path.rmdir()
if error:
sys.exit(1)