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.

130 rader
4.0 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_bookmarks", "add_bookmarks", "delete_bookmarks",
  13. "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 not os.path.exists(dirname): # Race condition, meh...
  21. os.makedirs(dirname)
  22. def _get_config_path():
  23. """Return the path to the configuration file."""
  24. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  25. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini")
  26. def _migrate_old_config_path():
  27. """Migrate the old config location (~/.gitup) to the new one."""
  28. old_path = os.path.expanduser(os.path.join("~", ".gitup"))
  29. if os.path.exists(old_path):
  30. new_path = _get_config_path()
  31. _ensure_dirs(new_path)
  32. os.rename(old_path, new_path)
  33. def _load_config_file():
  34. """Read the config file and return a SafeConfigParser() object."""
  35. _migrate_old_config_path()
  36. config = configparser.SafeConfigParser()
  37. # Don't lowercase option names, because we are storing paths there:
  38. config.optionxform = lambda opt: opt
  39. config.read(_get_config_path())
  40. return config
  41. def _save_config_file(config):
  42. """Save config changes to the config file returned by _get_config_path."""
  43. _migrate_old_config_path()
  44. cfg_path = _get_config_path()
  45. _ensure_dirs(cfg_path)
  46. with open(cfg_path, "w") as config_file:
  47. config.write(config_file)
  48. def get_bookmarks():
  49. """Get a list of all bookmarks, or an empty list if there are none."""
  50. config = _load_config_file()
  51. try:
  52. return [path for path, _ in config.items("bookmarks")]
  53. except configparser.NoSectionError:
  54. return []
  55. def add_bookmarks(paths):
  56. """Add a list of paths as bookmarks to the config file."""
  57. config = _load_config_file()
  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)
  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):
  79. """Remove a list of paths from the bookmark config file."""
  80. config = _load_config_file()
  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)
  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():
  102. """Print all of our current bookmarks."""
  103. bookmarks = get_bookmarks()
  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.")