common.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Copyright (C) 2012-2015 Diego Torres Milano
  4. Created on Jan 5, 2015
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. @author: Diego Torres Milano
  15. '''
  16. import platform
  17. import os
  18. def _nd(name):
  19. '''
  20. @return: Returns a named decimal regex
  21. '''
  22. return '(?P<%s>\d+)' % name
  23. def _nh(name):
  24. '''
  25. @return: Returns a named hex regex
  26. '''
  27. return '(?P<%s>[0-9a-f]+)' % name
  28. def _ns(name, greedy=False):
  29. '''
  30. NOTICE: this is using a non-greedy (or minimal) regex
  31. @type name: str
  32. @param name: the name used to tag the expression
  33. @type greedy: bool
  34. @param greedy: Whether the regex is greedy or not
  35. @return: Returns a named string regex (only non-whitespace characters allowed)
  36. '''
  37. return '(?P<%s>\S+%s)' % (name, '' if greedy else '?')
  38. def obtainPxPy(m):
  39. px = int(m.group('px'))
  40. py = int(m.group('py'))
  41. return (px, py)
  42. def obtainVxVy(m):
  43. wvx = int(m.group('vx'))
  44. wvy = int(m.group('vy'))
  45. return wvx, wvy
  46. def obtainVwVh(m):
  47. (wvx, wvy) = obtainVxVy(m)
  48. wvx1 = int(m.group('vx1'))
  49. wvy1 = int(m.group('vy1'))
  50. return (wvx1-wvx, wvy1-wvy)
  51. def obtainAdbPath():
  52. '''
  53. Obtains the ADB path attempting know locations for different OSs
  54. '''
  55. osName = platform.system()
  56. isWindows = False
  57. if osName.startswith('Windows'):
  58. adb = 'adb.exe'
  59. isWindows = True
  60. else:
  61. adb = 'adb'
  62. ANDROID_HOME = os.environ['ANDROID_HOME'] if os.environ.has_key('ANDROID_HOME') else '/opt/android-sdk'
  63. HOME = os.environ['HOME'] if os.environ.has_key('HOME') else ''
  64. possibleChoices = [ os.path.join(ANDROID_HOME, 'platform-tools', adb),
  65. os.path.join(HOME, "android", 'platform-tools', adb),
  66. os.path.join(HOME, "android-sdk", 'platform-tools', adb),
  67. ]
  68. if osName.startswith('Windows'):
  69. possibleChoices.append(os.path.join("""C:\Program Files\Android\android-sdk\platform-tools""", adb))
  70. possibleChoices.append(os.path.join("""C:\Program Files (x86)\Android\android-sdk\platform-tools""", adb))
  71. elif osName.startswith('Linux'):
  72. possibleChoices.append(os.path.join("opt", "android-sdk-linux", 'platform-tools', adb))
  73. possibleChoices.append(os.path.join(HOME, "opt", "android-sdk-linux", 'platform-tools', adb))
  74. possibleChoices.append(os.path.join(HOME, "android-sdk-linux", 'platform-tools', adb))
  75. possibleChoices.append(os.path.join(HOME, 'Android', 'Sdk', 'platform-tools', adb))
  76. elif osName.startswith('Mac'):
  77. possibleChoices.append(os.path.join("opt", "android-sdk-mac_x86", 'platform-tools', adb))
  78. possibleChoices.append(os.path.join(HOME, "opt", "android-sdk-mac", 'platform-tools', adb))
  79. possibleChoices.append(os.path.join(HOME, "android-sdk-mac", 'platform-tools', adb))
  80. possibleChoices.append(os.path.join(HOME, "opt", "android-sdk-mac_x86", 'platform-tools', adb))
  81. possibleChoices.append(os.path.join(HOME, "android-sdk-mac_x86", 'platform-tools', adb))
  82. else:
  83. # Unsupported OS
  84. pass
  85. possibleChoices.append(adb)
  86. for exeFile in possibleChoices:
  87. if os.access(exeFile, os.X_OK):
  88. return exeFile
  89. for path in os.environ["PATH"].split(os.pathsep):
  90. exeFile = os.path.join(path, adb)
  91. if exeFile != None and os.access(exeFile, os.X_OK if not isWindows else os.F_OK):
  92. return exeFile
  93. raise Exception('adb="%s" is not executable. Did you forget to set ANDROID_HOME in the environment?' % adb)