Skip to content

Commit

Permalink
Merge pull request #127 from SaltieRL/rattletrapdownload
Browse files Browse the repository at this point in the history
Rattletrapdownload
  • Loading branch information
dtracers committed May 4, 2019
2 parents 7ed5275 + bd9bb0c commit f6b203b
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ exclude_lines =
if __name__ == .__main__.:
omit =
tests/*
carball/tests/*
utils/tests/*
init.py
setup.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ venv/
output
.env
.coverage
rattletrap*
*.iml
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ include requirements.txt
include carball/rattletrap/*
include carball/analysis/*
exclude carball/generated/*
exclude carball/rattletrap/rattletrap*
recursive-include carball/generated/api *.py
recursive-include carball *.xlsx
12 changes: 0 additions & 12 deletions carball/decompile_replays.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import json
import os
import subprocess
import logging
import platform as pt

Expand All @@ -25,16 +23,6 @@ def decompile_replay(replay_path, output_path: str=None, overwrite=True):
:param overwrite: True if we should recreate the json even if it already exists.
:return: The json created from rattle trap.
"""
binaries = [f for f in os.listdir(os.path.join(BASE_DIR, 'rattletrap')) if not f.endswith('.py')]
platform = pt.system()
if platform == 'Windows':
binary = [f for f in binaries if f.endswith('.exe')][0]
elif platform == 'Linux':
binary = [f for f in binaries if 'linux' in f][0]
elif platform == 'Darwin':
binary = [f for f in binaries if 'osx' in f][0]
else:
raise Exception('Unknown platform, unable to process replay file.')
return run_rattletrap.decompile_replay(replay_path, output_path)


Expand Down
58 changes: 37 additions & 21 deletions carball/rattletrap/check_rattletrap_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,40 @@
import json
from distutils.version import StrictVersion

response = request.urlopen('https://api.github.com/repos/tfausak/rattletrap/releases/latest')

js = json.loads(response.read())

cur_ver = '0.0.0'
binaries = [f for f in os.listdir('.') if not f.endswith('.py') and os.path.isfile(f)]
print (binaries)
if len(binaries) > 0:
cur_ver = binaries[0].split('-')[1]
update = StrictVersion(js['name']) > StrictVersion(cur_ver)
print (f'GitHub version: {js["name"]}\n'
f'Current version: {cur_ver}\n'
f'Update? {update}')
if update:
for file in binaries:
os.remove(file)

for asset in js['assets']:

print('Downloading {}'.format(asset['name']))
request.urlretrieve(asset['browser_download_url'], asset['name'])
from carball.rattletrap.rattletrap_utils import get_rattletrap_binaries, get_rattletrap_path


def update_rattletrap():
path = get_rattletrap_path()
print('updating rattletrap in path', path)
files = os.listdir(path)
print('files in path', files)

cur_ver = '0.0.0'
binaries = get_rattletrap_binaries(path)
print('existing found', binaries)

response = request.urlopen('https://api.github.com/repos/tfausak/rattletrap/releases/latest')

js = json.loads(response.read())

if len(binaries) > 0:
cur_ver = binaries[0].split('-')[1]
update = StrictVersion(js['name']) > StrictVersion(cur_ver)
print (f'GitHub version: {js["name"]}\n'
f'Current version: {cur_ver}\n'
f'Update? {update}')
if update:
for file in binaries:
os.remove(file)

for asset in js['assets']:
print('Downloading {}'.format(asset['name']))
new_file = os.path.join(path, asset['name'])
request.urlretrieve(asset['browser_download_url'], filename=new_file)
print('making file executable', new_file)
os.chmod(new_file, 0o777)


if __name__ == "__main__":
update_rattletrap()
Binary file removed carball/rattletrap/rattletrap-6.3.0-linux
Binary file not shown.
Binary file removed carball/rattletrap/rattletrap-6.3.0-osx
Binary file not shown.
Binary file removed carball/rattletrap/rattletrap-6.3.0-windows.exe
Binary file not shown.
34 changes: 34 additions & 0 deletions carball/rattletrap/rattletrap_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import inspect
import os
import fnmatch
from typing import List, Union


def get_rattletrap_path() -> Union[bytes, str]:
filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
return path


def get_rattletrap_binaries(path: Union[bytes, str]) -> List[Union[bytes, str]]:
files = os.listdir(path)
binaries = [f for f in files if not f.endswith('.py') and fnmatch.fnmatch(f, "*rattletrap*")]
print(binaries)
return binaries


def download_rattletrap():
from carball.rattletrap.check_rattletrap_version import update_rattletrap
update_rattletrap()


def get_binary_for_platform(platform, binaries):
if platform == 'Windows':
binary = [f for f in binaries if f.endswith('.exe')][0]
elif platform == 'Linux':
binary = [f for f in binaries if 'linux' in f][0]
elif platform == 'Darwin':
binary = [f for f in binaries if 'osx' in f][0]
else:
raise Exception('Unknown platform, unable to process replay file.')
return binary
26 changes: 14 additions & 12 deletions carball/rattletrap/run_rattletrap.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import inspect
import logging
import os
import subprocess
import json
import platform as pt
from typing import List

from carball.rattletrap.rattletrap_utils import get_rattletrap_binaries, download_rattletrap, get_rattletrap_path, \
get_binary_for_platform

logger = logging.getLogger(__name__)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


def create_rattletrap_command(replay_path: str, output_path: str, overwrite: bool=True) -> List[str]:
Expand All @@ -18,17 +21,16 @@ def create_rattletrap_command(replay_path: str, output_path: str, overwrite: boo
:param overwrite: True if we should recreate the json even if it already exists.
:return: The json created from rattle trap.
"""
binaries = [f for f in os.listdir(os.path.join(BASE_DIR, 'rattletrap')) if not f.endswith('.py')]
platform = pt.system()
if platform == 'Windows':
binary = [f for f in binaries if f.endswith('.exe')][0]
elif platform == 'Linux':
binary = [f for f in binaries if 'linux' in f][0]
elif platform == 'Darwin':
binary = [f for f in binaries if 'osx' in f][0]
else:
raise Exception('Unknown platform, unable to process replay file.')
cmd = [os.path.join(os.path.join(BASE_DIR, 'rattletrap'), '{}'.format(binary)), '--compact', '-i',
rattletrap_path = get_rattletrap_path()
binaries = get_rattletrap_binaries(rattletrap_path)
if len(binaries) == 0:
logger.warning("Need to redownload rattletrap")
download_rattletrap()
binaries = get_rattletrap_binaries(rattletrap_path)
binary = get_binary_for_platform(pt.system(), binaries)
if binary is None:
print('no binary!')
cmd = [os.path.join(rattletrap_path, '{}'.format(binary)), '--compact', '-i',
replay_path]
if output_path:
output_dirs = os.path.dirname(output_path)
Expand Down
7 changes: 3 additions & 4 deletions carball/tests/game_creation_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import unittest

from carball.analysis.analysis_manager import AnalysisManager
from carball.analysis.constants.playlist import get_playlist_from_game, get_team_size_from_game

from ..json_parser.game import Game
from ..tests.utils import run_tests_on_list, run_analysis_test_on_replay, get_raw_replays
from carball.json_parser.game import Game
from carball.tests.utils import run_tests_on_list, run_analysis_test_on_replay, get_raw_replays

from .. import decompile_replays
from carball import decompile_replays


class DBTest(unittest.TestCase):
Expand Down
41 changes: 41 additions & 0 deletions carball/tests/setup_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import unittest

from carball.rattletrap.rattletrap_utils import get_rattletrap_binaries, get_rattletrap_path, download_rattletrap
from carball.rattletrap.run_rattletrap import create_rattletrap_command
from carball.rattletrap.check_rattletrap_version import update_rattletrap


class setup_tests(unittest.TestCase):

def cleanup(self):
path = get_rattletrap_path()
binaries = get_rattletrap_binaries(get_rattletrap_path())

# clean out existing
for binary in binaries:
os.remove(os.path.join(path, binary))

def test_rattle(self):
self.cleanup()

download_rattletrap()

def test_create_rattletrap_command(self):
self.cleanup()

create_rattletrap_command('file.replay', 'outputdir')

# check post download
create_rattletrap_command('file.replay', 'outputdir2')

def test_direct_check_version(self):
self.cleanup()
update_rattletrap()

#skip update
update_rattletrap()

def test_get_correct_version_from_platform(self):
path = get_rattletrap_path()
binaries = get_rattletrap_binaries(get_rattletrap_path())
11 changes: 9 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ coverage:
range: "70...100"

status:
project: yes
patch: yes
project:
default:
threshold: 1
carball:
threshold: 0.5
paths:
- "!carball/tests"
- carball/*
patch: off
changes: no

parsers:
Expand Down
18 changes: 14 additions & 4 deletions init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from utils.create_proto import create_proto_files
from utils.import_fixer import convert_to_relative_imports
def initalize_rattletrap():
from carball.rattletrap.check_rattletrap_version import update_rattletrap
try:
update_rattletrap()
except e:
print('Issue adding rattletrap')


if __name__ == "__main__":
def initialize_project():
from utils.create_proto import create_proto_files
from utils.import_fixer import convert_to_relative_imports

create_proto_files()
convert_to_relative_imports()
initalize_rattletrap()

if __name__ == "__main__":
initialize_project()
26 changes: 25 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import setuptools
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
import os

with open(os.path.join('carball', 'analysis', 'PROTOBUF_VERSION'), 'r') as f:
Expand All @@ -16,6 +18,24 @@
else:
long_description = ''


class PostDevelopCommand(develop):
"""Post-installation for development mode."""
def run(self):
from init import initialize_project
initialize_project()
# this needs to be last
develop.run(self)

class PostInstallCommand(install):
"""Post-installation for installation mode."""
def run(self):
from init import initialize_project
initialize_project()
# this needs to be last
install.run(self)


setup(
name='carball',
version=version_string,
Expand All @@ -29,5 +49,9 @@
author_email='[email protected]',
description='Rocket League replay parsing and analysis.',
long_description=long_description,
exclude_package_data={'': ['.gitignore', '.git/*', '.git/**/*', 'replays/*']}
exclude_package_data={'': ['.gitignore', '.git/*', '.git/**/*', 'replays/*']},
cmdclass={
'develop': PostDevelopCommand,
'install': PostInstallCommand,
},
)
7 changes: 7 additions & 0 deletions utils/tests/test_create_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ def test_is_not_windows(self, is_windows):

protoc_path = get_proto()
self.assertTrue(protoc_path.endswith('protoc'))

def test_create_do_protos(self):
from utils.create_proto import create_proto_files
from utils.import_fixer import convert_to_relative_imports

create_proto_files()
convert_to_relative_imports()

0 comments on commit f6b203b

Please sign in to comment.