website/rmCreated.py

109 lines
3.5 KiB
Python
Executable file

#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
"""Delete latex created files.
Go through a directory and remove all files that
are created by latex and are not necessary due to the
existing original .tex files.
"""
import os
import os.path
# from os.path import islink, join, basename, abspath, lexists, samefile, isfile
# from os.path import isdir, getsize, splitext, dirname # , exists
# from shutil import move
# from os import walk, sep, symlink, readlink
from argparse import ArgumentParser
# from binaryornot.check import is_binary
createdExtensions = ["aux", "fls", "log", "synctex.gz",
"pgf-plot", "dvi", "bbl", "toc", "run.xml",
"out", "blg", "bcf", "lco", "vrb", "snm",
"nav", "auxlock", "upb", "upa", "glg", "glo",
"gls", "glsdefs", "idx", "ilg", "ind", "ist",
"fdb_latexmk", "pdf"]
# first check for command line arguments
parser = ArgumentParser(description="Remove by latex created files.")
parser.add_argument(
dest='filenames',
metavar='F',
help="the directories to be processed",
nargs='*',
default='.')
parser.add_argument(
'-v', '--verbose',
dest='verbose',
action='store_true',
help="turn on verbose output, " +
"additionally printing all tex files.")
parser.add_argument(
'-l', '--onlylog',
dest='onlylog',
action='store_true',
help=("do not actually move anything but tells what would have been " +
"done ('dry run')."))
parser.add_argument(
'-i', "--ignore",
dest='ignore',
nargs='+',
default=['.git', '.svn'],
help=("""ignore the directories and files (and their subdirectories) """ +
"""given after -i. """ +
"""Since this takes all following arguments as ignored """ +
"""subdirectories,""" +
""" place the processed directories in front of -i.
Default: .git .svn"""))
args = parser.parse_args()
if args.ignore is None:
args.ignore = ['.git', '.svn']
ignore = args.ignore
igndirs = [d for d in args.ignore if os.path.isdir(d)]
ignfiles = [f for f in args.ignore if os.path.isfile(f)]
def possiblyRemove(rootDir, toRemove):
"""Remove root/toRemove if exists.
Attributes:
rootDir: directory where toRemove should lie.
toRemove: filename that is to be removed.
"""
path = os.path.join(rootDir, toRemove)
# if os.path.exists(path):
if args.onlylog:
if os.path.exists(path):
print("removed file:", path)
else:
try:
os.remove(path)
except FileNotFoundError:
# do nothing, not to be removed
pass
else:
print("removed file:", path)
for top in args.filenames:
top = os.path.abspath(top)
for root, dirs, files in os.walk(top, topdown=True):
# print("currdir:", root, dirs)
# remove directories and files that are to be ignored
dirs[:] = [d for d in dirs if
not any(os.path.samefile(os.path.join(root, d), ign)
for ign in igndirs)]
files[:] = [f for f in files if
not any(os.path.samefile(os.path.join(root, f), ign)
for ign in ignfiles)]
for tex in [os.path.splitext(f)[0] for f in files if
f.endswith(".tex") and len(f) > 4]:
if args.verbose:
print("remove associated files to:", tex)
for ext in createdExtensions:
possiblyRemove(root, tex + "." + ext)