build.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python
  2. from browser.linkjs import link
  3. import os
  4. import shutil
  5. import stat
  6. import subprocess
  7. import sys
  8. # http://stackoverflow.com/questions/1889597/deleting-directory-in-python
  9. def remove_readonly(fn, path, excinfo):
  10. if fn is os.rmdir:
  11. os.chmod(path, stat.S_IWRITE)
  12. os.rmdir(path)
  13. elif fn is os.remove:
  14. os.chmod(path, stat.S_IWRITE)
  15. os.remove(path)
  16. def norm_path(path):
  17. return os.path.normcase(os.path.normpath(os.path.realpath(os.path.abspath(path))))
  18. def is_parent_for(parent, child):
  19. parent = norm_path(parent)
  20. child = norm_path(child)
  21. while True:
  22. if parent == child:
  23. return True
  24. next = os.path.dirname(child)
  25. if next == child:
  26. return False
  27. child = next
  28. def cleanup(dir):
  29. this_dir = os.path.dirname(__file__)
  30. if is_parent_for(dir, this_dir):
  31. raise Exception("cannot delete itself: %s" % this_dir)
  32. shutil.rmtree(dir, onerror=remove_readonly)
  33. def copy(src, dst_dir):
  34. dst = os.path.join(dst_dir, os.path.basename(src))
  35. if os.path.exists(dst):
  36. os.chmod(dst, stat.S_IWRITE)
  37. print('%s -> %s' % (src, dst))
  38. shutil.copy(src, dst)
  39. def copytree(src, dst):
  40. if os.path.exists(dst):
  41. cleanup(dst)
  42. print('%s -> %s' % (src, dst))
  43. shutil.copytree(src, dst)
  44. def run(cmd):
  45. p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)
  46. return p.stdout.read().decode()
  47. def build(out, use_git):
  48. version = None
  49. if use_git:
  50. print(run('git pull'))
  51. version = run('git log -1 --format="%ci%n%H"')
  52. build_version = None
  53. build_version_path = os.path.join(out, 'version.txt')
  54. try:
  55. with open(build_version_path) as f:
  56. build_version = f.read()
  57. except:
  58. pass
  59. if (not build_version is None) and build_version == version:
  60. print("current html is up to date, do nothing")
  61. return
  62. if not os.path.exists(out):
  63. os.mkdir(out)
  64. link('oc.js', os.path.join(out, 'oc.js'), ['src', 'src/oberon.js'], version)
  65. copy('browser/oberonjs.html', out)
  66. for d in ['codemirror', 'jslibs']:
  67. copytree(os.path.join('browser', d), os.path.join(out, d))
  68. if version is None:
  69. if os.path.exists(build_version_path):
  70. os.remove(build_version_path)
  71. else:
  72. with open(build_version_path, 'w') as f:
  73. f.write(version)
  74. if __name__ == '__main__':
  75. if len(sys.argv) < 2:
  76. print('Pull repo and build html page\nUsage: build.py <output directory> [--no-git]')
  77. exit(-1)
  78. use_git = len(sys.argv) < 3 or sys.argv[2] != '--no-git'
  79. build(sys.argv[1], use_git)