Initial release.
[robmyers:dogecode.git] / bin / dcrun
1 #!/bin/env python3
2 # -*- mode: python -*-
3
4 # dcrun - Fetch and execute programs from the Dogeparty blockchain.
5 # Copyright (C) 2014 Rob Myers rob@robmyers.org
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or 
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from optparse import OptionParser
21 import sys
22
23 import dogecode.brainfuck, dogecode.network, dogecode.translation
24
25 def getopts():
26     usage = "usage: %prog [options] source_address"
27     parser = OptionParser(usage=usage)
28     parser.add_option("-r", "--rpc-host", dest="host", default="localhost",
29                       help="the Dogeparty json-rpc host, probably localhost")
30     parser.add_option("-p", "--rpc-port", dest="port", default="5000",
31                       help="the Dogeparty json-rpc port")
32     parser.add_option("-u", "--rpc-user", dest="user", default="rpc",
33                       help="the Dogeparty json-rpc username")
34     parser.add_option("-w", "--rpc-password", dest="password", default=None,
35                       help="the Dogeparty json-rpc password")
36     parser.add_option("-d", "--dump-code",
37                       action="store_true", dest="dump", default=False,
38                       help="dump the program code to STDERR")
39     opts, args = parser.parse_args()
40     if(len(args) != 1):
41         parser.error("must supply source_address")
42     return opts, args
43
44 def main():
45     (opts, args) = getopts()
46     address = args[0]
47     #TODO: make sure address is valid
48     dump_code = opts.dump
49     user = opts.user
50     password = opts.password
51     if not password:
52         print("Enter Dogeparty json-rpc password:")
53         password = sys.stdin.readline()
54     host = opts.host
55     port = opts.port
56     api = dogecode.network.DogepartyApi(user, password, host, port)
57     tokens = api.token_transactions_for_address(address)
58     pairs = dogecode.translation.json_token_amounts_to_pairs(tokens)
59     code = dogecode.translation.token_runs_to_code(pairs)
60     if dump_code:
61         print(code, file=sys.stderr)
62     try:
63         dogecode.brainfuck.evaluate(code)
64     except Exception as e:
65         print("Error evaluating program after fetching.", file=sys.stderr)
66         print("Program was: {}".format(code), file=sys.stderr)
67         print("Exception was: {}".format(e), file=sys.stderr)
68         
69 if __name__ == "__main__":
70     main()