dev/files/settings_south_syncdb.py
changeset 136 66972b143124
equal deleted inserted replaced
135:98822a0e9573 136:66972b143124
       
     1 #!/usr/bin/python
       
     2 
       
     3 import re
       
     4 import sys, getopt
       
     5 from shutil import move
       
     6 from os import remove, close
       
     7 from tempfile import mkstemp
       
     8 
       
     9 def replace(file_path, pattern, subst):
       
    10     #Create temp file
       
    11     fh, abs_path = mkstemp()
       
    12     new_file = open(abs_path,'w')
       
    13     old_file = open(file_path)
       
    14     for line in old_file:
       
    15         new_file.write(line.replace(pattern, subst))
       
    16     #close temp file
       
    17     new_file.close()
       
    18     close(fh)
       
    19     old_file.close()
       
    20     #Remove original file
       
    21     remove(file_path)
       
    22     #Move new file
       
    23     move(abs_path, file_path)
       
    24 
       
    25 def main(argv):
       
    26     in_installed_apps=None 
       
    27     uncomment=None 
       
    28     done=None 
       
    29     file = ''
       
    30     instructions='settings_south_syncdb.py -i <settings.py file> to comment south in installed apps, settings_south_syncdb.py -r -i <settings.py file> to decomment south in installed apps'
       
    31     
       
    32     try:
       
    33        opts, args = getopt.getopt(argv,"hri:",["file="])
       
    34     except getopt.GetoptError:
       
    35        print 'settings_south_syncdb.py -i <settings.py file>'
       
    36        sys.exit(2)
       
    37     if not opts:
       
    38         print instructions
       
    39         sys.exit()
       
    40     for opt, arg in opts:
       
    41        if opt == '-h':
       
    42         print instructions
       
    43         sys.exit()
       
    44        elif opt == '-r':
       
    45          uncomment=True
       
    46        elif opt in ("-i", "--file"):
       
    47          file = arg
       
    48        else:
       
    49          print instructions
       
    50          sys.exit()
       
    51     if uncomment:
       
    52         replace(file, '#\'south\',', '\'south\',')
       
    53     else:
       
    54         replace(file, '\'south\',', '#\'south\',')
       
    55 
       
    56 
       
    57 
       
    58                         
       
    59                     
       
    60 if __name__ == "__main__":
       
    61    main(sys.argv[1:])