/
upload_core.py
executable file
·113 lines (90 loc) · 3.36 KB
/
upload_core.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
#!/usr/bin/env python3
## ---------------------------------------------------------------------
##
## Copyright (C) 2019 by the adcc authors
##
## This file is part of adcc.
##
## adcc is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## adcc is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
import os
import re
import ast
import sys
import glob
import subprocess
def get_adccore_data():
"""Get a class providing info about the adccore library"""
abspath = os.path.abspath("extension")
if abspath not in sys.path:
sys.path.insert(0, abspath)
from AdcCore import AdcCore
return AdcCore()
def extract_postfix(fn):
with open(fn, "r") as fp:
for line in fp:
match = re.match(r"^ *adccore_version *= *(\([^()]*\))", line)
if match:
return ast.literal_eval(match.group(1))[1]
raise RuntimeError("Could not extract adccore version from " + fn)
def make_tarball(adccore, postfix=None):
filename = adccore.get_tarball_name(postfix=postfix)
fullpath = os.path.abspath("dist/" + filename)
os.makedirs("dist", exist_ok=True)
olddir = os.getcwd()
os.chdir(adccore.top_dir)
filelist = []
for globstr in adccore.file_globs:
relglob = os.path.relpath(globstr, adccore.top_dir)
filelist += glob.glob(relglob)
subprocess.run(["tar", "cvzf", fullpath] + filelist)
os.chdir(olddir)
return "dist/" + filename
def print_input(text, interactive=sys.stdin.isatty()):
if interactive:
try:
input(text)
except KeyboardInterrupt:
raise SystemExit("... aborted.")
else:
print(text, "... yes")
def upload_tarball(filename):
import json
with open(os.path.dirname(__file__) + "/config.json") as fp:
target = json.load(fp)["adccore"]
print()
print_input("Press enter to upload {} -> {}".format(filename, target))
# TODO Ideally one would create a hash here and verify it on download
# to ensure integrity of the download process.
if ":" in target: # Remote deployment
host, tdir = target.split(":")
command = "put {} {}/".format(filename, tdir)
subprocess.run(["sftp", "-b", "-", host], input=command.encode(),
check=True)
else: # Dummy local deployment
subprocess.run(["cp", filename, target], check=True)
def main():
if not os.path.isfile("scripts/upload_core.py") or \
not os.path.isfile("setup.py"):
raise SystemExit("Please run from top dir of repository")
# Get postfix from setup.py
postfix = extract_postfix("setup.py")
# Build adccore and pack tarball
adccore = get_adccore_data()
adccore.build()
filename = make_tarball(adccore, postfix=postfix)
upload_tarball(filename)
if __name__ == "__main__":
main()