A console script that allows you to easily update multiple git repositories at once
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

migrate.py 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. __all__ = ["run_migrations"]
  6. import os
  7. try:
  8. from configparser import ConfigParser, NoSectionError
  9. PY3K = True
  10. except ImportError: # Python 2
  11. from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError
  12. PY3K = False
  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. new_path = os.path.join(os.path.split(old_path)[0], "bookmarks")
  40. os.rename(old_path, new_path)
  41. with open(new_path, "wb") as handle:
  42. handle.write("\n".join(bookmarks))
  43. def run_migrations():
  44. """Run any necessary migrations to ensure the config file is up-to-date."""
  45. _migrate_old_path()
  46. _migrate_old_format()