ssh.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. '''
  11. # Copyright (C) 2006 OpenedHand Ltd.
  12. #
  13. #
  14. # Based in part on svk.py:
  15. # Copyright (C) 2006 Holger Hans Peter Freyther
  16. # Based on svn.py:
  17. # Copyright (C) 2003, 2004 Chris Larson
  18. # Based on functions from the base bb module:
  19. # Copyright 2003 Holger Schurig
  20. #
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License version 2 as
  24. # published by the Free Software Foundation.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU General Public License along
  32. # with this program; if not, write to the Free Software Foundation, Inc.,
  33. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  34. import re, os
  35. from bb import data
  36. from bb.fetch2 import Fetch
  37. from bb.fetch2 import FetchError
  38. __pattern__ = re.compile(r'''
  39. \s* # Skip leading whitespace
  40. ssh:// # scheme
  41. ( # Optional username/password block
  42. (?P<user>\S+) # username
  43. (:(?P<pass>\S+))? # colon followed by the password (optional)
  44. )?
  45. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  46. @
  47. (?P<host>\S+?) # non-greedy match of the host
  48. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  49. /
  50. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  51. # and may include the use of '~' to reference the remote home
  52. # directory
  53. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  54. $
  55. ''', re.VERBOSE)
  56. class SSH(Fetch):
  57. '''Class to fetch a module or modules via Secure Shell'''
  58. def supports(self, url, urldata, d):
  59. return __pattern__.match(url) != None
  60. def localpath(self, url, urldata, d):
  61. m = __pattern__.match(urldata.url)
  62. path = m.group('path')
  63. host = m.group('host')
  64. lpath = os.path.join(data.getVar('DL_DIR', d, True), host, os.path.basename(path))
  65. return lpath
  66. def download(self, url, urldata, d):
  67. dldir = data.getVar('DL_DIR', d, 1)
  68. m = __pattern__.match(url)
  69. path = m.group('path')
  70. host = m.group('host')
  71. port = m.group('port')
  72. user = m.group('user')
  73. password = m.group('pass')
  74. ldir = os.path.join(dldir, host)
  75. lpath = os.path.join(ldir, os.path.basename(path))
  76. if not os.path.exists(ldir):
  77. os.makedirs(ldir)
  78. if port:
  79. port = '-P %s' % port
  80. else:
  81. port = ''
  82. if user:
  83. fr = user
  84. if password:
  85. fr += ':%s' % password
  86. fr += '@%s' % host
  87. else:
  88. fr = host
  89. fr += ':%s' % path
  90. import commands
  91. cmd = 'scp -B -r %s %s %s/' % (
  92. port,
  93. commands.mkarg(fr),
  94. commands.mkarg(ldir)
  95. )
  96. bb.fetch2.check_network_access(d, cmd)
  97. (exitstatus, output) = commands.getstatusoutput(cmd)
  98. if exitstatus != 0:
  99. print(output)
  100. raise FetchError('Unable to fetch %s' % url)