| 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """ |
| 4 | Create source tarball, create rpm from it together with spec file. |
| 5 | |
| 6 | Create a source tarball that is compatible with standard build pipelines. Then |
| 7 | run rpmbuild on it to create an installable rpm. |
| 8 | |
| 9 | .. codeauthor:: Intra2net AG <info@intra2net.com> |
| 10 | """ |
| 11 | |
| 12 | import sys |
| 13 | from os.path import join, basename, dirname, expanduser |
| 14 | from re import compile as re_compile |
| 15 | import tarfile |
| 16 | from shutil import copy |
| 17 | from subprocess import run |
| 18 | |
| 19 | # path to spec file |
| 20 | BASE_DIR = dirname(__file__) |
| 21 | SPEC_FILE = join(BASE_DIR, 'pyi2ncommon.spec') |
| 22 | |
| 23 | # regular expressions extracting relevant information from spec file |
| 24 | VERSION_REGEX = re_compile(r'^Version:\s*(\S+)\s*$') |
| 25 | NAME_REGEX = re_compile(r'^Name:\s*(\S+)\s*$') |
| 26 | SOURCE_REGEX = re_compile(r'^Source0:\s*(\S+)\s*$') |
| 27 | |
| 28 | # dir to contain tarball |
| 29 | DIST_DIR = join(BASE_DIR, 'dist') |
| 30 | |
| 31 | # files and dirs to add to tarball; keep in sync with %files section in spec |
| 32 | DATA = ( |
| 33 | 'COPYING.GPL', 'Linking-Exception.txt', 'README', 'CONTRIBUTING', |
| 34 | 'setup.py', 'src', 'test', 'doc/about_docu.rst', 'doc/conf.py', |
| 35 | 'doc/cnfvar-api-ascii-art.rst' |
| 36 | ) |
| 37 | |
| 38 | # dir where rpmbuild expects its tarballs |
| 39 | RPM_SOURCE_DIR = expanduser(join('~', 'rpmbuild', 'SOURCES')) |
| 40 | |
| 41 | |
| 42 | def get_spec_info(): |
| 43 | """Extract name, version and source0 from spec file.""" |
| 44 | name = None |
| 45 | version = None |
| 46 | source = None |
| 47 | with open(SPEC_FILE, 'rt') as reader: |
| 48 | for line in reader: |
| 49 | if name is not None and version is not None and source is not None: |
| 50 | source = source.replace('%{name}', name)\ |
| 51 | .replace('%{version}', version) |
| 52 | return name, version, source |
| 53 | match = NAME_REGEX.match(line) |
| 54 | if match: |
| 55 | name = match.group(1) |
| 56 | continue |
| 57 | match = VERSION_REGEX.match(line) |
| 58 | if match: |
| 59 | version = match.group(1) |
| 60 | continue |
| 61 | match = SOURCE_REGEX.match(line) |
| 62 | if match: |
| 63 | source = match.group(1) |
| 64 | continue |
| 65 | |
| 66 | |
| 67 | def tar_add_filter(tarinfo): |
| 68 | """ |
| 69 | Filter function for adding files to tarball |
| 70 | |
| 71 | Return `None` for pycache and pyc, meaning "do not add". Return input |
| 72 | otherwise to add the file. |
| 73 | """ |
| 74 | if tarinfo.name.endswith('.pyc'): |
| 75 | print(f'Skip {tarinfo.name}') |
| 76 | return None |
| 77 | if '__pycache__' in tarinfo.name: |
| 78 | print(f'Skip {tarinfo.name}') |
| 79 | return None |
| 80 | print(f'Adding {tarinfo.name}') |
| 81 | return tarinfo |
| 82 | |
| 83 | |
| 84 | def create_tarball(): |
| 85 | """Create tarball, return its full path.""" |
| 86 | name, version, tarball_file = get_spec_info() |
| 87 | tarball_path = join(DIST_DIR, tarball_file) |
| 88 | print(f'Creating {tarball_path}') |
| 89 | dir_name = f'{name}-{version}' |
| 90 | with tarfile.open(tarball_path, 'w:gz') as tarball: |
| 91 | for entry in DATA: |
| 92 | tarball.add(entry, join(dir_name, entry), filter=tar_add_filter) |
| 93 | return tarball_path |
| 94 | |
| 95 | |
| 96 | def build_rpm(tarball_path): |
| 97 | """Create rpm using rpmbuild with spec file and newly created tarball.""" |
| 98 | copy(tarball_path, RPM_SOURCE_DIR) |
| 99 | run(('rpmbuild', '-bb', SPEC_FILE)) |
| 100 | |
| 101 | |
| 102 | def main(): |
| 103 | """ |
| 104 | Main function, called when running file as script. |
| 105 | |
| 106 | See module doc for more info. |
| 107 | """ |
| 108 | tarball_path = create_tarball() |
| 109 | build_rpm(tarball_path) |
| 110 | print(f'{basename(__file__)} finished successfully.') |
| 111 | |
| 112 | return 0 |
| 113 | |
| 114 | |
| 115 | if __name__ == '__main__': |
| 116 | sys.exit(main()) |