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.
 
 
 
 
 
 

96 lines
2.4 KiB

  1. class generateTimestamp:
  2. ts_filename = 'timestamp.js'
  3. @staticmethod
  4. def list_js_files(jsdir,ext='js'):
  5. import os
  6. all_files= []
  7. nono = ['./tiny_mce','./jquery']
  8. oldcwd = os.getcwd()
  9. os.chdir(jsdir)
  10. # TODO Sanitize the loop below
  11. for root, subfolders, files in os.walk('.'):
  12. if generateTimestamp.is_allowed(nono,root):
  13. for filename in files:
  14. if filename.endswith(ext):
  15. all_files.append(os.path.join(root,filename))
  16. os.chdir(oldcwd)
  17. for i in nono:
  18. for j in all_files:
  19. if j.startswith(i):
  20. all_files.remove(j)
  21. return all_files
  22. @staticmethod
  23. def is_allowed(disallowed,item):
  24. for i in disallowed:
  25. if item.startswith(i):
  26. return False
  27. return True
  28. @staticmethod
  29. def get_timestamp_dict(jsdir,filelist):
  30. tsdict={}
  31. import os
  32. from webnotes.utils import get_file_timestamp
  33. oldcwd = os.getcwd()
  34. os.chdir(jsdir)
  35. for filename in generateTimestamp.list_js_files('.'):
  36. ts = get_file_timestamp(filename)
  37. filename = filename.lstrip('./')
  38. filename = filename.rstrip('.js')
  39. filename = filename.replace('/','.')
  40. if generateTimestamp.is_package(filename):
  41. # Whoa its a package
  42. # Remove _packagename from the end if file is a package
  43. filename = generateTimestamp.convert_to_packagename(filename)
  44. tsdict[filename] = ts
  45. os.chdir(oldcwd)
  46. return tsdict
  47. @staticmethod
  48. def is_package(filename):
  49. from webnotes.utils import jsnamespace
  50. p = jsnamespace.jsNamespace.package_prefix
  51. return filename.split('.')[-1].startswith()
  52. @staticmethod
  53. def convert_to_packagename(filename):
  54. t = []
  55. for i in filename.split('.')[:-1]:
  56. t.append(i)
  57. t.append('.')
  58. del t[-1]
  59. filename = ''.join(t)
  60. return filename
  61. @staticmethod
  62. def read_ts_from_file(jsdir):
  63. import json
  64. filename=generateTimestamp.ts_filename
  65. f = open(generateTimestamp.ts_filename)
  66. tsjson = eval(f.read())
  67. f.close()
  68. ret = json.loads(tsjson)
  69. return ret
  70. @staticmethod
  71. def gents(jsdir):
  72. fl=generateTimestamp.list_js_files(jsdir)
  73. return generateTimestamp.get_timestamp_dict(jsdir,fl)
  74. @staticmethod
  75. def gentsfile(jsdir):
  76. """
  77. function to generate timestamps of all files in spath
  78. dpath is the file in which the timestamps JSON is stored
  79. """
  80. import json
  81. import os
  82. tsdict = generateTimestamp.gents(jsdir)
  83. f = open(os.path.join(jsdir,'wn',generateTimestamp.ts_filename),'w') #FIXME Hard coded!
  84. f.write('wn={}\n')
  85. f.write('wn.timestamp=')
  86. f.write(json.dumps(tsdict))
  87. f.close()