2012-07-12 20:57:05 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from tempfile import mkdtemp
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
|
2012-07-13 20:47:38 +00:00
|
|
|
IMAGE_WIDTH = re.compile(r':width: ([\d]+)px', re.UNICODE)
|
2012-07-12 20:57:05 +00:00
|
|
|
|
2012-07-13 20:34:39 +00:00
|
|
|
def copy_files(source_dir, temp_dir):
|
|
|
|
for root, dirs, files in os.walk(source_dir):
|
2012-07-13 20:47:38 +00:00
|
|
|
curr_dir = root[len(source_dir) + 1:]
|
2012-07-13 20:34:39 +00:00
|
|
|
full_dir = os.path.join(temp_dir, curr_dir)
|
|
|
|
for name in files:
|
|
|
|
if name.endswith(u'.rst'):
|
|
|
|
if not os.path.exists(full_dir):
|
|
|
|
os.makedirs(full_dir)
|
|
|
|
shutil.copy2(os.path.join(root, name),
|
|
|
|
os.path.join(full_dir, name))
|
|
|
|
|
2012-07-12 20:57:05 +00:00
|
|
|
def restore_files(temp_dir, restore_dir):
|
2012-07-13 20:34:39 +00:00
|
|
|
copy_files(temp_dir, restore_dir)
|
2012-07-12 20:57:05 +00:00
|
|
|
shutil.rmtree(temp_dir)
|
|
|
|
|
|
|
|
def adjust_image(match):
|
|
|
|
try:
|
|
|
|
width = int(match.group(1))
|
|
|
|
width = int(round(width * 2.4))
|
2012-07-13 20:50:36 +00:00
|
|
|
return ':width: %spx' % width
|
2012-07-12 20:57:05 +00:00
|
|
|
except:
|
2012-07-13 20:50:36 +00:00
|
|
|
return match.group(0)
|
2012-07-12 20:57:05 +00:00
|
|
|
|
|
|
|
def process_images(filename):
|
2012-07-13 20:47:38 +00:00
|
|
|
fd = open(filename, 'rb')
|
|
|
|
contents = fd.read()
|
|
|
|
fd.close()
|
2012-07-12 20:57:05 +00:00
|
|
|
contents = IMAGE_WIDTH.sub(adjust_image, contents)
|
|
|
|
fd = open(filename, 'wb')
|
|
|
|
fd.write(contents)
|
|
|
|
fd.close()
|
|
|
|
|
2012-07-13 20:34:39 +00:00
|
|
|
def process_files(base_dir):
|
|
|
|
for root, dirs, files in os.walk(base_dir):
|
2012-07-12 20:57:05 +00:00
|
|
|
for name in files:
|
|
|
|
if name.endswith(u'.rst'):
|
|
|
|
process_images(os.path.join(root, name))
|
|
|
|
|
|
|
|
def main():
|
|
|
|
here = os.path.abspath(os.path.split(__file__)[0])
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'restore':
|
|
|
|
temp_dir = os.path.abspath(sys.argv[2])
|
|
|
|
restore_files(temp_dir, here)
|
2012-07-13 20:34:39 +00:00
|
|
|
else:
|
|
|
|
temp_dir = mkdtemp()
|
|
|
|
print temp_dir
|
|
|
|
copy_files(here, temp_dir)
|
|
|
|
process_files(here)
|
2012-07-12 20:57:05 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|