File: genhtml/__docs__/fix-readmes.py

#!/usr/bin/python
# Runs on Python 2 or 3
"""
-----------------------------------------------------------
fix-readmes.py: copy readmes for Apache index displays.
Shipped as an example in genhtml's package: see source
code of genhtml.py for copyright, author, and license.

Local usage: 
   Run AUTOMATICALLY by publish scripts on UNION folder.
Remote usage:
   Run site's copy via SSH on html/ root folder - only.
Do *not* run this elsewhere: it may create lots of files.

Run me on the site root of local and live copies, to 
repair all README*.txt files in Apache autoindex pages
broken by an Apache rewrite rule for text-file display.
The display rule's importance offset its consequences. 

Renaming files to _README*.txt makes them appear in index 
lists where the user can click to view, but the files' 
text does not show up inline on the index page itself.  
For prominent folders where inline display is desired, 
also copy to README.html and set ReadmeName in .htaccess.

Note: readme.txt appears in this site's autoindex lists 
okay, so there's no need to rename these to _readme.txt 
too (and no need to make comparisons caseless here).
Also note: all README*.txt broke, not just README.txt;
this site had 159 README.txt, and 9 README*.txt others!
-----------------------------------------------------------
"""
import os, shutil, sys
rootdir = sys.argv[1]      # arg=root dir
TRACEONLY = 0 #True           # 0/False=copy

# visit all folders in rootdir
copies = 0
for (thisdir, dirshere, fileshere) in os.walk(rootdir):

    # fix any and all README*.txt here
    for filename in fileshere:
        if filename.startswith('README') and filename.endswith('.txt'):
 
            # prefix with "_" to see in index
            pathfrom = os.path.join(thisdir, filename)
            pathto   = os.path.join(thisdir, '_' + filename)
            state    = 'Replace' if os.path.exists(pathto) else 'Add new'
            print('%s %s' % (state, pathfrom))

            if not TRACEONLY:
                # copy even if already exists: may have changed
                shutil.copyfile(pathfrom, pathto)   # none are symlinks (py3 arg)
                shutil.copystat(pathfrom, pathto)   # modtime: make backups happy

            copies += 1

print('Total copies: %d' % copies)



[Home page] Books Code Blog Python Author Train Find ©M.Lutz