|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright (C) 2023 The C++ Plus Project. |
| 4 | +# This file is part of the cppp library. |
| 5 | +# |
| 6 | +# The cppp library is free software; you can redistribute it |
| 7 | +# and/or modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either version 3 |
| 9 | +# of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# The cppp library is distributed in the hope that it will be |
| 12 | +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public |
| 17 | +# License along with the cppp library; see the file COPYING. |
| 18 | +# If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +# C++ Plus dist utils. |
| 21 | + |
| 22 | +import os |
| 23 | +import sys |
| 24 | +import json |
| 25 | +import shutil |
| 26 | +import importlib.util |
| 27 | + |
| 28 | +PACKAGE_INFO = "CPPPPKG" |
| 29 | +PROGNAME = "cpppdist.py" |
| 30 | + |
| 31 | +def import_file(path): |
| 32 | + """ |
| 33 | + Import a Python file from it's path |
| 34 | +
|
| 35 | + Args: |
| 36 | + path (str): Python file's path |
| 37 | +
|
| 38 | + Raises: |
| 39 | + ModuleNotFoundError: When module invalid, load error or not found. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + ModuleType: module |
| 43 | + """ |
| 44 | + spec = importlib.util.spec_from_file_location(os.path.basename(path).replace(".py", ""), path) |
| 45 | + if(spec == None): |
| 46 | + raise ModuleNotFoundError("Invalid module or module not found: " + path) |
| 47 | + module = importlib.util.module_from_spec(spec) |
| 48 | + spec.loader.exec_module(module) |
| 49 | + return module |
| 50 | + |
| 51 | +class Package: |
| 52 | + """ |
| 53 | + A dist package type |
| 54 | + """ |
| 55 | + name = "" |
| 56 | + version = "" |
| 57 | + list_file_path = "" |
| 58 | + filelist = [] |
| 59 | + subpackages = {} |
| 60 | + |
| 61 | + def __init__(self, pkginfo_filepath) -> None: |
| 62 | + try: |
| 63 | + os.chdir(fwd) |
| 64 | + with open(pkginfo_filepath, "r", encoding="UTF-8") as info_file: |
| 65 | + data = json.load(info_file) |
| 66 | + self.name = data["name"] |
| 67 | + self.version = data["version"] |
| 68 | + self.list_file_path = data["list_file_path"] |
| 69 | + try: |
| 70 | + _subpackages = data["subpackages"] |
| 71 | + for pkgname in _subpackages: |
| 72 | + pkgpath = _subpackages[pkgname]["path"] |
| 73 | + try: |
| 74 | + pkgmodule = import_file(os.path.join(pkgpath, PROGNAME)) |
| 75 | + self.subpackages[pkgpath] = pkgmodule.package |
| 76 | + except Exception as e: |
| 77 | + if(not _subpackages[pkgname]["ignore"]): |
| 78 | + raise |
| 79 | + else: |
| 80 | + print(f"DEBUG: Ignore a subpackage '{pkgname}': {e}", file=sys.stderr) |
| 81 | + except KeyError: |
| 82 | + pass |
| 83 | + self.filelist = self.get_file_list() |
| 84 | + finally: |
| 85 | + os.chdir(cwd) |
| 86 | + |
| 87 | + def get_file_list(self) -> list: |
| 88 | + """ |
| 89 | + Get file list for dist. |
| 90 | + """ |
| 91 | + try: |
| 92 | + os.chdir(fwd) |
| 93 | + file_list = [] |
| 94 | + with open(self.list_file_path, "rt", encoding="UTF-8") as list_file: |
| 95 | + data = list_file.read() |
| 96 | + file_list = data.strip().split("\n") |
| 97 | + return file_list |
| 98 | + finally: |
| 99 | + os.chdir(cwd) |
| 100 | + |
| 101 | + def copy_files(self, dest): |
| 102 | + """ |
| 103 | + Copy files to dist dest |
| 104 | + """ |
| 105 | + try: |
| 106 | + os.chdir(fwd) |
| 107 | + if(os.path.exists(dest)): |
| 108 | + shutil.rmtree(dest) |
| 109 | + print(f"Copy package '{self.name}' to '{dest}' ... ", file=sys.stderr) |
| 110 | + progressbar = ProgressBar(len(self.filelist)) |
| 111 | + for file in self.filelist: |
| 112 | + relpath = os.path.relpath(file, fwd) |
| 113 | + filedir = os.path.abspath(os.path.join(dest, relpath, "..")) |
| 114 | + if(not os.path.exists(filedir)): |
| 115 | + os.makedirs(filedir) |
| 116 | + shutil.copy(file, os.path.join(dest, relpath)) |
| 117 | + progressbar.add(1) |
| 118 | + progressbar.end() |
| 119 | + print("", end="\n", file=sys.stderr) |
| 120 | + # Copy subpackages |
| 121 | + for (pkgpath, pkg) in self.subpackages.items(): |
| 122 | + pkg.copy_files(os.path.join(dest, os.path.join(dest, pkgpath))) |
| 123 | + finally: |
| 124 | + os.chdir(cwd) |
| 125 | + |
| 126 | +class ProgressBar: |
| 127 | + """ |
| 128 | + Progress bar type. |
| 129 | + """ |
| 130 | + def __init__(self, total): |
| 131 | + self.total = total |
| 132 | + self.cur = 0 |
| 133 | + sys.stderr.write("\n") |
| 134 | + self.update() |
| 135 | + |
| 136 | + def add(self, step): |
| 137 | + """ |
| 138 | + Add current value and update. |
| 139 | + """ |
| 140 | + self.cur += step |
| 141 | + self.update() |
| 142 | + |
| 143 | + def set(self, cur): |
| 144 | + """ |
| 145 | + Set current value and update. |
| 146 | + """ |
| 147 | + self.cur = cur |
| 148 | + self.update() |
| 149 | + |
| 150 | + def update(self): |
| 151 | + """ |
| 152 | + Update progress bar. |
| 153 | + """ |
| 154 | + try: |
| 155 | + progress = 0.0 |
| 156 | + if(self.cur): |
| 157 | + progress = self.cur / self.total |
| 158 | + term_width = os.get_terminal_size(sys.stderr.fileno()).columns |
| 159 | + part_width = term_width - len("[]100.0%") |
| 160 | + if(part_width <= 0): |
| 161 | + # Terminal is too small, ignore. |
| 162 | + return |
| 163 | + sys.stderr.write("\r") |
| 164 | + # Part1 is '#' |
| 165 | + part1 = int(part_width * progress) * '#' |
| 166 | + # Part2 is ' ' |
| 167 | + part2 = int(part_width - len(part1)) * ' ' |
| 168 | + # Part3 is 'xxx.x%' |
| 169 | + part3 = str(int(progress * 1000) / 10) + "%" |
| 170 | + sys.stderr.write(f"[{part1}{part2}]{part3}") |
| 171 | + sys.stderr.flush() |
| 172 | + except: |
| 173 | + return |
| 174 | + |
| 175 | + def end(self): |
| 176 | + """ |
| 177 | + End this progress bar |
| 178 | + """ |
| 179 | + self.set(self.total) |
| 180 | + sys.stderr.write("\n") |
| 181 | + sys.stderr.flush() |
| 182 | + |
| 183 | +cwd = os.path.abspath(os.curdir) |
| 184 | +fwd = os.path.abspath(os.path.join(__file__, "..")) |
| 185 | + |
| 186 | +package = None |
| 187 | +try: |
| 188 | + package = Package(PACKAGE_INFO) |
| 189 | +except Exception as exc: |
| 190 | + print("Getting packge infomation error:", exc, file=sys.stderr) |
| 191 | + raise exc |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + distdir = f"{package.name}-v{package.version}" |
| 195 | + package.copy_files(os.path.abspath(distdir)) |
0 commit comments