use_example_configs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. #
  3. # use_example_configs [repo:]configpath
  4. #
  5. # Examples:
  6. # use_example_configs
  7. # use_example_configs Creality/CR-10/CrealityV1
  8. # use_example_configs release-2.0.9.4:Creality/CR-10/CrealityV1
  9. #
  10. # If a configpath has spaces (or quotes) escape them or enquote the path
  11. # If no branch: prefix is given use configs based on the current branch name.
  12. # e.g., For `latest-2.1.x` name the working branch something like "my_work-2.1.x."
  13. # The branch or tag must first exist at MarlinFirmware/Configurations.
  14. # The fallback branch is bugfix-2.1.x.
  15. #
  16. import os, subprocess, sys, urllib.request
  17. def get_current_branch():
  18. try:
  19. result = subprocess.run(['git', 'branch'], capture_output=True, text=True, check=True)
  20. for line in result.stdout.splitlines():
  21. if line.startswith('*'):
  22. return line[2:]
  23. except subprocess.CalledProcessError:
  24. return None
  25. def fetch_configs(branch, config_path):
  26. print(f"Fetching {config_path} configurations from {branch}...")
  27. base_url = f"https://raw.githubusercontent.com/MarlinFirmware/Configurations/{branch}/config/{config_path}"
  28. files = ["Configuration.h", "Configuration_adv.h", "_Bootscreen.h", "_Statusscreen.h"]
  29. marlin_dir = os.path.join(os.getcwd(), "Marlin")
  30. if not os.path.exists(marlin_dir):
  31. print(f"Directory {marlin_dir} does not exist.")
  32. sys.exit(1)
  33. for file in files:
  34. url = f"{base_url}/{file}"
  35. dest_file = os.path.join(marlin_dir, file)
  36. if os.getenv('DEBUG', '0') == '1':
  37. print(f"Fetching {file} from {url} to {dest_file}")
  38. try:
  39. urllib.request.urlretrieve(url, dest_file)
  40. except urllib.error.HTTPError as e:
  41. if e.code == 404:
  42. if os.getenv('DEBUG', '0') == '1':
  43. print(f"File {file} not found (404), skipping.")
  44. else:
  45. raise
  46. def main():
  47. branch = get_current_branch()
  48. if not branch:
  49. print("Not a git repository or no branch found.")
  50. sys.exit(1)
  51. if branch.startswith("bugfix-2."):
  52. branch = branch
  53. elif branch.endswith("-2.1.x") or branch == "2.1.x":
  54. branch = "latest-2.1.x"
  55. elif branch.endswith("-2.0.x") or branch == "2.0.x":
  56. branch = "latest-2.0.x"
  57. elif branch.endswith("-1.1.x") or branch == "1.1.x":
  58. branch = "latest-1.1.x"
  59. elif branch.endswith("-1.0.x") or branch == "1.0.x":
  60. branch = "latest-1.0.x"
  61. else:
  62. branch = "bugfix-2.1.x"
  63. if len(sys.argv) > 1:
  64. arg = sys.argv[1]
  65. if ':' in arg:
  66. part1, part2 = arg.split(':', 1)
  67. config_path = part2
  68. branch = part1
  69. else:
  70. config_path = arg
  71. config_path = 'examples/'+config_path.replace(' ', '%20')
  72. else:
  73. config_path = "default"
  74. try:
  75. subprocess.run(['restore_configs'], check=True)
  76. except FileNotFoundError:
  77. print("restore_configs not found, skipping.")
  78. fetch_configs(branch, config_path)
  79. if __name__ == "__main__":
  80. main()