pavement.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import platform
  4. import fnmatch
  5. import zipfile
  6. from paver.easy import *
  7. def get_extlibs():
  8. if platform.system() == "Windows":
  9. return 'extlibs_windows'
  10. if platform.system() == "Darwin":
  11. return 'extlibs_macos'
  12. if platform.system() == "Linux":
  13. return 'extlibs_linux'
  14. options(
  15. plugin=Bunch(
  16. name='ee_plugin',
  17. ext_libs=path(get_extlibs()),
  18. source_dir=path('.'),
  19. package_dir=path('.'),
  20. tests=['test', 'tests'],
  21. excludes=[
  22. "*.pyc",
  23. ".git",
  24. ".idea",
  25. ".gitignore",
  26. ".travis.yml",
  27. "__pycache__",
  28. "media",
  29. "ee_plugin.zip"
  30. ]
  31. ),
  32. )
  33. @task
  34. @cmdopts([('clean', 'c', 'clean out dependencies first')])
  35. def setup():
  36. clean = getattr(options, 'clean', False)
  37. ext_libs = options.plugin.ext_libs
  38. if clean:
  39. ext_libs.rmtree()
  40. ext_libs.makedirs()
  41. reqs = read_requirements()
  42. os.environ['PYTHONPATH'] = ext_libs.abspath()
  43. for req in reqs:
  44. if platform.system() == "Windows":
  45. sh('pip install -U -t "{ext_libs}" "{dep}"'.format(ext_libs=ext_libs.abspath(), dep=req))
  46. else:
  47. sh('pip3 install -U -t "{ext_libs}" "{dep}"'.format(ext_libs=ext_libs.abspath(), dep=req))
  48. @task
  49. def install(options):
  50. '''install plugin to qgis'''
  51. plugin_name = options.plugin.name
  52. src = path(__file__).dirname()
  53. if platform.system() == "Windows":
  54. dst = path('~/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins').expanduser() / plugin_name
  55. if platform.system() == "Darwin":
  56. dst = path(
  57. '~/Library/Application Support/QGIS/QGIS3/profiles/default/python/plugins').expanduser() / plugin_name
  58. if platform.system() == "Linux":
  59. dst = path('~/.local/share/QGIS/QGIS3/profiles/default/python/plugins').expanduser() / plugin_name
  60. src = src.abspath()
  61. dst = dst.abspath()
  62. if not hasattr(os, 'symlink'):
  63. dst.rmtree()
  64. src.copytree(dst)
  65. elif not dst.exists():
  66. src.symlink(dst)
  67. def read_requirements():
  68. '''return a list of packages in requirements file'''
  69. with open('requirements.txt') as f:
  70. return [l.strip('\n') for l in f if l.strip('\n') and not l.startswith('#')]
  71. @task
  72. @cmdopts([('tests', 't', 'Package tests with plugin')])
  73. def package(options):
  74. '''create package for plugin'''
  75. package_file = options.plugin.package_dir / ('%s.zip' % options.plugin.name)
  76. with zipfile.ZipFile(package_file, "w", zipfile.ZIP_DEFLATED) as f:
  77. if not hasattr(options.package, 'tests'):
  78. options.plugin.excludes.extend(options.plugin.tests)
  79. make_zip(f, options)
  80. def make_zip(zipFile, options):
  81. excludes = set(options.plugin.excludes)
  82. src_dir = options.plugin.source_dir
  83. exclude = lambda p: any([fnmatch.fnmatch(p, e) for e in excludes])
  84. def filter_excludes(files):
  85. if not files: return []
  86. # to prevent descending into dirs, modify the list in place
  87. for i in range(len(files) - 1, -1, -1):
  88. f = files[i]
  89. if exclude(f):
  90. files.remove(f)
  91. return files
  92. for root, dirs, files in os.walk(src_dir):
  93. for f in filter_excludes(files):
  94. relpath = os.path.relpath(root, '.')
  95. zipFile.write(path(root) / f, path('ee_plugin') / path(relpath) / f)
  96. filter_excludes(dirs)