forked from ycm-core/ycmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_omnisharp.py
171 lines (132 loc) · 4.81 KB
/
update_omnisharp.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import contextlib
from os import ( listdir, mkdir )
import os.path as p
import shutil
import sys
import tempfile
import hashlib
DIR_OF_THIS_SCRIPT = p.dirname( p.abspath( __file__ ) )
DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
def GetStandardLibraryIndexInSysPath():
for index, path in enumerate( sys.path ):
if p.isfile( p.join( path, 'os.py' ) ):
return index
raise RuntimeError( 'Could not find standard library path in Python path.' )
def AddRequestDependencies():
request_dep_root = p.abspath( p.join( DIR_OF_THIRD_PARTY,
'requests_deps' ) )
for path in listdir( request_dep_root ):
sys.path.insert( 0, p.join( request_dep_root, path ) )
sys.path.insert( 0, p.abspath( p.join( DIR_OF_THIRD_PARTY,
'requests_deps',
'urllib3',
'src' ) ) )
sys.path.insert( GetStandardLibraryIndexInSysPath() + 1,
p.abspath( p.join( DIR_OF_THIRD_PARTY, 'python-future',
'src' ) ) )
AddRequestDependencies()
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa
from future.utils import iteritems
import requests
URL_FORMAT = {
'release': ( "https://github.com/OmniSharp/omnisharp-roslyn/"
"releases/download/{version}/{file_name}" ),
'ci': ( "https://roslynomnisharp.blob.core.windows.net/"
"releases/{version}/{file_name}" ),
}
FILE_NAME = {
'win32': 'omnisharp.http-win-x86.zip',
'win64': 'omnisharp.http-win-x64.zip',
'macos': 'omnisharp.http-osx.tar.gz',
'linux32': 'omnisharp.http-linux-x86.tar.gz',
'linux64': 'omnisharp.http-linux-x64.tar.gz',
}
@contextlib.contextmanager
def TemporaryDirectory():
temp_dir = tempfile.mkdtemp()
try:
yield temp_dir
finally:
shutil.rmtree( temp_dir )
def Download( url ):
print( 'Downloading {}'.format( url.rsplit( '/', 1 )[ -1 ] ) )
request = requests.get( url, stream=True )
request.raise_for_status()
content = request.content
request.close()
return content
def ParseArguments():
parser = argparse.ArgumentParser()
parser.add_argument( 'version', action='store',
help = 'The Omnisharp version' )
parser.add_argument( '--cache-dir', action='store',
help = 'For testing, directory to cache packages.' )
args = parser.parse_args()
return args
def GetDownloadUrl( version, file_name ):
download_url_key = 'ci' if "-" in version else 'release'
return URL_FORMAT[ download_url_key ].format( version = version,
file_name = file_name )
def FetchAndHash( download_url, output_dir, file_name ):
try:
archive = p.join( output_dir, file_name )
if not p.exists( archive ):
compressed_data = Download( download_url )
with open( archive, 'wb' ) as f:
f.write( compressed_data )
except requests.exceptions.HTTPError as error:
if error.response.status_code != 404:
raise
print( 'Cannot download {}'.format( file_name ) )
return
with open( archive, 'rb' ) as f:
return hashlib.sha256( f.read() ).hexdigest()
def Process( output_dir, version ):
result = {}
for os_name, file_name in iteritems( FILE_NAME ):
download_url = GetDownloadUrl( version, file_name )
result[ os_name ] = {
'version': version,
'download_url': download_url,
'file_name': file_name,
'check_sum': FetchAndHash( download_url, output_dir, file_name )
}
return result
def MkDirIfMissing( dir ):
try:
mkdir( dir )
except OSError:
pass
def Main():
args = ParseArguments()
version = args.version
if args.cache_dir:
MkDirIfMissing( args.cache_dir )
cache_dir = p.join( args.cache_dir, version )
MkDirIfMissing( cache_dir )
output = Process( cache_dir, version )
else:
with TemporaryDirectory() as temp_dir:
output = Process( temp_dir, version )
print( "Omnisharp configration for {} is:".format( version ) )
for os_name, os_data in iteritems( output ):
print( " {}: {{".format( repr( os_name ) ) )
for key, value in iteritems( os_data ):
line = " {}: {},".format( repr( key ), repr( value ) )
if len( line ) > 80:
line = " {}: ( {} ),".format( repr( key ), repr( value ) )
format_index = line.index( '(' ) + 2
while len( line ) > 80:
print( line[ 0:78 ] + "'" )
line = ( ' ' * format_index ) + "'" + line[ 78: ]
print( line )
print( " }," )
if __name__ == "__main__":
Main()