A console script that allows you to easily update multiple git repositories at once
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

114 行
3.5 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"]
  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 config parser object."""
  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. return config_file.read().split("\n")
  26. except IOError:
  27. return []
  28. def _save_config_file(config, config_path=None):
  29. """Save config changes to the given config file."""
  30. run_migrations()
  31. cfg_path = config_path or get_default_config_path()
  32. _ensure_dirs(cfg_path)
  33. with open(cfg_path, "wb") as config_file:
  34. config_file.write("\n".join(config))
  35. def get_default_config_path():
  36. """Return the default path to the configuration file."""
  37. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  38. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "bookmarks")
  39. def get_bookmarks(config_path=None):
  40. """Get a list of all bookmarks, or an empty list if there are none."""
  41. return _load_config_file(config_path)
  42. def add_bookmarks(paths, config_path=None):
  43. """Add a list of paths as bookmarks to the config file."""
  44. config = _load_config_file(config_path)
  45. added, exists = [], []
  46. for path in paths:
  47. path = os.path.abspath(path)
  48. if path in config:
  49. exists.append(path)
  50. else:
  51. config.append(path)
  52. added.append(path)
  53. _save_config_file(config, config_path)
  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, config_path=None):
  63. """Remove a list of paths from the bookmark config file."""
  64. config = _load_config_file(config_path)
  65. deleted, notmarked = [], []
  66. if config:
  67. for path in paths:
  68. path = os.path.abspath(path)
  69. if path in config:
  70. config.remove(path)
  71. deleted.append(path)
  72. else:
  73. notmarked.append(path)
  74. _save_config_file(config, config_path)
  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(config_path=None):
  86. """Print all of our current bookmarks."""
  87. bookmarks = get_bookmarks(config_path=config_path)
  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.")