-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathimport_srpm.py
executable file
·122 lines (99 loc) · 4.85 KB
/
import_srpm.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
#!/usr/bin/env python3
import argparse
import os
import subprocess
def main():
parser = argparse.ArgumentParser(description='Imports the contents of a source RPM into a git repository')
parser.add_argument('source_rpm', help='local path to source RPM')
parser.add_argument('repository', help='local path to the repository')
parser.add_argument('parent_branch', help='git parent branch from which to branch')
parser.add_argument('branch', help='destination branch')
parser.add_argument('tag', nargs='?', help='tag')
parser.add_argument('-c', '--commit', action='store_true', help='commit the changes')
parser.add_argument('-p', '--push', action='store_true', help='commit and push')
parser.add_argument('-m', '--master', action='store_true', help='merge to master afterwards')
args = parser.parse_args()
# check that the source RPM file exists
if not os.path.isfile(args.source_rpm):
parser.error("File %s does not exist." % args.source_rpm)
if not args.source_rpm.endswith('.src.rpm'):
parser.error("File %s does not appear to be a source RPM." % args.source_rpm)
source_rpm_abs = os.path.abspath(args.source_rpm)
# enter repository directory
if not os.path.isdir(args.repository):
parser.error("Repository directory %s does not exist." % args.repository)
os.chdir(args.repository)
# check that the working copy is clean
try:
subprocess.check_call(['git', 'diff-index', '--quiet', 'HEAD', '--'])
print("Working copy is clean.")
except:
parser.error("Git repository seems to have local modifications.")
# check that there are no untracked files
if len(subprocess.check_output(['git', 'ls-files', '--others', '--exclude-standard'])):
parser.error("There are untracked files.")
print(" checking out parent ref...")
subprocess.check_call(['git', 'fetch'])
subprocess.check_call(['git', 'checkout', args.parent_branch])
if args.push:
subprocess.check_call(['git', 'pull'])
print(" removing everything from SOURCES and SPECS...")
if os.path.isdir('SOURCES') and len(os.listdir('SOURCES')) > 0:
subprocess.check_call(['git', 'rm', 'SOURCES/*', '-r'])
if os.path.isdir('SOURCES') and len(os.listdir('SOURCES')) > 0:
parser.error("Files remaining in SOURCES/ after removing the tracked ones. ")
parser.error("Delete them (including hidden files), reset --hard.")
os.mkdir('SOURCES')
if os.path.isdir('SPECS'):
subprocess.check_call(['git', 'rm', 'SPECS/*', '-r'])
os.mkdir('SPECS')
print(" extracting SRPM...")
os.chdir('SOURCES')
os.system('rpm2cpio "%s" | cpio -idmv' % source_rpm_abs)
os.chdir('..')
os.system('mv SOURCES/*.spec SPECS/')
print(" removing trademarked or copyrighted files...")
sources = os.listdir('SOURCES')
deletemsg = "File deleted from the original sources for trademark-related or copyright-related legal reasons.\n"
deleted = []
for f in ['Citrix_Logo_Black.png', 'COPYING.CitrixCommercial']:
if f in sources:
os.unlink(os.path.join('SOURCES', f))
open(os.path.join('SOURCES', "%s.deleted-by-XCP-ng.txt" % f), 'w').write(deletemsg)
deleted.append(f)
if subprocess.call(['git', 'rev-parse', '--quiet', '--verify', args.branch]) != 0:
subprocess.check_call(['git', 'checkout', '-b', args.branch])
else:
subprocess.check_call(['git', 'checkout', args.branch])
subprocess.check_call(['git', 'add', '--all'])
if args.commit or args.push:
print(" committing...")
has_changes = False
try:
subprocess.check_call(['git', 'diff-index', '--quiet', 'HEAD', '--'])
except:
has_changes = True
if not has_changes:
print("\nWorking copy has no modifications. Nothing to commit. No changes from previous release?\n")
else:
msg = 'Import %s' % os.path.basename(args.source_rpm)
if deleted:
msg += "\n\nFiles deleted for legal reasons:\n - " + '\n - '.join(deleted)
subprocess.check_call(['git', 'commit', '-s', '-m', msg])
# tag
if args.tag is not None:
subprocess.check_call(['git', 'tag', args.tag])
# push to remote
if args.push:
subprocess.check_call(['git', 'push', '--set-upstream', 'origin', args.branch])
if args.tag is not None:
subprocess.check_call(['git', 'push', 'origin', args.tag])
print(" switching to master before leaving...")
subprocess.check_call(['git', 'checkout', 'master'])
# merge to master if needed
if args.push and args.master:
print(" merging to master...")
subprocess.check_call(['git', 'push', 'origin', '%s:master' % args.branch])
subprocess.check_call(['git', 'pull'])
if __name__ == "__main__":
main()