Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ __pycache__/
bluezip.db
flashpoint.sqlite
DB_PATH
.venv
Binary file added bin/7za
Binary file not shown.
Binary file added bin/Compress.pdb
Binary file not shown.
Binary file added bin/RVIO.pdb
Binary file not shown.
Binary file added bin/TrrntZip.NET.pdb
Binary file not shown.
Binary file added bin/Trrntzip.pdb
Binary file not shown.
Binary file added bin/trrntzip
Binary file not shown.
35 changes: 15 additions & 20 deletions bluezip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import re
import os

import yaml
from ruamel.yaml import YAML
import bluezip_dat
import bluezip_hook
from util import TermColor, pcolor
Expand Down Expand Up @@ -71,7 +71,6 @@ def __iter__(self):
class Game:
uid: str
title: str
platform: str
content_path: str

def game_from_curation(uid, curation):
Expand All @@ -84,30 +83,29 @@ def game_from_curation(uid, curation):

with open(meta, 'r', encoding='utf-8') as f:
try:
meta = yaml.safe_load(f)
yaml = YAML(typ="safe")
meta: dict = yaml.load(f)
except yaml.YAMLError as e:
raise ValueError('Malformed metadata') from e
if not 'Title' in meta or not 'Platform' in meta:
if not 'Title' in meta:
raise ValueError('Incomplete metadata')
return Game(uid, meta['Title'], meta['Platform'], content)
return Game(uid, meta['Title'], content)

def game_from_fp_database(uid, content_path):
fp_db = util.open_db()
c = fp_db.cursor()
c.execute('SELECT title, platform FROM game WHERE id = ?', (uid,))
c.execute('SELECT title FROM game WHERE id = ?', (uid,))
game = c.fetchone()
fp_db.close()
if not game:
raise ValueError(f'No game found by UUID: {uid}')

title, platform = game
return Game(uid, title, platform, content_path)
return Game(uid, game[0], content_path)

def create_torrentzip(uid, platform, build_dir, dist_file):
def create_torrentzip(uid, build_dir, dist_file):
content_meta = {
'version': 1,
'uniqueId': uid,
'platform': platform
'uniqueId': uid
}
with open(os.path.join(build_dir, 'content.json'), 'w', encoding='utf-8', newline='\r\n') as f:
json.dump(content_meta, f, indent=4)
Expand Down Expand Up @@ -196,7 +194,7 @@ def process_game(self, game):
revision, prev_sha256, prev_title = c.fetchone() or (1, None, None)
os.mkdir(build_dir)
shutil.move(game.content_path, os.path.join(build_dir, 'content'))
sha256 = create_torrentzip(game.uid, game.platform, build_dir, dist)
sha256 = create_torrentzip(game.uid, build_dir, dist)
outfile = os.path.join(DIST_DIR, f'{game.uid}.zip')
shutil.move(dist, outfile)
if prev_sha256:
Expand All @@ -218,7 +216,7 @@ def process_game(self, game):
if prev_title and prev_title != game.title:
pcolor('yellow', f'Warning: {game.uid} has been renamed ({prev_title} -> {game.title})')
try:
self.db.execute('INSERT INTO game VALUES (?,?,?,?,?,?)', (game.uid, revision, sha256, game.title, game.platform, self.session))
self.db.execute('INSERT INTO game VALUES (?,?,?,?,?,?)', (game.uid, revision, sha256, game.title, "unused", self.session))
except sqlite3.IntegrityError as e:
pcolor('red', f'Error: {e} when storing {game.title}. Skipped.')
return
Expand All @@ -228,6 +226,7 @@ def process_game(self, game):
self.cleanup_obsolete(game, sha256)

def process_game_from_path(self, uid, path, from_db):
print('proccing')
if from_db:
game = game_from_fp_database(uid, path)
else:
Expand All @@ -237,19 +236,13 @@ def process_game_from_path(self, uid, path, from_db):
def process_archive(self, fname, from_db=False):
tmp = tempfile.mkdtemp()
path = os.path.join(tmp, 'curation')
subprocess.check_call(['bin/7za', 'x', f'-o{path}', fname], stdout=subprocess.DEVNULL)
subprocess.check_call(['./bin/7za', 'x', f'-o{path}', fname], stdout=subprocess.DEVNULL)
entries = os.listdir(path)
if len(entries) == 1: # must be root folder
uid = entries[0]
if not util.validate_uuid(uid):
pcolor('red', f'\nError: Root folder in {fname} not a valid UUID. Skipped.')
return
path = os.path.join(path, uid)
else:
uid, _ = os.path.splitext(os.path.basename(fname))
if not util.validate_uuid(uid):
pcolor('red', f'\nError: No root folder in {fname} and archive filename not a valid UUID. Skipped.')
return
if util.is_gamezip(entries):
path = os.path.join(path, 'content')
try:
Expand All @@ -258,6 +251,7 @@ def process_archive(self, fname, from_db=False):
shutil.rmtree(tmp)

def process_auto(self, path, from_db=False):
print('auto')
kind = 'Content' if from_db else 'Curation'
fname = os.path.basename(path)
exts = self.settings['archive_extensions'].split(',')
Expand Down Expand Up @@ -331,6 +325,7 @@ def log_session(operation):
except FileExistsError:
pass

print(args.path)
bluezip = Bluezip(db, settings, session, args)
if args.path:
if args.batch:
Expand Down
9 changes: 4 additions & 5 deletions bluezip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ def test_incomplete_meta(self):
def test_integrity(self):
self.mkdir('content')
title = 'Alien Hominid'
platform = 'Flash'
meta = { 'Title': title, 'Platform': platform }
expect = bluezip.Game(RANDOM_UUID, title, platform, self.join('content'))
meta = { 'Title': title }
expect = bluezip.Game(RANDOM_UUID, title, self.join('content'))
with open(self.join('meta.yaml'), 'w') as f:
yaml.dump(meta, f)
game = bluezip.game_from_curation(RANDOM_UUID, self.cwd)
Expand All @@ -82,7 +81,7 @@ def test_ok(self, util):
util.open_db.return_value = self.db
args = (RANDOM_UUID, 'Alien Hominid', 'Flash')
self.db.execute('INSERT INTO game VALUES (?,?,?)', args)
expect = bluezip.Game(*args, '')
expect = bluezip.Game(*args[:-1], '')
self.assertEqual(expect, bluezip.game_from_fp_database(RANDOM_UUID, ''))

class Test_create_torrentzip(TestTempFile):
Expand All @@ -94,7 +93,7 @@ def test_ok(self):
self.write('game.swf')
self.write('assets/data.xml')
self.mkdir('music')
digest = bluezip.create_torrentzip(RANDOM_UUID, 'Flash', build, dist)
digest = bluezip.create_torrentzip(RANDOM_UUID, build, dist)
self.assertEqual(expect, digest.hex())

if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pyyaml
ruamel.yaml
lxml