forked from artefactual/archivematica-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp-rpc-cli.py
executable file
·164 lines (136 loc) · 4.88 KB
/
mcp-rpc-cli.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of the Archivematica development tools.
#
# Copyright 2010-2016 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica 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 Archivematica. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import gearman
try:
import cPickle
except ModuleNotFoundError:
import _pickle as cPickle
try:
input = raw_input
except NameError:
pass
import lxml.etree as etree
import traceback
import os
import time
import sys
class Settings:
MCP_SERVER = ("localhost", 62004)
settings = Settings()
class MCPClient:
def __init__(self, host=settings.MCP_SERVER[0], port=settings.MCP_SERVER[1]):
self.server = "%s:%d" % (host, port)
def list(self):
gm_client = gearman.GearmanClient([self.server])
completed_job_request = gm_client.submit_job(
"getJobsAwaitingApproval", "", None
)
# self.check_request_status(completed_job_request)
return cPickle.loads(completed_job_request.result)
def execute(self, uuid, choice):
gm_client = gearman.GearmanClient([self.server])
data = {}
data["jobUUID"] = uuid
data["chain"] = choice
data["uid"] = "1"
data["user_id"] = "1"
completed_job_request = gm_client.submit_job(
"approveJob", cPickle.dumps(data, protocol=0), None
)
# self.check_request_status(completed_job_request)
return
mcpClient = MCPClient()
def getTagged(root, tag): # bad, I use this elsewhere, should be imported
ret = []
for element in root:
if element.tag == tag:
ret.append(element)
return ret # only return the first encounter
return ret
def updateJobsAwaitingApproval():
return etree.XML(mcpClient.list())
def printJobsAwaitingApproval(jobsAwaitingApproval):
for i, job in enumerate(jobsAwaitingApproval):
print(i)
print(etree.tostring(job, pretty_print=True, encoding="unicode"))
def approveJob(jobsAwaitingApproval, choice, choice2):
try:
index = int(choice)
if index >= len(jobsAwaitingApproval):
print("index out of range")
return
sipUUID = getTagged(
getTagged(getTagged(jobsAwaitingApproval[index], "unit")[0], "unitXML")[0],
"UUID",
)[0].text
uuid = getTagged(jobsAwaitingApproval[index], "UUID")[0].text
try:
chain = getTagged(
getTagged(jobsAwaitingApproval[index], "choices")[0][int(choice2)],
"chainAvailable",
)[0].text
except IndexError:
# Invalid choice, but no reason to fail catastrophically.
return
print("Approving: " + uuid, chain, sipUUID)
mcpClient.execute(uuid, chain)
del jobsAwaitingApproval[index]
except ValueError:
print("Value error")
traceback.print_exc(file=sys.stdout)
return
def main():
"""Primary entry point for this script"""
os.system("clear")
jobsAwaitingApproval = updateJobsAwaitingApproval()
choice = "No-op"
while choice != "q":
while not (len(jobsAwaitingApproval)):
print("Fetching...")
time.sleep(2)
jobsAwaitingApproval = updateJobsAwaitingApproval()
printJobsAwaitingApproval(jobsAwaitingApproval)
print("q to quit")
print("u to update List")
print("number to approve Job")
choice = input("Please enter a value:")
print("choice: " + choice)
if choice == "u":
jobsAwaitingApproval = updateJobsAwaitingApproval()
else:
if choice == "q":
break
choice2 = "No-op"
while choice2 != "q":
try:
printJobsAwaitingApproval(jobsAwaitingApproval[int(choice)][2])
except IndexError:
# Invalid choice, simply go back to main loop.
break
choice2 = input("Please enter a value:")
print("choice2: " + choice2)
approveJob(jobsAwaitingApproval, choice, choice2)
choice2 = "q"
# except:
# print "invalid choice"
# choice2 = "q"
os.system("clear")
if __name__ == "__main__":
main()