-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.py
161 lines (128 loc) · 5.93 KB
/
init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import re
import sys
import os
import argparse
import codecs
import shutil
import requests
import hashlib
# Class defining methods to edit package's source files given desired Pulsar version
class PackageFilesEditor():
# Constants
PACKAGE_SOURCES_PATH = "./pulsar/"
NUSPEC_FILE = PACKAGE_SOURCES_PATH + "pulsar.nuspec"
INSTALL_SCRIPT_PATH = PACKAGE_SOURCES_PATH + "tools/chocolateyinstall.ps1"
TOOLS_PATH = PACKAGE_SOURCES_PATH + "tools/"
VERIFICATION_FILE_PATH = TOOLS_PATH + "VERIFICATION.txt"
INSTALLER_CHECKSUM_FILE_NAME = "SHA256SUMS.txt"
LICENSE_URL = "https://github.com/pulsar-edit/pulsar/blob/master/LICENSE.md"
LICENSE_URL_RAW = "https://raw.githubusercontent.com/pulsar-edit/pulsar/master/LICENSE.md"
LICENSE_FILE_PATH = TOOLS_PATH + "LICENSE.txt"
# Class properties
pulsarVersion_ = ""
pulsarVersionName_ = ""
githubReleaseUrl_ = "https://github.com/pulsar-edit/pulsar/releases/tag/"
assetsDownloadUrl_ = "https://github.com/pulsar-edit/pulsar/releases/download/"
installerFileName_ = "Windows.Pulsar.Setup"
installerDownloadUrl_ = ""
installerChecksumDownloadUrl_ = ""
# Class initializer
def __init__(self, pulsarVersion):
# Initialize file names and URLs given required version
self.pulsarVersion_ = pulsarVersion
if(self.pulsarVersion_[0] == "v"):
self.pulsarVersion_ = self.pulsarVersion_[1:]
self.pulsarVersionName_ = "v" + self.pulsarVersion_
self.installerFileName_ = "Windows.Pulsar.Setup." + self.pulsarVersion_ + ".exe"
self.githubReleaseUrl_ = self.githubReleaseUrl_ + self.pulsarVersionName_
self.assetsDownloadUrl_ = self.assetsDownloadUrl_ + self.pulsarVersionName_
self.installerDownloadUrl_ = self.assetsDownloadUrl_ + "/" + self.installerFileName_
self.installerChecksumDownloadUrl_ = self.assetsDownloadUrl_ + "/" + self.INSTALLER_CHECKSUM_FILE_NAME
# Main function run from main
def run(self):
print("Init configuration files to generate package for version " + self.pulsarVersion_)
self.retirieveInstallerChecksum()
self.editNuspecPackage()
self.editInstallScript()
self.generateVerificationFile()
self.downloadLicenseFile()
print("Package sources initialized")
def retirieveInstallerChecksum(self):
print("Retrieve installer file's checksum for verification")
response = requests.get(self.installerChecksumDownloadUrl_)
if(response.status_code != 200):
print("Error " + str(response.status_code) + " while downloading checksum file")
sys.exit(1)
open(self.INSTALLER_CHECKSUM_FILE_NAME, "wb").write(response.content)
print("DONE")
print("Extract SHA256 checksum for installer file from checksums list")
with open(self.INSTALLER_CHECKSUM_FILE_NAME, "r") as f:
lines = f.readlines()
for index, line in enumerate(lines):
if(self.installerFileName_ in line):
self.installerChecksum_ = line.split()[0]
print("Installer file " + self.installerFileName_ + " checksum: " + self.installerChecksum_)
os.remove(self.INSTALLER_CHECKSUM_FILE_NAME)
print("DONE")
def editNuspecPackage(self):
print("Edit " + self.NUSPEC_FILE + " file...")
nuspecFileOriginal = codecs.open(self.NUSPEC_FILE, "r", "utf-8")
nuspecFileNew = codecs.open("tmp", "w", "utf-8")
for line in nuspecFileOriginal:
if "<version>" in line:
nuspecFileNew.write(" <version>" + self.pulsarVersion_ + "</version>" + os.linesep)
elif "<releaseNotes>" in line:
nuspecFileNew.write(" <releaseNotes>https://github.com/pulsar-edit/pulsar/blob/master/CHANGELOG.md#" + self.pulsarVersion_.replace(".","") + "</releaseNotes>" + os.linesep)
elif "<licenseUrl>" in line:
nuspecFileNew.write(" <licenseUrl>" + self.LICENSE_URL + "</licenseUrl>" + os.linesep)
else:
nuspecFileNew.write(line)
nuspecFileOriginal.close()
nuspecFileNew.close()
shutil.copy2("tmp", self.NUSPEC_FILE)
os.remove("tmp")
print("DONE")
def editInstallScript(self):
print("Edit " + self.INSTALL_SCRIPT_PATH + " file...")
installScriptOriginal = codecs.open(self.INSTALL_SCRIPT_PATH, "r", "utf-8")
installScriptNew = codecs.open("tmp", "w", "utf-8")
for line in installScriptOriginal:
if "https" in line:
installScriptNew.write("$url = '" + self.installerDownloadUrl_ + "'" + os.linesep)
elif "checksum" in line and not "Type" in line:
installScriptNew.write(" checksum = '" + self.installerChecksum_ + "'" + os.linesep)
else:
installScriptNew.write(line)
installScriptOriginal.close()
installScriptNew.close()
shutil.copy2("tmp", self.INSTALL_SCRIPT_PATH)
os.remove("tmp")
print("DONE")
def generateVerificationFile(self):
print("Generate verification file...")
verificationFile = codecs.open(self.VERIFICATION_FILE_PATH, "w", "utf-8")
verificationFile.write("VERIFICATION" + os.linesep)
verificationFile.write(os.linesep)
verificationFile.write("Install script will download " + self.installerFileName_ + " from " + self.githubReleaseUrl_ + os.linesep)
verificationFile.write("Installer checksum (SHA256): " + self.installerChecksum_ + os.linesep)
verificationFile.write("Checksum will be checked automatically by installer script." + os.linesep)
verificationFile.close()
print("DONE")
def downloadLicenseFile(self):
print("Download license file...")
response = requests.get(self.LICENSE_URL_RAW)
if(response.status_code != 200):
print("Error " + str(response.status_code) + " while downloading license file")
sys.exit(1)
open(self.LICENSE_FILE_PATH, "wb").write(response.content)
print("DONE")
# Main function
if __name__ == '__main__':
# Command line arguments parsing
parser = argparse.ArgumentParser(description="Init Chocolatey Pulsar package configuration files", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("version", help="Pulsar version")
args = parser.parse_args()
# Run files edit
packageFilesEditor = PackageFilesEditor(args.version)
packageFilesEditor.run()
print("Navigate to pulsar folder and generate package with 'choco pack' command")