1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
import os
from pathlib import Path
from subprocess import Popen, PIPE
from setuptools import setup
MODULE_NAME = 'fact_helper_file'
MIME_DIR = Path(__file__).parent / MODULE_NAME / 'mime'
class OperateInDirectory:
def __init__(self, target_directory: str):
self._current_working_dir = None
self._target_directory = target_directory
def __enter__(self):
self._current_working_dir = os.getcwd()
os.chdir(self._target_directory)
def __exit__(self, *_):
os.chdir(self._current_working_dir)
def execute_shell_command(shell_command):
with Popen(shell_command, shell=True, stdout=PIPE, stderr=PIPE) as pl:
output = pl.communicate()[0].decode('utf-8', errors='replace')
return output, pl.returncode
os.makedirs(str(MIME_DIR.parent / 'bin'), exist_ok=True)
with OperateInDirectory(str(MIME_DIR)):
file_output, file_code = execute_shell_command(
'(cat custom_* > custommime)'
' && file -C -m custommime'
' && mv -f custommime.mgc ../bin/'
' && rm custommime'
)
if file_code != 0:
exit('Failed to properly compile magic file\n{}'.format(file_output))
setup(
name=MODULE_NAME,
version='0.2',
description='Helper functions for file type generation',
author='Johannes vom Dorp',
url='https://github.com/fkie-cad/fact_helper_file',
install_requires=['python-magic'],
python_requires='>=3.5',
packages=[MODULE_NAME, ],
package_data={MODULE_NAME: [str(MIME_DIR.parent / 'bin' / 'custommime.mgc'), ]}
)