utils.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import os
  2. import sys
  3. from .common import PY_VERSION, IS_PYPY, IS_WIN, ABI_FLAGS
  4. from .logger import LoggerInstance as _LoggerInstance
  5. def mkdir(at_path):
  6. if not os.path.exists(at_path):
  7. _LoggerInstance.info("Creating %s", at_path)
  8. os.makedirs(at_path)
  9. else:
  10. _LoggerInstance.info("Directory %s already exists", at_path)
  11. def is_executable(exe):
  12. """Checks a file is executable"""
  13. return os.path.isfile(exe) and os.access(exe, os.X_OK)
  14. def path_locations(home_dir, dry_run=False):
  15. """Return the path locations for the environment (where libraries are,
  16. where scripts go, etc)"""
  17. home_dir = os.path.abspath(home_dir)
  18. lib_dir, inc_dir, bin_dir = None, None, None
  19. # XXX: We'd use distutils.sysconfig.get_python_inc/lib but its
  20. # prefix arg is broken: http://bugs.python.org/issue3386
  21. if IS_WIN:
  22. # Windows has lots of problems with executables with spaces in
  23. # the name; this function will remove them (using the ~1
  24. # format):
  25. if not dry_run:
  26. mkdir(home_dir)
  27. if " " in home_dir:
  28. import ctypes
  29. get_short_path_name = ctypes.windll.kernel32.GetShortPathNameW
  30. size = max(len(home_dir) + 1, 256)
  31. buf = ctypes.create_unicode_buffer(size)
  32. try:
  33. # noinspection PyUnresolvedReferences
  34. u = unicode
  35. except NameError:
  36. u = str
  37. ret = get_short_path_name(u(home_dir), buf, size)
  38. if not ret:
  39. print('Error: the path "{}" has a space in it'.format(home_dir))
  40. print("We could not determine the short pathname for it.")
  41. print("Exiting.")
  42. sys.exit(3)
  43. home_dir = str(buf.value)
  44. lib_dir = os.path.join(home_dir, "Lib")
  45. inc_dir = os.path.join(home_dir, "Include")
  46. bin_dir = os.path.join(home_dir, "Scripts")
  47. if IS_PYPY:
  48. lib_dir = home_dir
  49. inc_dir = os.path.join(home_dir, "include")
  50. bin_dir = os.path.join(home_dir, "bin")
  51. elif not IS_WIN:
  52. lib_dir = os.path.join(home_dir, "lib", PY_VERSION)
  53. inc_dir = os.path.join(home_dir, "include", PY_VERSION + ABI_FLAGS)
  54. bin_dir = os.path.join(home_dir, "bin")
  55. return home_dir, lib_dir, inc_dir, bin_dir