Debian Codegraph @

setup.py

raw ◀ ▶
2 Debian patches touch this file
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """Installation and deployment script."""
4
5 import glob
6 import os
7 import sys
8
9 try:
10 from setuptools import find_packages, setup
11 except ImportError:
12 from distutils.core import find_packages, setup
13
14 try:
15 from distutils.command.bdist_rpm import bdist_rpm
16 except ImportError:
17 bdist_rpm = None
18
19 version_tuple = (sys.version_info[0], sys.version_info[1])
20 if version_tuple < (3, 7):
21 print(f'Unsupported Python version: {sys.version:s}, version 3.7 or higher '
22 f'required.')
23 sys.exit(1)
24
25 # Change PYTHONPATH to include artifacts so that we can get the version.
26 sys.path.insert(0, '.')
27
28 import artifacts # pylint: disable=wrong-import-position
29
30
31 if not bdist_rpm:
32 BdistRPMCommand = None
33 else:
34 class BdistRPMCommand(bdist_rpm):
35 """Custom handler for the bdist_rpm command."""
36
37 # pylint: disable=invalid-name
38 def _make_spec_file(self):
39 """Generates the text of an RPM spec file.
40
41 Returns:
42 list[str]: lines of the RPM spec file.
43 """
44 # Note that bdist_rpm can be an old style class.
45 if issubclass(BdistRPMCommand, object):
46 spec_file = super(BdistRPMCommand, self)._make_spec_file()
47 else:
48 spec_file = bdist_rpm._make_spec_file(self)
49
50 python_package = 'python3'
51
52 description = []
53 requires = ''
54 summary = ''
55 in_description = False
56
57 python_spec_file = []
58 for line in iter(spec_file):
59 if line.startswith('Summary: '):
60 summary = line[9:]
61
62 elif line.startswith('BuildRequires: '):
63 line = (f'BuildRequires: {python_package:s}-setuptools, '
64 f'{python_package:s}-devel')
65
66 elif line.startswith('Requires: '):
67 requires = line[10:]
68 continue
69
70 elif line.startswith('%description'):
71 in_description = True
72
73 elif line.startswith('python setup.py build'):
74 if python_package == 'python3':
75 line = '%py3_build'
76 else:
77 line = '%py2_build'
78
79 elif line.startswith('python setup.py install'):
80 if python_package == 'python3':
81 line = '%py3_install'
82 else:
83 line = '%py2_install'
84
85 elif line.startswith('%files'):
86 lines = [
87 '%files -n %{name}-data',
88 '%defattr(644,root,root,755)',
89 '%license LICENSE',
90 '%doc ACKNOWLEDGEMENTS AUTHORS README',
91 '%{_datadir}/%{name}/*',
92 '',
93 f'%files -n {python_package:s}-%{{name}}',
94 '%defattr(644,root,root,755)',
95 '%license LICENSE',
96 '%doc ACKNOWLEDGEMENTS AUTHORS README']
97
98 lines.extend([
99 '%{python3_sitelib}/artifacts/*.py',
100 '%{python3_sitelib}/artifacts*.egg-info/*',
101 '',
102 '%exclude %{_prefix}/share/doc/*',
103 '%exclude %{python3_sitelib}/artifacts/__pycache__/*'])
104
105 python_spec_file.extend(lines)
106 break
107
108 elif line.startswith('%prep'):
109 in_description = False
110
111 python_spec_file.extend([
112 '%package -n %{name}-data',
113 f'Summary: Data files for {summary:s}',
114 '',
115 '%description -n %{name}-data'])
116
117 python_spec_file.extend(description)
118
119 python_spec_file.append(f'%package -n {python_package:s}-%{{name}}')
120 python_summary = f'Python 3 module of {summary:s}'
121
122 python_spec_file.extend([
123 f'Requires: artifacts-data >= %{{version}} {requires:s}',
124 f'Summary: {python_summary:s}',
125 '',
126 f'%description -n {python_package:s}-%{{name}}'])
127
128 python_spec_file.extend(description)
129
130 python_spec_file.extend([
131 '%package -n %{name}-tools',
132 f'Requires: {python_package:s}-artifacts >= %{{version}}',
133 f'Summary: Tools for {summary:s}',
134 '',
135 '%description -n %{name}-tools'])
136
137 python_spec_file.extend(description)
138
139 elif in_description:
140 # Ignore leading white lines in the description.
141 if not description and not line:
142 continue
143
144 description.append(line)
145
146 python_spec_file.append(line)
147
148 python_spec_file.extend([
149 '',
150 '%files -n %{name}-tools',
151 '%{_bindir}/*.py'])
152
153 return python_spec_file
154
155
156 def parse_requirements_from_file(path):
157 """Parses requirements from a requirements file.
158
159 Args:
160 path (str): path to the requirements file.
161
162 Returns:
163 list[str]: name and optional version information of the required packages.
164 """
165 requirements = []
166 if os.path.isfile(path):
167 with open(path, 'r') as file_object:
168 file_contents = file_object.read()
169
•170 for name in file_contents.splitlines():
171 if not name.startswith('pip '):
172 requirements.append(name)
173
174 return requirements
175
176
177 artifacts_description = (
178 'ForensicArtifacts.com Artifact Repository.')
179
180 artifacts_long_description = (
181 'A free, community-sourced, machine-readable knowledge base of forensic '
182 'artifacts that the world can use both as an information source and within'
183 ' other tools.')
184
185 command_classes = {}
186 if BdistRPMCommand:
187 command_classes['bdist_rpm'] = BdistRPMCommand
188
189 setup(
190 name='artifacts',
191 version=artifacts.__version__,
192 description=artifacts_description,
193 long_description=artifacts_long_description,
194 long_description_content_type='text/plain',
195 license='Apache License, Version 2.0',
196 url='https://github.com/ForensicArtifacts/artifacts',
197 maintainer='Forensic artifacts',
198 maintainer_email='[email protected]',
199 cmdclass=command_classes,
200 classifiers=[
201 'Development Status :: 3 - Alpha',
202 'Environment :: Console',
203 'Operating System :: OS Independent',
204 'Programming Language :: Python',
205 ],
206 packages=find_packages('.', exclude=[
207 'docs', 'tests', 'tests.*', 'tools', 'utils']),
208 package_dir={
209 'artifacts': 'artifacts'
210 },
211 install_requires=parse_requirements_from_file('requirements.txt'),
212 tests_require=parse_requirements_from_file('test_requirements.txt'),
213 )