• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xapi-project / xen-api / 10468129840

20 Aug 2024 08:33AM UTC coverage: 78.279% (+30.7%) from 47.566%
10468129840

Pull #5896

github

web-flow
Merge pull request #5944 from liulinC/private/linl/dev
Pull Request #5896: Python3 update feature merge

1472 of 1582 new or added lines in 30 files covered. (93.05%)

3456 of 4415 relevant lines covered (78.28%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.31
/python3/bin/hfx_filename
1
#!/usr/bin/env python3
2

3
# Copyright (c) 2015 Citrix, Inc.
4
#
5
# Permission to use, copy, modify, and distribute this software for any
6
# purpose with or without fee is hereby granted, provided that the above
7
# copyright notice and this permission notice appear in all copies.
8
#
9
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16

17
# pylint: disable=redefined-outer-name
18
# pyright: reportFunctionMemberAccess=false
19
# pyright: reportOptionalMemberAccess=false, reportAttributeAccessIssue=false
20

21
import sys
1✔
22
import socket
1✔
23

24
import XenAPI
1✔
25

26
db_url = "/remote_db_access"
1✔
27

28
def rpc(session_id, request):
1✔
29

30
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1✔
31
    s.connect(("127.0.0.1", 80))
1✔
32
    try:
1✔
33
        # HTTP/1.0 with no transfer-encoding
34
        headers = [
1✔
35
          "POST %s?session_id=%s HTTP/1.0" % (db_url, session_id),
36
          "Connection:close",
37
          "content-length:%d" % (len(request.encode('utf-8'))),
38
          ""
39
        ]
40
        for h in headers:
1✔
41
          s.send((h + "\r\n").encode('utf-8'))
1✔
42
        s.send(request.encode('utf-8'))
1✔
43

44
        result = ""
1✔
45
        while True:
1✔
46
            chunk = s.recv(1024)
1✔
47
            if not chunk:
1✔
48
                break
1✔
49
            result += chunk.decode('utf-8')
1✔
50

51
        if "200 OK" not in result:
1✔
52
          print("Expected an HTTP 200, got %s" % result, file=sys.stderr)
×
53
          return
×
54
        id = result.find("\r\n\r\n")
1✔
55
        headers = result[:id]
1✔
56
        for line in headers.split("\r\n"):
1✔
57
          cl = "content-length:"
1✔
58
          if line.lower().startswith(cl):
1✔
59
             length = int(line[len(cl):].strip())
1✔
60
        body = result[id+4:]
1✔
61
        return body
1✔
62
    finally:
63
        s.close()
1✔
64

65
def parse_string(txt):
1✔
66
    if not txt:
1✔
NEW
67
        raise Exception("Unable to parse string response: None")
×
68
    prefix = "<value><array><data><value>success</value><value>"
1✔
69
    if not txt.startswith(prefix):
1✔
NEW
70
        raise Exception("Unable to parse string response: Wrong prefix")
×
71
    txt = txt[len(prefix):]
1✔
72
    suffix = "</value></data></array></value>"
1✔
73
    if not txt.endswith(suffix):
1✔
NEW
74
        raise Exception("Unable to parse string response: Wrong suffix")
×
75
    txt = txt[:len(txt)-len(suffix)]
1✔
76
    return txt
1✔
77

78
def db_get_by_uuid(session_id, table, uuid):
1✔
79
    request = "<value><array><data><value>db_get_by_uuid</value><value/><value><array><data><value>%s</value><value>%s</value></data></array></value></data></array></value>" % (table, uuid)
1✔
80
    response = parse_string(rpc(session_id, request))
1✔
81
    return response
1✔
82

83
def read_field(session_id, table, fld, rf):
1✔
84
    request = "<value><array><data><value>read_field</value><value/><value><array><data><value>%s</value><value>%s</value><value>%s</value></data></array></value></data></array></value>" % (table, fld, rf)
1✔
85
    response = parse_string(rpc(session_id, request))
1✔
86
    return response
1✔
87

88
if __name__ == "__main__":
1✔
89
    xapi = XenAPI.xapi_local()
×
90
    xapi.xenapi.login_with_password("root", "", "1.0", "hfx_filename")
91
    session_id = xapi._session
×
92
    try:
×
93
        rf = db_get_by_uuid(session_id, "pool_patch", sys.argv[1])
×
94
        filename = read_field(session_id, "pool_patch", "filename", rf)
×
95
        print(filename)
×
96
    finally:
97
        xapi.xenapi.logout()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc