forked from c00w/bitHopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtcnet_wrapper.py
127 lines (111 loc) · 4.1 KB
/
btcnet_wrapper.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
#Copyright (C) 2011,2012 Colin Rice
#This software is licensed under an included MIT license.
#See the file entitled LICENSE
#If you were not provided with a copy of the license please contact:
# Colin Rice [email protected]
import sys
import traceback
import logging
import httplib2
import traceback
import os.path
# TODO : These imports should occur within Pull_git. If they're not present,
# fall back to retrieving the zip.
from git import Repo
try:
from git import GitCommandError
except ImportError:
# Sometimes (apparently), the exception classes are in a subpackage.
# One of these should be deprecated, but I haven't figured out which.
from git.exc import GitCommandError
def Pull_git():
"""
Tries to load btcnet_info as git repository and download the files
"""
try:
logging.info('Trying to load btcnet_info submodule')
#Try load load btcnet_info as a git repo (Submodule)
repo = Repo("btcnet_info")
except GitCommandError:
# Here we have some sort of error probably an empty directory in a zip download instead of
# A git submodule
logging.info('Making new btcnet_info repo')
repo = Repo.init("btcnet_info")
logging.info('Cloning into it')
repo = repo.clone_from("git://github.com/c00w/btcnet_info.git", 'btcnet_info')
try:
#For some reason the above doesn't always set the origin correctly.
logging.info('Checking if we need to add the origin')
origin = repo.create_remote('origin', 'git://github.com/c00w/btcnet_info.git')
except GitCommandError:
logging.info('We do not need to add the origin')
logging.info('Updating btcnet_info')
#Select the origin
origin = repo.remotes.origin
#Update it
origin.fetch()
#Pull the master branch
origin.pull('master')
logging.info('Done')
def Pull_zip():
logging.info('Downloading zip archive')
#Get the actual zip file
headers, content = httplib2.Http(disable_ssl_certificate_validation=True).request('https://github.com/c00w/btcnet_info/zipball/master')
#Proccess the zip file
from StringIO import StringIO
from zipfile import ZipFile
zipfile = ZipFile(StringIO(content))
logging.info('Extracting zip archive')
for name in zipfile.namelist():
#Put it in btcnet_info
dest = name.split('/',1)[1]
dest = os.path.join('btcnet_info',dest)
destdir = os.path.dirname(dest)
#Make directory if we need it
if not os.path.isdir(destdir):
os.makedirs(destdir)
#Get the data
data = zipfile.read(name)
#Write the data to the correct file if we have data (Not a directory)
if data:
with open(dest, 'w') as f:
f.write(data)
with open('.zip', 'w') as f:
f.write('Switched to zip mode')
logging.info('Done')
def Install_btcnet():
if os.path.exists('.zip'):
try:
Pull_zip()
except Exception as error:
logging.error(traceback.format_exc())
else:
try:
Pull_git()
import btcnet_info
except Exception as e:
logging.error(traceback.format_exc())
try:
Pull_zip()
except Exception as error:
logging.error(traceback.format_exc())
# other modules should use:
# from bcnet_wrapper import btcnet_info
# to import btcnet_nifo into their namespace, automatically downloading and
# installing the module if necessary
try:
# If we already have the module, we're done! This will probably only work
# if the CWD is the parent of the btcnet_info submodule.
import btcnet_info
except ImportError:
# Fall-through to installation attempt
pass
if 'btcnet_info' not in globals():
try:
Install_btcnet()
import btcnet_info
except Exception:
logging.error(traceback.format_exc())
logging.error('Could not install btcnet_info, please report logs online plus python version' )
sys.exit(2)
btcnet_info = None