You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

150 line
3.2 KiB

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