|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# for python 2.x |
| 4 | + |
| 5 | +from argparse import ArgumentParser, RawTextHelpFormatter |
| 6 | + |
| 7 | +import os |
| 8 | +import hashlib |
| 9 | + |
| 10 | +class AgentVersionItem(object): |
| 11 | + def __init__(self, name): |
| 12 | + self.name = str(name) |
| 13 | + self.md5 = '' |
| 14 | + self.filepath = '' |
| 15 | + |
| 16 | + @property |
| 17 | + def compatible_text(self): |
| 18 | + return 'compatible-version-of-zwatch-vm-agent.%s=4.2.0' % self.name |
| 19 | + |
| 20 | + @property |
| 21 | + def md5_text(self): |
| 22 | + return 'md5-zwatch-vm-agent.%s=%s' % (self.name, self.md5) |
| 23 | + |
| 24 | + def with_filepath(self, filepath): |
| 25 | + self.filepath = filepath |
| 26 | + return self |
| 27 | + |
| 28 | + def calculate_md5(self): |
| 29 | + if not os.path.exists(self.filepath): |
| 30 | + return |
| 31 | + with open(self.filepath, 'r') as f: |
| 32 | + # file size < 15M |
| 33 | + m = hashlib.md5(f.read()) |
| 34 | + self.md5 = m.hexdigest() |
| 35 | + |
| 36 | + def is_valid(self): |
| 37 | + return bool(self.md5) |
| 38 | + |
| 39 | +class AgentVersionBuilder(object): |
| 40 | + def __init__(self, sourcepath, outputpath): |
| 41 | + self.sourcepath = str(sourcepath).strip() |
| 42 | + self.outputpath = str(outputpath).strip() |
| 43 | + self.items = [ |
| 44 | + AgentVersionItem('linux-amd64').with_filepath('zwatch-vm-agent'), |
| 45 | + AgentVersionItem('linux-aarch64').with_filepath('zwatch-vm-agent_aarch64'), |
| 46 | + AgentVersionItem('linux-mips64el').with_filepath('zwatch-vm-agent_mips64el'), |
| 47 | + AgentVersionItem('linux-loongarch64').with_filepath('zwatch-vm-agent_loongarch64'), |
| 48 | + AgentVersionItem('freebsd-amd64').with_filepath('zwatch-vm-agent-freebsd') |
| 49 | + ] |
| 50 | + |
| 51 | + def _read_version_from_source(self): |
| 52 | + version_line = '' |
| 53 | + path = os.path.join(self.sourcepath, 'src', 'zwatch-vm-agent', 'utils', 'config.go') |
| 54 | + with open(path, 'r') as f: |
| 55 | + for l in f.readlines(): |
| 56 | + index = l.find('VERSION') |
| 57 | + if index == -1 or l.find('=', index + len('VERSION')) == -1: |
| 58 | + continue |
| 59 | + version_line = l |
| 60 | + break |
| 61 | + |
| 62 | + if version_line == '': |
| 63 | + raise Exception('failed to find version in file %s' % path) |
| 64 | + text = version_line.split()[-1] |
| 65 | + if text.startswith('"'): |
| 66 | + text = text[1:-1] |
| 67 | + return text |
| 68 | + |
| 69 | + def write(self): |
| 70 | + lines_for_compatible = '' |
| 71 | + lines_for_md5 = '' |
| 72 | + |
| 73 | + for item in self.items: |
| 74 | + item.calculate_md5() |
| 75 | + if not item.is_valid(): |
| 76 | + continue |
| 77 | + lines_for_compatible += ('# %s\n' % item.compatible_text) # must start with '#' |
| 78 | + lines_for_md5 += ('%s\n' % item.md5_text) |
| 79 | + |
| 80 | + with open(self.outputpath, 'w') as f: |
| 81 | + f.write('''zwatch-vm-agent=%s |
| 82 | +
|
| 83 | +# This agent version text file is generated by agent_version_generator_py2 script, |
| 84 | +# and the comments below are due to compatibility (version <= 4.2). |
| 85 | +# Please DO NOT DELETE or MODIFY them! |
| 86 | +# See more in ZSTAC-41487 |
| 87 | +# |
| 88 | +%s |
| 89 | +%s''' % (self._read_version_from_source(), lines_for_compatible, lines_for_md5)) |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + parser = ArgumentParser("agent_version_generator_py2", description=__doc__, formatter_class=RawTextHelpFormatter) |
| 93 | + parser.add_argument('-s', '--sourcepath', required=True, default="", help='the source path for zstack-zwatch project') |
| 94 | + parser.add_argument('-o', '--outputpath', required=True, default="", help='the agent version file path for output') |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + builder = AgentVersionBuilder(args.sourcepath, args.outputpath) |
| 98 | + builder.write() |
0 commit comments