A console script that allows you to easily update multiple git repositories at once
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

135 rader
4.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # Released under the terms of the MIT License. See LICENSE for details.
  5. from __future__ import print_function
  6. import os
  7. try:
  8. from configparser import ConfigParser, NoSectionError
  9. py3k = True
  10. except ImportError: # Python 2
  11. from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError
  12. py3k = False
  13. from colorama import Fore, Style
  14. __all__ = ["get_default_config_path", "get_bookmarks", "add_bookmarks",
  15. "delete_bookmarks", "list_bookmarks"]
  16. YELLOW = Fore.YELLOW + Style.BRIGHT
  17. RED = Fore.RED + Style.BRIGHT
  18. INDENT1 = " " * 3
  19. def _ensure_dirs(path):
  20. """Ensure the directories within the given pathname exist."""
  21. dirname = os.path.dirname(path)
  22. if dirname and not os.path.exists(dirname): # Race condition, meh...
  23. os.makedirs(dirname)
  24. def _migrate_old_config_path():
  25. """Migrate the old config location (~/.gitup) to the new one."""
  26. old_path = os.path.expanduser(os.path.join("~", ".gitup"))
  27. if os.path.exists(old_path):
  28. new_path = get_default_config_path()
  29. _ensure_dirs(new_path)
  30. os.rename(old_path, new_path)
  31. def _load_config_file(config_path=None):
  32. """Read the config file and return a config parser object."""
  33. _migrate_old_config_path()
  34. if py3k:
  35. config = ConfigParser(delimiters='=')
  36. else:
  37. config = ConfigParser()
  38. # Don't lowercase option names, because we are storing paths there:
  39. config.optionxform = lambda opt: opt
  40. config.read(config_path or get_default_config_path())
  41. return config
  42. def _save_config_file(config, config_path=None):
  43. """Save config changes to the given config file."""
  44. _migrate_old_config_path()
  45. cfg_path = config_path or get_default_config_path()
  46. _ensure_dirs(cfg_path)
  47. with open(cfg_path, "w") as config_file:
  48. config.write(config_file)
  49. def get_default_config_path():
  50. """Return the default path to the configuration file."""
  51. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  52. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini")
  53. def get_bookmarks(config_path=None):
  54. """Get a list of all bookmarks, or an empty list if there are none."""
  55. config = _load_config_file(config_path)
  56. try:
  57. return [path for path, _ in config.items("bookmarks")]
  58. except NoSectionError:
  59. return []
  60. def add_bookmarks(paths, config_path=None):
  61. """Add a list of paths as bookmarks to the config file."""
  62. config = _load_config_file(config_path)
  63. if not config.has_section("bookmarks"):
  64. config.add_section("bookmarks")
  65. added, exists = [], []
  66. for path in paths:
  67. path = os.path.abspath(path)
  68. if config.has_option("bookmarks", path):
  69. exists.append(path)
  70. else:
  71. path_name = os.path.split(path)[1]
  72. config.set("bookmarks", path, path_name)
  73. added.append(path)
  74. _save_config_file(config, config_path)
  75. if added:
  76. print(YELLOW + "Added bookmarks:")
  77. for path in added:
  78. print(INDENT1, path)
  79. if exists:
  80. print(RED + "Already bookmarked:")
  81. for path in exists:
  82. print(INDENT1, path)
  83. def delete_bookmarks(paths, config_path=None):
  84. """Remove a list of paths from the bookmark config file."""
  85. config = _load_config_file(config_path)
  86. deleted, notmarked = [], []
  87. if config.has_section("bookmarks"):
  88. for path in paths:
  89. path = os.path.abspath(path)
  90. config_was_changed = config.remove_option("bookmarks", path)
  91. if config_was_changed:
  92. deleted.append(path)
  93. else:
  94. notmarked.append(path)
  95. _save_config_file(config, config_path)
  96. else:
  97. notmarked = [os.path.abspath(path) for path in paths]
  98. if deleted:
  99. print(YELLOW + "Deleted bookmarks:")
  100. for path in deleted:
  101. print(INDENT1, path)
  102. if notmarked:
  103. print(RED + "Not bookmarked:")
  104. for path in notmarked:
  105. print(INDENT1, path)
  106. def list_bookmarks(config_path=None):
  107. """Print all of our current bookmarks."""
  108. bookmarks = get_bookmarks(config_path=config_path)
  109. if bookmarks:
  110. print(YELLOW + "Current bookmarks:")
  111. for bookmark_path in bookmarks:
  112. print(INDENT1, bookmark_path)
  113. else:
  114. print("You have no bookmarks to display.")