G
gb345
The following fragment is from a tiny maintenance script that,
among other things, edits itself, by rewriting the line that ends
with '### REPLACE'.
######################################################################
import re
import fileinput
LAST_VERSION = 'VERSION 155' ### REPLACE
service = Service(url='http://url.to.service')
if service.version_string == LAST_VERSION:
sys.exit(0)
for line in fileinput.input(sys.argv[0], inplace=True):
if re.search(r"### REPLACE$", line):
print ("LAST_VERSION = '%s' ### REPLACE" %
service.version_string)
else:
print line,
# ...and goes on to do more stuff...
######################################################################
This script is meant to run periodically (via cron), and "do more
stuff" whenever the fetched value of service.version_string differs
from what it was at the time of the script's prior invocation.
(The interval of time between such changes of value varies from
one change to the next, but it is always of the order of several
weeks.)
Hence this script needs to preserve state between invocations.
The rationale for the acrobatics with fileinput above is to make
this script completely self-contained, by circumventing the need
some external means (e.g. a second file, or a DB) of preserving
state between invocations.
Is there a better way to circumvent the requirement for an external
repository of state information?
G
among other things, edits itself, by rewriting the line that ends
with '### REPLACE'.
######################################################################
import re
import fileinput
LAST_VERSION = 'VERSION 155' ### REPLACE
service = Service(url='http://url.to.service')
if service.version_string == LAST_VERSION:
sys.exit(0)
for line in fileinput.input(sys.argv[0], inplace=True):
if re.search(r"### REPLACE$", line):
print ("LAST_VERSION = '%s' ### REPLACE" %
service.version_string)
else:
print line,
# ...and goes on to do more stuff...
######################################################################
This script is meant to run periodically (via cron), and "do more
stuff" whenever the fetched value of service.version_string differs
from what it was at the time of the script's prior invocation.
(The interval of time between such changes of value varies from
one change to the next, but it is always of the order of several
weeks.)
Hence this script needs to preserve state between invocations.
The rationale for the acrobatics with fileinput above is to make
this script completely self-contained, by circumventing the need
some external means (e.g. a second file, or a DB) of preserving
state between invocations.
Is there a better way to circumvent the requirement for an external
repository of state information?
G