Last active
June 29, 2018 12:28
-
-
Save pozitron57/f3db39f790e12519c50210d9a1be239a to your computer and use it in GitHub Desktop.
Python3 script to find and move unused plots in latex project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
#coding=utf8 | |
import re, os | |
path = '/home/slisakov/Dropbox/documents/science/thesis/' | |
plots = '/home/slisakov/Dropbox/documents/science/thesis/plots/' | |
used_plots = [] # present in tex | |
commented_plots = [] # present in tex, commented | |
all_plots =[] # present in tex (commented and uncommented) | |
unused_plots = [] # not present in tex | |
real_plots = [] # physically existing files in plots/ dir | |
# Create [all_plots], [used_plots], [commented_plots] | |
for f in os.listdir(path): | |
if re.search(r'\.tex$', f): | |
with open(path + f) as f: | |
for line in f: | |
if re.search(r'.*%.*\includegraphics', line): | |
g = re.sub(r'.*{(.*)}', r'\1', line).strip() | |
commented_plots.append(g) | |
all_plots.append(g) | |
if re.search(r'^[^%]*\includegraphics', line): | |
g = re.sub(r'.*{(.*)}', r'\1', line).strip() | |
used_plots.append(g) | |
all_plots.append(g) | |
# Create [real_plots] | |
for f in os.listdir(plots): | |
g = os.path.splitext(f)[0] | |
real_plots.append(g) | |
# Check consistency | |
if len(commented_plots) + len(used_plots) != len(all_plots): | |
raise Exception('Check your script sucker, it is not working properly') | |
# Create [real_plots] | |
for rp in real_plots: | |
if rp not in used_plots and rp not in commented_plots: | |
unused_plots.append(rp) | |
#print( len(unused_plots), unused_plots) | |
# Create plots/unused dir if doesn't exist | |
if len(unused_plots) > 0: | |
os.makedirs(plots + 'unused/', exist_ok=True) | |
# Move [unused_plots] to plots/unused/, DON'T rewrite files | |
for f in os.listdir(plots): | |
if os.path.isfile(plots+f): | |
if os.path.splitext(f)[0] in unused_plots: | |
if not os.path.isfile(plots + 'unused/' + f): | |
os.rename(plots + f, plots + 'unused/' + f) | |
print('file',f,'is moved to unused/ dir') | |
else: | |
print('file',f,'already exists. File ___IS NOT___ moved') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment