second try
[vsorcdistro/.git] / ryu / .eggs / pbr-5.3.1-py2.7.egg / pbr / hooks / files.py
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
16 import os
17 import shlex
18 import sys
19
20 from pbr import find_package
21 from pbr.hooks import base
22
23
24 def get_manpath():
25     manpath = 'share/man'
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.
31         manpath = 'man'
32     return manpath
33
34
35 def get_man_section(section):
36     return os.path.join(get_manpath(), 'man%s' % section)
37
38
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.
44
45     if os.name == 'nt':
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("/", "\\")
50
51     return "".join(shlex.split(path))
52
53
54 class FilesConfig(base.BaseConfig):
55
56     section = 'files'
57
58     def __init__(self, config, name):
59         super(FilesConfig, self).__init__(config)
60         self.name = name
61         self.data_files = self.config.get('data_files', '')
62
63     def save(self):
64         self.config['data_files'] = self.data_files
65         super(FilesConfig, self).save()
66
67     def expand_globs(self):
68         finished = []
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):
75                     target += 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
82                     # the path
83                     new_prefix = dirpath.replace(unquoted_prefix,
84                                                  unquoted_target, 1)
85                     finished.append("'%s' = " % new_prefix)
86                     finished.extend(
87                         [" '%s'" % os.path.join(dirpath, f) for f in fnames])
88             else:
89                 finished.append(line)
90
91         self.data_files = "\n".join(finished)
92
93     def add_man_path(self, man_path):
94         self.data_files = "%s\n'%s' =" % (self.data_files, man_path)
95
96     def add_man_page(self, man_page):
97         self.data_files = "%s\n  '%s'" % (self.data_files, man_page)
98
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
107         return man_sections
108
109     def hook(self):
110         packages = self.config.get('packages', self.name).strip()
111         expanded = []
112         for pkg in packages.split("\n"):
113             if os.path.isdir(pkg.strip()):
114                 expanded.append(find_package.smart_find_packages(pkg.strip()))
115
116         self.config['packages'] = "\n".join(expanded)
117
118         self.expand_globs()
119
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)
125                 for page in pages:
126                     self.add_man_page(page)