-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
102 lines (85 loc) · 3.77 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Simple and hastily written configuration system that parses
environment variables/cli arguments parser
"""
import os
import sys
from dataclasses import dataclass, field, fields, Field
from typing import List
@dataclass
class Config:
###########################################################################
# Required options #
###########################################################################
# User for the open build system if no repos are specified. Needed for OBS api access
obs_user: str | None = None
# Password for the open build system
obs_pass: str | None = None
###########################################################################
# Optional options (with provided defaults) #
###########################################################################
# Directory to store output
out_dir: str = "out/"
# File to output the job summary to (a Markdown file giving a summary of the job)
job_summary: str = "out/summary.md"
# A list of repos to download
repos: List[str] = field(default_factory=list)
# The open build service location
obs_url: str = "https://build.sailfishos.org/"
# The Open Build Service project that contains Chum
obs_project: str = "sailfishos:chum"
# The prefix of the repository where packages are published. The values from architecture-specific
# repos will be appended
repo_url_prefix: str = "https://repo.sailfishos.org/obs/sailfishos:/chum/"
# Whether to show package parsing warnings and other debugging information
debug: bool = False
# Package names that are considered installers for Chum. These will not show the
# "please install via chum" notice.
chum_installer_pkgs: List[str] = field(default_factory=lambda: ["sailfishos-chum-gui-installer"])
# Public, canonical URL
public_url: str = "http://localhost:8000/"
# Whether to download MarkDown descriptions et cetera. Turn off for faster local testing
download_extra_metadata: bool = True
# Directory with {arch}-primary.xml.gz files downloaded from an earlier run.
repo_data_dir: str | None = None
user_agent: str = "chumweb/1.0"
source_code_url: str = "https://github.com/sailfishos-chum/sailfishos-chum.github.io/"
# The amount of featured apps to show on the home page
featured_apps_count = 11
# The amount of updated apps to show in the sidebar
updated_apps_count = 12
# The amount of updated apps to put in the Atom feed
feed_updated_apps_count = 20
def init_config() -> Config:
"""
Loads config values from either command line arguments as ``--<config-value>=x`` or environment variables
``CHUM_<CONFIG_VALUE>=x``.
"""
import getopt
import sys
c = Config()
def env_name(key: str):
return "CHUM_" + key.upper()
def arg_name(key: str):
return key.replace("_", "-")
def set_option(f: Field, val: str):
if f.type is bool:
val = val.lower() == "true"
if f.type is List[str]:
val = val.split(",")
setattr(c, f.name, val)
cli_opts, cli_args = getopt.getopt(sys.argv[1:], "", [f"{arg_name(f.name)}=" for f in fields(Config)])
for f in fields(Config):
env_val = os.getenv(env_name(f.name))
if env_val:
set_option(f, env_val)
for opt, arg in cli_opts:
if opt.removeprefix("--") == arg_name(f.name):
set_option(f, arg)
return c
def validate_config(c: Config):
def exit_with_error(err: str):
sys.exit(err)
if len(c.repos) == 0 and not c.obs_user:
exit_with_error("Either a list of repos must be specified or an obs_user and obs_password must be given.")
CONFIG = init_config()