files.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 shlex
  17. import sys
  18. from pbr import find_package
  19. from pbr.hooks import base
  20. def get_manpath():
  21. manpath = 'share/man'
  22. if os.path.exists(os.path.join(sys.prefix, 'man')):
  23. # This works around a bug with install where it expects every node
  24. # in the relative data directory to be an actual directory, since at
  25. # least Debian derivatives (and probably other platforms as well)
  26. # like to symlink Unixish /usr/local/man to /usr/local/share/man.
  27. manpath = 'man'
  28. return manpath
  29. def get_man_section(section):
  30. return os.path.join(get_manpath(), 'man%s' % section)
  31. def unquote_path(path):
  32. # unquote the full path, e.g: "'a/full/path'" becomes "a/full/path", also
  33. # strip the quotes off individual path components because os.walk cannot
  34. # handle paths like: "'i like spaces'/'another dir'", so we will pass it
  35. # "i like spaces/another dir" instead.
  36. if os.name == 'nt':
  37. # shlex cannot handle paths that contain backslashes, treating those
  38. # as escape characters.
  39. path = path.replace("\\", "/")
  40. return "".join(shlex.split(path)).replace("/", "\\")
  41. return "".join(shlex.split(path))
  42. class FilesConfig(base.BaseConfig):
  43. section = 'files'
  44. def __init__(self, config, name):
  45. super(FilesConfig, self).__init__(config)
  46. self.name = name
  47. self.data_files = self.config.get('data_files', '')
  48. def save(self):
  49. self.config['data_files'] = self.data_files
  50. super(FilesConfig, self).save()
  51. def expand_globs(self):
  52. finished = []
  53. for line in self.data_files.split("\n"):
  54. if line.rstrip().endswith('*') and '=' in line:
  55. (target, source_glob) = line.split('=')
  56. source_prefix = source_glob.strip()[:-1]
  57. target = target.strip()
  58. if not target.endswith(os.path.sep):
  59. target += os.path.sep
  60. unquoted_prefix = unquote_path(source_prefix)
  61. unquoted_target = unquote_path(target)
  62. for (dirpath, dirnames, fnames) in os.walk(unquoted_prefix):
  63. # As source_prefix is always matched, using replace with a
  64. # a limit of one is always going to replace the path prefix
  65. # and not accidentally replace some text in the middle of
  66. # the path
  67. new_prefix = dirpath.replace(unquoted_prefix,
  68. unquoted_target, 1)
  69. finished.append("'%s' = " % new_prefix)
  70. finished.extend(
  71. [" '%s'" % os.path.join(dirpath, f) for f in fnames])
  72. else:
  73. finished.append(line)
  74. self.data_files = "\n".join(finished)
  75. def add_man_path(self, man_path):
  76. self.data_files = "%s\n'%s' =" % (self.data_files, man_path)
  77. def add_man_page(self, man_page):
  78. self.data_files = "%s\n '%s'" % (self.data_files, man_page)
  79. def get_man_sections(self):
  80. man_sections = dict()
  81. manpages = self.pbr_config['manpages']
  82. for manpage in manpages.split():
  83. section_number = manpage.strip()[-1]
  84. section = man_sections.get(section_number, list())
  85. section.append(manpage.strip())
  86. man_sections[section_number] = section
  87. return man_sections
  88. def hook(self):
  89. packages = self.config.get('packages', self.name).strip()
  90. expanded = []
  91. for pkg in packages.split("\n"):
  92. if os.path.isdir(pkg.strip()):
  93. expanded.append(find_package.smart_find_packages(pkg.strip()))
  94. self.config['packages'] = "\n".join(expanded)
  95. self.expand_globs()
  96. if 'manpages' in self.pbr_config:
  97. man_sections = self.get_man_sections()
  98. for (section, pages) in man_sections.items():
  99. manpath = get_man_section(section)
  100. self.add_man_path(manpath)
  101. for page in pages:
  102. self.add_man_page(page)