#!/usr/bin/env python3 """ This file is part of indexify. indexify is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. indexify is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with indexify. If not, see . Copyright (c) 2022, Maciej Barć Licensed under the GNU GPL v3 License SPDX-License-Identifier: GPL-3.0-only """ import math import os import sys def file_size(file_path: str) -> str: size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") siz_b = os.path.getsize(file_path) if siz_b == 0: return "0 B" i = int(math.floor(math.log(siz_b, 1024))) p = math.pow(1024, i) s = round(siz_b / p, 2) return f"{s} {size_name[i]}" def create_body(start_dir: str) -> str: res = "" for root, dirs, files in sorted(os.walk(start_dir)): if "/.git" in root: continue bas_dir = os.path.basename(root) top_dir = os.path.relpath(root, start_dir) level = root.replace(start_dir, '').count(os.sep) print(f"Directory: {bas_dir}") res += f"\n {' ' * (level * 3)} " res += '' res += f" {bas_dir}" res += "
" for f in sorted(files): if f[-1] == "~": continue print(f"File: {f}") ful = os.path.join(start_dir, top_dir, f) siz = (file_size(ful) if os.path.exists(ful) else 0) res += f"\n {' ' * ((level + 1) * 3)} " res += '' res += f" {f}" res += f" {siz} " res += "
" return res def main() -> None: if len(sys.argv) == 2: start_dir = os.path.abspath(sys.argv[1]) else: print("Wrong number of args, pass 1 arg as a directory to indexify.") exit(1) fcdn = """""" styl = """""" head = f" {fcdn} {styl} " body = f" {create_body(start_dir)} " html = f"\n{head}\n{body}\n" with open(os.path.join(start_dir, "index.html"), "w") as f: f.write(html) if __name__ == "__main__": main()