extension_language_map.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ]
  50. EXTENSION_LANGUAGE_MAP: dict[str, str] = {}
  51. yaml_languages = languages_dict.keys()
  52. def add_lang_to_map(language, map):
  53. v = languages_dict[language]
  54. if v["type"] != "programming":
  55. return map
  56. if "extensions" not in v:
  57. return map
  58. extensions = v["extensions"]
  59. for ext in extensions:
  60. if ext[1:].lower() in extensions:
  61. raise Exception
  62. map[ext[1:].lower()] = language.lower()
  63. return map
  64. for yaml_lang in yaml_languages:
  65. lowercase_yaml_lang = yaml_lang.lower()
  66. if lowercase_yaml_lang in languages:
  67. EXTENSION_LANGUAGE_MAP = add_lang_to_map(yaml_lang, EXTENSION_LANGUAGE_MAP)
  68. elif "group" in languages_dict[yaml_lang]:
  69. if languages_dict[yaml_lang]["group"].lower() in languages:
  70. EXTENSION_LANGUAGE_MAP = add_lang_to_map(yaml_lang, EXTENSION_LANGUAGE_MAP)
  71. else:
  72. # check if substring exists
  73. for lang in languages:
  74. if lang in lowercase_yaml_lang and len(lang) > 2:
  75. EXTENSION_LANGUAGE_MAP = add_lang_to_map(yaml_lang, EXTENSION_LANGUAGE_MAP)