app.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from flask import Flask, render_template, abort, send_file
  2. from flask_assets import Environment, Bundle
  3. import os
  4. import json
  5. from typing import List
  6. import markdown as md
  7. app = Flask(__name__)
  8. assets = Environment(app)
  9. assets.url = app.static_url_path
  10. scss = Bundle('style/main.scss', filters='libsass',
  11. output='all.css', depends='**/*.scss')
  12. assets.config['PYSCSS_LOAD_PATHS'] = assets.load_path
  13. assets.config['PYSCSS_STATIC_URL'] = assets.url
  14. assets.config['PYSCSS_STATIC_ROOT'] = assets.directory
  15. assets.config['PYSCSS_ASSETS_URL'] = assets.url
  16. assets.config['PYSCSS_ASSETS_ROOT'] = assets.directory
  17. assets.register('scss_all', scss)
  18. class ArtifactItem:
  19. file: str
  20. description: str
  21. def __init__(self, file, description):
  22. self.file = file
  23. self.description = description
  24. class Artifact:
  25. id: str
  26. date: str
  27. changelog: str
  28. artifacts: List[ArtifactItem]
  29. def __init__(self, id, date, changelog, artifacts):
  30. self.id = id
  31. self.date = date
  32. self.changelog = changelog
  33. self.artifacts = [ArtifactItem(**item)
  34. for item in artifacts]
  35. @classmethod
  36. def from_json(cls, json_str):
  37. json_dict = json.loads(json_str)
  38. return cls(**json_dict)
  39. class ProjectInfo:
  40. name: str
  41. def __init__(self, name):
  42. self.name = name
  43. @classmethod
  44. def from_json(cls, json_str):
  45. json_dict = json.loads(json_str)
  46. return cls(**json_dict)
  47. class Project:
  48. id: str
  49. info: ProjectInfo
  50. def get_artifacts(self) -> List[Artifact]:
  51. result = []
  52. artifacts_path = os.path.join("../builds", self.id, "artifacts")
  53. artifact_folders = sorted([folder.path for folder in os.scandir(
  54. artifacts_path) if folder.is_dir()], reverse=True)
  55. for artifact_folder in artifact_folders:
  56. info_file_path = os.path.join(artifact_folder, "info.json")
  57. if not os.path.exists(info_file_path):
  58. continue
  59. try:
  60. with open(info_file_path, "r", encoding="utf-8") as f:
  61. artifact = Artifact.from_json(f.read())
  62. result.append(artifact)
  63. except:
  64. continue
  65. return result
  66. def get_projects() -> List[Project]:
  67. result = []
  68. projects = [f.path for f in os.scandir("../builds") if f.is_dir()]
  69. for project in projects:
  70. info_path = os.path.join(project, "info.json")
  71. if not os.path.exists(info_path):
  72. continue
  73. try:
  74. proj = Project()
  75. _, proj.id = os.path.split(project)
  76. with open(info_path, "r", encoding="utf-8") as f:
  77. proj.info = ProjectInfo.from_json(f.read())
  78. result.append(proj)
  79. except:
  80. continue
  81. return result
  82. @app.route("/projects/<string:project_id>/<string:artifact_id>/<string:download_item>")
  83. def download_item(project_id, artifact_id, download_item):
  84. file_path = os.path.join("../builds", project_id,
  85. "artifacts", artifact_id, download_item)
  86. if not os.path.exists(file_path):
  87. return abort(404)
  88. return send_file(file_path)
  89. @app.route("/projects/<string:project_id>")
  90. def display_project(project_id):
  91. projects = get_projects()
  92. selected_project = next(
  93. (project for project in projects if project.id == project_id), None)
  94. if not selected_project:
  95. return abort(404)
  96. info_path = os.path.join("../builds", selected_project.id, "info.md")
  97. readme = None
  98. try:
  99. if os.path.exists(info_path):
  100. with open(info_path, "r", encoding="utf-8") as f:
  101. readme = md.markdown(f.read())
  102. except:
  103. readme = None
  104. artifacts = selected_project.get_artifacts()
  105. return render_template("project_view.html", projects=projects, selected_project=selected_project, readme=readme, artifacts=artifacts)
  106. @app.route("/")
  107. def index():
  108. projects = get_projects()
  109. return render_template("main.html", projects=projects)
  110. if __name__ == "__main__":
  111. app.run(host='0.0.0.0')