ssh.py 3.5 KB

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