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.

migrate.py 1.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2011-2018 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # Released under the terms of the MIT License. See LICENSE for details.
  5. import os
  6. try:
  7. from configparser import ConfigParser, NoSectionError
  8. PY3K = True
  9. except ImportError: # Python 2
  10. from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError
  11. PY3K = False
  12. __all__ = ["run_migrations"]
  13. def _get_old_path():
  14. """Return the old default path to the configuration file."""
  15. xdg_cfg = os.environ.get("XDG_CONFIG_HOME") or os.path.join("~", ".config")
  16. return os.path.join(os.path.expanduser(xdg_cfg), "gitup", "config.ini")
  17. def _migrate_old_path():
  18. """Migrate the old config location (~/.gitup) to the new one."""
  19. old_path = os.path.expanduser(os.path.join("~", ".gitup"))
  20. if not os.path.exists(old_path):
  21. return
  22. temp_path = _get_old_path()
  23. temp_dir = os.path.dirname(temp_path)
  24. if not os.path.exists(temp_dir):
  25. os.makedirs(temp_dir)
  26. os.rename(old_path, temp_path)
  27. def _migrate_old_format():
  28. """Migrate the old config file format (.INI) to our custom list format."""
  29. old_path = _get_old_path()
  30. if not os.path.exists(old_path):
  31. return
  32. config = ConfigParser(delimiters="=") if PY3K else ConfigParser()
  33. config.optionxform = lambda opt: opt
  34. config.read(old_path)
  35. try:
  36. bookmarks = [path for path, _ in config.items("bookmarks")]
  37. except NoSectionError:
  38. bookmarks = []
  39. if PY3K:
  40. bookmarks = [path.encode("utf8") for path in bookmarks]
  41. new_path = os.path.join(os.path.split(old_path)[0], "bookmarks")
  42. os.rename(old_path, new_path)
  43. with open(new_path, "wb") as handle:
  44. handle.write(b"\n".join(bookmarks))
  45. def run_migrations():
  46. """Run any necessary migrations to ensure the config file is up-to-date."""
  47. _migrate_old_path()
  48. _migrate_old_format()