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.

setup.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import argparse
  2. import fileinput
  3. from importlib import util
  4. import logging
  5. import os
  6. import subprocess
  7. import sys
  8. spec = util.spec_from_file_location("_common", os.path.join(os.path.dirname(os.path.abspath(__file__)), "./_common.py"))
  9. c = util.module_from_spec(spec)
  10. spec.loader.exec_module(c)
  11. def add_arg_parser(parser):
  12. parser.add_argument('-d', '--debug', action='store_true',
  13. help='Set log level to Debug')
  14. parser.add_argument('-np', '--no_pyinstaller', action='store_true',
  15. help='Don\'t install pyinstaller')
  16. parser.add_argument('--cpu', action='store_true',
  17. help='No cuda support')
  18. parser.add_argument('-pnc', '--pip_no_cache_dir', action='store_true',
  19. help='Use --no_cache_dir for pip commands')
  20. parser.add_argument('-pu', '--pip_user', action='store_true',
  21. help='Use --user for pip commands')
  22. def check_dependencies():
  23. c.log.debug("OS : {}".format(c.get_os()))
  24. c.log.debug("Python version : {}".format(c.get_python_version()))
  25. if c.get_os() == c.OS.UNKNOWN:
  26. c.log.fatal("Unknown OS !")
  27. sys.exit(1)
  28. if c.get_python_version() < (3, 5):
  29. c.log.fatal("Unsupported python version !")
  30. sys.exit(1)
  31. def pyinstaller(args, pip_commands_extend=None):
  32. if pip_commands_extend is None:
  33. pip_commands_extend = []
  34. c.log.info('Installing pyinstaller')
  35. r = subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyinstaller'] + pip_commands_extend)
  36. if r.returncode != 0:
  37. c.log.fatal("Pyinstaller installation failed")
  38. sys.exit(1)
  39. c.log.info('Pyinstaller successfully installed')
  40. def cli_setup(args, pip_commands_extend=None):
  41. if pip_commands_extend is None:
  42. pip_commands_extend = []
  43. def torch_version():
  44. if args.cpu:
  45. if c.get_os() == c.OS.LINUX:
  46. return "https://download.pytorch.org/whl/cpu/torch-1.1.0-cp{0}{1}-cp{0}{1}m-linux_x86_64.whl".format(
  47. *c.get_python_version())
  48. if c.get_os() == c.OS.MAC:
  49. return "torch"
  50. if c.get_os() == c.OS.WIN:
  51. return "https://download.pytorch.org/whl/cpu/torch-1.1.0-cp{0}{1}-cp{0}{1}m-win_amd64.whl".format(
  52. *c.get_python_version())
  53. else:
  54. if c.get_os() == c.OS.LINUX:
  55. return "https://download.pytorch.org/whl/cu100/torch-1.1.0-cp{0}{1}-cp{0}{1}m-linux_x86_64.whl".format(
  56. *c.get_python_version())
  57. if c.get_os() == c.OS.MAC:
  58. c.log.warning(
  59. "# MacOS Binaries dont support CUDA, install from source if CUDA is needed. "
  60. "This script will install the cpu version.")
  61. return "torch"
  62. if c.get_os() == c.OS.WIN:
  63. return "https://download.pytorch.org/whl/cu100/torch-1.1.0-cp{0}{1}-cp{0}{1}m-win_amd64.whl".format(
  64. *c.get_python_version())
  65. c.log.info('Installing Cli dependencies')
  66. path = c.create_temporary_copy(
  67. os.path.join(os.path.dirname(os.path.abspath(__file__)), "../requirements.txt"), "cli-requirements.txt")
  68. with fileinput.FileInput(path, inplace=True) as f:
  69. for l in f:
  70. print(l.replace("torch==1.1.0", torch_version()), end='')
  71. r = subprocess.run([sys.executable, '-m', 'pip', 'install', '-r', path] + pip_commands_extend)
  72. os.remove(path)
  73. if r.returncode != 0:
  74. c.log.fatal("Cli dependencies installation failed")
  75. sys.exit(1)
  76. c.log.info('Cli dependencies successfully installed')
  77. def run(args):
  78. # System & Dependencies Check
  79. check_dependencies()
  80. if args.debug:
  81. c.log.setLevel(logging.DEBUG)
  82. # Cli dependencies
  83. pip_commands_extend = (['--user'] if args.pip_user else []) + (['--no-cache-dir'] if args.pip_no_cache_dir else [])
  84. # Pyinstaller
  85. if not args.no_pyinstaller:
  86. pyinstaller(pip_commands_extend)
  87. cli_setup(args, pip_commands_extend)
  88. if __name__ == '__main__':
  89. parser = argparse.ArgumentParser(description='cli dependencies setup')
  90. add_arg_parser(parser)
  91. args = parser.parse_args()
  92. run(args)