~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/migrater/rename_zcml.py

  • Committer: Curtis Hovey
  • Date: 2011-12-24 17:50:18 UTC
  • mfrom: (14600.1.13 apocalypta-1)
  • mto: This revision was merged to the branch mainline in revision 14602.
  • Revision ID: curtis.hovey@canonical.com-20111224175018-nsk2d6vvez77iscb
Merged i18n fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
            continue
45
45
        migrate_zcml(old_top, old_path, new_path)
46
46
        dummy, file_name = os.path.split(old_path)
47
 
        rewrite_zcml_class_paths(
48
 
            old_top, new_path, file_name, app_members, app_name)
49
 
        create_browser_zcml(app_name, new_path, file_name, app_members)
50
47
        not_moved.remove(lib_path)
51
48
    package_names = consolidate_app_zcml(app_name, new_path)
52
49
    register_zcml(package_names, old_top)
64
61
        print "    Removed old configure include."
65
62
 
66
63
 
67
 
def rewrite_zcml_class_paths(old_top, new_path,
68
 
                             file_name, app_members, app_name):
69
 
    """Rewrite app classes to use relative paths."""
70
 
    abs_lib = 'canonical\.launchpad'
71
 
    module_name, dummy = os.path.splitext(file_name)
72
 
    for package_name in app_members:
73
 
        try:
74
 
            members = '|'.join(app_members[package_name][module_name])
75
 
        except KeyError:
76
 
            old_module_path = os.path.join(
77
 
                old_top, package_name, '%s.py' % module_name)
78
 
            if os.path.isfile(old_module_path):
79
 
                print '    ** missing %s.%s' % (package_name, module_name)
80
 
            continue
81
 
        module_pattern = r'\b%s(\.%s\.)(?:\w*\.)?(%s)\b' % (
82
 
            abs_lib, package_name, members)
83
 
        substitution = r'lp.%s\1%s.\2' % (app_name, module_name)
84
 
        for dummy in find_matches(
85
 
            new_path, file_name, module_pattern, substitution=substitution):
86
 
            # This function is an iterator, but we do not care about the
87
 
            # summary of what is changed.
88
 
            pass
89
 
    # Update the menu and navigation directives.
90
 
    module_pattern = r'module="canonical.launchpad.browser"'
91
 
    substitution = r'module="lp.%s.browser.%s"' % (app_name, module_name)
92
 
    for dummy in find_matches(
93
 
        new_path, file_name, module_pattern, substitution=substitution):
94
 
        pass
95
 
 
96
 
 
97
 
def create_browser_zcml(app_name, new_path, file_name, app_members):
98
 
    """Extract browser ZCML to the browser/ directory."""
99
 
    module_name, dummy = os.path.splitext(file_name)
100
 
    browser_module_name = '.browser.%s' % module_name
101
 
    new_file_path = os.path.join(new_path, file_name)
102
 
    source = open(new_file_path)
103
 
    try:
104
 
        parser = etree.XMLParser(remove_blank_text=True)
105
 
        tree = etree.parse(source, parser)
106
 
    finally:
107
 
        source.close()
108
 
    doc = tree.getroot()
109
 
    browser_doc = etree.fromstring(EMPTY_ZCML)
110
 
    # Move the facet browser directives first.
111
 
    for facet_node in doc.xpath('./zope:facet', namespaces=namespaces):
112
 
        facet = facet_node.get('facet')
113
 
        browser_facet = etree.Element('facet', facet=facet)
114
 
        for node in facet_node.xpath('./browser:*', namespaces=namespaces):
115
 
            facet_node.remove(node)
116
 
            browser_facet.append(node)
117
 
        if len(browser_facet) > 0:
118
 
            browser_doc.append(browser_facet)
119
 
    # All remaining browser directives can be moved.
120
 
    for node in doc.xpath('./browser:*', namespaces=namespaces):
121
 
        doc.remove(node)
122
 
        browser_doc.append(node)
123
 
    # Split the menus directive into two nodes, one for the this app, and
124
 
    # one for the old Launchpad app.
125
 
    module_class_path = "lp.%s.browser.%s" % (app_name, module_name)
126
 
    other_class_path = "canonical.launchpad.browser"
127
 
    for node in browser_doc.xpath('./browser:menus', namespaces=namespaces):
128
 
        app_menus = []
129
 
        other_menus = []
130
 
        module_members = app_members['browser'][module_name]
131
 
        for menu in node.get('classes').split():
132
 
            if menu in module_members:
133
 
                app_menus.append(menu)
134
 
            else:
135
 
                other_menus.append(menu)
136
 
        if len(other_menus) > 0:
137
 
            browser_doc.append(
138
 
                create_menu_node(other_class_path, other_menus))
139
 
        if len(app_menus) > 0:
140
 
            browser_doc.append(
141
 
                create_menu_node(module_class_path, app_menus))
142
 
        browser_doc.remove(node)
143
 
    # Only save the browser and other doc if information was put into them.
144
 
    if len(browser_doc) > 0:
145
 
        file_path = os.path.join(new_path, 'browser', file_name)
146
 
        write_zcml_file(file_path, browser_doc)
147
 
        write_zcml_file(new_file_path, doc)
148
 
 
149
 
 
150
64
def create_menu_node(module, menus):
151
65
    """Create a browser:menus node for the module and list of menu classes."""
152
66
    menus_tag = '{%s}menus' % namespaces['browser']