forked from pisilinux/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history_reset.py
74 lines (68 loc) · 2.51 KB
/
history_reset.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
'''
Created on 24-12-2012
@author: marcin
'''
import os
import re
rootpath = "."
specfile = "pspec.xml"
newcomment = "First release"
newrelease = "1"
relative_backup_path = "../backup"
def read_file(path):
with open(path) as f:
return f.read().strip()
def reset_history(spec):
release = re.compile("(.*?release\s*=\s*)[\"\']\d+[\"\'](.*?)")
newspec = ''
history = False
update1 = False
skipline = False
for line in spec.split("\n"):
if "</History>" in line: history = False
if "<History>" in line: history = True
if history:
if "</Update>" in line:
if not update1: newspec = "\n".join((newspec, line))
update1 = True
if update1: continue
else:
if "<Update" in line: line = re.sub(release, r'\1"%s"\2' % newrelease, line)
elif "<Comment" in line:
if not "</Comment" in line:
skipline = True
if "</Comment" in line:
skipline = False
line = " " * 12 + "<Comment>%s</Comment>" % newcomment
if not skipline: newspec = "\n".join((newspec, line))
else: newspec = "\n".join((newspec, line))
return newspec
if __name__ == '__main__':
backup_path = os.path.normpath(os.path.join(rootpath, relative_backup_path))
if not os.path.isdir(backup_path):
os.makedirs(backup_path)
else:
suffix = 0
parent_backup = os.path.dirname(backup_path)
base_name = os.path.basename(relative_backup_path)
for d in os.listdir(parent_backup):
if d.startswith(base_name):
ext = d.replace(base_name, '')
if os.path.isdir("%s/%s" % (parent_backup, d)) and ext.isdigit():
if int(ext) > suffix:
suffix = int(ext)
suffix +=1
suffix = "0%d" % suffix if suffix < 10 else str(suffix)
os.rename(backup_path, backup_path + suffix)
os.makedirs(backup_path)
for root, dirs, files in os.walk(rootpath):
if specfile in files:
f = "%s/%s" % (root, specfile)
print(f)
spec = read_file(f)
bf = "%s/%s" % (os.path.normpath(root.replace(rootpath, relative_backup_path)), specfile)
os.makedirs(os.path.dirname(bf))
open(bf, "w").write(spec)
open(bf, "a").write("\n")
open(f, "w").write(reset_history(spec))
open(f, "a").write("\n")