-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupdatelib.py
executable file
·87 lines (73 loc) · 2.43 KB
/
updatelib.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
import shutil
import tempfile
VERSION = "nightly"
API_URL = "https://portal.ampl.com/dl/API/future/{}/libampl.zip".format(VERSION)
def updatelib(package):
from zipfile import ZipFile
try:
from urllib import urlretrieve
except Exception:
from urllib.request import urlretrieve
os.chdir(os.path.dirname(__file__) or os.curdir)
tmpfile = tempfile.mktemp(".zip")
tmpdir = os.path.join(os.curdir, "tmp")
libampldir = os.path.join(tmpdir, "libampl")
try:
shutil.rmtree(tmpdir)
except Exception:
pass
if package.startswith("http"):
# Disable SSL verification
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
print("Downloading:", API_URL)
urlretrieve(API_URL, tmpfile)
with ZipFile(tmpfile) as zp:
zp.extractall(tmpdir)
try:
os.remove(tmpfile)
except Exception:
pass
else:
with ZipFile(package) as zp:
zp.extractall(tmpdir)
include_dir = os.path.join(libampldir, "include", "ampl")
rampl_include = os.path.join("src", "include", "ampl")
try:
shutil.rmtree(rampl_include)
except Exception:
pass
shutil.copytree(include_dir, rampl_include)
print("*\n!.gitignore\n", file=open(os.path.join(rampl_include, ".gitignore"), "w"))
print("headers: {}".format(rampl_include))
dstbase = os.path.join("inst", "libs")
try:
shutil.rmtree(dstbase)
os.mkdir(dstbase)
except Exception:
pass
print("*\n!.gitignore\n", file=open(os.path.join(dstbase, ".gitignore"), "w"))
for lib, arch in [("amd64", "x64")]:
libdir = os.path.join(libampldir, lib)
dst = os.path.join("inst", "libs", arch)
print("{} ({}):".format(arch, dst))
try:
shutil.rmtree(dst)
except Exception:
pass
if not os.path.exists(dst):
os.makedirs(dst)
print("*\n!.gitignore\n", file=open(os.path.join(dst, ".gitignore"), "w"))
for filename in os.listdir(libdir):
print("\t{}".format(filename))
shutil.copyfile(os.path.join(libdir, filename), os.path.join(dst, filename))
if __name__ == "__main__":
if len(sys.argv) == 2:
updatelib(sys.argv[1])
else:
updatelib(API_URL)