TortoiseHg and dos2unix/unix2dos

Bill Freeman bfreeman at appropriatesolutions.com
Wed Mar 4 11:06:35 CST 2009


Manlio Perillo wrote:
> Hi.
>
> Does the TortoiseHg installer provide dos2unix and unix2dos.
>   
No, but you do get (have) python, so:

def dos2unix(infilename, outfilename):
    o = open(outfilename, 'wb')
    for ln in open(infilename, 'rb'):
        o.write(ln.replace('\r\n', '\n')
    o.close()

def unix2dos(infilename, outfileame):
    o = open(outfilename, 'wb')
    for ln in open(infilename, 'rb'):
        if ln.endswith('\r\n'):
            o.write(ln)
        else:
            o.write(ln.replace('\n', '\r\n')
    o.close()

It is left as an exercise for the reader to do suitable renameing of the 
outfile to the infile's name, or to organize code so that the infile is 
read before the outfile  is opened (thus wiping out the infile), and for 
wrapping this in suitable if __name__ =="__main__" stuff to make it a 
command line utility.

Bill



More information about the Mercurial mailing list