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."
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:
74
members = '|'.join(app_members[package_name][module_name])
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)
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.
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):
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)
104
parser = etree.XMLParser(remove_blank_text=True)
105
tree = etree.parse(source, parser)
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):
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):
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)
135
other_menus.append(menu)
136
if len(other_menus) > 0:
138
create_menu_node(other_class_path, other_menus))
139
if len(app_menus) > 0:
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)
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']