1import distutils.command.bdist_rpm as orig
2
3
4class bdist_rpm(orig.bdist_rpm):
5    """
6    Override the default bdist_rpm behavior to do the following:
7
8    1. Run egg_info to ensure the name and version are properly calculated.
9    2. Always run 'install' using --single-version-externally-managed to
10       disable eggs in RPM distributions.
11    3. Replace dash with underscore in the version numbers for better RPM
12       compatibility.
13    """
14
15    def run(self):
16        # ensure distro name is up-to-date
17        self.run_command('egg_info')
18
19        orig.bdist_rpm.run(self)
20
21    def _make_spec_file(self):
22        version = self.distribution.get_version()
23        rpmversion = version.replace('-', '_')
24        spec = orig.bdist_rpm._make_spec_file(self)
25        line23 = '%define version ' + version
26        line24 = '%define version ' + rpmversion
27        spec = [
28            line.replace(
29                "Source0: %{name}-%{version}.tar",
30                "Source0: %{name}-%{unmangled_version}.tar"
31            ).replace(
32                "setup.py install ",
33                "setup.py install --single-version-externally-managed "
34            ).replace(
35                "%setup",
36                "%setup -n %{name}-%{unmangled_version}"
37            ).replace(line23, line24)
38            for line in spec
39        ]
40        insert_loc = spec.index(line24) + 1
41        unmangled_version = "%define unmangled_version " + version
42        spec.insert(insert_loc, unmangled_version)
43        return spec
44