1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import os
4import re
5import subprocess
6from cpt.packager import ConanMultiPackager
7
8
9def set_appveyor_environment():
10    if os.getenv("APPVEYOR") is not None:
11        compiler_version = os.getenv("CMAKE_VS_VERSION").split(" ")[0].replace('"', '')
12        os.environ["CONAN_VISUAL_VERSIONS"] = compiler_version
13        os.environ["CONAN_STABLE_BRANCH_PATTERN"] = "master"
14        ci_platform = os.getenv("Platform").replace('"', '')
15        ci_platform = "x86" if ci_platform == "x86" else "x86_64"
16        os.environ["CONAN_ARCHS"] = ci_platform
17        os.environ["CONAN_BUILD_TYPES"] = os.getenv("Configuration").replace('"', '')
18
19
20def get_branch():
21    try:
22        for line in subprocess.check_output("git branch", shell=True).decode().splitlines():
23            line = line.strip()
24            if line.startswith("*") and " (HEAD detached" not in line:
25                return line.replace("*", "", 1).strip()
26        return ""
27    except Exception:
28        pass
29    return ""
30
31
32def get_version():
33    version = get_branch()
34    if os.getenv("TRAVIS", False):
35        version = os.getenv("TRAVIS_BRANCH")
36
37    if os.getenv("APPVEYOR", False):
38        version = os.getenv("APPVEYOR_REPO_BRANCH")
39        if os.getenv("APPVEYOR_REPO_TAG") == "true":
40            version = os.getenv("APPVEYOR_REPO_TAG_NAME")
41
42    match = re.search(r"v(\d+\.\d+\.\d+.*)", version)
43    if match:
44        return match.group(1)
45    return version
46
47
48def get_reference(username):
49    return "flatbuffers/{}@google/stable".format(get_version())
50
51
52if __name__ == "__main__":
53    login_username = os.getenv("CONAN_LOGIN_USERNAME", "aardappel")
54    username = os.getenv("CONAN_USERNAME", "google")
55    upload = os.getenv("CONAN_UPLOAD", "https://api.bintray.com/conan/aardappel/flatbuffers")
56    stable_branch_pattern = os.getenv("CONAN_STABLE_BRANCH_PATTERN", r"v\d+\.\d+\.\d+.*")
57    test_folder = os.getenv("CPT_TEST_FOLDER", os.path.join("conan", "test_package"))
58    upload_only_when_stable = os.getenv("CONAN_UPLOAD_ONLY_WHEN_STABLE", True)
59    set_appveyor_environment()
60
61    builder = ConanMultiPackager(reference=get_reference(username),
62                                 username=username,
63                                 login_username=login_username,
64                                 upload=upload,
65                                 stable_branch_pattern=stable_branch_pattern,
66                                 upload_only_when_stable=upload_only_when_stable,
67                                 test_folder=test_folder)
68    builder.add_common_builds(pure_c=False)
69    builder.run()
70