python.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package buildpacks
  2. import (
  3. "strings"
  4. "sync"
  5. "github.com/google/go-github/v41/github"
  6. "github.com/xanzy/go-gitlab"
  7. )
  8. type pythonRuntime struct {
  9. wg sync.WaitGroup
  10. }
  11. func NewPythonRuntime() Runtime {
  12. return &pythonRuntime{}
  13. }
  14. func (runtime *pythonRuntime) detectPipenvGithub(results chan struct {
  15. string
  16. bool
  17. }, directoryContent []*github.RepositoryContent) {
  18. pipfileFound := false
  19. pipfileLockFound := false
  20. for i := 0; i < len(directoryContent); i++ {
  21. name := directoryContent[i].GetName()
  22. if name == "Pipfile" {
  23. pipfileFound = true
  24. } else if name == "Pipfile.lock" {
  25. pipfileLockFound = true
  26. }
  27. if pipfileFound && pipfileLockFound {
  28. break
  29. }
  30. }
  31. if pipfileFound && pipfileLockFound {
  32. results <- struct {
  33. string
  34. bool
  35. }{pipenv, true}
  36. }
  37. runtime.wg.Done()
  38. }
  39. func (runtime *pythonRuntime) detectPipenvGitlab(results chan struct {
  40. string
  41. bool
  42. }, tree []*gitlab.TreeNode) {
  43. pipfileFound := false
  44. pipfileLockFound := false
  45. for i := 0; i < len(tree); i++ {
  46. name := tree[i].Name
  47. if name == "Pipfile" {
  48. pipfileFound = true
  49. } else if name == "Pipfile.lock" {
  50. pipfileLockFound = true
  51. }
  52. if pipfileFound && pipfileLockFound {
  53. break
  54. }
  55. }
  56. if pipfileFound && pipfileLockFound {
  57. results <- struct {
  58. string
  59. bool
  60. }{pipenv, true}
  61. }
  62. runtime.wg.Done()
  63. }
  64. func (runtime *pythonRuntime) detectPipGithub(results chan struct {
  65. string
  66. bool
  67. }, directoryContent []*github.RepositoryContent) {
  68. requirementsTxtFound := false
  69. for i := 0; i < len(directoryContent); i++ {
  70. name := directoryContent[i].GetName()
  71. if name == "requirements.txt" {
  72. requirementsTxtFound = true
  73. }
  74. }
  75. if requirementsTxtFound {
  76. results <- struct {
  77. string
  78. bool
  79. }{pip, true}
  80. }
  81. runtime.wg.Done()
  82. }
  83. func (runtime *pythonRuntime) detectPipGitlab(results chan struct {
  84. string
  85. bool
  86. }, tree []*gitlab.TreeNode) {
  87. requirementsTxtFound := false
  88. for i := 0; i < len(tree); i++ {
  89. name := tree[i].Name
  90. if name == "requirements.txt" {
  91. requirementsTxtFound = true
  92. }
  93. }
  94. if requirementsTxtFound {
  95. results <- struct {
  96. string
  97. bool
  98. }{pip, true}
  99. }
  100. runtime.wg.Done()
  101. }
  102. func (runtime *pythonRuntime) detectCondaGithub(results chan struct {
  103. string
  104. bool
  105. }, directoryContent []*github.RepositoryContent) {
  106. environmentFound := false
  107. packageListFound := false
  108. for i := 0; i < len(directoryContent); i++ {
  109. name := directoryContent[i].GetName()
  110. if name == "environment.yml" {
  111. environmentFound = true
  112. break
  113. } else if name == "package-list.txt" {
  114. packageListFound = true
  115. break
  116. }
  117. }
  118. if environmentFound || packageListFound {
  119. results <- struct {
  120. string
  121. bool
  122. }{conda, true}
  123. }
  124. runtime.wg.Done()
  125. }
  126. func (runtime *pythonRuntime) detectCondaGitlab(results chan struct {
  127. string
  128. bool
  129. }, tree []*gitlab.TreeNode) {
  130. environmentFound := false
  131. packageListFound := false
  132. for i := 0; i < len(tree); i++ {
  133. name := tree[i].Name
  134. if name == "environment.yml" {
  135. environmentFound = true
  136. break
  137. } else if name == "package-list.txt" {
  138. packageListFound = true
  139. break
  140. }
  141. }
  142. if environmentFound || packageListFound {
  143. results <- struct {
  144. string
  145. bool
  146. }{conda, true}
  147. }
  148. runtime.wg.Done()
  149. }
  150. func (runtime *pythonRuntime) detectStandaloneGithub(results chan struct {
  151. string
  152. bool
  153. }, directoryContent []*github.RepositoryContent) {
  154. pyFound := false
  155. for i := 0; i < len(directoryContent); i++ {
  156. name := directoryContent[i].GetName()
  157. if strings.HasSuffix(name, ".py") {
  158. pyFound = true
  159. break
  160. }
  161. }
  162. if pyFound {
  163. results <- struct {
  164. string
  165. bool
  166. }{standalone, true}
  167. }
  168. runtime.wg.Done()
  169. }
  170. func (runtime *pythonRuntime) detectStandaloneGitlab(results chan struct {
  171. string
  172. bool
  173. }, tree []*gitlab.TreeNode) {
  174. pyFound := false
  175. for i := 0; i < len(tree); i++ {
  176. name := tree[i].Name
  177. if strings.HasSuffix(name, ".py") {
  178. pyFound = true
  179. break
  180. }
  181. }
  182. if pyFound {
  183. results <- struct {
  184. string
  185. bool
  186. }{standalone, true}
  187. }
  188. runtime.wg.Done()
  189. }
  190. func (runtime *pythonRuntime) DetectGithub(
  191. client *github.Client,
  192. directoryContent []*github.RepositoryContent,
  193. owner, name, path string,
  194. repoContentOptions github.RepositoryContentGetOptions,
  195. paketo, heroku *BuilderInfo,
  196. ) error {
  197. results := make(chan struct {
  198. string
  199. bool
  200. }, 4)
  201. runtime.wg.Add(4)
  202. go runtime.detectPipenvGithub(results, directoryContent)
  203. go runtime.detectPipGithub(results, directoryContent)
  204. go runtime.detectCondaGithub(results, directoryContent)
  205. go runtime.detectStandaloneGithub(results, directoryContent)
  206. runtime.wg.Wait()
  207. close(results)
  208. paketoBuildpackInfo := BuildpackInfo{
  209. Name: "Python",
  210. Buildpack: "gcr.io/paketo-buildpacks/python",
  211. }
  212. herokuBuildpackInfo := BuildpackInfo{
  213. Name: "Python",
  214. Buildpack: "heroku/python",
  215. }
  216. if len(results) == 0 {
  217. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  218. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  219. return nil
  220. }
  221. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  222. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  223. return nil
  224. }
  225. func (runtime *pythonRuntime) DetectGitlab(
  226. client *gitlab.Client,
  227. tree []*gitlab.TreeNode,
  228. owner, name, path, ref string,
  229. paketo, heroku *BuilderInfo,
  230. ) error {
  231. results := make(chan struct {
  232. string
  233. bool
  234. }, 4)
  235. runtime.wg.Add(4)
  236. go runtime.detectPipenvGitlab(results, tree)
  237. go runtime.detectPipGitlab(results, tree)
  238. go runtime.detectCondaGitlab(results, tree)
  239. go runtime.detectStandaloneGitlab(results, tree)
  240. runtime.wg.Wait()
  241. close(results)
  242. paketoBuildpackInfo := BuildpackInfo{
  243. Name: "Python",
  244. Buildpack: "gcr.io/paketo-buildpacks/python",
  245. }
  246. herokuBuildpackInfo := BuildpackInfo{
  247. Name: "Python",
  248. Buildpack: "heroku/python",
  249. }
  250. if len(results) == 0 {
  251. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  252. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  253. return nil
  254. }
  255. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  256. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  257. return nil
  258. }