FAQ
Hi there.

Another newbie, armed with Learning Python. Setting myself a task of
manipulating all the VirtualHost entries on a Linux Apache server....

The first thing I wanted to do was to clean up the number of newlines
between eachvirthost entry... Which then allowed me to make a list of
VirtualHost entries...

Just because it was easy I decided to sort them, though this will lead to
another challenge later when I try to put a specific entry abck to the top
of the list.

Next, and this was the main purpose of the project in the first place, I
need to check through each VirtualHost to see if they are logging, and then
if they aren't insert a TransferLog and ErrorLog entry into their
VirtualHost.

This is where I get into a spot of bother. I need to do something similart
to creating a file scanner loop, but instead of a file I need to use this
list. I just don't see how.

Is it possible? If I write the list out to a file, then do a file scanner
loop all the \n in each list entry will make everything screwy.


The code I have below works great, I just can't see where to take it next
to accomplish what I need. Any advice, comments, even style guide would
be greatly appreciated.

Tefol


***

import string,re

Hosts = open(r'd:\proj\python\virts\virts.conf', 'r')
sVirts = Hosts.read()

sFile = re.sub('VirtualHost>\n*<VirtualHost ',
'VirtualHost>\n\n<VirtualHost ', sVirts)
lFile = string.split(sFile, '\n\n')
lFile.sort()



output = open(r'd:\proj\python\virts\list.txt', 'w')

output.writelines(lFile)

output.close()

INL = open(r'd:\proj\python\virts\list.txt', 'r') #Insert New Line
sINL = INL.read()

sSNL = re.sub('VirtualHost><VirtualHost ', 'VirtualHost>\n\n<VirtualHost ',
sINL) #Substitute New Line

output = open(r'd:\proj\python\virts\file.txt', 'w')

output.write(sSNL)

output.close()

Search Discussions

  • Ignacio Vazquez-Abrams at Aug 28, 2001 at 7:56 pm

    On Tue, 28 Aug 2001, tefol wrote:

    Hi there.

    Another newbie, armed with Learning Python. Setting myself a task of
    manipulating all the VirtualHost entries on a Linux Apache server....
    Oh hell...

    Not for being a newbie, for wanting to go anywhere near VirtualHost with a
    program. I guess sending a newbie after that is probably best. You all seem to
    think of such a thing as a "challenge". To any sane person such a thing would
    be nuts ;)
    The first thing I wanted to do was to clean up the number of newlines
    between eachvirthost entry... Which then allowed me to make a list of
    VirtualHost entries...

    Just because it was easy I decided to sort them, though this will lead to
    another challenge later when I try to put a specific entry abck to the top
    of the list.
    IIRC, if you use __default__:80, then you won't need a specific one at the
    top of the list.
    Next, and this was the main purpose of the project in the first place, I
    need to check through each VirtualHost to see if they are logging, and then
    if they aren't insert a TransferLog and ErrorLog entry into their
    VirtualHost.

    This is where I get into a spot of bother. I need to do something similart
    to creating a file scanner loop, but instead of a file I need to use this
    list. I just don't see how.
    You need to go through each element of the list:

    ---
    for i in lFile:
    [do something to i]
    [do something else]
    ...
    ---

    i will contain each of the elements of the list one at a time in order.
    Is it possible? If I write the list out to a file, then do a file scanner
    loop all the \n in each list entry will make everything screwy.
    Don't write it to a file until you are completely, absolutely, 100% done. File
    operations are SLOOOOOOW compared to in-memory tasks.
    The code I have below works great, I just can't see where to take it next
    to accomplish what I need. Any advice, comments, even style guide would
    be greatly appreciated.

    Tefol
    Something you can do is write code to tear apart the VirtualHost sections
    line-by-line and store the directive and its options separately. I'm sure that
    there are plenty of people who would find such a tool useful :)

    --
    Ignacio Vazquez-Abrams <ignacio at openservices.net>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 28, '01 at 7:17p
activeAug 28, '01 at 7:56p
posts2
users2
websitepython.org

2 users in discussion

Ignacio Vazquez-Abrams: 1 post Tefol: 1 post

People

Translate

site design / logo © 2023 Grokbase