config.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. # config.py - Reading and writing Git config files
  2. # Copyright (C) 2011-2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Reading and writing Git configuration files.
  21. TODO:
  22. * preserve formatting when updating configuration files
  23. * treat subsection names as case-insensitive for [branch.foo] style
  24. subsections
  25. """
  26. import errno
  27. import os
  28. from collections import (
  29. OrderedDict,
  30. MutableMapping,
  31. )
  32. from dulwich.file import GitFile
  33. DEFAULT_ENCODING = 'utf-8'
  34. class Config(object):
  35. """A Git configuration."""
  36. def get(self, section, name):
  37. """Retrieve the contents of a configuration setting.
  38. :param section: Tuple with section name and optional subsection namee
  39. :param subsection: Subsection name
  40. :return: Contents of the setting
  41. :raise KeyError: if the value is not set
  42. """
  43. raise NotImplementedError(self.get)
  44. def get_boolean(self, section, name, default=None):
  45. """Retrieve a configuration setting as boolean.
  46. :param section: Tuple with section name and optional subsection namee
  47. :param name: Name of the setting, including section and possible
  48. subsection.
  49. :return: Contents of the setting
  50. :raise KeyError: if the value is not set
  51. """
  52. try:
  53. value = self.get(section, name)
  54. except KeyError:
  55. return default
  56. if value.lower() == b"true":
  57. return True
  58. elif value.lower() == b"false":
  59. return False
  60. raise ValueError("not a valid boolean string: %r" % value)
  61. def set(self, section, name, value):
  62. """Set a configuration value.
  63. :param section: Tuple with section name and optional subsection namee
  64. :param name: Name of the configuration value, including section
  65. and optional subsection
  66. :param: Value of the setting
  67. """
  68. raise NotImplementedError(self.set)
  69. def iteritems(self, section):
  70. """Iterate over the configuration pairs for a specific section.
  71. :param section: Tuple with section name and optional subsection namee
  72. :return: Iterator over (name, value) pairs
  73. """
  74. raise NotImplementedError(self.iteritems)
  75. def itersections(self):
  76. """Iterate over the sections.
  77. :return: Iterator over section tuples
  78. """
  79. raise NotImplementedError(self.itersections)
  80. def has_section(self, name):
  81. """Check if a specified section exists.
  82. :param name: Name of section to check for
  83. :return: boolean indicating whether the section exists
  84. """
  85. return (name in self.itersections())
  86. class ConfigDict(Config, MutableMapping):
  87. """Git configuration stored in a dictionary."""
  88. def __init__(self, values=None):
  89. """Create a new ConfigDict."""
  90. if values is None:
  91. values = OrderedDict()
  92. self._values = values
  93. def __repr__(self):
  94. return "%s(%r)" % (self.__class__.__name__, self._values)
  95. def __eq__(self, other):
  96. return (
  97. isinstance(other, self.__class__) and
  98. other._values == self._values)
  99. def __getitem__(self, key):
  100. return self._values.__getitem__(key)
  101. def __setitem__(self, key, value):
  102. return self._values.__setitem__(key, value)
  103. def __delitem__(self, key):
  104. return self._values.__delitem__(key)
  105. def __iter__(self):
  106. return self._values.__iter__()
  107. def __len__(self):
  108. return self._values.__len__()
  109. @classmethod
  110. def _parse_setting(cls, name):
  111. parts = name.split(".")
  112. if len(parts) == 3:
  113. return (parts[0], parts[1], parts[2])
  114. else:
  115. return (parts[0], None, parts[1])
  116. def get(self, section, name):
  117. if not isinstance(section, tuple):
  118. section = (section, )
  119. if not all([isinstance(subsection, bytes) for subsection in section]):
  120. raise TypeError(section)
  121. if not isinstance(name, bytes):
  122. raise TypeError(name)
  123. if len(section) > 1:
  124. try:
  125. return self._values[section][name]
  126. except KeyError:
  127. pass
  128. return self._values[(section[0],)][name]
  129. def set(self, section, name, value):
  130. if not isinstance(section, tuple):
  131. section = (section, )
  132. if not isinstance(name, bytes):
  133. raise TypeError(name)
  134. if type(value) not in (bool, bytes):
  135. raise TypeError(value)
  136. self._values.setdefault(section, OrderedDict())[name] = value
  137. def iteritems(self, section):
  138. return self._values.get(section, OrderedDict()).items()
  139. def itersections(self):
  140. return self._values.keys()
  141. def _format_string(value):
  142. if (value.startswith(b" ") or
  143. value.startswith(b"\t") or
  144. value.endswith(b" ") or
  145. b'#' in value or
  146. value.endswith(b"\t")):
  147. return b'"' + _escape_value(value) + b'"'
  148. else:
  149. return _escape_value(value)
  150. _ESCAPE_TABLE = {
  151. ord(b"\\"): ord(b"\\"),
  152. ord(b"\""): ord(b"\""),
  153. ord(b"n"): ord(b"\n"),
  154. ord(b"t"): ord(b"\t"),
  155. ord(b"b"): ord(b"\b"),
  156. }
  157. _COMMENT_CHARS = [ord(b"#"), ord(b";")]
  158. _WHITESPACE_CHARS = [ord(b"\t"), ord(b" ")]
  159. def _parse_string(value):
  160. value = bytearray(value.strip())
  161. ret = bytearray()
  162. whitespace = bytearray()
  163. in_quotes = False
  164. i = 0
  165. while i < len(value):
  166. c = value[i]
  167. if c == ord(b"\\"):
  168. i += 1
  169. try:
  170. v = _ESCAPE_TABLE[value[i]]
  171. except IndexError:
  172. raise ValueError(
  173. "escape character in %r at %d before end of string" %
  174. (value, i))
  175. except KeyError:
  176. raise ValueError(
  177. "escape character followed by unknown character "
  178. "%s at %d in %r" % (value[i], i, value))
  179. if whitespace:
  180. ret.extend(whitespace)
  181. whitespace = bytearray()
  182. ret.append(v)
  183. elif c == ord(b"\""):
  184. in_quotes = (not in_quotes)
  185. elif c in _COMMENT_CHARS and not in_quotes:
  186. # the rest of the line is a comment
  187. break
  188. elif c in _WHITESPACE_CHARS:
  189. whitespace.append(c)
  190. else:
  191. if whitespace:
  192. ret.extend(whitespace)
  193. whitespace = bytearray()
  194. ret.append(c)
  195. i += 1
  196. if in_quotes:
  197. raise ValueError("missing end quote")
  198. return bytes(ret)
  199. def _escape_value(value):
  200. """Escape a value."""
  201. value = value.replace(b"\\", b"\\\\")
  202. value = value.replace(b"\n", b"\\n")
  203. value = value.replace(b"\t", b"\\t")
  204. value = value.replace(b"\"", b"\\\"")
  205. return value
  206. def _check_variable_name(name):
  207. for i in range(len(name)):
  208. c = name[i:i+1]
  209. if not c.isalnum() and c != b'-':
  210. return False
  211. return True
  212. def _check_section_name(name):
  213. for i in range(len(name)):
  214. c = name[i:i+1]
  215. if not c.isalnum() and c not in (b'-', b'.'):
  216. return False
  217. return True
  218. def _strip_comments(line):
  219. comment_bytes = {ord(b"#"), ord(b";")}
  220. quote = ord(b'"')
  221. string_open = False
  222. # Normalize line to bytearray for simple 2/3 compatibility
  223. for i, character in enumerate(bytearray(line)):
  224. # Comment characters outside balanced quotes denote comment start
  225. if character == quote:
  226. string_open = not string_open
  227. elif not string_open and character in comment_bytes:
  228. return line[:i]
  229. return line
  230. class ConfigFile(ConfigDict):
  231. """A Git configuration file, like .git/config or ~/.gitconfig.
  232. """
  233. @classmethod
  234. def from_file(cls, f):
  235. """Read configuration from a file-like object."""
  236. ret = cls()
  237. section = None
  238. setting = None
  239. for lineno, line in enumerate(f.readlines()):
  240. line = line.lstrip()
  241. if setting is None:
  242. # Parse section header ("[bla]")
  243. if len(line) > 0 and line[:1] == b"[":
  244. line = _strip_comments(line).rstrip()
  245. try:
  246. last = line.index(b"]")
  247. except ValueError:
  248. raise ValueError("expected trailing ]")
  249. pts = line[1:last].split(b" ", 1)
  250. line = line[last+1:]
  251. pts[0] = pts[0].lower()
  252. if len(pts) == 2:
  253. if pts[1][:1] != b"\"" or pts[1][-1:] != b"\"":
  254. raise ValueError(
  255. "Invalid subsection %r" % pts[1])
  256. else:
  257. pts[1] = pts[1][1:-1]
  258. if not _check_section_name(pts[0]):
  259. raise ValueError("invalid section name %r" %
  260. pts[0])
  261. section = (pts[0], pts[1])
  262. else:
  263. if not _check_section_name(pts[0]):
  264. raise ValueError(
  265. "invalid section name %r" % pts[0])
  266. pts = pts[0].split(b".", 1)
  267. if len(pts) == 2:
  268. section = (pts[0], pts[1])
  269. else:
  270. section = (pts[0], )
  271. ret._values[section] = OrderedDict()
  272. if _strip_comments(line).strip() == b"":
  273. continue
  274. if section is None:
  275. raise ValueError("setting %r without section" % line)
  276. try:
  277. setting, value = line.split(b"=", 1)
  278. except ValueError:
  279. setting = line
  280. value = b"true"
  281. setting = setting.strip().lower()
  282. if not _check_variable_name(setting):
  283. raise ValueError("invalid variable name %s" % setting)
  284. if value.endswith(b"\\\n"):
  285. continuation = value[:-2]
  286. else:
  287. continuation = None
  288. value = _parse_string(value)
  289. ret._values[section][setting] = value
  290. setting = None
  291. else: # continuation line
  292. if line.endswith(b"\\\n"):
  293. continuation += line[:-2]
  294. else:
  295. continuation += line
  296. value = _parse_string(continuation)
  297. ret._values[section][setting] = value
  298. continuation = None
  299. setting = None
  300. return ret
  301. @classmethod
  302. def from_path(cls, path):
  303. """Read configuration from a file on disk."""
  304. with GitFile(path, 'rb') as f:
  305. ret = cls.from_file(f)
  306. ret.path = path
  307. return ret
  308. def write_to_path(self, path=None):
  309. """Write configuration to a file on disk."""
  310. if path is None:
  311. path = self.path
  312. with GitFile(path, 'wb') as f:
  313. self.write_to_file(f)
  314. def write_to_file(self, f):
  315. """Write configuration to a file-like object."""
  316. for section, values in self._values.items():
  317. try:
  318. section_name, subsection_name = section
  319. except ValueError:
  320. (section_name, ) = section
  321. subsection_name = None
  322. if subsection_name is None:
  323. f.write(b"[" + section_name + b"]\n")
  324. else:
  325. f.write(b"[" + section_name +
  326. b" \"" + subsection_name + b"\"]\n")
  327. for key, value in values.items():
  328. if value is True:
  329. value = b"true"
  330. elif value is False:
  331. value = b"false"
  332. else:
  333. value = _format_string(value)
  334. f.write(b"\t" + key + b" = " + value + b"\n")
  335. class StackedConfig(Config):
  336. """Configuration which reads from multiple config files.."""
  337. def __init__(self, backends, writable=None):
  338. self.backends = backends
  339. self.writable = writable
  340. def __repr__(self):
  341. return "<%s for %r>" % (self.__class__.__name__, self.backends)
  342. @classmethod
  343. def default(cls):
  344. return cls(cls.default_backends())
  345. @classmethod
  346. def default_backends(cls):
  347. """Retrieve the default configuration.
  348. See git-config(1) for details on the files searched.
  349. """
  350. paths = []
  351. paths.append(os.path.expanduser("~/.gitconfig"))
  352. xdg_config_home = os.environ.get(
  353. "XDG_CONFIG_HOME", os.path.expanduser("~/.config/"),
  354. )
  355. paths.append(os.path.join(xdg_config_home, "git", "config"))
  356. if "GIT_CONFIG_NOSYSTEM" not in os.environ:
  357. paths.append("/etc/gitconfig")
  358. backends = []
  359. for path in paths:
  360. try:
  361. cf = ConfigFile.from_path(path)
  362. except (IOError, OSError) as e:
  363. if e.errno != errno.ENOENT:
  364. raise
  365. else:
  366. continue
  367. backends.append(cf)
  368. return backends
  369. def get(self, section, name):
  370. if not isinstance(section, tuple):
  371. section = (section, )
  372. if not all([isinstance(subsection, bytes) for subsection in section]):
  373. raise TypeError(section)
  374. if not isinstance(name, bytes):
  375. raise TypeError(name)
  376. for backend in self.backends:
  377. try:
  378. return backend.get(section, name)
  379. except KeyError:
  380. pass
  381. raise KeyError(name)
  382. def set(self, section, name, value):
  383. if self.writable is None:
  384. raise NotImplementedError(self.set)
  385. return self.writable.set(section, name, value)
  386. def parse_submodules(config):
  387. """Parse a gitmodules GitConfig file, returning submodules.
  388. :param config: A `ConfigFile`
  389. :return: list of tuples (submodule path, url, name),
  390. where name is quoted part of the section's name.
  391. """
  392. for section in config.keys():
  393. section_kind, section_name = section
  394. if section_kind == b'submodule':
  395. sm_path = config.get(section, b'path')
  396. sm_url = config.get(section, b'url')
  397. yield (sm_path, sm_url, section_name)