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