import os
import system

def unrewritehis(file):
  try:
    fo = open(file+'/__init__2.py','w')
    print("Opened outputfile\n")
    
    f = open(file+'/__init__.py','r')
    print("Opened app\n")

    found_beginning = False
    for line in f.readlines():
      if line.rstrip() == '#History rewritten by THOR':
        found_beginning = True
        continue
      if found_beginning:
        fo.write(line)
    print("Rewritten: " +file)
    f.close()
    fo.close()
    os.remove(file+'/__init__.py')
    os.rename(file+'/__init__2.py', file+'/__init__.py')
  except OSError:
    print("No __init__.py\n")

for direc in os.listdir('/apps'):
  unrewritehis('/apps/'+direc)