-
Notifications
You must be signed in to change notification settings - Fork 19
/
fetch-debian-changelog
executable file
·130 lines (110 loc) · 4.39 KB
/
fetch-debian-changelog
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Packaging Scripts
# Copyright (C) 2021-2024 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact: [email protected]
#
import glob
import os
import re
import shutil
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.request
# ====== Handle arguments ==================================================
if len(sys.argv) < 2:
sys.stderr.write('Usage: ' + sys.argv[0] + ' package [distribution]\n')
sys.exit(1)
debianPackage = sys.argv[1]
if len(sys.argv) == 2:
debianDistribution = 'unstable'
else:
debianDistribution = sys.argv[2]
if debianDistribution in [ 'unstable', 'testing', 'stable', 'oldstable' ]:
# Debian
statusPageURL = 'https://packages.debian.org/source/' + debianDistribution + '/' + debianPackage
else:
# Ubuntu
statusPageURL = 'https://packages.ubuntu.com/source/' + debianDistribution + '/' + debianPackage
debianVersion = None
debianLocation = None
debianArchive = None
debianArchiveFormat = None
# ====== Get package status =================================================
statusPageContents = None
sys.stderr.write('Looking for package status on ' + statusPageURL + ' ... ')
sys.stderr.flush()
try:
statusPage = urllib.request.urlopen(statusPageURL)
statusPageContents = statusPage.readlines()
statusPage.close()
re_debian_package = re.compile(r'.*Source Package: ' + debianPackage + ' \(([0-9-+~\.a-z]+)\)')
re_debian_archive = re.compile(r'.*href="((http|https)://[a-zA-Z0-9\./+-]+/' + \
debianPackage[0:1] + '/' + \
debianPackage + '/' + \
r')(' + debianPackage + r'_[0-9-+~\.a-z]+\.debian\.tar\.[a-zA-Z]+)"')
for line in statusPageContents:
line = line.decode('utf-8')
match = re_debian_package.match(line)
if match != None:
debianVersion = match.group(1)
else:
match = re_debian_archive.match(line)
if match != None:
debianLocation = match.group(1)
debianArchive = match.group(3)
except urllib.error.HTTPError as e:
sys.stderr.write('not found (HTTP ' + str(e.code) + ')!\n')
sys.exit(1)
if (debianVersion == None) or (debianArchive == None):
sys.stderr.write('not found!\n')
sys.exit(1)
sys.stderr.write('Version in ' + debianDistribution + ' is ' + debianVersion + '\n')
# ====== Determine necessary compression option =============================
debianArchiveFormat = debianArchive[len(debianArchive) - 2 : len(debianArchive)]
tarCompressionOption = ''
if debianArchiveFormat == 'gz':
tarCompressionOption = 'z'
elif debianArchiveFormat == 'bz2':
tarCompressionOption = 'j'
elif debianArchiveFormat == 'xz':
tarCompressionOption = 'J'
# ====== Fetch debian archive ===============================================
archiveFileURL = debianLocation + debianArchive
sys.stderr.write('Looking for \"debian\" archive at ' + archiveFileURL + ' ... ')
sys.stderr.flush()
try:
archiveFile = urllib.request.urlopen(archiveFileURL)
debianArchiveFile = tempfile.NamedTemporaryFile(delete=False)
shutil.copyfileobj(archiveFile, debianArchiveFile)
debianArchiveFile.close()
archiveFile.close()
sys.stderr.write('found!\n')
try:
process = subprocess.run([ 'tar', 'x' + tarCompressionOption + 'fO', debianArchiveFile.name, 'debian/changelog'],
stderr=subprocess.PIPE, check=True, universal_newlines=True)
os.unlink(debianArchiveFile.name)
except Exception as e:
sys.stderr.write('ERROR: Failed to extract debian/changelog from ' + debianArchiveFile.name + ': ' + str(e) + '\n')
sys.exit(1)
except urllib.error.HTTPError as e:
sys.stderr.write('not found (HTTP ' + str(e.code) + ')!\n')
sys.exit(1)