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 14, 2021
2 parents f76a159 + 3b621cd commit 890afde
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 124 deletions.
3 changes: 2 additions & 1 deletion pyro/BuildFacade.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def _find_modified_scripts(self) -> list:
script_name, _ = os.path.splitext(os.path.basename(script_path))

# if pex exists, compare time_t in pex header with psc's last modified timestamp
pex_match: list = [pex_path for pex_path in self.ppj.pex_paths if endswith(pex_path, f'{script_name}.pex', ignorecase=True)]
pex_match: list = [pex_path for pex_path in self.ppj.pex_paths
if endswith(pex_path, f'{script_name}.pex', ignorecase=True)]
if not pex_match:
continue

Expand Down
5 changes: 1 addition & 4 deletions pyro/CaseInsensitiveList.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ def __contains__(self, item: object) -> bool:
return item in self.data

def append(self, item: object) -> None:
if isinstance(item, str):
self.data.append(item.casefold())
else:
self.data.append(item)
self.data.append(item.casefold() if isinstance(item, str) else item)
101 changes: 50 additions & 51 deletions pyro/PackageManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,24 @@ def _check_write_permission(file_path: str) -> None:
PackageManager.log.error(f'Cannot create file without write permission to: "{file_path}"')
sys.exit(1)

@staticmethod
def _match(root_dir: str, file_pattern: str, *, exclude_pattern: str = '', user_path: str = '', no_recurse: bool = False) -> typing.Generator:
user_flags = wcmatch.RECURSIVE if not no_recurse else 0x0
matcher = wcmatch.WcMatch(root_dir, file_pattern,
exclude_pattern=exclude_pattern,
flags=PackageManager.DEFAULT_WCFLAGS | user_flags)

matcher.on_reset()
matcher._skipped = 0
for file_path in matcher._walk():
yield file_path, user_path

@staticmethod
def _generate_include_paths(includes_node: etree.ElementBase, root_path: str, zip_mode: bool = False) -> typing.Generator:
for include_node in filter(is_include_node, includes_node):
attr_no_recurse: bool = include_node.get(XmlAttributeName.NO_RECURSE) == 'True'
attr_path: str = (include_node.get(XmlAttributeName.PATH) or '').strip()
search_path: str = include_node.text.strip()
search_path: str = include_node.text

if not search_path:
PackageManager.log.error(f'Include path at line {include_node.sourceline} in project file is empty')
Expand Down Expand Up @@ -89,13 +101,9 @@ def _generate_include_paths(includes_node: etree.ElementBase, root_path: str, zi
if os.path.isfile(test_path):
yield test_path, attr_path
elif os.path.isdir(test_path):
user_flags = wcmatch.RECURSIVE if not attr_no_recurse else 0x0
matcher = wcmatch.WcMatch(test_path, '*.*', flags=wcmatch.IGNORECASE | user_flags)

matcher.on_reset()
matcher._skipped = 0
for f in matcher._walk():
yield f, attr_path
yield from PackageManager._match(test_path, '*.*',
user_path=attr_path,
no_recurse=attr_no_recurse)
else:
for include_path in glob.iglob(search_path,
root_dir=root_path,
Expand All @@ -113,36 +121,25 @@ def _generate_include_paths(includes_node: etree.ElementBase, root_path: str, zi
if os.path.isfile(search_path):
yield search_path, attr_path
else:
user_flags = wcmatch.RECURSIVE if not attr_no_recurse else 0x0

matcher = wcmatch.WcMatch(search_path, '*.*',
flags=PackageManager.DEFAULT_WCFLAGS | user_flags)

matcher.on_reset()
matcher._skipped = 0
for f in matcher._walk():
yield f, attr_path
yield from PackageManager._match(search_path, '*.*',
user_path=attr_path,
no_recurse=attr_no_recurse)

for match_node in filter(is_match_node, includes_node):
attr_in: str = match_node.get(XmlAttributeName.IN).strip()
attr_no_recurse: bool = match_node.get(XmlAttributeName.NO_RECURSE) == 'True'
attr_exclude: str = (match_node.get(XmlAttributeName.EXCLUDE) or '').strip()

if not attr_in:
PackageManager.log.error(f'Include path at line {match_node.sourceline} in project file is empty')
PackageManager.log.error(f'"In" attribute of "Match" tag at line {match_node.sourceline} in project file is empty')
sys.exit(1)

in_path: str = os.path.normpath(attr_in)

if in_path == os.curdir:
in_path = in_path.replace(os.curdir, root_path, 1)
elif in_path == os.pardir:
if in_path == os.pardir or startswith(in_path, os.pardir):
in_path = in_path.replace(os.pardir, os.path.normpath(os.path.join(root_path, os.pardir)), 1)
elif os.path.sep in os.path.normpath(in_path):
if startswith(in_path, os.pardir):
in_path = in_path.replace(os.pardir, os.path.normpath(os.path.join(root_path, os.pardir)), 1)
elif startswith(in_path, os.curdir):
in_path = in_path.replace(os.curdir, root_path, 1)
elif in_path == os.curdir or startswith(in_path, os.curdir):
in_path = in_path.replace(os.curdir, root_path, 1)

if not os.path.isabs(in_path):
in_path = os.path.join(root_path, in_path)
Expand All @@ -154,16 +151,15 @@ def _generate_include_paths(includes_node: etree.ElementBase, root_path: str, zi
PackageManager.log.error(f'Cannot match path that does not exist or is not a directory: "{in_path}"')
sys.exit(1)

user_flags = wcmatch.RECURSIVE if not attr_no_recurse else 0x0
match_text: str = match_node.text

matcher = wcmatch.WcMatch(in_path, match_node.text,
exclude_pattern=attr_exclude,
flags=PackageManager.DEFAULT_WCFLAGS | user_flags)
if startswith(match_text, '.'):
PackageManager.log.error(f'Match pattern at line {match_node.sourceline} in project file is not a valid wildcard pattern')
sys.exit(1)

matcher.on_reset()
matcher._skipped = 0
for f in matcher._walk():
yield f, attr_in
yield from PackageManager._match(in_path, match_text,
exclude_pattern=attr_exclude,
no_recurse=attr_no_recurse)

def _fix_package_extension(self, package_name: str) -> str:
if not endswith(package_name, ('.ba2', '.bsa'), ignorecase=True):
Expand Down Expand Up @@ -225,9 +221,9 @@ def create_packages(self) -> None:
attr_file_name: str = package_node.get(XmlAttributeName.NAME)

# noinspection PyProtectedMember
root_dir = self.ppj._get_path(package_node.get(XmlAttributeName.ROOT_DIR),
relative_root_path=self.ppj.project_path,
fallback_path=[self.ppj.project_path, os.path.basename(attr_file_name)])
root_dir: str = self.ppj._get_path(package_node.get(XmlAttributeName.ROOT_DIR),
relative_root_path=self.ppj.project_path,
fallback_path=[self.ppj.project_path, os.path.basename(attr_file_name)])

# prevent clobbering files previously created in this session
if attr_file_name in file_names:
Expand All @@ -244,16 +240,18 @@ def create_packages(self) -> None:

PackageManager.log.info(f'Creating "{attr_file_name}"...')

for source_path, _ in self._generate_include_paths(package_node, root_dir):
for source_path, attr_path in self._generate_include_paths(package_node, root_dir):
if os.path.isabs(source_path):
relpath = os.path.relpath(source_path, root_dir)
relpath: str = os.path.relpath(source_path, root_dir)
else:
relpath = source_path
relpath: str = source_path
source_path = os.path.join(self.ppj.project_path, source_path)

target_path = os.path.join(self.options.temp_path, relpath)
adj_relpath = os.path.normpath(os.path.join(attr_path, relpath))

PackageManager.log.info(f'+ "{adj_relpath.casefold()}"')

PackageManager.log.info(f'+ "{relpath.casefold()}"')
target_path: str = os.path.join(self.options.temp_path, adj_relpath)

# fix target path if user passes a deeper package root (RootDir)
if endswith(source_path, '.pex', ignorecase=True) and not startswith(relpath, 'scripts', ignorecase=True):
Expand Down Expand Up @@ -301,24 +299,25 @@ def create_zip(self) -> None:
except KeyError:
compress_type = ZipCompression.STORE

attr_root_dir: str = zip_node.get(XmlAttributeName.ROOT_DIR)
zip_root_path: str = self._try_resolve_project_relative_path(attr_root_dir)
root_dir: str = self.ppj._get_path(zip_node.get(XmlAttributeName.ROOT_DIR),
relative_root_path=self.ppj.project_path,
fallback_path='')

if zip_root_path:
if root_dir:
PackageManager.log.info(f'Creating "{attr_file_name}"...')

try:
with zipfile.ZipFile(file_path, mode='w', compression=compress_type.value) as z:
for include_path, user_path in self._generate_include_paths(zip_node, zip_root_path, True):
if not user_path:
if zip_root_path in include_path:
arcname = os.path.relpath(include_path, zip_root_path)
for include_path, attr_path in self._generate_include_paths(zip_node, root_dir, True):
if not attr_path:
if root_dir in include_path:
arcname = os.path.relpath(include_path, root_dir)
else:
# just add file to zip root
arcname = os.path.basename(include_path)
else:
_, attr_file_name = os.path.split(include_path)
arcname = attr_file_name if user_path == os.curdir else os.path.join(user_path, attr_file_name)
arcname = attr_file_name if attr_path == os.curdir else os.path.join(attr_path, attr_file_name)

PackageManager.log.info('+ "{}"'.format(arcname))
z.write(include_path, arcname, compress_type=compress_type.value)
Expand All @@ -328,5 +327,5 @@ def create_zip(self) -> None:
PackageManager.log.error(f'Cannot open ZIP file for writing: "{file_path}"')
sys.exit(1)
else:
PackageManager.log.error(f'Cannot resolve RootDir path to existing folder: "{attr_root_dir}"')
PackageManager.log.error(f'Cannot resolve RootDir path to existing folder: "{root_dir}"')
sys.exit(1)
Loading

0 comments on commit 890afde

Please sign in to comment.