Changes between Initial Version and Version 1 of Ticket #4276, comment 1


Ignore:
Timestamp:
Jun 30, 2015, 5:15:08 PM (9 years ago)
Author:
Ryan J Ollos
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #4276, comment 1

    initial v1  
    11I'm revisiting fixing the problems that my attached solution has and I've come up with a much simpler solution (conceptually).  Specifically, if a file is opened in mode 'w', it is automatically truncated to empty, but otherwise retains unix permissions and ownership.  So, I'd recommend the following model (and this is more or less what I'm using in my existing code):
    22
    3 ---
     3{{{#!python
    44password_lines = open('trac.htpasswd').readlines()
    55
    66(changed, password_lines) = process(password_lines) # i.e. change a password or delete a line or add a line
     7}}}
    78
    89if changed:
     10
     11{{{#!python
    912    open('trac.htpasswd', 'w').writelines(password_lines)
    10 ---
     13}}}
    1114
    1215You should of course break things out and trap file IO exceptions, but this allows you to do a quick proof of concept in ipython (if that's your fancy).