|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# vim: set ts=4 sw=4 tw=99 et: |
| 3 | +import argparse |
| 4 | +import datetime |
| 5 | +import ftplib |
| 6 | +import os |
| 7 | +import platform |
| 8 | +import re |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +import time |
| 12 | + |
| 13 | +kPackages = ['base', 'cstrike', 'dod', 'esf', 'ns', 'tfc', 'ts'] |
| 14 | +kMacExclude = set(['esf', 'ns', 'ts']) |
| 15 | + |
| 16 | +class PackageBuilder(object): |
| 17 | + def __init__(self, args): |
| 18 | + self.args_ = args |
| 19 | + |
| 20 | + if platform.system() == 'Linux': |
| 21 | + self.os_ = 'linux' |
| 22 | + elif platform.system() == 'Windows': |
| 23 | + self.os_ = 'windows' |
| 24 | + elif platform.system() == 'Darwin': |
| 25 | + self.os_ = 'mac' |
| 26 | + else: |
| 27 | + raise Exception('Unknown platform: {}'.format(platform.system())) |
| 28 | + |
| 29 | + if self.os_ == 'linux': |
| 30 | + self.archive_type_ = 'tar.gz' |
| 31 | + else: |
| 32 | + self.archive_type_ = 'zip' |
| 33 | + self.packages_ = [] |
| 34 | + |
| 35 | + def run(self): |
| 36 | + self.read_ftp_info() |
| 37 | + self.read_product_version() |
| 38 | + |
| 39 | + # Switch to the output folder. |
| 40 | + with Chdir(os.path.join('..', 'OUTPUT')): |
| 41 | + self.build() |
| 42 | + self.upload() |
| 43 | + |
| 44 | + def build(self): |
| 45 | + print("Creating package for {}".format(self.version_)) |
| 46 | + |
| 47 | + for package in kPackages: |
| 48 | + if self.os_ == 'mac' and package in kMacExclude: |
| 49 | + continue |
| 50 | + |
| 51 | + package_dir = os.path.join('packages', package) |
| 52 | + with Chdir(package_dir): |
| 53 | + package_name, alt_name = self.build_package(package) |
| 54 | + |
| 55 | + if os.path.exists(package_name): |
| 56 | + os.unlink(package_name) |
| 57 | + |
| 58 | + src_path = os.path.join(package_dir, package_name) |
| 59 | + shutil.move(src_path, package_name) |
| 60 | + |
| 61 | + self.packages_.append((package_name, alt_name)) |
| 62 | + |
| 63 | + def upload(self): |
| 64 | + |
| 65 | + m = re.search("^(\d+)\.(\d+)", self.version_) |
| 66 | + ftp_path = '{}/{}.{}'.format(self.ftp_path_, m.group(1), m.group(2)) |
| 67 | + |
| 68 | + print("Connecting to drop site: {}".format(ftp_path)) |
| 69 | + ftp = ftplib.FTP(self.ftp_host_) |
| 70 | + ftp.login(self.ftp_user_, self.ftp_pass_) |
| 71 | + ftp.set_pasv(True) |
| 72 | + ftp.cwd(ftp_path) |
| 73 | + |
| 74 | + for package_file, alt_file in self.packages_: |
| 75 | + self.upload_package(ftp, package_file, package_file) |
| 76 | + self.upload_package(ftp, package_file, alt_file) |
| 77 | + |
| 78 | + print("Files sent to drop site -- build succeeded.") |
| 79 | + |
| 80 | + def upload_package(self, ftp, package_file, dest_name): |
| 81 | + with open(package_file, 'rb') as fp: |
| 82 | + print("Sending {} as {}".format(package_file, dest_name)) |
| 83 | + ftp.storbinary('STOR {}'.format(dest_name), fp) |
| 84 | + |
| 85 | + def build_package(self, package): |
| 86 | + package_file = 'amxmodx-{}-{}-{}.{}'.format(self.version_, package, self.os_, |
| 87 | + self.archive_type_) |
| 88 | + if os.path.exists(package_file): |
| 89 | + os.unlink(package_file) |
| 90 | + |
| 91 | + if self.archive_type_ == 'tar.gz': |
| 92 | + Run(['tar', 'zcvf', package_file, 'addons']) |
| 93 | + else: |
| 94 | + Run(['zip', '-r', package_file, 'addons']) |
| 95 | + |
| 96 | + alt_name = 'amxmodx-latest-{}-{}.{}'.format(package, self.os_, self.archive_type_) |
| 97 | + return package_file, alt_name |
| 98 | + |
| 99 | + def read_ftp_info(self): |
| 100 | + with open(self.args_.ftp_file, 'rt') as fp: |
| 101 | + lines = [line.strip() for line in fp.read().splitlines()] |
| 102 | + self.ftp_host_ = lines[0] |
| 103 | + self.ftp_user_ = lines[1] |
| 104 | + self.ftp_pass_ = lines[2] |
| 105 | + self.ftp_path_ = lines[3] |
| 106 | + |
| 107 | + def read_product_version(self): |
| 108 | + with open('product.version', 'rt') as fp: |
| 109 | + self.version_ = fp.read().strip() |
| 110 | + |
| 111 | + output = subprocess.check_output(['git', 'rev-list', '--count', 'HEAD'], |
| 112 | + universal_newlines = True, |
| 113 | + stderr = subprocess.STDOUT) |
| 114 | + output = output.strip() |
| 115 | + if output: |
| 116 | + self.version_ += '-git' + output |
| 117 | + |
| 118 | +def main(): |
| 119 | + parser = argparse.ArgumentParser() |
| 120 | + parser.add_argument('ftp_file', type = str, help = 'FTP config file') |
| 121 | + |
| 122 | + args = parser.parse_args() |
| 123 | + |
| 124 | + builder = PackageBuilder(args) |
| 125 | + builder.run() |
| 126 | + |
| 127 | +class Chdir(object): |
| 128 | + def __init__(self, target): |
| 129 | + self.prevdir_ = None |
| 130 | + self.target_ = target |
| 131 | + |
| 132 | + def __enter__(self): |
| 133 | + prevdir = os.getcwd() |
| 134 | + os.chdir(self.target_) |
| 135 | + self.prevdir_ = prevdir |
| 136 | + return self |
| 137 | + |
| 138 | + def __exit__(self, exc_type, exc_value, traceback): |
| 139 | + if self.prevdir_: |
| 140 | + os.chdir(self.prevdir_) |
| 141 | + return False |
| 142 | + |
| 143 | +def Run(argv): |
| 144 | + try: |
| 145 | + output = subprocess.check_output(argv, stderr = subprocess.STDOUT, |
| 146 | + universal_newlines = True) |
| 147 | + print(output) |
| 148 | + except subprocess.CalledProcessError as e: |
| 149 | + print(e.output.decode('utf-8')) |
| 150 | + raise |
| 151 | + |
| 152 | +if __name__ == '__main__': |
| 153 | + main() |
0 commit comments