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. import bb
  36. from bb import data
  37. from bb.fetch import Fetch
  38. from bb.fetch import FetchError
  39. from bb.fetch import MissingParameterError
  40. __pattern__ = re.compile(r'''
  41. \s* # Skip leading whitespace
  42. ssh:// # scheme
  43. ( # Optional username/password block
  44. (?P<user>\S+) # username
  45. (:(?P<pass>\S+))? # colon followed by the password (optional)
  46. )?
  47. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  48. @
  49. (?P<host>\S+?) # non-greedy match of the host
  50. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  51. /
  52. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  53. # and may include the use of '~' to reference the remote home
  54. # directory
  55. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  56. $
  57. ''', re.VERBOSE)
  58. class SSH(Fetch):
  59. '''Class to fetch a module or modules via Secure Shell'''
  60. def supports(self, url, urldata, d):
  61. return __pattern__.match(url) != None
  62. def localpath(self, url, urldata, d):
  63. m = __pattern__.match(url)
  64. path = m.group('path')
  65. host = m.group('host')
  66. lpath = os.path.join(data.getVar('DL_DIR', d, True), host, os.path.basename(path))
  67. return lpath
  68. def go(self, url, urldata, d):
  69. dldir = data.getVar('DL_DIR', d, 1)
  70. m = __pattern__.match(url)
  71. path = m.group('path')
  72. host = m.group('host')
  73. port = m.group('port')
  74. user = m.group('user')
  75. password = m.group('pass')
  76. ldir = os.path.join(dldir, host)
  77. lpath = os.path.join(ldir, os.path.basename(path))
  78. if not os.path.exists(ldir):
  79. os.makedirs(ldir)
  80. if port:
  81. port = '-P %s' % port
  82. else:
  83. port = ''
  84. if user:
  85. fr = user
  86. if password:
  87. fr += ':%s' % password
  88. fr += '@%s' % host
  89. else:
  90. fr = host
  91. fr += ':%s' % path
  92. import commands
  93. cmd = 'scp -B -r %s %s %s/' % (
  94. port,
  95. commands.mkarg(fr),
  96. commands.mkarg(ldir)
  97. )
  98. (exitstatus, output) = commands.getstatusoutput(cmd)
  99. if exitstatus != 0:
  100. print output
  101. raise FetchError('Unable to fetch %s' % url)