forked from NaN-tic/tryton-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
157 lines (128 loc) · 3.9 KB
/
features.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
#!/usr/bin/env python
from invoke import task, Collection
import os
import yaml
import subprocess
from string import Template
patches_dir = "./features"
series_file = 'series'
def read_series():
return yaml.load(open(os.path.join(patches_dir, series_file)).read(),
Loader=yaml.FullLoader)
def write_series(data):
with open(os.path.join(patches_dir, series_file), 'a') as fp:
fp.write(data)
class Patch(object):
def __init__(self, yaml_obj, conflict=False):
self.name, = yaml_obj.keys()
self.patchfile = os.path.join(patches_dir, yaml_obj[self.name]['file'])
self.milestone = yaml_obj[self.name]['milestone']
self.task = yaml_obj[self.name]['task']
self.conflict = conflict
def __repr__(self):
applied = 'applied' if self.applied() else 'Not applied'
conflict = '' if not self.conflict else 'Conflict'
return "[%s] - file: %s , %s , %s" % (self.name, self.patchfile,
applied, conflict)
def applied(self):
command = ["patch", "-N", "-p1", "--silent", "--dry-run", "-i", self.patchfile]
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output, err = process.communicate()
self.conflict = 'FAILED' in str(output)
if not output or self.conflict:
return False
return True
def push(self):
command = ["patch", "-N", "-p1", "-i", self.patchfile]
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output, err = process.communicate()
return True if not err else False
def pop(self):
command = ["patch", "-R", "-p1", "-i", self.patchfile]
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output, err = process.communicate()
return True if not err else False
@task
def add(ctx):
template = Template(
'- ${task_num}:\n'
' file: ${patch_file}\n'
' milestone: ${urlmilestone}\n'
' task: ${urltask}\n'
)
task_num = input('Num. tasca: ')
patch_file = input('Fixter diff: ')
urlmilestone = input('Url del milestone: ')
urltask = input('Url de la tasca: ')
data = {
'task_num': task_num,
'patch_file': patch_file,
'urlmilestone': urlmilestone,
'urltask': urltask
}
write_series(template.substitute(data))
@task()
def applied(ctx):
series = read_series()
if not series:
print("Series is empty")
return
applied = []
for patch_yml in series:
patch = Patch(patch_yml)
if patch.applied():
applied.append(str(patch))
if applied:
print("Patch applied:")
for p in applied:
print(p)
return True
@task()
def unnapplied(ctx):
series = read_series()
if not series:
print("Series is empty")
return
unnapplied = []
for patch_yml in series:
patch = Patch(patch_yml)
if not patch.applied():
unnapplied.append(str(patch))
if unnapplied:
print("Patch Not applied:")
for p in unnapplied:
print(p)
return True
def _pop():
series = read_series()
if not series:
print("Series is empty")
return
for patch_yml in series:
patch = Patch(patch_yml)
if patch.applied():
patch.pop()
print(patch)
@task()
def pop(ctx):
_pop()
def _push():
series = read_series()
if not series:
print("Series is empty")
return
for patch_yml in series:
patch = Patch(patch_yml)
if not patch.applied():
if not patch.conflict:
patch.push()
print(patch)
@task()
def push(ctx):
_push()
FeatureCollection = Collection()
FeatureCollection.add_task(pop)
FeatureCollection.add_task(applied)
FeatureCollection.add_task(unnapplied)
FeatureCollection.add_task(push)
FeatureCollection.add_task(add)