Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

make.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # intentions
  2. # ::pseudo code::
  3. # get file names
  4. # open & parse
  5. # check if template; if output should I stop for some random reason
  6. # for templates get input files
  7. # run and save
  8. import chevron
  9. import os
  10. from os.path import isfile,islink,isdir
  11. p=print
  12. class Eobj:pass
  13. C = Eobj
  14. C.isTemplateStr = '{{{"makestache_template"}}}'
  15. C.isTemplateStr2 = 'THIS_FILE_IS_A_MAKESTACHE_TEMPLATE'
  16. C.isNotTemplateStr = 'this_file_is_not_a_makestache_template'
  17. # this string prolly shouldn't appear in any files you wouldn't want to actually overwrite
  18. C.ALLOW_TEMPLATE_OVERWRITE = "BUILT_FILE_" + "OVERWRITE_ALLOWED"
  19. # output_filename # mustache_var
  20. p("MAKE.PY")
  21. p("MAKESTACHE")
  22. def wfile(file_name,out):
  23. f = open(file_name,"w")
  24. f.write(out)
  25. f.close()
  26. def rfile(file_name):
  27. f = open(file_name,"r")
  28. ftxt = f.read()
  29. f.close()
  30. return ftxt
  31. # TODO:rename this class
  32. class JsObj(dict):
  33. pass
  34. def __init__(self,*a,**kw):
  35. super().__init__(*a,**kw)
  36. self._template_paths = []
  37. def _maybe_path(self,name, *a,**kw):
  38. # p("_maybe_path",name)
  39. if "*" in name:
  40. if name not in self._template_paths:
  41. p("possible dependency",name)
  42. self._template_paths.append(name)
  43. def __getattr__(self, name):
  44. # p(">>>>",name,self)
  45. return self.__getitem__(name)
  46. def __getitem__(self, name):
  47. self._maybe_path(name)
  48. # p("__getitem__","*" in name,name)
  49. ret = super().__getitem__(name)
  50. return ret
  51. def __setattr__(self, name, val):
  52. return self.__setitem__(name, val)
  53. def __delattr__(self, name):
  54. return self.__delitem__(name)
  55. # yeah yeah I know where reading the same junk
  56. def is_template_file(file_name):
  57. r={
  58. "is_template":0
  59. }
  60. ftxt = ""
  61. try:
  62. # ftxt = rfile(file_name)
  63. # with open(file_path, "r") as file:
  64. with open(file_name, "r") as file:
  65. ftxt = file.read()
  66. r["is_template"] = is_template_ftxt(ftxt)
  67. except Exception as excp:
  68. p("\t\\t\tFILE ERR:::",file_name)
  69. p("")
  70. p("LRN",len(ftxt))
  71. return r
  72. def is_template_ftxt(ftxt):
  73. ret = 0
  74. if C.isTemplateStr in ftxt or C.isTemplateStr2 in ftxt:
  75. ret =1
  76. if C.isNotTemplateStr in ftxt:
  77. ret = 0
  78. return ret
  79. # this should be called render or somthing
  80. def parse(ftxt):
  81. p("PARSE")
  82. # o={}
  83. _o={
  84. }
  85. o=JsObj(_o)
  86. out = chevron.render(ftxt,o)
  87. # p(out)
  88. p("o.idk?")
  89. p("~",o._template_paths,"~")
  90. mdict = get_calculated_templates(o._template_paths)
  91. o2 = {**o,**mdict["file_stache_dict"],
  92. "output_filename":mdict["outfile"]["path"],
  93. }
  94. out2 = chevron.render(ftxt,o2)
  95. if C.isTemplateStr in ftxt:
  96. p("IS A TEMPLATE!!!!!!!!!!!!!!!!!!!")
  97. else:
  98. p("IS NOT A TEMPLATE!!!!!!!!!!!!!!!!!!!")
  99. p("THE WE WILL NOT write")
  100. return ""
  101. os.chdir(tglob["OUT_DIR"])
  102. # dbg_out
  103. wfile("mgen1.js",out)
  104. wfile("mgen2.js",out2)
  105. p()
  106. if mdict["output_ok"]:
  107. p(mdict["outfile"]["_path"])
  108. p("WRITING TO:",mdict["outfile"]["path"])
  109. wfile(mdict["outfile"]["path"],out2)
  110. p("FILE WRITTEN")
  111. else:
  112. p("BUILD_NOT FINALIZED")
  113. def get_calculated_templates(arr):
  114. # we assume we're in the right dir and don't worry about anything
  115. ret = {}
  116. file_deps = {}
  117. for en in arr:
  118. file_content_path = en.replace("@*","",1)
  119. file_content_path = file_content_path.replace("*",".")
  120. file_deps[en] = {
  121. "path":file_content_path,
  122. "content":":::__BAD_PATH__::: does the show go on?",
  123. }
  124. # now we get our paths
  125. file_stache_dict = {}
  126. for var_name,file_data in file_deps.items():
  127. if ">" in var_name:continue
  128. ftxt = rfile(file_data["path"])
  129. file_stache_dict[var_name]=ftxt
  130. # file_deps[var_name]["content"]=ftxt
  131. # get_out_put_path and set output_ok key
  132. for en in (_en for _en in file_deps if (">" in _en or 0)):
  133. v = file_deps[en]
  134. ret["outfile"]= {
  135. "_path":v["path"].split(">")[-1],
  136. }
  137. os.chdir(tglob["OUT_DIR"])
  138. ret["output_ok"]=None
  139. current_output_contents=None
  140. assert ret["outfile"]["_path"][0] not in r"\/" , "\033[36m \n\twe won't allow absolute paths that easily\033[0m"
  141. full_path = os.path.abspath(ret["outfile"]["_path"])
  142. ret["outfile"]["path"] = full_path
  143. if os.path.isfile(ret["outfile"]["path"]):
  144. current_output_contents = rfile(ret["outfile"]["path"])
  145. if C.ALLOW_TEMPLATE_OVERWRITE in current_output_contents:
  146. ret["output_ok"]="ALLOW_TEMPLATE_OVERWRITE"
  147. else:
  148. ret["output_ok"]= False
  149. else:
  150. ret["output_ok"]=1
  151. p("current_output_ EXIST?",bool(current_output_contents))
  152. p(":ALLOW_TEMPLATE_OVERWRITE??",ret["output_ok"])
  153. return {
  154. "file_deps":file_deps,
  155. "file_stache_dict":file_stache_dict,
  156. **ret
  157. }
  158. def parse_possible_template(fn):
  159. ftxt = rfile(fn)
  160. parse(ftxt)
  161. def dev_main():
  162. p("DEV_MAIN")
  163. os.chdir(tglob["IN_DIR"])
  164. # for en in os.walk(r"C:\ws\repos\script_utils\makestache"):
  165. # for en in os.listdir(r"C:\ws\repos\script_utils\makestache"):
  166. # for now lets skip links you're not really safe im not checking of thinking about junctions or whatever
  167. o = {}
  168. # for en in os.scandir(r"C:\ws\repos\script_utils\makestache"):
  169. for en in os.scandir():
  170. if isfile(en):
  171. o[en] = is_template_file(en)
  172. p([en],isfile(en))
  173. r'''
  174. for en2 in dir(en):
  175. if en2[:2]=="__":
  176. continue
  177. p(en2)
  178. return
  179. # '''
  180. p("parse_possible_template")
  181. p()
  182. for k,v in o.items():
  183. if not v["is_template"]:continue
  184. p(k)
  185. p(v)
  186. p()
  187. parse_possible_template(k)
  188. # this will prolly be a pain later
  189. tglob = {
  190. # "file_cache":{}
  191. }
  192. tglob["OUT_DIR"] = r"C:\ws\repos\script_utils\makestache\test_out"
  193. tglob["IN_DIR"] = r"C:\ws\repos\script_utils\makestache\test_src"
  194. tglob["OUT_DIR"] = r"C:\ws\repos\script_utils\makestache\test_src"
  195. dev_main()