A console script that allows you to easily update multiple git repositories at once
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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