tags:

views:

45

answers:

1

Hello,

I have code to read in the version number from a make file.

VERSION_ID=map(int,re.match("VERSION_ID\s*=\s*(\S+)",open("version.mk").read()).group(1).split("."))

This code takes VERSION_ID=0.0.2 and stores it as [0, 0, 2].

Is there any way I can increment this number by one and write the new version number into the version.mk file with the variable VERSION_ID.

Thanks

I have tried the same statement with write() instead of read() but I am getting an error saying that I can not write a list. I have also tried to write it as a string but am getting a bad file descriptor message.

s = str(VERSION_ID)

VERSION_ID=map(int,re.search("VERSION_ID\s*=\s*(\S+)",open("version.mk").write(s)).group(1).split("."))

I know this is rubbish, I just can't seem to find what to do here in the online docs.

I have also tried the pickle module to no avail. Maybe I'd be able to write a pickled list instead and then unpickle it. Or I was thinking I could just write over the whole line altogether.

I've tried to do anther approach, Ihave tried to get list to be entered as a string. I have tried this but I am not sure if it will work.

for x in VERSION_ID:
    "VERSION_ID={0}.{1}.{2}.format(x)
+1  A: 

perhpas something like this (you should also check for errors and that)

#! /usr/bin/python

import re

fn = "version.mk"
omk = open(fn).readlines()
nmk = open(fn, "w")
r = re.compile(r'(VERSION_ID\s*=\s*)(\S+)')

for l in omk:
    m1 = r.match(l)
    if m1:
        VERSION_ID=map(int,m1.group(2).split("."))
        VERSION_ID[2]+=1 # increment version
        l = r.sub(r'\g<1>' + '.'.join(['%s' % (v) for v in VERSION_ID]), l)
    nmk.write(l)
nmk.close()
dtmilano
Thanks, Genius, that works perfectly, I don't know what I was at.
chrissygormley