ssh.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. '''
  2. BitBake 'Fetch' implementations
  3. This implementation is for Secure Shell (SSH), and attempts to comply with the
  4. IETF secsh internet draft:
  5. http://tools.ietf.org/wg/secsh/draft-ietf-secsh-scp-sftp-ssh-uri/
  6. Currently does not support the sftp parameters, as this uses scp
  7. Also does not support the 'fingerprint' connection parameter.
  8. Please note that '/' is used as host, path separator not ':' as you may
  9. be used to, also '~' can be used to specify user HOME, but again after '/'
  10. Example SRC_URI:
  11. SRC_URI = "ssh://user@host.example.com/dir/path/file.txt"
  12. SRC_URI = "ssh://user@host.example.com/~/file.txt"
  13. '''
  14. # Copyright (C) 2006 OpenedHand Ltd.
  15. #
  16. #
  17. # Based in part on svk.py:
  18. # Copyright (C) 2006 Holger Hans Peter Freyther
  19. # Based on svn.py:
  20. # Copyright (C) 2003, 2004 Chris Larson
  21. # Based on functions from the base bb module:
  22. # Copyright 2003 Holger Schurig
  23. #
  24. #
  25. # SPDX-License-Identifier: GPL-2.0-only
  26. #
  27. import re, os
  28. from bb.fetch2 import FetchMethod
  29. from bb.fetch2 import FetchError
  30. from bb.fetch2 import logger
  31. from bb.fetch2 import runfetchcmd
  32. __pattern__ = re.compile(r'''
  33. \s* # Skip leading whitespace
  34. ssh:// # scheme
  35. ( # Optional username/password block
  36. (?P<user>\S+) # username
  37. (:(?P<pass>\S+))? # colon followed by the password (optional)
  38. )?
  39. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  40. @
  41. (?P<host>\S+?) # non-greedy match of the host
  42. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  43. /
  44. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  45. # and may include the use of '~' to reference the remote home
  46. # directory
  47. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  48. $
  49. ''', re.VERBOSE)
  50. class SSH(FetchMethod):
  51. '''Class to fetch a module or modules via Secure Shell'''
  52. def supports(self, urldata, d):
  53. return __pattern__.match(urldata.url) != None
  54. def supports_checksum(self, urldata):
  55. return False
  56. def urldata_init(self, urldata, d):
  57. if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git':
  58. raise bb.fetch2.ParameterError(
  59. "Invalid protocol - if you wish to fetch from a git " +
  60. "repository using ssh, you need to use " +
  61. "git:// prefix with protocol=ssh", urldata.url)
  62. m = __pattern__.match(urldata.url)
  63. path = m.group('path')
  64. host = m.group('host')
  65. urldata.localpath = os.path.join(d.getVar('DL_DIR'),
  66. os.path.basename(os.path.normpath(path)))
  67. def download(self, urldata, d):
  68. dldir = d.getVar('DL_DIR')
  69. m = __pattern__.match(urldata.url)
  70. path = m.group('path')
  71. host = m.group('host')
  72. port = m.group('port')
  73. user = m.group('user')
  74. password = m.group('pass')
  75. if port:
  76. portarg = '-P %s' % port
  77. else:
  78. portarg = ''
  79. if user:
  80. fr = user
  81. if password:
  82. fr += ':%s' % password
  83. fr += '@%s' % host
  84. else:
  85. fr = host
  86. fr += ':%s' % path
  87. cmd = 'scp -B -r %s %s %s/' % (
  88. portarg,
  89. fr,
  90. dldir
  91. )
  92. bb.fetch2.check_network_access(d, cmd, urldata.url)
  93. runfetchcmd(cmd, d)