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. from typing import Dict
  5. import requests
  6. import yaml
  7. response = requests.get(
  8. "https://raw.githubusercontent.com/github-linguist/linguist/master/lib/linguist/languages.yml"
  9. )
  10. languages_dict = yaml.safe_load(response.content)
  11. # referenced from platforms.tsx and _platforms.json
  12. # node.js contains JavaScript, Python, C, C++, CoffeeScript
  13. # .NET contains C#, F#, visual basic
  14. # apple: swift, objective-c
  15. # android: java, kotlin, dart
  16. # flutter: dart
  17. # pulled from the sentry_projectplatform table: groovy, haskell
  18. languages = [
  19. "go",
  20. "javascript",
  21. "typescript",
  22. "java",
  23. "python",
  24. "php",
  25. "c",
  26. "c++",
  27. "coffeescript",
  28. "c#",
  29. "f#",
  30. "visual basic",
  31. "swift",
  32. "objective-c",
  33. "objective-c++",
  34. "ruby",
  35. "kotlin",
  36. "dart",
  37. "elixir",
  38. "rust",
  39. "scala",
  40. "perl",
  41. "groovy",
  42. "haskell",
  43. # community supported sdks
  44. "clojure",
  45. "coldfusion",
  46. "crystal",
  47. "lua",
  48. "ocaml",
  49. "hcl",
  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)