Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fireundubh committed Jun 13, 2021
2 parents 81a6606 + 5f031e6 commit daa688a
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 169 deletions.
20 changes: 11 additions & 9 deletions pyro/Anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import os
import random
import string
import sys

from pyro.PexHeader import PexHeader
from pyro.PexReader import PexReader

from pyro.Comparators import endswith


class Anonymizer:
log: logging.Logger = logging.getLogger('pyro')

Expand All @@ -25,7 +27,7 @@ def anonymize_script(path: str) -> None:
header: PexHeader = PexReader.get_header(path)
except ValueError:
Anonymizer.log.error(f'Cannot anonymize script due to unknown file magic: "{path}"')
return
sys.exit(1)

file_path: str = header.script_path.value
user_name: str = header.user_name.value
Expand All @@ -36,20 +38,20 @@ def anonymize_script(path: str) -> None:
return

if not endswith(file_path, '.psc', ignorecase=True):
Anonymizer.log.warning(f'Cannot anonymize script due to invalid file extension: "{path}"')
return
Anonymizer.log.error(f'Cannot anonymize script due to invalid file extension: "{path}"')
sys.exit(1)

if not len(file_path) > 0:
Anonymizer.log.warning(f'Cannot anonymize script due to zero-length file path: "{path}"')
return
Anonymizer.log.error(f'Cannot anonymize script due to zero-length file path: "{path}"')
sys.exit(1)

if not len(user_name) > 0:
Anonymizer.log.warning(f'Cannot anonymize script due to zero-length user name: "{path}"')
return
Anonymizer.log.error(f'Cannot anonymize script due to zero-length user name: "{path}"')
sys.exit(1)

if not len(computer_name) > 0:
Anonymizer.log.warning(f'Cannot anonymize script due to zero-length computer name: "{path}"')
return
Anonymizer.log.error(f'Cannot anonymize script due to zero-length computer name: "{path}"')
sys.exit(1)

with open(path, mode='r+b') as f:
f.seek(header.script_path.offset, os.SEEK_SET)
Expand Down
56 changes: 32 additions & 24 deletions pyro/Application.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,16 @@ def _try_fix_input_path(input_path: str) -> str:

if not os.path.isabs(input_path):
cwd = os.getcwd()
Application.log.warning(f'Using working directory: "{cwd}"')
Application.log.info(f'Using working directory: "{cwd}"')

input_path = os.path.join(cwd, input_path)

Application.log.warning(f'Using input path: "{input_path}"')
Application.log.info(f'Using input path: "{input_path}"')

return input_path

@staticmethod
def _validate_project(ppj: PapyrusProject) -> None:
compiler_path = ppj.get_compiler_path()
if not compiler_path or not os.path.isfile(compiler_path):
Application.log.error('Cannot proceed without compiler path')
sys.exit(1)

flags_path = ppj.get_flags_path()
if not flags_path:
Application.log.error('Cannot proceed without flags path')
sys.exit(1)

if not ppj.options.game_type:
Application.log.error('Cannot determine game type from arguments or Papyrus Project')
sys.exit(1)

def _validate_project_file(ppj: PapyrusProject):
if not ppj.has_imports_node:
Application.log.error('Cannot proceed without imports defined in project')
sys.exit(1)
Expand All @@ -86,6 +72,22 @@ def _validate_project(ppj: PapyrusProject) -> None:
Application.log.error('Cannot proceed with Zip enabled without ZipFile defined in project')
sys.exit(1)

@staticmethod
def _validate_project_paths(ppj: PapyrusProject) -> None:
compiler_path = ppj.get_compiler_path()
if not compiler_path or not os.path.isfile(compiler_path):
Application.log.error('Cannot proceed without compiler path')
sys.exit(1)

flags_path = ppj.get_flags_path()
if not flags_path:
Application.log.error('Cannot proceed without flags path')
sys.exit(1)

if not ppj.options.game_type:
Application.log.error('Cannot determine game type from arguments or Papyrus Project')
sys.exit(1)

if not os.path.isabs(flags_path) and \
not any([os.path.isfile(os.path.join(import_path, flags_path)) for import_path in ppj.import_paths]):
Application.log.error('Cannot proceed without flags file in any import folder')
Expand All @@ -107,6 +109,9 @@ def run(self) -> int:

options = ProjectOptions(self.args.__dict__)
ppj = PapyrusProject(options)

self._validate_project_file(ppj)

ppj.try_initialize_remotes()

ppj.try_import_event(ImportEvent.PRE)
Expand All @@ -117,7 +122,7 @@ def run(self) -> int:
ppj.find_missing_scripts()
ppj.try_set_game_path()

self._validate_project(ppj)
self._validate_project_paths(ppj)

Application.log.info('Imports found:')
for path in ppj.import_paths:
Expand All @@ -142,25 +147,28 @@ def run(self) -> int:
if build.failed_count == 0 or ppj.options.ignore_errors:
build.try_anonymize()
else:
Application.log.warning(f'Cannot anonymize scripts because {build.failed_count} scripts failed to compile')
Application.log.error(f'Cannot anonymize scripts because {build.failed_count} scripts failed to compile')
sys.exit(build.failed_count)
else:
Application.log.warning('Cannot anonymize scripts because Anonymize is disabled in project')
Application.log.info('Cannot anonymize scripts because Anonymize is disabled in project')

if ppj.options.package:
if build.failed_count == 0 or ppj.options.ignore_errors:
build.try_pack()
else:
Application.log.warning(f'Cannot create Packages because {build.failed_count} scripts failed to compile')
Application.log.error(f'Cannot create Packages because {build.failed_count} scripts failed to compile')
sys.exit(build.failed_count)
else:
Application.log.warning('Cannot create Packages because Package is disabled in project')
Application.log.info('Cannot create Packages because Package is disabled in project')

if ppj.options.zip:
if build.failed_count == 0 or ppj.options.ignore_errors:
build.try_zip()
else:
Application.log.warning(f'Cannot create ZipFile because {build.failed_count} scripts failed to compile')
Application.log.error(f'Cannot create ZipFile because {build.failed_count} scripts failed to compile')
sys.exit(build.failed_count)
else:
Application.log.warning('Cannot create ZipFile because Zip is disabled in project')
Application.log.info('Cannot create ZipFile because Zip is disabled in project')

Application.log.info(build.build_time if build.success_count > 0 else 'No scripts were compiled.')

Expand Down
8 changes: 4 additions & 4 deletions pyro/BuildFacade.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def _find_modified_scripts(self) -> list:
try:
header = PexReader.get_header(pex_path)
except ValueError:
BuildFacade.log.warning(f'Cannot determine compilation time due to unknown magic: "{pex_path}"')
continue
BuildFacade.log.error(f'Cannot determine compilation time due to unknown magic: "{pex_path}"')
sys.exit(1)

psc_last_modified: float = os.path.getmtime(script_path)
pex_last_compiled: float = float(header.compilation_time.value)
Expand Down Expand Up @@ -132,8 +132,8 @@ def try_anonymize(self) -> None:
# these are absolute paths. there's no reason to manipulate them.
for pex_path in self.ppj.pex_paths:
if not os.path.isfile(pex_path):
BuildFacade.log.warning(f'Cannot locate file to anonymize: "{pex_path}"')
continue
BuildFacade.log.error(f'Cannot locate file to anonymize: "{pex_path}"')
sys.exit(1)

Anonymizer.anonymize_script(pex_path)

Expand Down
4 changes: 4 additions & 0 deletions pyro/Comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def is_include_node(node: etree.ElementBase) -> bool:
return node is not None and endswith(node.tag, 'Include') and node.text is not None


def is_match_node(node: etree.ElementBase) -> bool:
return node is not None and endswith(node.tag, 'Match') and node.text is not None


def is_package_node(node: etree.ElementBase) -> bool:
return node is not None and endswith(node.tag, 'Package')

Expand Down
3 changes: 3 additions & 0 deletions pyro/Constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class XmlAttributeName:
ANONYMIZE: str = 'Anonymize'
COMPRESSION: str = 'Compression'
DESCRIPTION: str = 'Description'
EXCLUDE: str = 'Exclude'
FINAL: str = 'Final'
FLAGS: str = 'Flags'
GAME: str = 'Game'
IN: str = 'In'
NAME: str = 'Name'
NO_RECURSE: str = 'NoRecurse'
OPTIMIZE: str = 'Optimize'
Expand All @@ -49,6 +51,7 @@ class XmlTagName:
IMPORT: str = 'Import'
IMPORTS: str = 'Imports'
INCLUDE: str = 'Include'
MATCH: str = 'Match'
PACKAGE: str = 'Package'
PACKAGES: str = 'Packages'
PAPYRUS_PROJECT: str = 'PapyrusProject'
Expand Down
Loading

0 comments on commit daa688a

Please sign in to comment.