File: class/Extras/Other/_old-Tools/packaging/Scripts/fixeoln_all.py

#########################################################
# Use: "python fixeoln_all.py [tounix|todos] patterns?".
# find and convert end-of-lines in all text files 
# at and below the directory where this script is
# run (the dir you are in when you type 'python').
# If needed, tries to use the Python find.py lib 
# module, else reads the output of a unix-style
# find executable command; we could also use a 
# find -exec option to spawn a coverter script.
# Uses default filename patterns list if absent.
#
# Examples:
#    cd examples
#    python Tools/fixeoln_all.py tounix
#
#    cd Html\Examples
#    python ..\..\Tools\fixeoln_all.py tounix
#
# converts any DOS end-lines to UNIX end-lines, in 
# all text files in and below the Examples directory 
# (i.e., all source-code files).  Replace "tounix" with
# "todos" to convert any UNIX end-lines to DOS form 
# instead.  This script only changes files that need to 
# be changed, so it's safe to run brute-force from a 
# root-level directory to force platform conformance.
# "python ..\..\Tools\fixeoln_all.py tounix *.txt"
# converts just .txt files (quote on UNIX: "*.txt").
# See also: fixeoln_one and fixeoln_dir versions,
# and the find-like logic in top-level Launcher.py.
#########################################################

import os, sys, string
debug    = 0
pyfind   = 0      # force py find
listonly = 0      # 1=show find results only

def findFiles(patts, debug=debug, pyfind=pyfind):
    try:
        if sys.platform[:3] == 'win' or pyfind:
            print 'Using Python find'
            import find                                # use python lib find.py
            matches = map(find.find, patts)            # start dir default = '.'
        else:
            print 'Using find executable'
            matches = []
            for patt in patts:
                findcmd = 'find . -name "%s" -print' % patt  # run find command
                lines = os.popen(findcmd).readlines()        # remove endlines
                matches.append(map(string.strip, lines))     # lambda x: x[:-1]
    except:
        assert 0, 'Sorry - cannot find files'
    if debug: print matches
    return matches

if __name__ == '__main__':
    from fixeoln_dir import patts
    from fixeoln_one import convertEndlines

    errmsg = 'Required first argument missing: "todos" or "tounix"'
    assert (len(sys.argv) >= 2 and sys.argv[1] in ['todos', 'tounix']), errmsg

    if len(sys.argv) > 2:                  # quote in unix shell 
        patts = sys.argv[2:]               # else tries to expand
    matches = findFiles(patts)

    count = 0
    for matchlist in matches:                 # a list of lists
        for fname in matchlist:               # one per pattern
            if listonly:
                print count+1, '=>', fname 
            else:  
                convertEndlines(sys.argv[1], fname)
            count = count + 1
    print 'Visited %d files' % count



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