clone_packages/patch_hgrc.py
changeset 61 3efaaf387e1a
equal deleted inserted replaced
60:e86c659b78a0 61:3efaaf387e1a
       
     1 #! /usr/bin/python
       
     2 # Copyright (c) 2009 Symbian Foundation Ltd
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of the License "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Symbian Foundation Ltd - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Python script to manipulate the hgrc files
       
    15 
       
    16 from ConfigParser import *
       
    17 import optparse
       
    18 import os
       
    19 import sys
       
    20 import re
       
    21 
       
    22 verbose = False;
       
    23 credentials= re.compile(r"//.*?@")
       
    24 
       
    25 def strip_credentials(hgrc):
       
    26     """  Remove the user credentials from the default path in hgrc file"""
       
    27     # e.g.
       
    28     # before http://user:pass@prod.foundationhost.org/sfl/MCL/sf/os/boardsupport/
       
    29     # after  http://prod.foundationhost.org/sfl/MCL/sf/os/boardsupport/
       
    30     if hgrc.has_section('paths'):
       
    31         if (verbose): print hgrc.items('paths')
       
    32         defpath = hgrc.get('paths', 'default')
       
    33         newpath = credentials.sub(r"//",defpath)
       
    34         #print "new path ", newpath
       
    35         hgrc.set('paths', 'default',newpath)
       
    36     elif (verbose):
       
    37         if (verbose): print "No [paths] section\n"
       
    38 
       
    39 def add_hooks(hgrc):
       
    40     if (hgrc.has_section('hooks')):
       
    41         # unpdate
       
    42         if (verbose) : print 'updating existing hooks section'
       
    43     else:
       
    44         if (verbose) : print 'adding hooks section'
       
    45         hgrc.add_section('hooks')
       
    46     # add example (windows only) hook to block local commit to the repo
       
    47     hgrc.set('hooks', 'pretxncommit.abort', 'exit /b 1')
       
    48     hgrc.set('hooks', 'pretxncommit.message', 'ERROR: This is a read only repo')
       
    49     
       
    50     
       
    51 def write_hgrcfile(hgrc,fout):
       
    52     fnewini = file(fout,'w')
       
    53     hgrc.write(fnewini)
       
    54     fnewini.close()
       
    55 
       
    56 def main():
       
    57     global verbose
       
    58     usage = "usage: %prog [options]"
       
    59     try:
       
    60         parser = optparse.OptionParser(usage)
       
    61         parser.set_defaults(filename=".hg/hgrc")
       
    62         parser.add_option("-f","--file", dest="filename", default=".hg/hgrc",metavar="FILE" , help='file to be patched')
       
    63         parser.add_option("-v", action="store_true",dest="verbose",default=False, help='Verbose trace information')
       
    64         (options, args) = parser.parse_args()
       
    65     except:
       
    66         parser.print_help()
       
    67         sys.exit(1)
       
    68 
       
    69     f = os.path.abspath(options.filename)
       
    70     if(options.verbose):
       
    71         verbose = True
       
    72         print f
       
    73     if(os.path.isfile(f)):
       
    74         try:
       
    75             #conff = file(f,'w')  #open file f for read/write
       
    76             hgrcfile = RawConfigParser()
       
    77             hgrcfile.read(f)
       
    78             if (verbose):
       
    79                 print hgrcfile.sections()
       
    80         except:
       
    81             print 'Something failed opening the configuration file'
       
    82             sys.exit(2)
       
    83     else:
       
    84         print "Configuration file does not exist? ",f
       
    85         sys.exit(2)
       
    86 
       
    87     strip_credentials(hgrcfile)
       
    88     add_hooks(hgrcfile)
       
    89     write_hgrcfile(hgrcfile,f)
       
    90 
       
    91 
       
    92 if __name__ == "__main__":
       
    93     main()