However...
cat <<EOF | sed -e's/\\input{\(.*\)}/\1.tex/' | xargs touch
\input{intro}
\input{cha1}
\input{cha2}
\input{cha3}
\input{cha4}
\input{cha5}
\input{conclusions}
EOF
cat <<EOF | sed -e's/\\input{\(.*\)}/\1.tex/' | xargs touch
\input{intro}
\input{cha1}
\input{cha2}
\input{cha3}
\input{cha4}
\input{cha5}
\input{conclusions}
EOF
To my horror and dismay I found out that my bash-fu (ok, zsh-fu) dropped to the point of seriously sucking. The question was simple... I made a judgment error and put a git repository on my dropbox are and I also used it actively from multiple computers (while I should have just use it to synchronize stuff and edit two clones).
So I basically found out some files like “./.git/index (enrico-eee's conflicted copy 2010-10-16)” and my commits disappeared. Dropbox renamed the “right” file with that name and used an older file. It is not clear why this happened. However, it was not pleasant.
Given my modest knowledge of git (modest, but not non-existent) I supposed that everything could be solved renaming the files to their rightful name. And in fact it did. The point was renaming the files.
My idea was using find. Ok. I tried some solutions and I “almost” got there. In fact a couple of cut and pastes could have finished it. Though I understood that:
I eventually put down a bunch of python lines doing the trick:
1 import os 2 import re 3 import shutil 4 from os import path 5 6 rex = re.compile('(?P<name>.*) \(enrico-eee\'s conflicted copy .*\)(?P<ext>.*)') 7 8 for root, dirs, files in os.walk(os.getcwd()): 9 for filename in files: 10 match = rex.match(filename) 11 if match: 12 new_name = "%(name)s%(ext)s" % match.groupdict() 13 shutil.move( 14 path.join(root, filename), 15 path.join(root, new_name) 16 )
I think that my shell scripting abilities are going to decrease. Almost every-time I have a problem I solve it with Python, since I’m far more skilled in Python scripting. Not even sure it is a bad thing.
'''Simple module containing a file-like object whose behavior is
to actually save the file only when closed. Before that, data is
stored in a temporary file: thus when creating a file, if another file
with the same name exists that is never substituted with a partial
file.'''
import os
import tempfile
class AtomicFile(object):
'''A file-like object where writes are committed only when closed.
A temporary named file is used in order to store the results.
On close, the temporary is renamed according to the name parameter.
AtomicFiles are meant to be used with context managers.'''
# Unfortunately the TemporaryFile interface uses a dir parameter
# pylint: disable-msg=R0913
# pylint: disable-msg=W0622
def __init__(self, name, bufsize=-1, suffix='', prefix='', dir=None):
mode = 'r+b'
self.name = name
self.tempfile = tempfile.NamedTemporaryFile(
mode=mode, bufsize=bufsize, suffix=suffix, prefix=prefix,
dir=dir, delete=False)
def __enter__(self):
return self
def __exit__(self, _exc_type, _exc_val, _exc_tb):
self.swap()
return False
def swap(self):
'Explicitly closes and renames the temporary file.'
self.tempfile.close()
os.rename(self.tempfile.name, self.name)
close = swap
def write(self, what):
'Writes a string to the file'
self.tempfile.write(what)
import unittest
import tempfile
import atomic_file
import os
from os import path
class TestAtomicFile(unittest.TestCase):
def setUp(self):
self.directory = path.dirname(__file__)
self.name = tempfile.mktemp()
def tearDown(self):
try:
os.remove(self.name)
except OSError:
pass
def testCreate(self):
af = atomic_file.AtomicFile(name=self.name, dir=self.directory)
self.assert_(not path.exists(self.name))
self.assert_(path.exists(path.join(self.directory,
af.tempfile.name)))
af.swap()
self.assert_(path.exists(self.name))
self.assert_(not path.exists(path.join(self.directory,
af.tempfile.name)))
def testClose(self):
af = atomic_file.AtomicFile(name=self.name, dir=self.directory)
self.assert_(not path.exists(self.name))
self.assert_(path.exists(path.join(self.directory,
af.tempfile.name)))
af.close()
self.assert_(path.exists(self.name))
self.assert_(not path.exists(path.join(self.directory,
af.tempfile.name)))
def testContext(self):
with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
self.assert_(not path.exists(self.name))
self.assert_(path.exists(path.join(self.directory, af.tempfile.name)))
self.assert_(path.exists(self.name))
self.assert_(not path.exists(path.join(self.directory,
af.tempfile.name)))
def testWrite(self):
with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
text = 'THE TEXT\n'
af.write(text)
self.assertEqual(file(self.name).read(), text)
def testMoreWrite(self):
with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
lines = ['THE TEXT', 'MORE TEXT', 'AGAIN!']
for line in lines:
print >> af, line
self.assertEqual(file(self.name).read(), '\n'.join(lines) + '\n')
def hasExplosion(self):
with atomic_file.AtomicFile(name=self.name, dir=self.directory) as af:
raise RuntimeError()
self.assert_(not path.exists(self.name))
self.assert_(not path.exists(path.join(self.directory,
af.tempfile.name)))
def testBoom(self):
self.assertRaises(RuntimeError, self.hasExplosion)
Don't touch my Python!Well... anyway. As a consequence I heavily rely on python own module manager, that is to say pip/easy_install. You can tell distutils the default base install directory. For example, on Linux, I have in my home directory this .pydistutil.cfg file:
% cat .pydistutils.cfg [install] prefix = ~/.local install_scripts = ~/bin install_lib = ~/.local/lib/python$py_version_short/site-packages