-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
FROM alpine:3.16 | ||
LABEL maintainer "[email protected]" | ||
FROM alpine:3.16 AS builder | ||
LABEL maintainer="[email protected]" | ||
ARG THREADS="4" | ||
ARG SHA="" | ||
|
||
# build minimized | ||
WORKDIR / | ||
RUN apk add --no-cache --virtual .build-tools git g++ build-base linux-headers cmake python3 && \ | ||
RUN set -xe && \ | ||
apk add --no-cache --virtual .build-tools git g++ build-base linux-headers cmake python3 && \ | ||
apk add --no-cache --virtual .build-deps curl-dev rapidjson-dev libevent-dev pcre2-dev yaml-cpp-dev && \ | ||
git clone https://github.com/ftk/quickjspp --depth=1 && \ | ||
cd quickjspp && \ | ||
|
@@ -29,7 +30,7 @@ RUN apk add --no-cache --virtual .build-tools git g++ build-base linux-headers c | |
install -d /usr/include/date/ && \ | ||
install -m644 libcron/externals/date/include/date/* /usr/include/date/ && \ | ||
cd .. && \ | ||
git clone https://github.com/ToruNiina/toml11 --depth=1 && \ | ||
git clone https://github.com/ToruNiina/toml11 --branch="v3.7.1" --depth=1 && \ | ||
cd toml11 && \ | ||
cmake -DCMAKE_CXX_STANDARD=11 . && \ | ||
make install -j $THREADS && \ | ||
|
@@ -41,14 +42,19 @@ RUN apk add --no-cache --virtual .build-tools git g++ build-base linux-headers c | |
python3 -m pip install gitpython && \ | ||
python3 scripts/update_rules.py -c scripts/rules_config.conf && \ | ||
cmake -DCMAKE_BUILD_TYPE=Release . && \ | ||
make -j $THREADS && \ | ||
mv subconverter /usr/bin && \ | ||
mv base ../ && \ | ||
cd .. && \ | ||
rm -rf subconverter quickjspp libcron toml11 /usr/lib/lib*.a /usr/include/* /usr/local/include/lib*.a /usr/local/include/* && \ | ||
apk add --no-cache --virtual subconverter-deps pcre2 libcurl yaml-cpp libevent && \ | ||
apk del .build-tools .build-deps | ||
make -j $THREADS | ||
|
||
# build final image | ||
FROM alpine:3.16 | ||
LABEL maintainer="[email protected]" | ||
|
||
RUN apk add --no-cache --virtual subconverter-deps pcre2 libcurl yaml-cpp libevent | ||
|
||
COPY --from=builder /subconverter/subconverter /usr/bin/ | ||
COPY --from=builder /subconverter/base /base/ | ||
|
||
# set entry | ||
WORKDIR /base | ||
CMD subconverter | ||
|
||
EXPOSE 25500/tcp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,78 @@ | ||
import configparser, shutil, glob, os, random, string | ||
import stat, logging, argparse | ||
from git import InvalidGitRepositoryError, repo as GitRepo | ||
import argparse | ||
import configparser | ||
import glob | ||
import logging | ||
import os | ||
import shutil | ||
import stat | ||
from git import InvalidGitRepositoryError, Repo | ||
|
||
|
||
def del_rw(action, name: str, exc): | ||
os.chmod(name, stat.S_IWRITE) | ||
os.remove(name) | ||
|
||
|
||
def open_repo(path: str): | ||
if os.path.exists(repo_path) == False: return None | ||
if not os.path.exists(path): | ||
return None | ||
try: | ||
return GitRepo.Repo(path) | ||
return Repo(path) | ||
except InvalidGitRepositoryError: | ||
return None | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-c", "--config", default="rules_config.conf") | ||
args = parser.parse_args() | ||
|
||
config = configparser.ConfigParser() | ||
config.read(args.config) | ||
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) | ||
|
||
for section in config.sections(): | ||
repo = config.get(section, "name", fallback=section) | ||
url = config.get(section, "url") | ||
commit = config.get(section, "checkout") | ||
matches = config.get(section, "match").split("|") | ||
save_path = config.get(section, "dest", fallback="base/rules/{}".format(repo)) | ||
keep_tree = config.getboolean(section, "keep_tree", fallback=True) | ||
|
||
logging.info("reading files from url {} with commit {} and matches {}, save to {} keep_tree {}".format(url, commit, matches, save_path, keep_tree)) | ||
|
||
# ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 16)) | ||
repo_path = os.path.join("./tmp/repo/", repo) | ||
|
||
r = open_repo(repo_path) | ||
if r != None: | ||
logging.info("repo {} exists, checking out...".format(repo_path)) | ||
r.git.checkout(commit) | ||
else: | ||
logging.info("repo {} not exist, cloning...".format(repo_path)) | ||
shutil.rmtree(repo_path, ignore_errors=True) | ||
os.makedirs(repo_path, exist_ok=True) | ||
r = GitRepo.Repo.clone_from(url, repo_path) | ||
r.git.checkout(commit) | ||
|
||
def update_rules(repo_path, save_path, commit, matches, keep_tree): | ||
os.makedirs(save_path, exist_ok=True) | ||
|
||
for pattern in matches: | ||
files = glob.glob("{}/{}".format(repo_path, pattern), recursive=True) | ||
files = glob.glob(os.path.join(repo_path, pattern), recursive=True) | ||
for file in files: | ||
if os.path.isdir(file): continue | ||
|
||
(file_rel_path, file_name) = os.path.split(file.removeprefix(repo_path)) | ||
if keep_tree: | ||
file_dest_dir = "{}{}".format(save_path, file_rel_path) | ||
file_dest_path = "{}/{}".format(file_dest_dir, file_name) | ||
if os.path.isdir(file): | ||
continue | ||
file_rel_path, file_name = os.path.split(os.path.relpath(file, repo_path)) | ||
if keep_tree: | ||
file_dest_dir = os.path.join(save_path, file_rel_path) | ||
os.makedirs(file_dest_dir, exist_ok=True) | ||
file_dest_path = os.path.join(file_dest_dir, file_name) | ||
else: | ||
file_dest_path = "{}{}".format(save_path, file_name) | ||
file_dest_path = os.path.join(save_path, file_name) | ||
shutil.copy2(file, file_dest_path) | ||
logging.info(f"copied {file} to {file_dest_path}") | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-c", "--config", default="rules_config.conf") | ||
args = parser.parse_args() | ||
|
||
config = configparser.ConfigParser() | ||
config.read(args.config) | ||
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) | ||
|
||
for section in config.sections(): | ||
repo = config.get(section, "name", fallback=section) | ||
url = config.get(section, "url") | ||
commit = config.get(section, "checkout") | ||
matches = config.get(section, "match").split("|") | ||
save_path = config.get(section, "dest", fallback=f"base/rules/{repo}") | ||
keep_tree = config.getboolean(section, "keep_tree", fallback=True) | ||
|
||
logging.info(f"reading files from url {url} with commit {commit} and matches {matches}, save to {save_path} keep_tree {keep_tree}") | ||
|
||
repo_path = os.path.join("./tmp/repo/", repo) | ||
|
||
r = open_repo(repo_path) | ||
if r is None: | ||
logging.info(f"cloning {url} to {repo_path}") | ||
r = Repo.clone_from(url, repo_path) | ||
else: | ||
logging.info(f"pulling changes for {repo} from {url}") | ||
r.remotes.origin.pull() | ||
|
||
r.git.checkout(commit) | ||
update_rules(repo_path, save_path, commit, matches, keep_tree) | ||
|
||
shutil.rmtree("./tmp", ignore_errors=True) | ||
|
||
shutil.copyfile(file, file_dest_path) | ||
logging.info("copied {} to {}".format(file, file_dest_path)) | ||
|
||
shutil.rmtree("./tmp", onerror=del_rw) | ||
if __name__ == "__main__": | ||
main() |