python.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ''' Python Server publish script '''
  2. import json
  3. import os
  4. import re
  5. import sys
  6. import shutil
  7. import subprocess
  8. def read(path):
  9. ''' Read file content '''
  10. with open(path, 'r', encoding='utf-8') as file:
  11. return file.read()
  12. def write(path, content):
  13. ''' Write file content '''
  14. with open(path, 'w', encoding='utf-8') as file:
  15. file.write(content)
  16. def update(path, regex, value):
  17. ''' Update regex patter in file content with value '''
  18. content = read(path)
  19. def repl(match):
  20. return match.group(1) + value + match.group(3)
  21. content = re.sub(regex, repl, content)
  22. write(path, content)
  23. def build():
  24. ''' Build dist/pypi '''
  25. shutil.rmtree('./source/__pycache__', ignore_errors=True)
  26. shutil.rmtree('./dist/pypi', ignore_errors=True)
  27. shutil.copytree('./source/', './dist/pypi/netron/')
  28. shutil.copyfile('./publish/setup.py', './dist/pypi/setup.py')
  29. os.remove('./dist/pypi/netron/electron.html')
  30. os.remove('./dist/pypi/netron/electron.js')
  31. os.remove('./dist/pypi/netron/app.js')
  32. def version():
  33. ''' Update version '''
  34. package = json.loads(read('./package.json'))
  35. update('./dist/pypi/setup.py', '( version=")(.*)(",)', package['version'])
  36. update('./dist/pypi/netron/server.py',
  37. "(__version__ = ')(.*)(')",
  38. package['version'])
  39. update('./dist/pypi/netron/index.html',
  40. '(<meta name="version" content=")(.*)(">)',
  41. package['version'])
  42. update('./dist/pypi/netron/index.html',
  43. '(<meta name="date" content=")(.*)(">)',
  44. package['date'])
  45. def start():
  46. ''' Start server '''
  47. sys.path.insert(0, './dist/pypi')
  48. args = [ sys.executable, '-c', 'import netron; netron.main()' ] + sys.args
  49. sys.args = []
  50. subprocess.run(args, env={ 'PYTHONPATH': './dist/pypi' }, check=False)
  51. def main(): # pylint: disable=missing-function-docstring
  52. command_table = { 'build': build, 'version': version, 'start': start }
  53. sys.args = sys.argv[1:]
  54. while len(sys.args) > 0:
  55. command = sys.args.pop(0)
  56. command_table[command]()
  57. if __name__ == '__main__':
  58. main()