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