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.

112 rivejä
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # See the LICENSE file for details.
  5. from __future__ import print_function
  6. import ConfigParser as configparser
  7. import os
  8. from colorama import Fore, Style
  9. __all__ = ["get_bookmarks", "add_bookmarks", "delete_bookmarks",
  10. "list_bookmarks"]
  11. YELLOW = Fore.YELLOW + Style.BRIGHT
  12. RED = Fore.RED + Style.BRIGHT
  13. INDENT1 = " " * 3
  14. def _get_config_path():
  15. """Return the path to the configuration file."""
  16. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  17. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini")
  18. def _load_config_file():
  19. """Read the config file and return a SafeConfigParser() object."""
  20. config = configparser.SafeConfigParser()
  21. # Don't lowercase option names, because we are storing paths there:
  22. config.optionxform = str
  23. config.read(_get_config_path())
  24. return config
  25. def _save_config_file(config):
  26. """Save config changes to the config file returned by _get_config_path."""
  27. cfg_path = _get_config_path()
  28. if not os.path.exists(os.path.dirname(cfg_path)): # Race condition, meh...
  29. os.makedirs(os.path.dirname(cfg_path))
  30. with open(cfg_path, "wb") as config_file:
  31. config.write(config_file)
  32. def get_bookmarks():
  33. """Get a list of all bookmarks, or an empty list if there are none."""
  34. config = _load_config_file()
  35. try:
  36. return config.items("bookmarks")
  37. except configparser.NoSectionError:
  38. return []
  39. def add_bookmarks(paths):
  40. """Add a list of paths as bookmarks to the config file."""
  41. config = _load_config_file()
  42. if not config.has_section("bookmarks"):
  43. config.add_section("bookmarks")
  44. added, exists = [], []
  45. for path in paths:
  46. path = os.path.abspath(path)
  47. if config.has_option("bookmarks", path):
  48. exists.append(path)
  49. else:
  50. path_name = os.path.split(path)[1]
  51. config.set("bookmarks", path, path_name)
  52. added.append(path)
  53. _save_config_file(config)
  54. if added:
  55. print(YELLOW + "Added bookmarks:")
  56. for path in added:
  57. print(INDENT1, path)
  58. if exists:
  59. print(RED + "Already bookmarked:")
  60. for path in exists:
  61. print(INDENT1, path)
  62. def delete_bookmarks(paths):
  63. """Remove a list of paths from the bookmark config file."""
  64. config = _load_config_file()
  65. deleted, notmarked = [], []
  66. if config.has_section("bookmarks"):
  67. for path in paths:
  68. path = os.path.abspath(path)
  69. config_was_changed = config.remove_option("bookmarks", path)
  70. if config_was_changed:
  71. deleted.append(path)
  72. else:
  73. notmarked.append(path)
  74. _save_config_file(config)
  75. else:
  76. notmarked = [os.path.abspath(path) for path in paths]
  77. if deleted:
  78. print(YELLOW + "Deleted bookmarks:")
  79. for path in deleted:
  80. print(INDENT1, path)
  81. if notmarked:
  82. print(RED + "Not bookmarked:")
  83. for path in notmarked:
  84. print(INDENT1, path)
  85. def list_bookmarks():
  86. """Print all of our current bookmarks."""
  87. bookmarks = get_bookmarks()
  88. if bookmarks:
  89. print(YELLOW + "Current bookmarks:")
  90. for bookmark_path, _ in bookmarks:
  91. print(INDENT1, bookmark_path)
  92. else:
  93. print("You have no bookmarks to display.")