~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to mozc_build_tools/gyp/pylib/gyp/MSVSProject.py

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
2
 
 
3
# Copyright (c) 2009 Google Inc. All rights reserved.
 
4
# Use of this source code is governed by a BSD-style license that can be
 
5
# found in the LICENSE file.
 
6
 
 
7
"""Visual Studio project reader/writer."""
 
8
 
 
9
import common
 
10
import xml.dom
 
11
import xml.dom.minidom
 
12
import MSVSNew
 
13
 
 
14
#------------------------------------------------------------------------------
 
15
 
 
16
 
 
17
class Tool(object):
 
18
  """Visual Studio tool."""
 
19
 
 
20
  def __init__(self, name, attrs=None):
 
21
    """Initializes the tool.
 
22
 
 
23
    Args:
 
24
      name: Tool name.
 
25
      attrs: Dict of tool attributes; may be None.
 
26
    """
 
27
    self.name = name
 
28
    self.attrs = attrs or {}
 
29
 
 
30
  def CreateElement(self, doc):
 
31
    """Creates an element for the tool.
 
32
 
 
33
    Args:
 
34
      doc: xml.dom.Document object to use for node creation.
 
35
 
 
36
    Returns:
 
37
      A new xml.dom.Element for the tool.
 
38
    """
 
39
    node = doc.createElement('Tool')
 
40
    node.setAttribute('Name', self.name)
 
41
    for k, v in self.attrs.items():
 
42
      node.setAttribute(k, v)
 
43
    return node
 
44
 
 
45
 
 
46
class Filter(object):
 
47
  """Visual Studio filter - that is, a virtual folder."""
 
48
 
 
49
  def __init__(self, name, contents=None):
 
50
    """Initializes the folder.
 
51
 
 
52
    Args:
 
53
      name: Filter (folder) name.
 
54
      contents: List of filenames and/or Filter objects contained.
 
55
    """
 
56
    self.name = name
 
57
    self.contents = list(contents or [])
 
58
 
 
59
 
 
60
#------------------------------------------------------------------------------
 
61
 
 
62
 
 
63
class Writer(object):
 
64
  """Visual Studio XML project writer."""
 
65
 
 
66
  def __init__(self, project_path, version):
 
67
    """Initializes the project.
 
68
 
 
69
    Args:
 
70
      project_path: Path to the project file.
 
71
      version: Format version to emit.
 
72
    """
 
73
    self.project_path = project_path
 
74
    self.doc = None
 
75
    self.version = version
 
76
 
 
77
  def Create(self, name, guid=None, platforms=None):
 
78
    """Creates the project document.
 
79
 
 
80
    Args:
 
81
      name: Name of the project.
 
82
      guid: GUID to use for project, if not None.
 
83
    """
 
84
    self.name = name
 
85
    self.guid = guid or MSVSNew.MakeGuid(self.project_path)
 
86
 
 
87
    # Default to Win32 for platforms.
 
88
    if not platforms:
 
89
      platforms = ['Win32']
 
90
 
 
91
    # Create XML doc
 
92
    xml_impl = xml.dom.getDOMImplementation()
 
93
    self.doc = xml_impl.createDocument(None, 'VisualStudioProject', None)
 
94
 
 
95
    # Add attributes to root element
 
96
    self.n_root = self.doc.documentElement
 
97
    self.n_root.setAttribute('ProjectType', 'Visual C++')
 
98
    self.n_root.setAttribute('Version', self.version.ProjectVersion())
 
99
    self.n_root.setAttribute('Name', self.name)
 
100
    self.n_root.setAttribute('ProjectGUID', self.guid)
 
101
    self.n_root.setAttribute('RootNamespace', self.name)
 
102
    self.n_root.setAttribute('Keyword', 'Win32Proj')
 
103
 
 
104
    # Add platform list
 
105
    n_platform = self.doc.createElement('Platforms')
 
106
    self.n_root.appendChild(n_platform)
 
107
    for platform in platforms:
 
108
      n = self.doc.createElement('Platform')
 
109
      n.setAttribute('Name', platform)
 
110
      n_platform.appendChild(n)
 
111
 
 
112
    # Add tool files section
 
113
    self.n_tool_files = self.doc.createElement('ToolFiles')
 
114
    self.n_root.appendChild(self.n_tool_files)
 
115
 
 
116
    # Add configurations section
 
117
    self.n_configs = self.doc.createElement('Configurations')
 
118
    self.n_root.appendChild(self.n_configs)
 
119
 
 
120
    # Add empty References section
 
121
    self.n_root.appendChild(self.doc.createElement('References'))
 
122
 
 
123
    # Add files section
 
124
    self.n_files = self.doc.createElement('Files')
 
125
    self.n_root.appendChild(self.n_files)
 
126
    # Keep a dict keyed on filename to speed up access.
 
127
    self.n_files_dict = dict()
 
128
 
 
129
    # Add empty Globals section
 
130
    self.n_root.appendChild(self.doc.createElement('Globals'))
 
131
 
 
132
  def AddToolFile(self, path):
 
133
    """Adds a tool file to the project.
 
134
 
 
135
    Args:
 
136
      path: Relative path from project to tool file.
 
137
    """
 
138
    n_tool = self.doc.createElement('ToolFile')
 
139
    n_tool.setAttribute('RelativePath', path)
 
140
    self.n_tool_files.appendChild(n_tool)
 
141
 
 
142
  def _AddConfigToNode(self, parent, config_type, config_name, attrs=None,
 
143
                       tools=None):
 
144
    """Adds a configuration to the parent node.
 
145
 
 
146
    Args:
 
147
      parent: Destination node.
 
148
      config_type: Type of configuration node.
 
149
      config_name: Configuration name.
 
150
      attrs: Dict of configuration attributes; may be None.
 
151
      tools: List of tools (strings or Tool objects); may be None.
 
152
    """
 
153
    # Handle defaults
 
154
    if not attrs:
 
155
      attrs = {}
 
156
    if not tools:
 
157
      tools = []
 
158
 
 
159
    # Add configuration node and its attributes
 
160
    n_config = self.doc.createElement(config_type)
 
161
    n_config.setAttribute('Name', config_name)
 
162
    for k, v in attrs.items():
 
163
      n_config.setAttribute(k, v)
 
164
    parent.appendChild(n_config)
 
165
 
 
166
    # Add tool nodes and their attributes
 
167
    if tools:
 
168
      for t in tools:
 
169
        if isinstance(t, Tool):
 
170
          n_config.appendChild(t.CreateElement(self.doc))
 
171
        else:
 
172
          n_config.appendChild(Tool(t).CreateElement(self.doc))
 
173
 
 
174
  def AddConfig(self, name, attrs=None, tools=None):
 
175
    """Adds a configuration to the project.
 
176
 
 
177
    Args:
 
178
      name: Configuration name.
 
179
      attrs: Dict of configuration attributes; may be None.
 
180
      tools: List of tools (strings or Tool objects); may be None.
 
181
    """
 
182
    self._AddConfigToNode(self.n_configs, 'Configuration', name, attrs, tools)
 
183
 
 
184
  def _AddFilesToNode(self, parent, files):
 
185
    """Adds files and/or filters to the parent node.
 
186
 
 
187
    Args:
 
188
      parent: Destination node
 
189
      files: A list of Filter objects and/or relative paths to files.
 
190
 
 
191
    Will call itself recursively, if the files list contains Filter objects.
 
192
    """
 
193
    for f in files:
 
194
      if isinstance(f, Filter):
 
195
        node = self.doc.createElement('Filter')
 
196
        node.setAttribute('Name', f.name)
 
197
        self._AddFilesToNode(node, f.contents)
 
198
      else:
 
199
        node = self.doc.createElement('File')
 
200
        node.setAttribute('RelativePath', f)
 
201
        self.n_files_dict[f] = node
 
202
      parent.appendChild(node)
 
203
 
 
204
  def AddFiles(self, files):
 
205
    """Adds files to the project.
 
206
 
 
207
    Args:
 
208
      files: A list of Filter objects and/or relative paths to files.
 
209
 
 
210
    This makes a copy of the file/filter tree at the time of this call.  If you
 
211
    later add files to a Filter object which was passed into a previous call
 
212
    to AddFiles(), it will not be reflected in this project.
 
213
    """
 
214
    self._AddFilesToNode(self.n_files, files)
 
215
    # TODO(rspangler) This also doesn't handle adding files to an existing
 
216
    # filter.  That is, it doesn't merge the trees.
 
217
 
 
218
  def AddFileConfig(self, path, config, attrs=None, tools=None):
 
219
    """Adds a configuration to a file.
 
220
 
 
221
    Args:
 
222
      path: Relative path to the file.
 
223
      config: Name of configuration to add.
 
224
      attrs: Dict of configuration attributes; may be None.
 
225
      tools: List of tools (strings or Tool objects); may be None.
 
226
 
 
227
    Raises:
 
228
      ValueError: Relative path does not match any file added via AddFiles().
 
229
    """
 
230
    # Find the file node with the right relative path
 
231
    parent = self.n_files_dict.get(path)
 
232
    if not parent:
 
233
      raise ValueError('AddFileConfig: file "%s" not in project.' % path)
 
234
 
 
235
    # Add the config to the file node
 
236
    self._AddConfigToNode(parent, 'FileConfiguration', config, attrs, tools)
 
237
 
 
238
  def Write(self, writer=common.WriteOnDiff):
 
239
    """Writes the project file."""
 
240
    f = writer(self.project_path)
 
241
    self.doc.writexml(f, encoding='Windows-1252', addindent='  ', newl='\r\n')
 
242
    f.close()
 
243
 
 
244
#------------------------------------------------------------------------------