0

I am trying to convert a JTE template into a JTE v2 template in python. However, I need to figure out how to update the line above the search pattern...

Jenkins file 

libraries {
  merge = true
  nexus {
    merge = true
  }
  sonarqube {
    merge = true
  }
}

git {
  merge = true
  checkout {
    override = true
    shallow = false
  }
}

and I need to convert it into a format like this ...

@merge libraries {
  @merge nexus {
  }
  @merge sonarqube {
  }
}

@merge git {
  @override checkout {
    shallow = false
  }
}

I basicaly have to go through the file line by line and search for merge or override and append it to the begining of the previous line and remove the line containing the match...

a_file = open("jenkins.groovy", "r")
lines = a_file.readlines()
a_file.close()

new_file = open("jenkins_patched.groovy", "w")
for line in lines:
    if line.strip("\n") != "merge" or line.strip("\n") != "override":
        new_file.write(line)

new_file.close()

But, how does one ammend the previous line in the file ?

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Mr. E
  • 457
  • 1
  • 5
  • 20
  • Nothing to do with `git`, removing the tag. – Romain Valeri Jun 08 '22 at 12:04
  • Why do you have to do this with Python? Can't you just use a text editor? – mkrieger1 Jun 08 '22 at 12:07
  • 1
    Does this answer your question? [How to get the previous element when using a for loop?](https://stackoverflow.com/questions/4002598/how-to-get-the-previous-element-when-using-a-for-loop) – mkrieger1 Jun 08 '22 at 12:08
  • @mkrieger1 I have a serveral hundered files to update in a a directory, it is not just one file and I would desperately like an automation to do this! But, your pointer is exactly what I was looking for! Thanks for that! I guess i forgot to turn my brain on yesturday! :) – Mr. E Jun 09 '22 at 03:17

0 Answers0