/
difio-virtualenv-python
executable file
·110 lines (90 loc) · 3.59 KB
/
difio-virtualenv-python
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
#!/usr/bin/env python
#####################################################################################
#
# Copyright (c) 2012,2014 Alexander Todorov <atodorov()dif.io>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#####################################################################################
import os
import sys
import json
try:
# python 3
from urllib.parse import urlencode
except (ImportError, NameError):
# python 2
from urllib import urlencode
try:
# python 3
import http.client as httplib
except (ImportError, NameError):
# python 2
import httplib
from socket import gethostname
try:
# python 2
from exceptions import BaseException
except:
pass
from difio_virtualenv_python import version
from pip.util import get_installed_distributions
if 'DIFIO_APP_NAME' in os.environ:
app_name = os.environ['DIFIO_APP_NAME']
else:
app_name = os.path.basename(os.environ['VIRTUAL_ENV'])
if 'DIFIO_APP_URL' in os.environ:
app_url = os.environ['DIFIO_APP_URL']
else:
app_url = 'http://%s' % gethostname()
data = {
'user_id' : int(os.environ['DIFIO_USER_ID']),
'app_name' : app_name,
'app_uuid' : os.environ['DIFIO_UUID'],
'app_type' : 'python-%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2]),
'app_url' : app_url,
'app_vendor' : 1000, # stand-alone virtualenv
'pkg_type' : 0, # Python PyPI
'installed' : [],
}
for dist in get_installed_distributions(local_only=True):
data['installed'].append({'n' : dist.project_name, 'v' : dist.version})
virtualenv_version = None
try:
from virtualenv import virtualenv_version
except (ImportError, NameError):
if 'DIFIO_VIRTUALENV_VERSION' in os.environ:
virtualenv_version = os.environ['DIFIO_VIRTUALENV_VERSION']
if virtualenv_version:
data['installed'].append({'n' : 'virtualenv', 'v' : virtualenv_version})
json_data = json.dumps(data)
params = urlencode({'json_data' : json_data})
headers = {"User-agent": "%s/%s" % (version.name, version.version)}
conn = httplib.HTTPSConnection('difio-otb.rhcloud.com')
conn.request('POST', '/application/register/', params, headers)
response = conn.getresponse()
response_text = response.read()
# In Python 3 converts bytes to str.
# In Python2 response_text is str and bytes is also str
response_text = bytes.decode(response_text)
if (response.status != 200) or (not response.getheader('Content-type').startswith('application/json')):
raise BaseException('Communication failed - %s' % response_text)
result = json.loads(response_text)
print("Difio:", result['message'])
sys.exit(result['exit_code'])