Mercurial > p > roundup > code
comparison scripts/schema-dump.py @ 4937:9369ade6c24b
scripts/schema-dump.py: New script to dump schema from tracker through XML-RPC
| author | anatoly techtonik <techtonik@gmail.com> |
|---|---|
| date | Fri, 03 Oct 2014 12:08:37 +0300 |
| parents | |
| children | 27f592f3d696 |
comparison
equal
deleted
inserted
replaced
| 4936:eeb9572b0508 | 4937:9369ade6c24b |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # -*- coding: utf-8 -*- | |
| 3 """ | |
| 4 Use recently documented XML-RPC API to dump | |
| 5 Roundup data schema in human readable form. | |
| 6 | |
| 7 Future development may cover: | |
| 8 [ ] unreadable dump formats | |
| 9 [ ] access to local database | |
| 10 [ ] lossless dump/restore cycle | |
| 11 [ ] data dump and filtering with preserved | |
| 12 """ | |
| 13 __license__ = "Public Domain" | |
| 14 __version__ = "1.0" | |
| 15 __authors__ = [ | |
| 16 "anatoly techtonik <techtonik@gmail.com>" | |
| 17 ] | |
| 18 | |
| 19 import os | |
| 20 import sys | |
| 21 import xmlrpclib | |
| 22 import pprint | |
| 23 import textwrap | |
| 24 from optparse import OptionParser | |
| 25 | |
| 26 sname = os.path.basename(sys.argv[0]) | |
| 27 usage = """\ | |
| 28 usage: %s [options] URL | |
| 29 | |
| 30 URL is XML-RPC endpoint for your tracker, such as: | |
| 31 | |
| 32 http://localhost:8917/demo/xmlrpc | |
| 33 | |
| 34 options: | |
| 35 --pprint (default) | |
| 36 --json | |
| 37 --yaml | |
| 38 --raw | |
| 39 | |
| 40 -h --help | |
| 41 --version | |
| 42 """ % sname | |
| 43 | |
| 44 def format_pprint(var): | |
| 45 return pprint.pformat(var) | |
| 46 | |
| 47 def format_json(var): | |
| 48 jout = pprint.pformat(var) | |
| 49 jout = jout.replace('"', "\\'") # " to \' | |
| 50 jout = jout.replace("'", '"') # ' to " | |
| 51 jout = jout.replace('\\"', "'") # \" to ' | |
| 52 return jout | |
| 53 | |
| 54 def format_yaml(var): | |
| 55 out = pprint.pformat(var) | |
| 56 out = out.replace('{', ' ') | |
| 57 out = out.replace('}', '') | |
| 58 out = textwrap.dedent(out) | |
| 59 out = out.replace("'", '') | |
| 60 out = out.replace(' [[', '\n [') | |
| 61 out = out.replace(']]', ']') | |
| 62 out = out.replace('],', '') | |
| 63 out = out.replace(']', '') | |
| 64 out2 = [] | |
| 65 for line in out.splitlines(): | |
| 66 if '[' in line: | |
| 67 line = ' ' + line.lstrip(' [') | |
| 68 line = line.replace('>', '') | |
| 69 line = line.replace('roundup.hyperdb.', '') | |
| 70 # expandtabs(16) with limit=1 | |
| 71 n, v = line.split(', <') | |
| 72 if len(n) > 14: | |
| 73 indent = 0 | |
| 74 else: | |
| 75 indent = 14 - len(n) | |
| 76 line = line.replace(', <', ': '+' '*indent) | |
| 77 line.split(",") | |
| 78 out2.append(line) | |
| 79 out = '\n'.join(out2) | |
| 80 return out | |
| 81 | |
| 82 if __name__ == "__main__": | |
| 83 if len(sys.argv) < 2 or "-h" in sys.argv or "--help" in sys.argv: | |
| 84 sys.exit(usage) | |
| 85 if "--version" in sys.argv: | |
| 86 sys.exit(sname + " " + __version__) | |
| 87 | |
| 88 parser = OptionParser() | |
| 89 parser.add_option("--raw", action='store_true') | |
| 90 parser.add_option("--yaml", action='store_true') | |
| 91 parser.add_option("--json", action='store_true') | |
| 92 (options, args) = parser.parse_args() | |
| 93 | |
| 94 print sys.argv, args | |
| 95 url = args[0] | |
| 96 roundup_server = xmlrpclib.ServerProxy(url, allow_none=True) | |
| 97 schema = roundup_server.schema() | |
| 98 if options.raw: | |
| 99 print(str(schema)) | |
| 100 elif options.yaml: | |
| 101 print(format_yaml(schema)) | |
| 102 elif options.json: | |
| 103 print(format_json(schema)) | |
| 104 else: | |
| 105 print(format_pprint(schema)) | |
| 106 | |
| 107 print("") |
