2019-04-14 22:10:51 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Build steps for the windows binary packages.
|
|
|
|
|
|
|
|
The script is designed to be called by appveyor. Subcommands map the steps in
|
|
|
|
'appveyor.yml'.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import shutil
|
|
|
|
import logging
|
|
|
|
import subprocess as sp
|
|
|
|
from glob import glob
|
2019-04-15 05:58:38 +03:00
|
|
|
from pathlib import Path
|
2019-04-15 00:43:28 +03:00
|
|
|
from zipfile import ZipFile
|
2020-10-27 13:48:27 +03:00
|
|
|
from argparse import ArgumentParser
|
2019-04-14 22:10:51 +03:00
|
|
|
from tempfile import NamedTemporaryFile
|
2019-04-15 00:43:28 +03:00
|
|
|
from urllib.request import urlopen
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
opt = None
|
|
|
|
STEP_PREFIX = 'step_'
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logging.basicConfig(
|
2019-04-21 14:41:09 +03:00
|
|
|
level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s'
|
2019-04-14 22:10:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global opt
|
|
|
|
opt = parse_cmdline()
|
2019-04-21 14:41:09 +03:00
|
|
|
logger.setLevel(opt.loglevel)
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
cmd = globals()[STEP_PREFIX + opt.step]
|
|
|
|
cmd()
|
|
|
|
|
|
|
|
|
2019-04-21 19:08:35 +03:00
|
|
|
def setup_build_env():
|
2019-04-14 22:10:51 +03:00
|
|
|
"""
|
|
|
|
Set the environment variables according to the build environment
|
|
|
|
"""
|
2019-04-22 14:33:03 +03:00
|
|
|
setenv('VS_VER', opt.vs_ver)
|
2019-04-14 22:10:51 +03:00
|
|
|
|
2019-04-15 00:02:52 +03:00
|
|
|
path = [
|
2019-04-22 14:33:03 +03:00
|
|
|
str(opt.py_dir),
|
|
|
|
str(opt.py_dir / 'Scripts'),
|
2020-10-28 22:15:23 +03:00
|
|
|
r'C:\Strawberry\Perl\bin',
|
2019-04-15 00:02:52 +03:00
|
|
|
r'C:\Program Files\Git\mingw64\bin',
|
2020-11-13 22:17:48 +03:00
|
|
|
str(opt.ssl_build_dir / 'bin'),
|
2019-04-15 00:02:52 +03:00
|
|
|
os.environ['PATH'],
|
|
|
|
]
|
|
|
|
setenv('PATH', os.pathsep.join(path))
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
logger.info("Configuring compiler")
|
2019-04-22 14:33:03 +03:00
|
|
|
bat_call([opt.vc_dir / "vcvarsall.bat", 'x86' if opt.arch_32 else 'amd64'])
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
def python_info():
|
|
|
|
logger.info("Python Information")
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python(['--version'], stderr=sp.STDOUT)
|
|
|
|
run_python(
|
|
|
|
['-c', "import sys; print('64bit: %s' % (sys.maxsize > 2**32))"]
|
2019-04-15 02:38:51 +03:00
|
|
|
)
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
|
2019-04-15 05:51:31 +03:00
|
|
|
def step_install():
|
2019-04-15 04:26:18 +03:00
|
|
|
python_info()
|
2019-04-21 15:33:15 +03:00
|
|
|
configure_sdk()
|
|
|
|
configure_postgres()
|
2019-04-15 04:26:18 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
if opt.is_wheel:
|
2019-04-21 19:08:35 +03:00
|
|
|
install_wheel_support()
|
|
|
|
|
|
|
|
|
|
|
|
def install_wheel_support():
|
|
|
|
"""
|
|
|
|
Install an up-to-date pip wheel package to build wheels.
|
|
|
|
"""
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python("-m pip install --upgrade pip".split())
|
|
|
|
run_python("-m pip install wheel".split())
|
2019-04-21 19:08:35 +03:00
|
|
|
|
2019-04-21 15:33:15 +03:00
|
|
|
|
|
|
|
def configure_sdk():
|
2019-04-14 22:10:51 +03:00
|
|
|
# The program rc.exe on 64bit with some versions look in the wrong path
|
|
|
|
# location when building postgresql. This cheats by copying the x64 bit
|
|
|
|
# files to that location.
|
|
|
|
if opt.arch_64:
|
|
|
|
for fn in glob(
|
|
|
|
r'C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\x64\rc*'
|
|
|
|
):
|
2019-04-15 05:58:38 +03:00
|
|
|
copy_file(
|
2019-04-14 22:10:51 +03:00
|
|
|
fn, r"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"
|
|
|
|
)
|
|
|
|
|
2019-04-21 15:33:15 +03:00
|
|
|
|
|
|
|
def configure_postgres():
|
2019-04-21 19:08:35 +03:00
|
|
|
"""
|
|
|
|
Set up PostgreSQL config before the service starts.
|
|
|
|
"""
|
2019-04-14 22:10:51 +03:00
|
|
|
logger.info("Configuring Postgres")
|
2019-04-22 14:33:03 +03:00
|
|
|
with (opt.pg_data_dir / 'postgresql.conf').open('a') as f:
|
2019-04-14 22:10:51 +03:00
|
|
|
# allow > 1 prepared transactions for test cases
|
|
|
|
print("max_prepared_transactions = 10", file=f)
|
2019-04-21 15:33:15 +03:00
|
|
|
print("ssl = on", file=f)
|
|
|
|
|
|
|
|
# Create openssl certificate to allow ssl connection
|
|
|
|
cwd = os.getcwd()
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.pg_data_dir)
|
2019-04-21 15:33:15 +03:00
|
|
|
run_openssl(
|
|
|
|
'req -new -x509 -days 365 -nodes -text '
|
|
|
|
'-out server.crt -keyout server.key -subj /CN=initd.org'.split()
|
|
|
|
)
|
|
|
|
run_openssl(
|
|
|
|
'req -new -nodes -text -out root.csr -keyout root.key '
|
|
|
|
'-subj /CN=initd.org'.split()
|
|
|
|
)
|
|
|
|
|
|
|
|
run_openssl(
|
|
|
|
'x509 -req -in root.csr -text -days 3650 -extensions v3_ca '
|
|
|
|
'-signkey root.key -out root.crt'.split()
|
|
|
|
)
|
|
|
|
|
|
|
|
run_openssl(
|
|
|
|
'req -new -nodes -text -out server.csr -keyout server.key '
|
|
|
|
'-subj /CN=initd.org'.split()
|
|
|
|
)
|
|
|
|
|
|
|
|
run_openssl(
|
|
|
|
'x509 -req -in server.csr -text -days 365 -CA root.crt '
|
|
|
|
'-CAkey root.key -CAcreateserial -out server.crt'.split()
|
|
|
|
)
|
|
|
|
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
|
|
|
|
|
|
|
def run_openssl(args):
|
2019-04-21 19:08:35 +03:00
|
|
|
"""Run the appveyor-installed openssl with some args."""
|
2019-04-21 15:33:15 +03:00
|
|
|
# https://www.appveyor.com/docs/windows-images-software/
|
|
|
|
openssl = Path(r"C:\OpenSSL-v111-Win64") / 'bin' / 'openssl'
|
|
|
|
return run_command([openssl] + args)
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
|
2019-04-15 05:51:31 +03:00
|
|
|
def step_build_script():
|
2019-04-21 19:08:35 +03:00
|
|
|
setup_build_env()
|
2019-04-15 04:26:18 +03:00
|
|
|
build_openssl()
|
2019-04-15 02:38:51 +03:00
|
|
|
build_libpq()
|
2019-04-15 04:07:03 +03:00
|
|
|
build_psycopg()
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
if opt.is_wheel:
|
2019-04-21 19:08:35 +03:00
|
|
|
build_binary_packages()
|
|
|
|
|
2019-04-15 04:07:03 +03:00
|
|
|
|
2019-04-15 00:43:28 +03:00
|
|
|
def build_openssl():
|
2019-04-22 14:33:03 +03:00
|
|
|
top = opt.ssl_build_dir
|
2019-04-15 05:58:38 +03:00
|
|
|
if (top / 'lib' / 'libssl.lib').exists():
|
2019-04-15 02:38:51 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
logger.info("Building OpenSSL")
|
|
|
|
|
|
|
|
# Setup directories for building OpenSSL libraries
|
2019-04-15 05:58:38 +03:00
|
|
|
ensure_dir(top / 'include' / 'openssl')
|
|
|
|
ensure_dir(top / 'lib')
|
2019-04-15 00:43:28 +03:00
|
|
|
|
|
|
|
# Setup OpenSSL Environment Variables based on processor architecture
|
|
|
|
if opt.arch_32:
|
|
|
|
target = 'VC-WIN32'
|
|
|
|
setenv('VCVARS_PLATFORM', 'x86')
|
|
|
|
else:
|
|
|
|
target = 'VC-WIN64A'
|
|
|
|
setenv('VCVARS_PLATFORM', 'amd64')
|
|
|
|
setenv('CPU', 'AMD64')
|
|
|
|
|
|
|
|
ver = os.environ['OPENSSL_VERSION']
|
|
|
|
|
|
|
|
# Download OpenSSL source
|
|
|
|
zipname = f'OpenSSL_{ver}.zip'
|
2019-04-22 14:33:03 +03:00
|
|
|
zipfile = opt.cache_dir / zipname
|
2019-04-15 05:58:38 +03:00
|
|
|
if not zipfile.exists():
|
2019-04-15 00:43:28 +03:00
|
|
|
download(
|
|
|
|
f"https://github.com/openssl/openssl/archive/{zipname}", zipfile
|
|
|
|
)
|
|
|
|
|
|
|
|
with ZipFile(zipfile) as z:
|
2019-04-22 14:33:03 +03:00
|
|
|
z.extractall(path=opt.build_dir)
|
2019-04-15 00:43:28 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
sslbuild = opt.build_dir / f"openssl-OpenSSL_{ver}"
|
2019-04-22 05:10:31 +03:00
|
|
|
os.chdir(sslbuild)
|
2019-04-15 02:38:51 +03:00
|
|
|
run_command(
|
|
|
|
['perl', 'Configure', target, 'no-asm']
|
|
|
|
+ ['no-shared', 'no-zlib', f'--prefix={top}', f'--openssldir={top}']
|
|
|
|
)
|
2019-04-15 00:43:28 +03:00
|
|
|
|
2020-11-13 22:17:48 +03:00
|
|
|
run_command("nmake build_libs install_sw".split())
|
2019-04-15 00:43:28 +03:00
|
|
|
|
2019-04-15 05:58:38 +03:00
|
|
|
assert (top / 'lib' / 'libssl.lib').exists()
|
2019-04-15 00:43:28 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.clone_dir)
|
2019-04-22 05:10:31 +03:00
|
|
|
shutil.rmtree(sslbuild)
|
2019-04-15 00:43:28 +03:00
|
|
|
|
|
|
|
|
2019-04-15 02:38:51 +03:00
|
|
|
def build_libpq():
|
2019-04-22 14:33:03 +03:00
|
|
|
top = opt.pg_build_dir
|
2019-04-15 05:58:38 +03:00
|
|
|
if (top / 'lib' / 'libpq.lib').exists():
|
2019-04-15 02:38:51 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
logger.info("Building libpq")
|
|
|
|
|
|
|
|
# Setup directories for building PostgreSQL librarires
|
2019-04-15 05:58:38 +03:00
|
|
|
ensure_dir(top / 'include')
|
|
|
|
ensure_dir(top / 'lib')
|
|
|
|
ensure_dir(top / 'bin')
|
2019-04-15 02:38:51 +03:00
|
|
|
|
|
|
|
ver = os.environ['POSTGRES_VERSION']
|
|
|
|
|
|
|
|
# Download PostgreSQL source
|
|
|
|
zipname = f'postgres-REL_{ver}.zip'
|
2019-04-22 14:33:03 +03:00
|
|
|
zipfile = opt.cache_dir / zipname
|
2019-04-15 05:58:38 +03:00
|
|
|
if not zipfile.exists():
|
2019-04-15 02:38:51 +03:00
|
|
|
download(
|
|
|
|
f"https://github.com/postgres/postgres/archive/REL_{ver}.zip",
|
|
|
|
zipfile,
|
|
|
|
)
|
|
|
|
|
|
|
|
with ZipFile(zipfile) as z:
|
2019-04-22 14:33:03 +03:00
|
|
|
z.extractall(path=opt.build_dir)
|
2019-04-15 02:38:51 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
pgbuild = opt.build_dir / f"postgres-REL_{ver}"
|
2019-04-15 02:38:51 +03:00
|
|
|
os.chdir(pgbuild)
|
|
|
|
|
|
|
|
# Setup build config file (config.pl)
|
|
|
|
os.chdir("src/tools/msvc")
|
|
|
|
with open("config.pl", 'w') as f:
|
|
|
|
print(
|
|
|
|
"""\
|
|
|
|
$config->{ldap} = 0;
|
|
|
|
$config->{openssl} = "%s";
|
|
|
|
|
|
|
|
1;
|
|
|
|
"""
|
2019-04-22 14:33:03 +03:00
|
|
|
% str(opt.ssl_build_dir).replace('\\', '\\\\'),
|
2019-04-15 02:38:51 +03:00
|
|
|
file=f,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Hack the Mkvcbuild.pm file so we build the lib version of libpq
|
|
|
|
file_replace('Mkvcbuild.pm', "'libpq', 'dll'", "'libpq', 'lib'")
|
|
|
|
|
|
|
|
# Build libpgport, libpgcommon, libpq
|
|
|
|
run_command([which("build"), "libpgport"])
|
|
|
|
run_command([which("build"), "libpgcommon"])
|
|
|
|
run_command([which("build"), "libpq"])
|
|
|
|
|
|
|
|
# Install includes
|
2019-04-15 05:58:38 +03:00
|
|
|
with (pgbuild / "src/backend/parser/gram.h").open("w") as f:
|
2019-04-15 02:38:51 +03:00
|
|
|
print("", file=f)
|
|
|
|
|
|
|
|
# Copy over built libraries
|
|
|
|
file_replace("Install.pm", "qw(Install)", "qw(Install CopyIncludeFiles)")
|
|
|
|
run_command(
|
|
|
|
["perl", "-MInstall=CopyIncludeFiles", "-e"]
|
|
|
|
+ [f"chdir('../../..'); CopyIncludeFiles('{top}')"]
|
|
|
|
)
|
|
|
|
|
|
|
|
for lib in ('libpgport', 'libpgcommon', 'libpq'):
|
2019-04-15 05:58:38 +03:00
|
|
|
copy_file(pgbuild / f'Release/{lib}/{lib}.lib', top / 'lib')
|
2019-04-15 02:38:51 +03:00
|
|
|
|
|
|
|
# Prepare local include directory for building from
|
|
|
|
for dir in ('win32', 'win32_msvc'):
|
2019-04-15 05:58:38 +03:00
|
|
|
merge_dir(pgbuild / f"src/include/port/{dir}", pgbuild / "src/include")
|
2019-04-15 02:38:51 +03:00
|
|
|
|
|
|
|
# Build pg_config in place
|
2019-04-15 05:58:38 +03:00
|
|
|
os.chdir(pgbuild / 'src/bin/pg_config')
|
2019-04-15 02:38:51 +03:00
|
|
|
run_command(
|
|
|
|
['cl', 'pg_config.c', '/MT', '/nologo', fr'/I{pgbuild}\src\include']
|
|
|
|
+ ['/link', fr'/LIBPATH:{top}\lib']
|
|
|
|
+ ['libpgcommon.lib', 'libpgport.lib', 'advapi32.lib']
|
|
|
|
+ ['/NODEFAULTLIB:libcmt.lib']
|
|
|
|
+ [fr'/OUT:{top}\bin\pg_config.exe']
|
|
|
|
)
|
|
|
|
|
2019-04-15 05:58:38 +03:00
|
|
|
assert (top / 'lib' / 'libpq.lib').exists()
|
|
|
|
assert (top / 'bin' / 'pg_config.exe').exists()
|
2019-04-15 02:38:51 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.clone_dir)
|
2019-04-15 05:58:38 +03:00
|
|
|
shutil.rmtree(pgbuild)
|
2019-04-15 02:38:51 +03:00
|
|
|
|
|
|
|
|
2019-04-15 04:07:03 +03:00
|
|
|
def build_psycopg():
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.package_dir)
|
2019-04-21 19:08:35 +03:00
|
|
|
patch_package_name()
|
|
|
|
add_pg_config_path()
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python(
|
|
|
|
["setup.py", "build_ext", "--have-ssl"]
|
2020-11-13 22:05:20 +03:00
|
|
|
+ ["-l", "libpgcommon libpgport"]
|
2019-04-22 14:33:03 +03:00
|
|
|
+ ["-L", opt.ssl_build_dir / 'lib']
|
|
|
|
+ ['-I', opt.ssl_build_dir / 'include']
|
2019-04-15 04:07:03 +03:00
|
|
|
)
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python(["setup.py", "build_py"])
|
2019-04-21 19:08:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
def patch_package_name():
|
|
|
|
"""Change the psycopg2 package name in the setup.py if required."""
|
2019-04-23 00:39:21 +03:00
|
|
|
if opt.package_name == 'psycopg2':
|
2019-04-21 19:08:35 +03:00
|
|
|
return
|
|
|
|
|
2019-04-23 00:39:21 +03:00
|
|
|
logger.info("changing package name to %s", opt.package_name)
|
2019-04-21 19:08:35 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
with (opt.package_dir / 'setup.py').open() as f:
|
2019-04-21 19:08:35 +03:00
|
|
|
data = f.read()
|
|
|
|
|
|
|
|
# Replace the name of the package with what desired
|
|
|
|
rex = re.compile(r"""name=["']psycopg2["']""")
|
|
|
|
assert len(rex.findall(data)) == 1, rex.findall(data)
|
2019-04-23 00:39:21 +03:00
|
|
|
data = rex.sub(f'name="{opt.package_name}"', data)
|
2019-04-21 19:08:35 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
with (opt.package_dir / 'setup.py').open('w') as f:
|
2019-04-21 19:08:35 +03:00
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
def build_binary_packages():
|
2021-06-30 16:52:27 +03:00
|
|
|
"""Create wheel binary packages."""
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.package_dir)
|
2019-04-21 19:08:35 +03:00
|
|
|
|
|
|
|
add_pg_config_path()
|
|
|
|
|
|
|
|
# Build .whl packages
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python(['setup.py', 'bdist_wheel', "-d", opt.dist_dir])
|
2019-04-21 19:08:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
def step_after_build():
|
2019-04-22 14:33:03 +03:00
|
|
|
if not opt.is_wheel:
|
2019-04-21 19:08:35 +03:00
|
|
|
install_built_package()
|
|
|
|
else:
|
|
|
|
install_binary_package()
|
|
|
|
|
|
|
|
|
|
|
|
def install_built_package():
|
|
|
|
"""Install the package just built by setup build."""
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.package_dir)
|
2019-04-21 19:08:35 +03:00
|
|
|
|
|
|
|
# Install the psycopg just built
|
|
|
|
add_pg_config_path()
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python(["setup.py", "install"])
|
2019-04-15 04:07:03 +03:00
|
|
|
shutil.rmtree("psycopg2.egg-info")
|
|
|
|
|
|
|
|
|
2019-04-21 19:08:35 +03:00
|
|
|
def install_binary_package():
|
|
|
|
"""Install the package from a packaged wheel."""
|
2019-04-22 14:33:03 +03:00
|
|
|
run_python(
|
|
|
|
['-m', 'pip', 'install', '--no-index', '-f', opt.dist_dir]
|
2019-04-23 00:39:21 +03:00
|
|
|
+ [opt.package_name]
|
2019-04-21 19:08:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def add_pg_config_path():
|
|
|
|
"""Allow finding in the path the pg_config just built."""
|
2019-04-22 14:33:03 +03:00
|
|
|
pg_path = str(opt.pg_build_dir / 'bin')
|
2019-04-21 19:08:35 +03:00
|
|
|
if pg_path not in os.environ['PATH'].split(os.pathsep):
|
|
|
|
setenv('PATH', os.pathsep.join([pg_path, os.environ['PATH']]))
|
|
|
|
|
|
|
|
|
2019-04-15 04:26:18 +03:00
|
|
|
def step_before_test():
|
2019-04-21 19:08:35 +03:00
|
|
|
print_psycopg2_version()
|
2019-04-15 04:26:18 +03:00
|
|
|
|
|
|
|
# Create and setup PostgreSQL database for the tests
|
2019-04-22 14:33:03 +03:00
|
|
|
run_command([opt.pg_bin_dir / 'createdb', os.environ['PSYCOPG2_TESTDB']])
|
2019-04-15 04:26:18 +03:00
|
|
|
run_command(
|
2019-04-22 14:33:03 +03:00
|
|
|
[opt.pg_bin_dir / 'psql', '-d', os.environ['PSYCOPG2_TESTDB']]
|
2019-04-15 04:26:18 +03:00
|
|
|
+ ['-c', "CREATE EXTENSION hstore"]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-21 19:08:35 +03:00
|
|
|
def print_psycopg2_version():
|
|
|
|
"""Print psycopg2 and libpq versions installed."""
|
2019-04-15 04:26:18 +03:00
|
|
|
for expr in (
|
|
|
|
'psycopg2.__version__',
|
|
|
|
'psycopg2.__libpq_version__',
|
|
|
|
'psycopg2.extensions.libpq_version()',
|
|
|
|
):
|
2019-04-22 14:33:03 +03:00
|
|
|
out = out_python(['-c', f"import psycopg2; print({expr})"])
|
2019-04-15 04:26:18 +03:00
|
|
|
logger.info("built %s: %s", expr, out.decode('ascii'))
|
|
|
|
|
|
|
|
|
|
|
|
def step_test_script():
|
2019-04-21 19:08:35 +03:00
|
|
|
check_libpq_version()
|
|
|
|
run_test_suite()
|
|
|
|
|
|
|
|
|
|
|
|
def check_libpq_version():
|
|
|
|
"""
|
|
|
|
Fail if the package installed is not using the expected libpq version.
|
|
|
|
"""
|
|
|
|
want_ver = tuple(map(int, os.environ['POSTGRES_VERSION'].split('_')))
|
|
|
|
want_ver = "%d%04d" % want_ver
|
|
|
|
got_ver = (
|
2019-04-22 14:33:03 +03:00
|
|
|
out_python(
|
|
|
|
['-c']
|
2019-04-21 19:08:35 +03:00
|
|
|
+ ["import psycopg2; print(psycopg2.extensions.libpq_version())"]
|
|
|
|
)
|
|
|
|
.decode('ascii')
|
|
|
|
.rstrip()
|
|
|
|
)
|
2020-11-18 00:52:11 +03:00
|
|
|
assert want_ver == got_ver, f"libpq version mismatch: {want_ver!r} != {got_ver!r}"
|
2019-04-21 19:08:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
def run_test_suite():
|
|
|
|
# Remove this var, which would make badly a configured OpenSSL 1.1 work
|
|
|
|
os.environ.pop('OPENSSL_CONF', None)
|
|
|
|
|
|
|
|
# Run the unit test
|
2019-04-22 14:33:03 +03:00
|
|
|
args = [
|
2019-04-22 13:10:47 +03:00
|
|
|
'-c',
|
|
|
|
"import tests; tests.unittest.main(defaultTest='tests.test_suite')",
|
|
|
|
]
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
if opt.is_wheel:
|
2019-04-22 13:10:47 +03:00
|
|
|
os.environ['PSYCOPG2_TEST_FAST'] = '1'
|
|
|
|
else:
|
2019-04-22 14:33:03 +03:00
|
|
|
args.append('--verbose')
|
2019-04-22 13:10:47 +03:00
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.package_dir)
|
|
|
|
run_python(args)
|
2019-04-15 04:26:18 +03:00
|
|
|
|
|
|
|
|
2019-04-21 19:08:35 +03:00
|
|
|
def step_on_success():
|
|
|
|
print_sha1_hashes()
|
|
|
|
if setup_ssh():
|
|
|
|
upload_packages()
|
|
|
|
|
|
|
|
|
|
|
|
def print_sha1_hashes():
|
|
|
|
"""
|
|
|
|
Print the packages sha1 so their integrity can be checked upon signing.
|
|
|
|
"""
|
|
|
|
logger.info("artifacts SHA1 hashes:")
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
os.chdir(opt.package_dir / 'dist')
|
2020-10-27 13:48:27 +03:00
|
|
|
run_command([which('sha1sum'), '-b', 'psycopg2-*/*'])
|
2019-04-21 19:08:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
def setup_ssh():
|
|
|
|
"""
|
|
|
|
Configure ssh to upload built packages where they can be retrieved.
|
|
|
|
|
|
|
|
Return False if can't configure and upload shoould be skipped.
|
|
|
|
"""
|
|
|
|
# If we are not on the psycopg AppVeyor account, the environment variable
|
|
|
|
# REMOTE_KEY will not be decrypted. In that case skip uploading.
|
|
|
|
if os.environ['APPVEYOR_ACCOUNT_NAME'] != 'psycopg':
|
|
|
|
logger.warn("skipping artifact upload: you are not psycopg")
|
|
|
|
return False
|
|
|
|
|
|
|
|
pkey = os.environ.get('REMOTE_KEY', None)
|
|
|
|
if not pkey:
|
|
|
|
logger.warn("skipping artifact upload: no remote key")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Write SSH Private Key file from environment variable
|
|
|
|
pkey = pkey.replace(' ', '\n')
|
2020-01-04 16:24:54 +03:00
|
|
|
with (opt.clone_dir / 'data/id_rsa-psycopg-upload').open('w') as f:
|
2019-04-21 19:08:35 +03:00
|
|
|
f.write(
|
|
|
|
f"""\
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
{pkey}
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make a directory to please MinGW's version of ssh
|
|
|
|
ensure_dir(r"C:\MinGW\msys\1.0\home\appveyor\.ssh")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def upload_packages():
|
|
|
|
# Upload built artifacts
|
|
|
|
logger.info("uploading artifacts")
|
|
|
|
|
2020-01-04 16:24:54 +03:00
|
|
|
os.chdir(opt.clone_dir)
|
2019-04-21 19:08:35 +03:00
|
|
|
run_command(
|
|
|
|
[r"C:\MinGW\msys\1.0\bin\rsync", "-avr"]
|
2020-01-04 16:24:54 +03:00
|
|
|
+ ["-e", r"C:\MinGW\msys\1.0\bin\ssh -F data/ssh_config"]
|
|
|
|
+ ["psycopg2/dist/", "upload:"]
|
2019-04-21 19:08:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-15 00:43:28 +03:00
|
|
|
def download(url, fn):
|
|
|
|
"""Download a file locally"""
|
2019-04-15 02:38:51 +03:00
|
|
|
logger.info("downloading %s", url)
|
2019-04-15 00:43:28 +03:00
|
|
|
with open(fn, 'wb') as fo, urlopen(url) as fi:
|
|
|
|
while 1:
|
|
|
|
data = fi.read(8192)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
fo.write(data)
|
|
|
|
|
2019-04-15 02:38:51 +03:00
|
|
|
logger.info("file downloaded: %s", fn)
|
|
|
|
|
|
|
|
|
|
|
|
def file_replace(fn, s1, s2):
|
|
|
|
"""
|
|
|
|
Replace all the occurrences of the string s1 into s2 in the file fn.
|
|
|
|
"""
|
|
|
|
assert os.path.exists(fn)
|
|
|
|
with open(fn, 'r+') as f:
|
|
|
|
data = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
f.write(data.replace(s1, s2))
|
|
|
|
f.truncate()
|
|
|
|
|
|
|
|
|
|
|
|
def merge_dir(src, tgt):
|
|
|
|
"""
|
|
|
|
Merge the content of the directory src into the directory tgt
|
|
|
|
|
|
|
|
Reproduce the semantic of "XCOPY /Y /S src/* tgt"
|
|
|
|
"""
|
2019-04-15 05:58:38 +03:00
|
|
|
src = str(src)
|
2019-04-15 02:38:51 +03:00
|
|
|
for dp, _dns, fns in os.walk(src):
|
|
|
|
logger.debug("dirpath %s", dp)
|
|
|
|
if not fns:
|
|
|
|
continue
|
|
|
|
assert dp.startswith(src)
|
|
|
|
subdir = dp[len(src) :].lstrip(os.sep)
|
|
|
|
tgtdir = ensure_dir(os.path.join(tgt, subdir))
|
|
|
|
for fn in fns:
|
2019-04-15 05:58:38 +03:00
|
|
|
copy_file(os.path.join(dp, fn), tgtdir)
|
2019-04-15 02:38:51 +03:00
|
|
|
|
2019-04-15 00:43:28 +03:00
|
|
|
|
2019-04-14 22:10:51 +03:00
|
|
|
def bat_call(cmdline):
|
|
|
|
"""
|
|
|
|
Simulate 'CALL' from a batch file
|
|
|
|
|
|
|
|
Execute CALL *cmdline* and export the changed environment to the current
|
|
|
|
environment.
|
|
|
|
|
|
|
|
nana-nana-nana-nana...
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not isinstance(cmdline, str):
|
2019-04-15 05:58:38 +03:00
|
|
|
cmdline = map(str, cmdline)
|
2019-04-14 22:10:51 +03:00
|
|
|
cmdline = ' '.join(c if ' ' not in c else '"%s"' % c for c in cmdline)
|
|
|
|
|
|
|
|
data = f"""\
|
|
|
|
CALL {cmdline}
|
2019-04-22 14:33:03 +03:00
|
|
|
{opt.py_exe} -c "import os, sys, json; \
|
|
|
|
json.dump(dict(os.environ), sys.stdout, indent=2)"
|
2019-04-14 22:10:51 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
logger.debug("preparing file to batcall:\n\n%s", data)
|
|
|
|
|
|
|
|
with NamedTemporaryFile(suffix='.bat') as tmp:
|
|
|
|
fn = tmp.name
|
|
|
|
|
|
|
|
with open(fn, "w") as f:
|
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
try:
|
2019-04-15 02:38:51 +03:00
|
|
|
out = out_command(fn)
|
2019-04-14 22:10:51 +03:00
|
|
|
# be vewwy vewwy caweful to print the env var as it might contain
|
|
|
|
# secwet things like your pwecious pwivate key.
|
|
|
|
# logger.debug("output of command:\n\n%s", out.decode('utf8', 'replace'))
|
|
|
|
|
|
|
|
# The output has some useless crap on stdout, because sure, and json
|
|
|
|
# indented so the last { on column 1 is where we have to start parsing
|
|
|
|
|
|
|
|
m = list(re.finditer(b'^{', out, re.MULTILINE))[-1]
|
|
|
|
out = out[m.start() :]
|
|
|
|
env = json.loads(out)
|
|
|
|
for k, v in env.items():
|
|
|
|
if os.environ.get(k) != v:
|
2019-04-15 00:02:52 +03:00
|
|
|
setenv(k, v)
|
2019-04-14 22:10:51 +03:00
|
|
|
finally:
|
|
|
|
os.remove(fn)
|
|
|
|
|
|
|
|
|
2019-04-15 00:43:28 +03:00
|
|
|
def ensure_dir(dir):
|
2019-04-15 05:58:38 +03:00
|
|
|
if not isinstance(dir, Path):
|
|
|
|
dir = Path(dir)
|
|
|
|
|
|
|
|
if not dir.is_dir():
|
2019-04-15 00:43:28 +03:00
|
|
|
logger.info("creating directory %s", dir)
|
2019-04-15 05:58:38 +03:00
|
|
|
dir.mkdir(parents=True)
|
2019-04-15 00:43:28 +03:00
|
|
|
|
|
|
|
return dir
|
|
|
|
|
|
|
|
|
2019-04-15 02:38:51 +03:00
|
|
|
def run_command(cmdline, **kwargs):
|
2019-04-22 14:33:03 +03:00
|
|
|
"""Run a command, raise on error."""
|
2019-04-15 05:58:38 +03:00
|
|
|
if not isinstance(cmdline, str):
|
|
|
|
cmdline = list(map(str, cmdline))
|
2019-04-21 14:41:09 +03:00
|
|
|
logger.info("running command: %s", cmdline)
|
2019-04-15 02:38:51 +03:00
|
|
|
sp.check_call(cmdline, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def out_command(cmdline, **kwargs):
|
2019-04-22 14:33:03 +03:00
|
|
|
"""Run a command, return its output, raise on error."""
|
2019-04-15 05:58:38 +03:00
|
|
|
if not isinstance(cmdline, str):
|
|
|
|
cmdline = list(map(str, cmdline))
|
2019-04-21 14:41:09 +03:00
|
|
|
logger.info("running command: %s", cmdline)
|
2019-04-15 02:38:51 +03:00
|
|
|
data = sp.check_output(cmdline, **kwargs)
|
|
|
|
return data
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
def run_python(args, **kwargs):
|
|
|
|
"""
|
|
|
|
Run a script in the target Python.
|
|
|
|
"""
|
|
|
|
return run_command([opt.py_exe] + args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def out_python(args, **kwargs):
|
|
|
|
"""
|
|
|
|
Return the output of a script run in the target Python.
|
|
|
|
"""
|
|
|
|
return out_command([opt.py_exe] + args, **kwargs)
|
|
|
|
|
|
|
|
|
2019-04-15 05:58:38 +03:00
|
|
|
def copy_file(src, dst):
|
|
|
|
logger.info("copying file %s -> %s", src, dst)
|
|
|
|
shutil.copy(src, dst)
|
|
|
|
|
|
|
|
|
2019-04-15 00:02:52 +03:00
|
|
|
def setenv(k, v):
|
2019-04-15 02:38:51 +03:00
|
|
|
logger.debug("setting %s=%s", k, v)
|
2019-04-15 00:02:52 +03:00
|
|
|
os.environ[k] = v
|
|
|
|
|
|
|
|
|
2019-04-15 02:38:51 +03:00
|
|
|
def which(name):
|
|
|
|
"""
|
|
|
|
Return the full path of a command found on the path
|
|
|
|
"""
|
|
|
|
base, ext = os.path.splitext(name)
|
|
|
|
if not ext:
|
|
|
|
exts = ('.com', '.exe', '.bat', '.cmd')
|
|
|
|
else:
|
|
|
|
exts = (ext,)
|
|
|
|
|
|
|
|
for dir in ['.'] + os.environ['PATH'].split(os.pathsep):
|
|
|
|
for ext in exts:
|
|
|
|
fn = os.path.join(dir, base + ext)
|
|
|
|
if os.path.isfile(fn):
|
|
|
|
return fn
|
|
|
|
|
2020-11-18 00:52:11 +03:00
|
|
|
raise Exception(f"couldn't find program on path: {name}")
|
2019-04-15 02:38:51 +03:00
|
|
|
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
class Options:
|
|
|
|
"""
|
|
|
|
An object exposing the script configuration from env vars and command line.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def py_ver(self):
|
2022-11-08 00:07:56 +03:00
|
|
|
"""The Python version to build as 2 digits string.
|
|
|
|
|
|
|
|
For large values of 2, occasionally.
|
|
|
|
"""
|
2019-04-22 14:33:03 +03:00
|
|
|
rv = os.environ['PY_VER']
|
2023-10-03 12:25:15 +03:00
|
|
|
assert rv in ('37', '38', '39', '310', '311'), rv
|
2019-04-22 14:33:03 +03:00
|
|
|
return rv
|
|
|
|
|
|
|
|
@property
|
|
|
|
def py_arch(self):
|
|
|
|
"""The Python architecture to build, 32 or 64."""
|
|
|
|
rv = os.environ['PY_ARCH']
|
|
|
|
assert rv in ('32', '64'), rv
|
|
|
|
return int(rv)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def arch_32(self):
|
|
|
|
"""True if the Python architecture to build is 32 bits."""
|
|
|
|
return self.py_arch == 32
|
|
|
|
|
|
|
|
@property
|
|
|
|
def arch_64(self):
|
|
|
|
"""True if the Python architecture to build is 64 bits."""
|
|
|
|
return self.py_arch == 64
|
|
|
|
|
2019-04-23 00:39:21 +03:00
|
|
|
@property
|
|
|
|
def package_name(self):
|
|
|
|
return os.environ.get('CONFIGURATION', 'psycopg2')
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
@property
|
|
|
|
def package_version(self):
|
|
|
|
"""The psycopg2 version number to build."""
|
|
|
|
with (self.package_dir / 'setup.py').open() as f:
|
|
|
|
data = f.read()
|
|
|
|
|
|
|
|
m = re.search(
|
|
|
|
r"""^PSYCOPG_VERSION\s*=\s*['"](.*)['"]""", data, re.MULTILINE
|
|
|
|
)
|
|
|
|
return m.group(1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_wheel(self):
|
|
|
|
"""Are we building the wheel packages or just the extension?"""
|
2021-05-25 14:59:19 +03:00
|
|
|
workflow = os.environ["WORKFLOW"]
|
|
|
|
return workflow == "packages"
|
2019-04-22 14:33:03 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def py_dir(self):
|
|
|
|
"""
|
|
|
|
The path to the target python binary to execute.
|
|
|
|
"""
|
|
|
|
dirname = ''.join(
|
|
|
|
[r"C:\Python", self.py_ver, '-x64' if self.arch_64 else '']
|
|
|
|
)
|
|
|
|
return Path(dirname)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def py_exe(self):
|
|
|
|
"""
|
|
|
|
The full path of the target python executable.
|
|
|
|
"""
|
|
|
|
return self.py_dir / 'python.exe'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def vc_dir(self):
|
|
|
|
"""
|
|
|
|
The path of the Visual C compiler.
|
|
|
|
"""
|
2020-10-28 21:50:57 +03:00
|
|
|
if self.vs_ver == '16.0':
|
|
|
|
path = Path(
|
2021-05-24 12:17:01 +03:00
|
|
|
r"C:\Program Files (x86)\Microsoft Visual Studio\2019"
|
|
|
|
r"\Community\VC\Auxiliary\Build"
|
2020-10-28 21:50:57 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
path = Path(
|
|
|
|
r"C:\Program Files (x86)\Microsoft Visual Studio %s\VC"
|
|
|
|
% self.vs_ver
|
|
|
|
)
|
|
|
|
return path
|
2019-04-22 14:33:03 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def vs_ver(self):
|
2019-10-19 15:35:05 +03:00
|
|
|
# https://wiki.python.org/moin/WindowsCompilers
|
2020-10-27 13:48:27 +03:00
|
|
|
# https://www.appveyor.com/docs/windows-images-software/#python
|
2020-11-17 23:17:12 +03:00
|
|
|
# Py 3.6--3.8 = VS Ver. 14.0 (VS 2015)
|
2020-10-27 13:48:27 +03:00
|
|
|
# Py 3.9 = VS Ver. 16.0 (VS 2019)
|
2019-04-22 14:33:03 +03:00
|
|
|
vsvers = {
|
|
|
|
'37': '14.0',
|
2019-10-19 15:35:05 +03:00
|
|
|
'38': '14.0',
|
2020-10-27 13:48:27 +03:00
|
|
|
'39': '16.0',
|
2021-10-05 01:44:09 +03:00
|
|
|
'310': '16.0',
|
2022-11-08 00:07:56 +03:00
|
|
|
'311': '16.0',
|
2019-04-22 14:33:03 +03:00
|
|
|
}
|
|
|
|
return vsvers[self.py_ver]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def clone_dir(self):
|
|
|
|
"""The directory where the repository is cloned."""
|
|
|
|
return Path(r"C:\Project")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def appveyor_pg_dir(self):
|
|
|
|
"""The directory of the postgres service made available by Appveyor."""
|
|
|
|
return Path(os.environ['POSTGRES_DIR'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pg_data_dir(self):
|
|
|
|
"""The data dir of the appveyor postgres service."""
|
|
|
|
return self.appveyor_pg_dir / 'data'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pg_bin_dir(self):
|
|
|
|
"""The bin dir of the appveyor postgres service."""
|
|
|
|
return self.appveyor_pg_dir / 'bin'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pg_build_dir(self):
|
|
|
|
"""The directory where to build the postgres libraries for psycopg."""
|
|
|
|
return self.cache_arch_dir / 'postgresql'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ssl_build_dir(self):
|
|
|
|
"""The directory where to build the openssl libraries for psycopg."""
|
|
|
|
return self.cache_arch_dir / 'openssl'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def cache_arch_dir(self):
|
|
|
|
rv = self.cache_dir / str(self.py_arch) / self.vs_ver
|
|
|
|
return ensure_dir(rv)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def cache_dir(self):
|
|
|
|
return Path(r"C:\Others")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def build_dir(self):
|
|
|
|
rv = self.cache_arch_dir / 'Builds'
|
|
|
|
return ensure_dir(rv)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def package_dir(self):
|
2021-05-25 14:59:19 +03:00
|
|
|
return self.clone_dir
|
2019-04-22 14:33:03 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def dist_dir(self):
|
|
|
|
"""The directory where to build packages to distribute."""
|
|
|
|
return (
|
2020-11-18 00:52:11 +03:00
|
|
|
self.package_dir / 'dist' / (f'psycopg2-{self.package_version}')
|
2019-04-22 14:33:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-14 22:10:51 +03:00
|
|
|
def parse_cmdline():
|
|
|
|
parser = ArgumentParser(description=__doc__)
|
|
|
|
|
2019-04-21 14:41:09 +03:00
|
|
|
g = parser.add_mutually_exclusive_group()
|
|
|
|
g.add_argument(
|
|
|
|
'-q',
|
|
|
|
'--quiet',
|
|
|
|
help="Talk less",
|
|
|
|
dest='loglevel',
|
|
|
|
action='store_const',
|
|
|
|
const=logging.WARN,
|
|
|
|
default=logging.INFO,
|
|
|
|
)
|
|
|
|
g.add_argument(
|
|
|
|
'-v',
|
|
|
|
'--verbose',
|
|
|
|
help="Talk more",
|
|
|
|
dest='loglevel',
|
|
|
|
action='store_const',
|
|
|
|
const=logging.DEBUG,
|
|
|
|
default=logging.INFO,
|
|
|
|
)
|
|
|
|
|
2019-04-14 22:10:51 +03:00
|
|
|
steps = [
|
|
|
|
n[len(STEP_PREFIX) :]
|
|
|
|
for n in globals()
|
|
|
|
if n.startswith(STEP_PREFIX) and callable(globals()[n])
|
|
|
|
]
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'step', choices=steps, help="the appveyor step to execute"
|
|
|
|
)
|
|
|
|
|
2019-04-22 14:33:03 +03:00
|
|
|
opt = parser.parse_args(namespace=Options())
|
2019-04-14 22:10:51 +03:00
|
|
|
|
|
|
|
return opt
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|