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.

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