tempbox is now working properly, also with help option
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
__pycache__
|
||||||
@@ -1,19 +1,42 @@
|
|||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import argparse
|
||||||
|
|
||||||
|
from tempbox_functions import execute_as_subprocess
|
||||||
|
|
||||||
|
|
||||||
def create_folders(number_folders, base_path):
|
# Argument parsing
|
||||||
"""creates an amount of folders and returning its path"""
|
parser = argparse.ArgumentParser(
|
||||||
while number_folders > 0:
|
prog="Tempbox",
|
||||||
number_folders = number_folders - 1
|
description="This program accepts an\
|
||||||
|
command whicht it executes in an temporary directory in /temp.",
|
||||||
|
# epilog="helloooooooo",
|
||||||
|
)
|
||||||
|
|
||||||
folder_name = f"folder_{number_folders}"
|
parser.add_argument(
|
||||||
full_path = os.path.join(base_path, folder_name)
|
"-v",
|
||||||
|
"--verbose",
|
||||||
|
action="store_true",
|
||||||
|
help="Activates or deactivates verbose output. (default=%(default)s)",
|
||||||
|
)
|
||||||
|
|
||||||
os.mkdir(full_path)
|
parser.add_argument(
|
||||||
print(f"'{full_path}' was created")
|
"-c",
|
||||||
|
"--command",
|
||||||
|
help="Takes the string right after the flag to execute it.",
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.command is not None:
|
||||||
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
|
if args.command is not None:
|
||||||
|
execute_as_subprocess(
|
||||||
|
args.command,
|
||||||
|
base_path=temp_dir,
|
||||||
|
verbosity=args.verbose,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
parser.print_help()
|
||||||
|
|
||||||
|
|
||||||
# Creates a temporary directory.
|
# Creates a temporary directory and executes the command in it.
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
|
||||||
print(f"Folderpath: {temp_dir}")
|
|
||||||
|
|||||||
34
tempbox/tempbox_functions.py
Normal file
34
tempbox/tempbox_functions.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def create_folders(number_folders, base_path, verbosity=False):
|
||||||
|
"""creates an amount of folders and returning its path"""
|
||||||
|
|
||||||
|
if verbosity:
|
||||||
|
while number_folders > 0:
|
||||||
|
number_folders = number_folders - 1
|
||||||
|
|
||||||
|
folder_name = f"folder_{number_folders}"
|
||||||
|
full_path = os.path.join(base_path, folder_name)
|
||||||
|
|
||||||
|
os.mkdir(full_path)
|
||||||
|
print(f"'{full_path}' was created")
|
||||||
|
else:
|
||||||
|
while number_folders > 0:
|
||||||
|
number_folders = number_folders - 1
|
||||||
|
|
||||||
|
folder_name = f"folder_{number_folders}"
|
||||||
|
full_path = os.path.join(base_path, folder_name)
|
||||||
|
os.mkdir(full_path)
|
||||||
|
|
||||||
|
|
||||||
|
def execute_as_subprocess(command, base_path, verbosity=False):
|
||||||
|
"""executes the string given with the '-c, --command' flag."""
|
||||||
|
if verbosity:
|
||||||
|
print(base_path)
|
||||||
|
print(command)
|
||||||
|
print(verbosity)
|
||||||
|
subprocess.run(command, cwd=base_path, shell=True)
|
||||||
|
else:
|
||||||
|
subprocess.run(command, cwd=base_path, shell=True)
|
||||||
Reference in New Issue
Block a user