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.

106 lines
3.2 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. CONFIG_FILENAME = os.path.join(os.path.expanduser('~/.config'), '.gitup') if \
  12. os.path.exists(os.path.expanduser('~/.config')) else os.path.join(os.path.expanduser('~'), '.gitup')
  13. YELLOW = Fore.YELLOW + Style.BRIGHT
  14. RED = Fore.RED + Style.BRIGHT
  15. INDENT1 = " " * 3
  16. def _load_config_file():
  17. """Read the config file and return a SafeConfigParser() object."""
  18. config = configparser.SafeConfigParser()
  19. # Don't lowercase option names, because we are storing paths there:
  20. config.optionxform = str
  21. config.read(CONFIG_FILENAME)
  22. return config
  23. def _save_config_file(config):
  24. """Save config changes to the config file specified by CONFIG_FILENAME."""
  25. with open(CONFIG_FILENAME, "wb") as config_file:
  26. config.write(config_file)
  27. def get_bookmarks():
  28. """Get a list of all bookmarks, or an empty list if there are none."""
  29. config = _load_config_file()
  30. try:
  31. return config.items("bookmarks")
  32. except configparser.NoSectionError:
  33. return []
  34. def add_bookmarks(paths):
  35. """Add a list of paths as bookmarks to the config file."""
  36. config = _load_config_file()
  37. if not config.has_section("bookmarks"):
  38. config.add_section("bookmarks")
  39. added, exists = [], []
  40. for path in paths:
  41. path = os.path.abspath(path)
  42. if config.has_option("bookmarks", path):
  43. exists.append(path)
  44. else:
  45. path_name = os.path.split(path)[1]
  46. config.set("bookmarks", path, path_name)
  47. added.append(path)
  48. _save_config_file(config)
  49. if added:
  50. print(YELLOW + "Added bookmarks:")
  51. for path in added:
  52. print(INDENT1, path)
  53. if exists:
  54. print(RED + "Already bookmarked:")
  55. for path in exists:
  56. print(INDENT1, path)
  57. def delete_bookmarks(paths):
  58. """Remove a list of paths from the bookmark config file."""
  59. config = _load_config_file()
  60. deleted, notmarked = [], []
  61. if config.has_section("bookmarks"):
  62. for path in paths:
  63. path = os.path.abspath(path)
  64. config_was_changed = config.remove_option("bookmarks", path)
  65. if config_was_changed:
  66. deleted.append(path)
  67. else:
  68. notmarked.append(path)
  69. _save_config_file(config)
  70. else:
  71. notmarked = [os.path.abspath(path) for path in paths]
  72. if deleted:
  73. print(YELLOW + "Deleted bookmarks:")
  74. for path in deleted:
  75. print(INDENT1, path)
  76. if notmarked:
  77. print(RED + "Not bookmarked:")
  78. for path in notmarked:
  79. print(INDENT1, path)
  80. def list_bookmarks():
  81. """Print all of our current bookmarks."""
  82. bookmarks = get_bookmarks()
  83. if bookmarks:
  84. print(YELLOW + "Current bookmarks:")
  85. for bookmark_path, _ in bookmarks:
  86. print(INDENT1, bookmark_path)
  87. else:
  88. print("You have no bookmarks to display.")