MSVSUserFile.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Copyright (c) 2012 Google Inc. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Visual Studio user preferences file writer."""
  5. import os
  6. import re
  7. import socket # for gethostname
  8. import gyp.common
  9. import gyp.easy_xml as easy_xml
  10. #------------------------------------------------------------------------------
  11. def _FindCommandInPath(command):
  12. """If there are no slashes in the command given, this function
  13. searches the PATH env to find the given command, and converts it
  14. to an absolute path. We have to do this because MSVS is looking
  15. for an actual file to launch a debugger on, not just a command
  16. line. Note that this happens at GYP time, so anything needing to
  17. be built needs to have a full path."""
  18. if '/' in command or '\\' in command:
  19. # If the command already has path elements (either relative or
  20. # absolute), then assume it is constructed properly.
  21. return command
  22. else:
  23. # Search through the path list and find an existing file that
  24. # we can access.
  25. paths = os.environ.get('PATH','').split(os.pathsep)
  26. for path in paths:
  27. item = os.path.join(path, command)
  28. if os.path.isfile(item) and os.access(item, os.X_OK):
  29. return item
  30. return command
  31. def _QuoteWin32CommandLineArgs(args):
  32. new_args = []
  33. for arg in args:
  34. # Replace all double-quotes with double-double-quotes to escape
  35. # them for cmd shell, and then quote the whole thing if there
  36. # are any.
  37. if arg.find('"') != -1:
  38. arg = '""'.join(arg.split('"'))
  39. arg = '"%s"' % arg
  40. # Otherwise, if there are any spaces, quote the whole arg.
  41. elif re.search(r'[ \t\n]', arg):
  42. arg = '"%s"' % arg
  43. new_args.append(arg)
  44. return new_args
  45. class Writer(object):
  46. """Visual Studio XML user user file writer."""
  47. def __init__(self, user_file_path, version, name):
  48. """Initializes the user file.
  49. Args:
  50. user_file_path: Path to the user file.
  51. version: Version info.
  52. name: Name of the user file.
  53. """
  54. self.user_file_path = user_file_path
  55. self.version = version
  56. self.name = name
  57. self.configurations = {}
  58. def AddConfig(self, name):
  59. """Adds a configuration to the project.
  60. Args:
  61. name: Configuration name.
  62. """
  63. self.configurations[name] = ['Configuration', {'Name': name}]
  64. def AddDebugSettings(self, config_name, command, environment = {},
  65. working_directory=""):
  66. """Adds a DebugSettings node to the user file for a particular config.
  67. Args:
  68. command: command line to run. First element in the list is the
  69. executable. All elements of the command will be quoted if
  70. necessary.
  71. working_directory: other files which may trigger the rule. (optional)
  72. """
  73. command = _QuoteWin32CommandLineArgs(command)
  74. abs_command = _FindCommandInPath(command[0])
  75. if environment and isinstance(environment, dict):
  76. env_list = ['%s="%s"' % (key, val)
  77. for (key,val) in environment.iteritems()]
  78. environment = ' '.join(env_list)
  79. else:
  80. environment = ''
  81. n_cmd = ['DebugSettings',
  82. {'Command': abs_command,
  83. 'WorkingDirectory': working_directory,
  84. 'CommandArguments': " ".join(command[1:]),
  85. 'RemoteMachine': socket.gethostname(),
  86. 'Environment': environment,
  87. 'EnvironmentMerge': 'true',
  88. # Currently these are all "dummy" values that we're just setting
  89. # in the default manner that MSVS does it. We could use some of
  90. # these to add additional capabilities, I suppose, but they might
  91. # not have parity with other platforms then.
  92. 'Attach': 'false',
  93. 'DebuggerType': '3', # 'auto' debugger
  94. 'Remote': '1',
  95. 'RemoteCommand': '',
  96. 'HttpUrl': '',
  97. 'PDBPath': '',
  98. 'SQLDebugging': '',
  99. 'DebuggerFlavor': '0',
  100. 'MPIRunCommand': '',
  101. 'MPIRunArguments': '',
  102. 'MPIRunWorkingDirectory': '',
  103. 'ApplicationCommand': '',
  104. 'ApplicationArguments': '',
  105. 'ShimCommand': '',
  106. 'MPIAcceptMode': '',
  107. 'MPIAcceptFilter': ''
  108. }]
  109. # Find the config, and add it if it doesn't exist.
  110. if config_name not in self.configurations:
  111. self.AddConfig(config_name)
  112. # Add the DebugSettings onto the appropriate config.
  113. self.configurations[config_name].append(n_cmd)
  114. def WriteIfChanged(self):
  115. """Writes the user file."""
  116. configs = ['Configurations']
  117. for config, spec in sorted(self.configurations.iteritems()):
  118. configs.append(spec)
  119. content = ['VisualStudioUserFile',
  120. {'Version': self.version.ProjectVersion(),
  121. 'Name': self.name
  122. },
  123. configs]
  124. easy_xml.WriteXmlIfChanged(content, self.user_file_path,
  125. encoding="Windows-1252")