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.

135 line
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2016 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. from colorama import Fore, Style
  8. from .migrate import run_migrations
  9. __all__ = ["get_default_config_path", "get_bookmarks", "add_bookmarks",
  10. "delete_bookmarks", "list_bookmarks", "clean_bookmarks"]
  11. YELLOW = Fore.YELLOW + Style.BRIGHT
  12. RED = Fore.RED + Style.BRIGHT
  13. INDENT1 = " " * 3
  14. def _ensure_dirs(path):
  15. """Ensure the directories within the given pathname exist."""
  16. dirname = os.path.dirname(path)
  17. if dirname and not os.path.exists(dirname): # Race condition, meh...
  18. os.makedirs(dirname)
  19. def _load_config_file(config_path=None):
  20. """Read the config file and return a list of bookmarks."""
  21. run_migrations()
  22. cfg_path = config_path or get_default_config_path()
  23. try:
  24. with open(cfg_path, "rb") as config_file:
  25. paths = config_file.read().strip().split(b"\n")
  26. return [path.decode("utf8") for path in paths]
  27. except IOError:
  28. return []
  29. def _save_config_file(bookmarks, config_path=None):
  30. """Save the bookmarks list to the given config file."""
  31. run_migrations()
  32. cfg_path = config_path or get_default_config_path()
  33. _ensure_dirs(cfg_path)
  34. dump = b"\n".join(path.encode("utf8") for path in bookmarks)
  35. with open(cfg_path, "wb") as config_file:
  36. config_file.write(dump)
  37. def get_default_config_path():
  38. """Return the default path to the configuration file."""
  39. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  40. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "bookmarks")
  41. def get_bookmarks(config_path=None):
  42. """Get a list of all bookmarks, or an empty list if there are none."""
  43. return _load_config_file(config_path)
  44. def add_bookmarks(paths, config_path=None):
  45. """Add a list of paths as bookmarks to the config file."""
  46. config = _load_config_file(config_path)
  47. added, exists = [], []
  48. for path in paths:
  49. path = os.path.abspath(path)
  50. if path in config:
  51. exists.append(path)
  52. else:
  53. config.append(path)
  54. added.append(path)
  55. _save_config_file(config, config_path)
  56. if added:
  57. print(YELLOW + "Added bookmarks:")
  58. for path in added:
  59. print(INDENT1, path)
  60. if exists:
  61. print(RED + "Already bookmarked:")
  62. for path in exists:
  63. print(INDENT1, path)
  64. def delete_bookmarks(paths, config_path=None):
  65. """Remove a list of paths from the bookmark config file."""
  66. config = _load_config_file(config_path)
  67. deleted, notmarked = [], []
  68. if config:
  69. for path in paths:
  70. path = os.path.abspath(path)
  71. if path in config:
  72. config.remove(path)
  73. deleted.append(path)
  74. else:
  75. notmarked.append(path)
  76. _save_config_file(config, config_path)
  77. else:
  78. notmarked = [os.path.abspath(path) for path in paths]
  79. if deleted:
  80. print(YELLOW + "Deleted bookmarks:")
  81. for path in deleted:
  82. print(INDENT1, path)
  83. if notmarked:
  84. print(RED + "Not bookmarked:")
  85. for path in notmarked:
  86. print(INDENT1, path)
  87. def list_bookmarks(config_path=None):
  88. """Print all of our current bookmarks."""
  89. bookmarks = _load_config_file(config_path)
  90. if bookmarks:
  91. print(YELLOW + "Current bookmarks:")
  92. for bookmark_path in bookmarks:
  93. print(INDENT1, bookmark_path)
  94. else:
  95. print("You have no bookmarks to display.")
  96. def clean_bookmarks(config_path=None):
  97. """Delete any bookmarks that don't exist."""
  98. bookmarks = _load_config_file(config_path)
  99. if not bookmarks:
  100. print("You have no bookmarks to clean up.")
  101. return
  102. delete = [path for path in bookmarks if not os.path.isdir(path)]
  103. if not delete:
  104. print("All of your bookmarks are valid.")
  105. return
  106. bookmarks = [path for path in bookmarks if path not in delete]
  107. _save_config_file(bookmarks, config_path)
  108. print(YELLOW + "Deleted bookmarks:")
  109. for path in delete:
  110. print(INDENT1, path)