Skip to content

Commit

Permalink
solve iipeace#206 add get_dirs method for get json format directory tree
Browse files Browse the repository at this point in the history
  • Loading branch information
yangssoy committed Mar 13, 2020
1 parent b7c5f57 commit b531e74
Showing 1 changed file with 60 additions and 8 deletions.
68 changes: 60 additions & 8 deletions guider/guider.py
Original file line number Diff line number Diff line change
Expand Up @@ -23420,6 +23420,57 @@ def doAddr2sym():

@staticmethod
def printDirs(path='.', maxLevel=-1):
def get_dirs(result, parent_path, level, max_level):
file_list = os.listdir(parent_path)

if len(file_list) == 0 or \
(max_level != -1 and max_level <= level):
return (0, 0, 0)

total_size = long(0)
total_file = long(0)
total_dir = long(0)

# sort by size #
if SysMgr.showAll:
file_list.sort( \
key=lambda name: os.path.getsize( \
'%s/%s' % (parent_path, name)), reverse=True)
# sort by type #
else:
file_list.sort( \
key=lambda f: os.path.isfile(os.path.join(parent_path, f)))

for idx, sub_path in enumerate(file_list):

full_path = os.path.join(parent_path, sub_path)

if os.path.islink(full_path):
continue

if os.path.isdir(full_path):
total_dir += 1
sub_abspath = "[%s]" % (os.path.abspath(sub_path))
info = dict(parent_path=sub_abspath, sub_dirs=[])
result['sub_dirs'].append(info)
total_info = get_dirs(info, full_path, level + 1, max_level)

total_size += total_info[0]
total_dir += total_info[1]
total_file += total_info[2]


elif os.path.isfile(full_path):
total_file += 1
size = os.stat(full_path).st_size
total_size += size

result['size'] = UtilMgr.convertSize2Unit(total_size)
result['dir'] = UtilMgr.convertNumber(total_dir)
result['file'] = UtilMgr.convertNumber(total_file)

return (total_size, total_dir, total_file)

def recurse(parentPath, fileList, prefix, result, level, maxLevel):
totalSize = long(0)
totalFile = long(0)
Expand Down Expand Up @@ -23511,24 +23562,25 @@ def recurse(parentPath, fileList, prefix, result, level, maxLevel):

return (totalSize, totalDir, totalFile)

# print start path #
abspath = "[%s]" % (os.path.abspath(path))
result = [abspath]
recurse(path, os.listdir(path), " ", result, 0, maxLevel)
output = "\n%s\n" % "\n".join(result)

# print start path #
if SysMgr.jsonOutputEnable:
json_result = dict(dirInfos=output)
json_result = UtilMgr.convertDict2Str(json_result)
result = dict()
result['parent_path'] = abspath
result['sub_dirs'] = []
get_dirs(result, path, 0, -1)
json_result = UtilMgr.convertDict2Str(result)
SysMgr.printPipe(json_result)
else:
result = [abspath]
recurse(path, os.listdir(path), " ", result, 0, maxLevel)
output = "\n%s\n" % "\n".join(result)
SysMgr.printStat( \
r"start traversing dirs from %s..." % abspath)
SysMgr.printPipe(output)




@staticmethod
def doSym2addr():
SysMgr.printLogo(big=True, onlyFile=True)
Expand Down

0 comments on commit b531e74

Please sign in to comment.