files.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2013 Hewlett-Packard Development Company, L.P.
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import os
  16. import sys
  17. from pbr import find_package
  18. from pbr.hooks import base
  19. def get_manpath():
  20. manpath = 'share/man'
  21. if os.path.exists(os.path.join(sys.prefix, 'man')):
  22. # This works around a bug with install where it expects every node
  23. # in the relative data directory to be an actual directory, since at
  24. # least Debian derivatives (and probably other platforms as well)
  25. # like to symlink Unixish /usr/local/man to /usr/local/share/man.
  26. manpath = 'man'
  27. return manpath
  28. def get_man_section(section):
  29. return os.path.join(get_manpath(), 'man%s' % section)
  30. class FilesConfig(base.BaseConfig):
  31. section = 'files'
  32. def __init__(self, config, name):
  33. super(FilesConfig, self).__init__(config)
  34. self.name = name
  35. self.data_files = self.config.get('data_files', '')
  36. def save(self):
  37. self.config['data_files'] = self.data_files
  38. super(FilesConfig, self).save()
  39. def expand_globs(self):
  40. finished = []
  41. for line in self.data_files.split("\n"):
  42. if line.rstrip().endswith('*') and '=' in line:
  43. (target, source_glob) = line.split('=')
  44. source_prefix = source_glob.strip()[:-1]
  45. target = target.strip()
  46. if not target.endswith(os.path.sep):
  47. target += os.path.sep
  48. for (dirpath, dirnames, fnames) in os.walk(source_prefix):
  49. finished.append(
  50. "%s = " % dirpath.replace(source_prefix, target))
  51. finished.extend(
  52. [" %s" % os.path.join(dirpath, f) for f in fnames])
  53. else:
  54. finished.append(line)
  55. self.data_files = "\n".join(finished)
  56. def add_man_path(self, man_path):
  57. self.data_files = "%s\n%s =" % (self.data_files, man_path)
  58. def add_man_page(self, man_page):
  59. self.data_files = "%s\n %s" % (self.data_files, man_page)
  60. def get_man_sections(self):
  61. man_sections = dict()
  62. manpages = self.pbr_config['manpages']
  63. for manpage in manpages.split():
  64. section_number = manpage.strip()[-1]
  65. section = man_sections.get(section_number, list())
  66. section.append(manpage.strip())
  67. man_sections[section_number] = section
  68. return man_sections
  69. def hook(self):
  70. packages = self.config.get('packages', self.name).strip()
  71. expanded = []
  72. for pkg in packages.split("\n"):
  73. if os.path.isdir(pkg.strip()):
  74. expanded.append(find_package.smart_find_packages(pkg.strip()))
  75. self.config['packages'] = "\n".join(expanded)
  76. self.expand_globs()
  77. if 'manpages' in self.pbr_config:
  78. man_sections = self.get_man_sections()
  79. for (section, pages) in man_sections.items():
  80. manpath = get_man_section(section)
  81. self.add_man_path(manpath)
  82. for page in pages:
  83. self.add_man_page(page)