Skip to content

Commit

Permalink
add log tree func
Browse files Browse the repository at this point in the history
  • Loading branch information
cxnt committed Oct 6, 2023
1 parent 466922b commit 07f76b4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
55 changes: 24 additions & 31 deletions src/functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import numpy as np
import supervisely as sly
import subprocess
import supervisely as sly

from supervisely.io.fs import mkdir
from itertools import groupby
Expand Down Expand Up @@ -230,34 +230,27 @@ def create_coco_ann_templates(dataset, user_name, meta: sly.ProjectMeta):
return coco_ann, coco_captions


def generate_file_tree(directory_path, max_files_per_directory=5):
try:
result = subprocess.run(
["tree", directory_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
def tree(dir_path: str) -> str:
out = subprocess.Popen(
["tree", "--filelimit", "500", "-h", "-n", dir_path],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout, stderr = out.communicate()
return stdout.decode("utf-8")


def log_tree(dir_path: str, logger, level):
out = tree(dir_path)
log_levels = {
"info": logger.info,
"debug": logger.debug,
"warning": logger.warning,
"error": logger.error,
}
if level not in log_levels:
raise ValueError(
f"Unknown logger level: {level}. Available levels: info, debug, warning, error"
)
tree_output = result.stdout
directory_file_count = {}
filtered_tree_output = []

lines = tree_output.split("\n")
for line in lines:
if line.startswith(directory_path):
current_directory = line
directory_file_count[current_directory] = 0
filtered_tree_output.append(line)
elif line.strip().startswith("├──") or line.strip().startswith("└──"):
if (
current_directory
and directory_file_count[current_directory] < max_files_per_directory
):
filtered_tree_output.append(line)
directory_file_count[current_directory] += 1
return "\n".join(filtered_tree_output)

except subprocess.CalledProcessError as e:
print(f"Couldn't print file tree for this project. Error: {e}")
return None
log_func = log_levels[level]
log_func("DIRECTORY_TREE", extra={"tree": out})
4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def export_to_coco(api: sly.Api, task_id, context, state, app_logger):

sly.logger.info(f"Dataset [{dataset.name}] processed!")

tree_output = f.generate_file_tree(g.storage_dir, 30)
sly.logger.info(tree_output)
# sly.fs.log_tree(g.storage_dir, sly.logger, level="info")
f.log_tree(g.storage_dir, sly.logger, level="info")

full_archive_name = f"{task_id}_{g.project.name}.tar"
result_archive = os.path.join(g.my_app.data_dir, full_archive_name)
Expand Down

0 comments on commit 07f76b4

Please sign in to comment.