-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload_world.py
executable file
·157 lines (135 loc) · 4.27 KB
/
upload_world.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
#!/usr/bin/python2
import sys
import os
import uuid
import tempfile
import subprocess
import stat
try:
import boto
except ImportError:
sys.path.append(os.path.expanduser("~/devel/boto"))
import boto
from boto.sdb.connection import SDBConnection
from boto.s3.connection import S3Connection
from boto.s3.key import Key
def add_from_url():
if len(sys.argv) < 3:
print "Usage:"
print " %s -url <url>" % sys.argv[0]
return
url = sys.argv[2]
print "Generate world with this url (\"%s\") [y/N]?" % url
if raw_input().lower() != 'y':
print "Ok, nevermind"
return
uid = uuid.uuid4()
data = dict()
data['uuid'] = uid
data['world_url'] = url
sdb = SDBConnection()
db = sdb.get_domain("overviewerdb")
if not db.put_attributes(uid, data):
print "***Error: Failed to update the db"
return 1
print "Ok. DB updated"
print uid
def add_from_path():
if len(sys.argv) < 3:
print "Usage:"
print " %s -path <path>" % sys.argv[0]
return
path = sys.argv[2]
if os.path.isdir(path):
print "You've specified a directory. I'll tar it up before uploading"
print "OK? [y/N] ",
if raw_input().lower() != 'y':
print "Ok, nevermind."
return
tmpdir = tempfile.mkdtemp(prefix="mc_gen")
print "tmpdir is", tmpdir
print "Making tarball..."
p = subprocess.Popen(["tar", "-cf", os.path.join(tmpdir, "world.tar"), "."],
cwd=path)
p.wait()
if p.returncode != 0:
print "***Error: tar failed"
return
print "OK."
print "Compressing..."
p = subprocess.Popen(["bzip2", "world.tar"],
shell=False,
cwd=tmpdir)
p.wait()
if p.returncode != 0:
print "***Error: compress failed"
return
print "OK."
print "Checking filesize..."
s = os.stat(os.path.join(tmpdir, "world.tar.bz2"))
if s.st_size > 10*1024*1024:
print "***Error: Compressed world is too big"
return 1
print "OK."
uid = uuid.uuid4()
print uid
s3 = S3Connection()
bucket = s3.get_bucket("overviewer-worlds")
k = Key(bucket)
k.key = "%s.tar.bz2" % uid
print "Uploading to S3..."
k.set_contents_from_filename(os.path.join(tmpdir, "world.tar.bz2"), reduced_redundancy=True)
print "OK."
k.make_public()
urlbase = "https://s3.amazonaws.com/overviewer-worlds/"
url = urlbase + k.key
print "World is now available at:", url
data = dict()
data['uuid'] = uid
data['world_url'] = url
sdb = SDBConnection()
db = sdb.get_domain("overviewerdb")
if not db.put_attributes(uid, data):
print "***Error: Failed to update the db"
return 1
print "Ok. DB updated"
elif os.path.isfile(path):
print "You've specified a file. I'll upload it without modification"
print "OK? [y/N] ",
if raw_input().lower() != 'y':
print "Ok, nevermind."
return
uid = uuid.uuid4()
print uid
s3 = S3Connection()
bucket = s3.get_bucket("overviewer-worlds")
k = Key(bucket)
k.key = "%s.tar.bz2" % uid
print "Uploading to S3..."
k.set_contents_from_filename(path, reduced_redundancy=True)
print "OK."
k.make_public()
urlbase = "https://s3.amazonaws.com/overviewer-worlds/"
url = urlbase + k.key
print "World is now available at:", url
data = dict()
data['uuid'] = uid
data['world_url'] = url
sdb = SDBConnection()
db = sdb.get_domain("overviewerdb")
if not db.put_attributes(uid, data):
print "***Error: Failed to update the db"
return 1
print "Ok. DB updated"
else:
print "Sorry, I can't find that."
return 1
if __name__ == "__main__":
if "-url" in sys.argv:
add_from_url()
elif "-path" in sys.argv:
add_from_path()
else:
print "Usage:"
print " %s -url <url>" % sys.argv[0]
print " %s -path <path>" % sys.argv[0]