| 538 | """ |
| 539 | |
| 540 | class NSISScript(object): |
| 541 | def create(self): |
| 542 | fileList, totalSize = self.getBuildDirContents(OUT_DIR) |
| 543 | print("Total size eq: {}".format(totalSize)) |
| 544 | installFiles = self.prepareInstallListTemplate(fileList) |
| 545 | uninstallFiles = self.prepareDeleteListTemplate(fileList) |
| 546 | |
| 547 | if os.path.isfile(SETUP_SCRIPT_PATH): |
| 548 | raise RuntimeError("Cannot create setup script, file exists at {}".format(SETUP_SCRIPT_PATH)) |
| 549 | contents = Template(NSIS_SCRIPT_TEMPLATE).substitute( |
| 550 | version=syncplay.version, |
| 551 | uninstallFiles=uninstallFiles, |
| 552 | installFiles=installFiles, |
| 553 | totalSize=totalSize, |
| 554 | ) |
| 555 | with codecs.open(SETUP_SCRIPT_PATH, "w", "utf-8-sig") as outfile: |
| 556 | outfile.write(contents) |
| 557 | |
| 558 | def compile(self): |
| 559 | if not os.path.isfile(NSIS_COMPILE): |
| 560 | return "makensis.exe not found, won't create the installer" |
| 561 | subproc = subprocess.Popen([NSIS_COMPILE, SETUP_SCRIPT_PATH], env=os.environ) |
| 562 | subproc.communicate() |
| 563 | retcode = subproc.returncode |
| 564 | os.remove(SETUP_SCRIPT_PATH) |
| 565 | if retcode: |
| 566 | raise RuntimeError("NSIS compilation return code: %d" % retcode) |
| 567 | |
| 568 | def getBuildDirContents(self, path): |
| 569 | fileList = {} |
| 570 | totalSize = 0 |
| 571 | for root, _, files in os.walk(path): |
| 572 | totalSize += sum(os.path.getsize(os.path.join(root, file_)) for file_ in files) |
| 573 | for file_ in files: |
| 574 | new_root = root.replace(OUT_DIR, "").strip("\\") |
| 575 | if new_root not in fileList: |
| 576 | fileList[new_root] = [] |
| 577 | fileList[new_root].append(file_) |
| 578 | return fileList, totalSize |
| 579 | |
| 580 | def prepareInstallListTemplate(self, fileList): |
| 581 | create = [] |
| 582 | for dir_ in fileList.keys(): |
| 583 | create.append('SetOutPath "$INSTDIR\\{}"'.format(dir_)) |
| 584 | for file_ in fileList[dir_]: |
| 585 | create.append('FILE "{}\\{}\\{}"'.format(OUT_DIR, dir_, file_)) |
| 586 | return "\n".join(create) |
| 587 | |
| 588 | def prepareDeleteListTemplate(self, fileList): |
| 589 | delete = [] |
| 590 | for dir_ in fileList.keys(): |
| 591 | for file_ in fileList[dir_]: |
| 592 | delete.append('DELETE "$INSTDIR\\{}\\{}"'.format(dir_, file_)) |
| 593 | delete.append('RMdir "$INSTDIR\\{}"'.format(file_)) |
| 594 | return "\n".join(delete) |
| 595 | |
| 596 | def pruneUnneededLibraries(): |
| 597 | from pathlib import Path |