extension_language_map.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Generate a mapping from file extensions to language for languages that are part of platforms supported by Sentry.
  3. """
  4. import requests
  5. import yaml
  6. response = requests.get(
  7. "https://raw.githubusercontent.com/github-linguist/linguist/master/lib/linguist/languages.yml"
  8. )
  9. languages_dict = yaml.safe_load(response.content)
  10. # referenced from platforms.tsx and _platforms.json
  11. # node.js contains JavaScript, Python, C, C++, CoffeeScript
  12. # .NET contains C#, F#, visual basic
  13. # apple: swift, objective-c
  14. # android: java, kotlin, dart
  15. # flutter: dart
  16. # pulled from the sentry_projectplatform table: groovy, haskell
  17. languages = [
  18. "go",
  19. "javascript",
  20. "typescript",
  21. "java",
  22. "python",
  23. "php",
  24. "c",
  25. "c++",
  26. "coffeescript",
  27. "c#",
  28. "f#",
  29. "visual basic",
  30. "swift",
  31. "objective-c",
  32. "objective-c++",
  33. "ruby",
  34. "kotlin",
  35. "dart",
  36. "elixir",
  37. "rust",
  38. "scala",
  39. "perl",
  40. "groovy",
  41. "haskell",
  42. # community supported sdks
  43. "clojure",
  44. "coldfusion",
  45. "crystal",
  46. "lua",
  47. "ocaml",
  48. "hcl",
  49. "powershell",
  50. ]
  51. EXTENSION_LANGUAGE_MAP: dict[str, str] = {}
  52. yaml_languages = languages_dict.keys()
  53. def add_lang_to_map(language, map):
  54. v = languages_dict[language]
  55. if v["type"] != "programming":
  56. return map
  57. if "extensions" not in v:
  58. return map
  59. extensions = v["extensions"]
  60. for ext in extensions:
  61. if ext[1:].lower() in extensions:
  62. raise Exception
  63. map[ext[1:].lower()] = language.lower()
  64. return map
  65. for yaml_lang in yaml_languages:
  66. lowercase_yaml_lang = yaml_lang.lower()
  67. if lowercase_yaml_lang in languages:
  68. EXTENSION_LANGUAGE_MAP = add_lang_to_map(yaml_lang, EXTENSION_LANGUAGE_MAP)
  69. elif "group" in languages_dict[yaml_lang]:
  70. if languages_dict[yaml_lang]["group"].lower() in languages:
  71. EXTENSION_LANGUAGE_MAP = add_lang_to_map(yaml_lang, EXTENSION_LANGUAGE_MAP)
  72. else:
  73. # check if substring exists
  74. for lang in languages:
  75. if lang in lowercase_yaml_lang and len(lang) > 2:
  76. EXTENSION_LANGUAGE_MAP = add_lang_to_map(yaml_lang, EXTENSION_LANGUAGE_MAP)