-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathppa_helper
executable file
·179 lines (157 loc) · 8.21 KB
/
ppa_helper
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
import re
import os
import sys
import time
import shutil
import requests
import subprocess
from optparse import OptionParser
from optparse import OptionGroup
DEBFULLNAME = os.environ["DEBFULLNAME"]
DEBEMAIL = os.environ["DEBEMAIL"]
print('DEBFULLNAME=%s'%DEBFULLNAME)
print('DEBEMAIL=%s'%DEBEMAIL)
DATENOW = subprocess.check_output(['date -R'], shell=True).strip().decode('utf-8')
print('DATENOW=%s'%DATENOW)
def download(url, dest):
print('Downloading %s...'%url)
response = requests.get(url, stream=True)
handle = open(dest, "wb")
for chunk in response.iter_content(chunk_size=512):
if chunk: # filter out keep-alive new chunks
handle.write(chunk)
print('Done!')
def seriesToVersion(series):
series = series.lower()
if series == 'xenial': return '16.04'
version = 18.0 + (ord(series[0])-ord('b'))/2.0 #bionic is 18.04
return "%d.%s"%(int(version), '04' if int(version) == version else '10')
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("--tmp", type="string", dest="tmp", help="path to temp directory", default='/tmp')
parser.add_option("--source", type="string", dest="source", help="path to source directory", default='')
parser.add_option("--branch", type="string", dest="branch", help="git branch to checkout", default='')
parser.add_option("--series", type="string", dest="series", help="the OS release series", default='')
parser.add_option("--suffix", type="string", dest="suffix", help="the ppa version suffix", default='')
parser.add_option("--upload", type="string", dest="upload", help="specify ppa to upload", default='')
advanced = OptionGroup(parser, "Advanced")
parser.add_option_group(advanced)
advanced.add_option("--format", type="string", dest="format", help="overload source format", default='3.0 (quilt)')
advanced.add_option("--epoch", type="int", dest="epoch", help="epoch for the version number", default=None)
advanced.add_option("--debian-branch", type="string", dest="debian_branch", help="branch with debian files", default=None)
(options, args) = parser.parse_args()
if not options.source: raise Exception('specify source')
if not options.branch: raise Exception('specify branch')
if not options.series: raise Exception('specify series')
if not options.suffix: raise Exception('specify suffix')
gitSourceDir = os.path.expanduser(options.source)
gitSourceName = os.path.basename(gitSourceDir)
#copy the source to tmp
binaryTmpDir = os.path.join(options.tmp, 'ppa_helper', gitSourceName+'_buildarea')
if not os.path.exists(binaryTmpDir): os.makedirs(binaryTmpDir)
sourceTmpDir = os.path.join(options.tmp, 'ppa_helper', gitSourceName+'_'+options.series)
if os.path.exists(sourceTmpDir): shutil.rmtree(sourceTmpDir)
print('sourceTmpDir %s'%sourceTmpDir)
print('binaryTmpDir %s'%binaryTmpDir)
shutil.copytree(src=os.path.expanduser(options.source), dst=sourceTmpDir)
#prepare copied git directory
print('Resetting %s'%sourceTmpDir)
os.system('git -C %s reset --hard HEAD'%sourceTmpDir)
print('Updating %s'%sourceTmpDir)
os.system('git -C %s remote update'%sourceTmpDir)
print('Cleaning %s'%sourceTmpDir)
os.system('git -C %s clean -ffxd'%sourceTmpDir)
print('Checkout %s'%options.branch)
subprocess.check_call(['git -C %s checkout %s'%(sourceTmpDir, options.branch)], shell=True)
#pull latest if its a branch
allTags = subprocess.check_output(['git -C %s tag --list'%(sourceTmpDir,)], shell=True).strip().decode('utf-8')
if options.branch not in allTags:
print('Pull latest %s'%options.branch)
os.system('git -C %s pull origin %s'%(sourceTmpDir, options.branch))
#use debian branch for deb files
if options.debian_branch:
print('Getting debian/ files from %s'%options.debian_branch)
os.system('git -C %s checkout %s debian/'%(sourceTmpDir, options.debian_branch))
#determine commits since tag
try:
latestTag = subprocess.check_output(['git -C %s describe --abbrev=0 --tags'%sourceTmpDir], shell=True).strip().decode('utf-8')
print('latestTag=%s'%latestTag)
commitsSince = subprocess.check_output(['git -C %s rev-list %s..HEAD --count'%(sourceTmpDir, latestTag)], shell=True).strip().decode('utf-8')
print('commitsSince=%s'%commitsSince)
except:
commitsSince = subprocess.check_output(['git -C %s rev-list HEAD --count'%(sourceTmpDir,)], shell=True).strip().decode('utf-8')
print('commitsSince=%s'%commitsSince)
commitsSince = int(commitsSince)
shortHash = subprocess.check_output(['git -C %s rev-parse --short HEAD'%sourceTmpDir], shell=True).strip().decode('utf-8')
print('shortHash=%s'%shortHash)
#parse the changelog
changelogPath = os.path.join(sourceTmpDir, 'debian', 'changelog')
if not os.path.exists(changelogPath): raise Exception('%s not found'%changelogPath)
changelogLines = open(changelogPath).readlines()
versionLine = changelogLines[0].strip()
print('versionLine=%s'%versionLine)
packageName,packageVersion,packageDistro,packageUrgency = map(str.strip, re.match('(.+)\\s*\\((.+)\\)\\s*(\\w+);\\s*urgency=(\\w+)', versionLine).groups())
packageVersion = packageVersion.rsplit("-", 1)[0]
print('packageName=%s'%packageName)
print('packageVersion=%s'%packageVersion)
print('packageDistro=%s'%packageDistro)
print('packageUrgency=%s'%packageUrgency)
#use commits since if this is not a tag
if commitsSince != 0:
packageVersion = "%s.%d.%s"%(packageVersion, commitsSince, shortHash)
#prefix the version number (helps with accidentally superseded packages)
packageEpoch = "%d:"%options.epoch if options.epoch is not None else ''
ubuntuVersion = 'ubuntu'+seriesToVersion(options.series)
#modify changelog
newChangelogEntries = [
'%s (%s%s-%s~%s) %s; urgency=%s\n'%(packageName, packageEpoch, packageVersion, options.suffix, ubuntuVersion, options.series, packageUrgency),
'\n',
' * PPA helper upload script\n',
'\n',
' -- %s <%s> %s\n'%(DEBFULLNAME, DEBEMAIL, DATENOW),
'\n']
open(changelogPath, 'w').write(''.join(newChangelogEntries+changelogLines))
#modify source format
if options.format:
print('Using debian source format: %s'%options.format)
sourceFormatPath = os.path.join(sourceTmpDir, 'debian', 'source', 'format')
open(sourceFormatPath, 'w').write('%s\n'%options.format)
#try to get a tarball from github
origTarBall = os.path.join(binaryTmpDir, '%s_%s.orig.tar.gz'%(packageName, packageVersion))
if commitsSince == 0 and not os.path.exists(origTarBall):
originURL = subprocess.check_output(['git -C %s remote get-url origin'%sourceTmpDir], shell=True).strip().decode('utf-8')
print('originURL=%s'%originURL)
m = re.match('^[email protected]:(.+)/(.+)\.git$', originURL)
if not m:
m = re.match('^https://github.com/(.+)/(.+)\.git$', originURL)
if not m: raise Exception('Cant parse URL: %s'%originURL)
orgName, proj = m.groups()
fetchURL = 'https://github.com/%s/%s/archive/%s.tar.gz'%(orgName, proj, latestTag)
download(fetchURL, origTarBall)
#build source package
print('Entering %s'%sourceTmpDir)
oldDir = os.getcwd()
os.chdir(sourceTmpDir)
gbpStatus = os.system(' '.join([
'gbp',
'buildpackage',
'--git-ignore-new',
'--git-ignore-branch',
'--git-export=WC',
'--git-upstream-tree=HEAD',
'--git-export-dir=%s'%binaryTmpDir,
'-S', #make source package
'-sa', #include source
'-d', #no-check-builddeps
]))
os.chdir(oldDir)
if gbpStatus != 0: raise Exception('buildpackage failed %d'%gbpStatus)
sourceChangesFile = os.path.join(binaryTmpDir, '%s_%s-%s~%s_source.changes'%(packageName, packageVersion, options.suffix, ubuntuVersion))
print(sourceChangesFile)
if not os.path.exists(sourceChangesFile): raise Exception('Failed %s'%sourceChangesFile)
#upload to ppa
if options.upload:
print('Upload to %s'%options.upload)
dputStatus = os.system('dput %s %s'%(options.upload, sourceChangesFile))
if dputStatus != 0: raise Exception('dput failed %d'%dputStatus)