25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

147 satır
3.2 KiB

  1. class Timestamps:
  2. """
  3. Build / manage json timestamp files
  4. """
  5. previous = {}
  6. dirty = []
  7. bundled = []
  8. current = {}
  9. ignore_hidden = True
  10. ignore_extn = ('pyc', 'DS_Store', 'gitignore')
  11. """
  12. load timestamps and dirty files
  13. """
  14. def __init__(self):
  15. self.load()
  16. self.get_current()
  17. self.check_dirty()
  18. def check_dirty(self):
  19. """
  20. Returns true if the current folder is dirty
  21. """
  22. from build import verbose
  23. import os
  24. self.dirty = []
  25. if not self.previous:
  26. if verbose:
  27. print 'Dirty: no timestamps!'
  28. self.dirty = self.current.keys()
  29. else:
  30. # check both ways for missing files
  31. for f in self.current:
  32. if self.current[f] != self.previous.get(f):
  33. print '**** %s changed | %s -> %s' % (f, self.previous.get(f), self.current.get(f))
  34. self.dirty.append(f)
  35. for f in self.previous:
  36. if self.previous[f] != self.current.get(f):
  37. if f not in self.dirty:
  38. print '**** %s changed | %s -> %s' % (f, self.previous.get(f), self.current.get(f))
  39. self.dirty.append(f)
  40. # unique
  41. self.dirty = list(set(self.dirty))
  42. def get_current(self):
  43. """
  44. build timestamps dict for specified files
  45. """
  46. try:
  47. import config.assets
  48. except ImportError:
  49. return self.get_current_from_folders()
  50. ts = {}
  51. for fname in config.assets.file_list:
  52. ts[fname] = str(int(os.stat(fname).st_mtime))
  53. self.current = ts
  54. def get_current_from_folders(self):
  55. """
  56. walk in all folders and build tree of all js, css, html, md files
  57. """
  58. import os
  59. ts = {}
  60. # walk the parent folder and build all files as defined in the build.json files
  61. for wt in os.walk('.', followlinks=True):
  62. # build timestamps
  63. if self.ignore_hidden:
  64. for d in wt[1]:
  65. if d.startswith('.'):
  66. wt[1].remove(d)
  67. if os.path.exists(os.path.join(wt[0], d, '.no_timestamps')):
  68. wt[1].remove(d)
  69. for f in wt[2]:
  70. if f.split('.')[-1] not in self.ignore_extn and f!='_timestamps.js':
  71. fname = os.path.relpath(os.path.join(wt[0], f), os.curdir)
  72. ts[fname] = str(int(os.stat(fname).st_mtime))
  73. self.current = ts
  74. def write(self):
  75. """
  76. Write timestamp if dirty
  77. """
  78. import json, os
  79. ts_path = 'config/_timestamps.js'
  80. # write timestamps
  81. f = open(ts_path, 'w')
  82. self.get_current()
  83. f.write(json.dumps(self.current))
  84. f.close()
  85. def load(self):
  86. """
  87. Get all timestamps from file
  88. """
  89. from build import verbose
  90. import json, os
  91. ts_path = os.path.join('config', '_timestamps.js')
  92. if os.path.exists(ts_path):
  93. ts = open(ts_path, 'r')
  94. # merge the timestamps
  95. tmp = json.loads(ts.read())
  96. ts.close()
  97. else:
  98. if verbose:
  99. print "** No timestamps **"
  100. tmp = {}
  101. self.previous = tmp
  102. def update(self, fname):
  103. """
  104. Update timestamp of the given file and add to dirty
  105. """
  106. import os
  107. self.current[fname] = str(int(os.stat(fname).st_mtime))
  108. self.dirty.append(fname)
  109. def get(self, rettype='dict', types=[]):
  110. """
  111. return timestamps (ignore the ones not wanted)
  112. """
  113. # remove all .md timestamps
  114. ret = {}
  115. for t in self.current:
  116. if t.split('.')[-1] in types:
  117. if t not in self.bundled:
  118. ret[t] = self.current[t]
  119. if rettype=='dict':
  120. return ret
  121. else:
  122. import json
  123. return json.dumps(ret)