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