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.

83 regels
2.6 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. import ConfigParser as configparser
  6. import os
  7. from .output import out, bold, yellow
  8. __all__ = ["get_bookmarks", "add_bookmarks", "delete_bookmarks",
  9. "list_bookmarks"]
  10. _config_filename = os.path.join(os.path.expanduser("~"), ".gitup")
  11. def _load_config_file():
  12. """Read the config file and return a SafeConfigParser() object."""
  13. config = configparser.SafeConfigParser()
  14. # Don't lowercase option names, because we are storing paths there:
  15. config.optionxform = str
  16. config.read(_config_filename)
  17. return config
  18. def _save_config_file(config):
  19. """Save config changes to the config file specified by _config_filename."""
  20. with open(_config_filename, "wb") as config_file:
  21. config.write(config_file)
  22. def get_bookmarks():
  23. """Get a list of all bookmarks, or an empty list if there are none."""
  24. config = _load_config_file()
  25. try:
  26. return config.items("bookmarks")
  27. except configparser.NoSectionError:
  28. return []
  29. def add_bookmarks(paths):
  30. """Add a list of paths as bookmarks to the config file."""
  31. config = _load_config_file()
  32. if not config.has_section("bookmarks"):
  33. config.add_section("bookmarks")
  34. out(0, yellow("Added bookmarks:"))
  35. for path in paths:
  36. path = os.path.abspath(path) # Convert relative to absolute path
  37. if config.has_option("bookmarks", path):
  38. out(1, "'{0}' is already bookmarked.".format(path))
  39. else:
  40. path_name = os.path.split(path)[1]
  41. config.set("bookmarks", path, path_name)
  42. out(1, bold(path))
  43. _save_config_file(config)
  44. def delete_bookmarks(paths):
  45. """Remove a list of paths from the bookmark config file."""
  46. config = _load_config_file()
  47. if config.has_section("bookmarks"):
  48. out(0, yellow("Deleted bookmarks:"))
  49. for path in paths:
  50. path = os.path.abspath(path) # Convert relative to absolute path
  51. config_was_changed = config.remove_option("bookmarks", path)
  52. if config_was_changed:
  53. out(1, bold(path))
  54. else:
  55. out(1, "'{0}' is not bookmarked.".format(path))
  56. _save_config_file(config)
  57. else:
  58. out(0, "There are no bookmarks to delete!")
  59. def list_bookmarks():
  60. """Print all of our current bookmarks."""
  61. bookmarks = get_bookmarks()
  62. if bookmarks:
  63. out(0, yellow("Current bookmarks:"))
  64. for bookmark_path, bookmark_name in bookmarks:
  65. out(1, bookmark_path)
  66. else:
  67. out(0, "You have no bookmarks to display.")