2020-09-27 04:54:49 -06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2018-05-07 03:55:38 -06:00
|
|
|
|
|
|
|
'''
|
|
|
|
A wrapper script to generate zip files for GitHub releases.
|
|
|
|
|
|
|
|
This script tends to be compatible with both Python 2 and Python 3.
|
|
|
|
'''
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
2020-09-26 14:22:47 -06:00
|
|
|
DEDRM_SRC_DIR = 'DeDRM_plugin'
|
2021-12-29 03:36:59 -07:00
|
|
|
DEDRM_SRC_TMP_DIR = 'DeDRM_plugin_temp'
|
2020-09-26 14:22:47 -06:00
|
|
|
DEDRM_README= 'DeDRM_plugin_ReadMe.txt'
|
2020-02-16 03:12:25 -07:00
|
|
|
OBOK_SRC_DIR = 'Obok_plugin'
|
2020-09-26 14:22:47 -06:00
|
|
|
OBOK_README = 'obok_plugin_ReadMe.txt'
|
2020-02-16 03:12:25 -07:00
|
|
|
RELEASE_DIR = 'release'
|
2018-05-07 03:55:38 -06:00
|
|
|
|
2021-12-29 03:36:59 -07:00
|
|
|
def patch_file(filepath):
|
|
|
|
f = open(filepath, "rb")
|
|
|
|
fn = open(filepath + ".tmp", "wb")
|
|
|
|
patch = open(os.path.join(DEDRM_SRC_DIR, "__calibre_compat_code.py"), "rb")
|
|
|
|
patchdata = patch.read()
|
|
|
|
patch.close()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if len(line) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
if line.strip().startswith(b"#@@CALIBRE_COMPAT_CODE@@"):
|
|
|
|
fn.write(patchdata)
|
|
|
|
else:
|
|
|
|
fn.write(line)
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
fn.close()
|
|
|
|
shutil.move(filepath + ".tmp", filepath)
|
|
|
|
|
|
|
|
|
2018-05-07 03:55:38 -06:00
|
|
|
|
2020-02-16 03:12:25 -07:00
|
|
|
def make_release(version):
|
2018-12-02 04:51:51 -07:00
|
|
|
try:
|
2020-02-16 03:12:25 -07:00
|
|
|
shutil.rmtree(RELEASE_DIR)
|
|
|
|
except:
|
2018-12-02 04:51:51 -07:00
|
|
|
pass
|
2021-12-29 03:36:59 -07:00
|
|
|
try:
|
|
|
|
shutil.rmtree(DEDRM_SRC_TMP_DIR)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-02-16 03:12:25 -07:00
|
|
|
os.mkdir(RELEASE_DIR)
|
2021-12-29 03:36:59 -07:00
|
|
|
|
|
|
|
# Copy folder
|
|
|
|
shutil.copytree(DEDRM_SRC_DIR, DEDRM_SRC_TMP_DIR)
|
|
|
|
|
|
|
|
# Modify folder
|
|
|
|
try:
|
|
|
|
shutil.rmtree(os.path.join(os.path.abspath(DEDRM_SRC_TMP_DIR), "__pycache__"))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Patch file to add compat code.
|
|
|
|
for root, dirs, files in os.walk(DEDRM_SRC_TMP_DIR):
|
|
|
|
for name in files:
|
|
|
|
if name.endswith(".py"):
|
|
|
|
patch_file(os.path.join(root, name))
|
|
|
|
|
|
|
|
|
|
|
|
# Package
|
|
|
|
shutil.make_archive(DEDRM_SRC_DIR, 'zip', DEDRM_SRC_TMP_DIR)
|
2020-02-16 03:12:25 -07:00
|
|
|
shutil.make_archive(OBOK_SRC_DIR, 'zip', OBOK_SRC_DIR)
|
|
|
|
shutil.move(DEDRM_SRC_DIR+'.zip', RELEASE_DIR)
|
|
|
|
shutil.move(OBOK_SRC_DIR+'.zip', RELEASE_DIR)
|
|
|
|
shutil.copy(DEDRM_README, RELEASE_DIR)
|
|
|
|
shutil.copy(OBOK_README, RELEASE_DIR)
|
|
|
|
shutil.copy("ReadMe_Overview.txt", RELEASE_DIR)
|
2018-05-07 04:27:05 -06:00
|
|
|
|
2021-12-29 03:36:59 -07:00
|
|
|
# Remove temp folder:
|
|
|
|
shutil.rmtree(DEDRM_SRC_TMP_DIR)
|
|
|
|
|
2021-11-29 08:27:51 -07:00
|
|
|
if version is not None:
|
|
|
|
release_name = 'DeDRM_tools_{}'.format(version)
|
|
|
|
else:
|
|
|
|
release_name = 'DeDRM_tools'
|
2020-02-16 03:12:25 -07:00
|
|
|
result = shutil.make_archive(release_name, 'zip', RELEASE_DIR)
|
|
|
|
try:
|
|
|
|
shutil.rmtree(RELEASE_DIR)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return result
|
2018-05-07 03:55:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-05-07 04:27:05 -06:00
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
version = sys.argv[1]
|
|
|
|
except IndexError:
|
2021-11-29 08:27:51 -07:00
|
|
|
version = None
|
2018-05-07 04:27:05 -06:00
|
|
|
|
|
|
|
print(make_release(version))
|