1 # Copyright 2013 Hewlett-Packard Development Company, L.P.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from pbr import find_package
21 from pbr.hooks import base
26 if os.path.exists(os.path.join(sys.prefix, 'man')):
27 # This works around a bug with install where it expects every node
28 # in the relative data directory to be an actual directory, since at
29 # least Debian derivatives (and probably other platforms as well)
30 # like to symlink Unixish /usr/local/man to /usr/local/share/man.
35 def get_man_section(section):
36 return os.path.join(get_manpath(), 'man%s' % section)
39 def unquote_path(path):
40 # unquote the full path, e.g: "'a/full/path'" becomes "a/full/path", also
41 # strip the quotes off individual path components because os.walk cannot
42 # handle paths like: "'i like spaces'/'another dir'", so we will pass it
43 # "i like spaces/another dir" instead.
46 # shlex cannot handle paths that contain backslashes, treating those
47 # as escape characters.
48 path = path.replace("\\", "/")
49 return "".join(shlex.split(path)).replace("/", "\\")
51 return "".join(shlex.split(path))
54 class FilesConfig(base.BaseConfig):
58 def __init__(self, config, name):
59 super(FilesConfig, self).__init__(config)
61 self.data_files = self.config.get('data_files', '')
64 self.config['data_files'] = self.data_files
65 super(FilesConfig, self).save()
67 def expand_globs(self):
69 for line in self.data_files.split("\n"):
70 if line.rstrip().endswith('*') and '=' in line:
71 (target, source_glob) = line.split('=')
72 source_prefix = source_glob.strip()[:-1]
73 target = target.strip()
74 if not target.endswith(os.path.sep):
76 unquoted_prefix = unquote_path(source_prefix)
77 unquoted_target = unquote_path(target)
78 for (dirpath, dirnames, fnames) in os.walk(unquoted_prefix):
79 # As source_prefix is always matched, using replace with a
80 # a limit of one is always going to replace the path prefix
81 # and not accidentally replace some text in the middle of
83 new_prefix = dirpath.replace(unquoted_prefix,
85 finished.append("'%s' = " % new_prefix)
87 [" '%s'" % os.path.join(dirpath, f) for f in fnames])
91 self.data_files = "\n".join(finished)
93 def add_man_path(self, man_path):
94 self.data_files = "%s\n'%s' =" % (self.data_files, man_path)
96 def add_man_page(self, man_page):
97 self.data_files = "%s\n '%s'" % (self.data_files, man_page)
99 def get_man_sections(self):
100 man_sections = dict()
101 manpages = self.pbr_config['manpages']
102 for manpage in manpages.split():
103 section_number = manpage.strip()[-1]
104 section = man_sections.get(section_number, list())
105 section.append(manpage.strip())
106 man_sections[section_number] = section
110 packages = self.config.get('packages', self.name).strip()
112 for pkg in packages.split("\n"):
113 if os.path.isdir(pkg.strip()):
114 expanded.append(find_package.smart_find_packages(pkg.strip()))
116 self.config['packages'] = "\n".join(expanded)
120 if 'manpages' in self.pbr_config:
121 man_sections = self.get_man_sections()
122 for (section, pages) in man_sections.items():
123 manpath = get_man_section(section)
124 self.add_man_path(manpath)
126 self.add_man_page(page)