1
|
|
|
import urllib.request |
2
|
|
|
import os |
3
|
|
|
import os.path |
4
|
|
|
import sys |
5
|
|
|
import tarfile |
6
|
|
|
from shutil import copytree,ignore_patterns,copy,rmtree |
7
|
|
|
from stat import * |
8
|
|
|
import fnmatch |
9
|
|
|
import re |
10
|
|
|
import hashlib |
11
|
|
|
import gzip |
12
|
|
|
from string import Template |
13
|
|
|
from subprocess import call |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
tmpDir="/tmp/" |
17
|
|
|
|
18
|
|
|
def dlTagFromGitHub(version): |
19
|
|
|
remoteFile = urllib.request.urlopen('https://github.com/QTodoTxt/QTodoTxt/archive/'+version+'.tar.gz') |
20
|
|
|
contentDisposition=remoteFile.info()['Content-Disposition'] |
21
|
|
|
fileName=contentDisposition.split('=')[1] |
22
|
|
|
|
23
|
|
|
localFile = open(tmpDir+fileName, 'wb') |
24
|
|
|
localFile.write(remoteFile.read()) |
25
|
|
|
localFile.close() |
26
|
|
|
return fileName |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def uncompressFile(fileName): |
30
|
|
|
os.chdir(tmpDir) |
31
|
|
|
bashCmd=" ".join(["tar xzf",tmpDir+fileName,"--exclude-vcs --no-same-permissions"]) |
32
|
|
|
call(bashCmd,shell=True) |
33
|
|
|
return fileName.rsplit(".",2)[0] |
34
|
|
|
|
35
|
|
|
def buildPackageFolder(folderName): |
36
|
|
|
buildDir=tmpDir+folderName+'_build' |
37
|
|
|
buildBinDir=buildDir+'/usr/share/qtodotxt/bin/' |
38
|
|
|
debianDir=buildDir+'/DEBIAN/' |
39
|
|
|
|
40
|
|
|
# Tree structure |
41
|
|
|
os.makedirs(debianDir) |
42
|
|
|
os.makedirs(buildDir+'/usr/bin/') |
43
|
|
|
os.makedirs(buildDir+'/usr/share/doc/qtodotxt') |
44
|
|
|
os.makedirs(buildDir+'/usr/share/applications') |
45
|
|
|
|
46
|
|
|
#Copy tag folder to build folder except the windows script |
47
|
|
|
copytree(tmpDir+folderName,buildDir+'/usr/share/qtodotxt',False,ignore_patterns('qtodotxt.pyw')) |
48
|
|
|
#Fix execution rights on bin folder |
49
|
|
|
for file in os.listdir(buildBinDir): |
50
|
|
|
filePath=os.path.join(buildBinDir,file) |
51
|
|
|
if os.path.isfile(filePath): |
52
|
|
|
st = os.stat(filePath) |
53
|
|
|
os.chmod(filePath, st.st_mode | S_IEXEC) |
54
|
|
|
|
55
|
|
|
# Adding copyright file |
56
|
|
|
copy(scriptDir+'/copyright',buildDir+'/usr/share/doc/qtodotxt/copyright') |
57
|
|
|
# Adding desktop file |
58
|
|
|
copy(scriptDir+'/qtodotxt.desktop',buildDir+'/usr/share/applications/qtodotxt.desktop') |
59
|
|
|
# Adding changelog file |
60
|
|
|
f_in = open(scriptDir+'/changelog', 'rb') |
61
|
|
|
f_out = gzip.open(buildDir+'/usr/share/doc/qtodotxt/changelog.gz', 'wb') |
62
|
|
|
f_out.writelines(f_in) |
63
|
|
|
f_out.close() |
64
|
|
|
f_in.close() |
65
|
|
|
|
66
|
|
|
return (buildDir,debianDir) |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def makeMd5sums(baseDir,outputFilePath): |
70
|
|
|
|
71
|
|
|
excludes = ['DEBIAN','*.pyc'] |
72
|
|
|
excludes = r'|'.join([fnmatch.translate(x) for x in excludes]) or r'$.' |
73
|
|
|
|
74
|
|
|
outputFile = open(outputFilePath, 'w') |
75
|
|
|
|
76
|
|
|
for (root,dirs,files) in os.walk(baseDir): |
77
|
|
|
dirs[:] = [d for d in dirs if not re.match(excludes,d)] |
78
|
|
|
files = [f for f in files if not re.match(excludes,f)] |
79
|
|
|
|
80
|
|
|
for fn in files: |
81
|
|
|
path = os.path.join(root,fn) |
82
|
|
|
md5 = hashlib.md5(open(path,'rb').read()).hexdigest() |
83
|
|
|
relativePath = root.replace(baseDir+'/',"",1) + os.sep + fn |
84
|
|
|
outputFile.write("%s %s\n" % (md5,relativePath)) |
85
|
|
|
|
86
|
|
|
outputFile.close() |
87
|
|
|
|
88
|
|
|
def generateControl(templateFile,packageVersion,outputFilePath): |
89
|
|
|
|
90
|
|
|
templateExp = open(templateFile,'r').read() |
91
|
|
|
template = Template(templateExp) |
92
|
|
|
|
93
|
|
|
substitute=template.safe_substitute(version=packageVersion) |
94
|
|
|
open(outputFilePath,'w').write(substitute) |
95
|
|
|
#Control file must be owned by root |
96
|
|
|
os.chown(outputFilePath,0,0) |
97
|
|
|
|
98
|
|
|
def buildDeb(version,buildDir): |
99
|
|
|
# Adding symlink to bin folder |
100
|
|
|
os.chdir(buildDir+'/usr/bin/') |
101
|
|
|
os.symlink('../share/qtodotxt/bin/qtodotxt','qtodotxt') |
102
|
|
|
|
103
|
|
|
bashCmd=" ".join(["dpkg -b",buildDir,tmpDir+"qtodotxt_"+version+"_all.deb"]) |
104
|
|
|
call(bashCmd,shell=True) |
105
|
|
|
|
106
|
|
|
def clean(fileName,folderName): |
107
|
|
|
# Removing tar.gz |
108
|
|
|
os.remove(tmpDir+fileName) |
109
|
|
|
# Removing untar folder |
110
|
|
|
rmtree(tmpDir+folderName) |
111
|
|
|
#Removing build folder |
112
|
|
|
rmtree(tmpDir+folderName+'_build') |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
version=sys.argv[1] |
116
|
|
|
scriptDir = os.path.dirname(os.path.realpath(sys.argv[0])) |
117
|
|
|
# Step 1: download tag from github |
118
|
|
|
fileName = dlTagFromGitHub(version) |
119
|
|
|
|
120
|
|
|
# Step 2: uncompress tag's archive |
121
|
|
|
folderName = uncompressFile(fileName) |
122
|
|
|
|
123
|
|
|
# Step 3: build Debian package structure |
124
|
|
|
(buildDir,debianDir)=buildPackageFolder(folderName) |
125
|
|
|
|
126
|
|
|
# Step 4: build DEBIAN/md5sums file |
127
|
|
|
makeMd5sums(buildDir,debianDir+'md5sums') |
128
|
|
|
|
129
|
|
|
# Step 5: generate DEBIAN/control file |
130
|
|
|
generateControl(scriptDir+'/control.tpl',version,debianDir+'control') |
131
|
|
|
|
132
|
|
|
# Step 6: build the deb package |
133
|
|
|
buildDeb(version,buildDir) |
134
|
|
|
|
135
|
|
|
# Step 7: clean all the mess |
136
|
|
|
clean(fileName,folderName) |
137
|
|
|
|