patch.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from dataclasses import dataclass
  2. from pathlib import Path
  3. import os
  4. import stat
  5. import subprocess
  6. from typing import Optional
  7. from .config import Arch, LinuxTag
  8. from ..utils.deps import CACHE_DIR
  9. from ..utils.log import debug, log
  10. from ..utils.url import urlretrieve
  11. @dataclass(frozen=True)
  12. class Patcher:
  13. '''Manylinux tag.'''
  14. tag: LinuxTag
  15. '''Platform architecture.'''
  16. arch: Optional[Arch] = None
  17. def patch(self, destination: Path):
  18. '''Apply any patch'''
  19. cache = Path(CACHE_DIR) / f'share/patches/'
  20. if self.tag == LinuxTag.MANYLINUX_1:
  21. patch = f'tk-manylinux1_{self.arch}'
  22. log('PATCH', patch)
  23. tarfile = f'{patch}.tar.gz'
  24. path = cache / tarfile
  25. if not path.exists():
  26. url = f'https://github.com/niess/python-appimage/releases/download/manylinux1/{tarfile}'
  27. urlretrieve(url, path)
  28. mode = os.stat(path)[stat.ST_MODE]
  29. os.chmod(path, mode | stat.S_IWGRP | stat.S_IWOTH)
  30. debug('EXTRACT', tarfile)
  31. cmd = ''.join((
  32. f'trap \'chmod u+rw -R {destination}\' EXIT ; ',
  33. f'mkdir -p {destination} && ',
  34. f'tar -xzf {path} -C {destination}',
  35. ))
  36. r = subprocess.run(f'/bin/bash -c "{cmd}"', shell=True,
  37. capture_output=True)
  38. if r.returncode != 0:
  39. raise ValueError(r.stderr.decode())