/paks-dl/pak_utils.py

https://bitbucket.org/MateuszBoryckiHG/magiccasino-buildscripts
Python | 126 lines | 93 code | 21 blank | 12 comment | 24 complexity | 63f69008206aa4f4e2ec055d96d106d3 MD5 | raw file
  1. #!/usr/bin/env python3
  2. import os
  3. import glob
  4. import subprocess
  5. import pathlib
  6. import hashlib
  7. PAKTOOL = os.environ['CLAW_ROOT'] + "/bin/claw-paktool"
  8. _pakCreatedCallback = None
  9. def onPakCreated(func):
  10. """Register callback which is called when pakTool creates pak file"""
  11. global _pakCreatedCallback
  12. _pakCreatedCallback = func
  13. def callCommand(args, stdin=""):
  14. """Launch given process with optional stdin input"""
  15. startupinfo = None
  16. if os.name == 'nt':
  17. startupinfo = subprocess.STARTUPINFO()
  18. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  19. p = subprocess.Popen(args,
  20. stdout=subprocess.PIPE,
  21. stdin=subprocess.PIPE,
  22. stderr=subprocess.STDOUT,
  23. startupinfo=startupinfo)
  24. grep_stdout = p.communicate(input=bytes(stdin, 'ascii'))[0]
  25. print(grep_stdout.decode())
  26. def md5(fname):
  27. """Calculates md5 for given file"""
  28. hash_md5 = hashlib.md5()
  29. with open(fname, "rb") as f:
  30. for chunk in iter(lambda: f.read(4096*10), b""):
  31. hash_md5.update(chunk)
  32. return hash_md5.hexdigest()
  33. def listFilesExtended(path, fileFilterFunc=None, folderFilterFunc=None, maxdepth=None):
  34. """Recursively lists all files under path that match criteria"""
  35. files = []
  36. folder_stack = [(pathlib.Path(path), 0)]
  37. while folder_stack:
  38. current_dir, depth = folder_stack.pop(0)
  39. depth = depth + 1
  40. for file in current_dir.iterdir():
  41. if file.is_dir():
  42. if not folderFilterFunc or folderFilterFunc(file):
  43. if not maxdepth or depth < maxdepth:
  44. folder_stack.append((file, depth))
  45. else:
  46. if not fileFilterFunc or fileFilterFunc(file):
  47. files.append(file.as_posix())
  48. return files
  49. def listFiles(path, includedExtensions=[], excludedExtensions=[], excludedFolders=[], excludedSubfoldersOf=[], maxdepth=None):
  50. """Recursively lists all files under path that match criteria"""
  51. def _excludeExtension(file):
  52. if includedExtensions:
  53. return file.suffix in includedExtensions
  54. return not file.suffix in excludedExtensions
  55. def _excludeFolder(file):
  56. if file.stem in excludedFolders:
  57. return False
  58. if excludedSubfoldersOf and file.parent.stem in excludedSubfoldersOf:
  59. return False
  60. return True
  61. return listFilesExtended(path,
  62. fileFilterFunc=_excludeExtension,
  63. folderFilterFunc=_excludeFolder,
  64. maxdepth=maxdepth)
  65. def globFiles(path):
  66. """Return a possibly-empty list of posix styled path names that match pathname,
  67. which must be a string containing a path specification.
  68. path can contain shell-style wildcards."""
  69. files = pathlib.Path('.').glob(path)
  70. return [file.as_posix() for file in files]
  71. def pakTool(output, files, stripPath=None):
  72. """Calls pakTool to create output pak file from given files"""
  73. if stripPath:
  74. args = [PAKTOOL, "-s", str(stripPath), output]
  75. else:
  76. args = [PAKTOOL, output]
  77. filelist = "\n".join(files)
  78. callCommand(args, filelist)
  79. if _pakCreatedCallback:
  80. _pakCreatedCallback(output, files)
  81. def pakFolder(output, path, includedExtensions=[], excludedExtensions=[], excludedFolders=[], excludedSubfoldersOf=[], maxdepth=None, stripPath=None, skipIfEmpty=False):
  82. """Creates pak file from all files under path that match criteria"""
  83. files = listFiles(path,
  84. maxdepth=maxdepth,
  85. includedExtensions=includedExtensions,
  86. excludedExtensions=excludedExtensions,
  87. excludedFolders=excludedFolders,
  88. excludedSubfoldersOf=excludedSubfoldersOf)
  89. if not files and skipIfEmpty:
  90. return None
  91. return pakTool(output, files, stripPath=stripPath)
  92. def paksFromSubfolders(rootDir, stripPathFromPakName, stripPath, skipIfEmpty=False, includedExtensions=[], pakPrefix=""):
  93. """Create separate pak files from each folder under rootDir (includes rootDir)"""
  94. subfolders = [rootDir.as_posix()]
  95. subfolders.extend(glob.glob(rootDir.as_posix() + "/*/"))
  96. subfolders.extend(glob.glob(rootDir.as_posix() + "/**/*/", recursive=True))
  97. for subfolder in subfolders:
  98. subfolder = pathlib.Path(subfolder)
  99. pakName = pakPrefix + subfolder.relative_to(stripPathFromPakName).as_posix().replace("/", "_") + ".pak"
  100. pakFolder(pakName,
  101. subfolder.as_posix() + "/",
  102. stripPath=stripPath,
  103. maxdepth=1,
  104. skipIfEmpty=skipIfEmpty,
  105. includedExtensions=includedExtensions)