forked from prfalken/debchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebchange.py
executable file
·169 lines (138 loc) · 5.23 KB
/
debchange.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
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
###################################
# by [email protected]
# Version 1.1 from 11.02.2022
# needs:
#
###################################
import os, tempfile
from subprocess import call
import shutil
import sys
import re
import time
import argparse
import subprocess
import filecmp
from time import sleep
EDITOR = os.environ.get('EDITOR','vim')
__version__ = 'version 0.1.0'
def main():
info = getInformation()
# Get environment values for template
info['debfullname'] = os.environ.get('DEBFULLNAME', 'Firstname Lastname')
info['debemail'] = os.environ.get('DEBEMAIL', '[email protected]')
info['debfullname'] = "Andreas Till"
info['debemail'] = "[email protected]"
# date format example: Fri, 13 Jul 2012 15:05:04 +0200
info['debian_formatted_date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0200", time.localtime())
version = info['pkg_version']
if "-" in version:
minor= version.split('-')[1]
version = version.split('.')
version[-1] = str(int(version[-1].split('-')[0]) + 1)
info['pkg_version']= '.'.join(version)+'-'+minor
else:
version = version.split('.')
version[-1] = str(int(version[-1]) + 1)
info['pkg_version']= '.'.join(version)
# Templated Changelog Entry
template = """%(pkg_name)s (%(pkg_version)s) %(pkg_distrib)s; urgency=low
*
-- %(debfullname)s <%(debemail)s> %(debian_formatted_date)s
"""
pushed_content = template %info + info['changelog_content']
with tempfile.NamedTemporaryFile() as f:
# Write template contents
f.write(pushed_content.encode())
f.flush()
# Spawn Editor
shutil.copyfile(f.name, "debian/check_tmp")
call([EDITOR, f.name])
# check if changes where made
if not filecmp.cmp(f.name, 'debian/check_tmp'):
print('Written updateded changelog')
# Copy new file
shutil.copyfile(f.name, "debian/changelog")
os.unlink('debian/check_tmp')
else:
print('Nothing changed')
os.unlink('debian/check_tmp')
def getInformation():
# read changelog file
if os.path.exists("debian/changelog"):
with open('debian/changelog') as f:
changelog_content = f.read()
else:
print ("You must run debchange.py in a source package containing debian/changelog")
sys.exit(1)
# Get original information ( package_name (version) distrib; urgency )
first_line = changelog_content.split('\n')[0].strip()
template = re.compile(r"^(?P<pkg_name>.*) \((?P<pkg_version>.*)\) (?P<pkg_distrib>.*); (?P<pkg_urgency>.*)$" )
m = template.match(first_line)
info = m.groupdict()
info['changelog_content'] = changelog_content
return info
def check_if_tag_exists(tag):
info = getInformation()
#print ('tags')
#output = subprocess.check_output(['git', 'tag'], shell=False, text=True).strip()
cmd = subprocess.run(['git', 'tag'], capture_output=True, text=True)
tags=[]
for line in cmd.stdout.splitlines():
tags.append(line)
if tag in tags:
return True
else:
return False
def tags():
info = getInformation()
if not check_if_tag_exists('v'+info['pkg_version']):
print ('Adding tag v'+info['pkg_version'])
cmd = subprocess.run(['git', 'tag', 'v'+info['pkg_version']], capture_output=True, text=True)
print (cmd.stdout)
print (cmd.stderr)
print ('Pushing tags')
cmd = subprocess.run(['git', 'push', '--tags'], capture_output=True, text=True)
print (cmd.stdout)
print (cmd.stderr)
#cmd = subprocess.run(['git', 'tag'], capture_output=True, text=True)
print (cmd.stdout)
print (cmd.stderr)
else:
print ('Tag v'+info['pkg_version']+' already exists')
def delete_last_tag():
info = getInformation()
if check_if_tag_exists('v'+info['pkg_version']):
print ('Delete tag locally...')
cmd = subprocess.run(['git', 'tag', '-d', 'v'+info['pkg_version']], capture_output=True, text=True)
print (cmd.stdout)
print (cmd.stderr)
print ('Delete tag remote...')
cmd = subprocess.run(['git', 'push', '--delete', 'origin', 'v'+info['pkg_version']], capture_output=True, text=True)
print (cmd.stdout)
print (cmd.stderr)
sleep(4)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='debhelper alternative written in python')
parser.add_argument('-v', dest='version', action='store_true', help='print the version and exit')
parser.add_argument('-f', dest='force', action='store_true',
help='force recreation of last tag')
parser.add_argument('-t', dest='tags', action='store_true',
help='update and push git tag')
parser.add_argument('-u', dest='update', action='store_true',
help='update and iterate debian/changelog')
args = parser.parse_args()
if args.version:
print (__version__)
quit()
if args.tags is not False:
if args.force is not False:
delete_last_tag()
tags()
quit()
if args.update is not False:
main()
quit()
else:
parser.print_help()