From f52cf2420afa5990aa628224a8faf6b1f65b9b2b Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Fri, 24 Mar 2023 04:27:32 -0700 Subject: [PATCH] Do not use wee_resources at all when doing Debian packaging --- TODO.md | 7 +------ bin/weecfg/station_config.py | 29 ++++++++++++++++++++--------- bin/weectllib/parse_station.py | 16 ++++++++++++++++ makefile | 10 +++++----- pkg/debian/postinst | 6 +++--- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/TODO.md b/TODO.md index 8bc35af94..11202f2c1 100644 --- a/TODO.md +++ b/TODO.md @@ -1,11 +1,6 @@ # To do -## `weectl` - -Do not have `weectl station create` copy in a fresh copy of the docs and examples. That's a job -for `weectl station upgrade`. - -Need an option `--no-backup` for `weectl station reconfigure`. +## Debian packaging Right now, the Debian installer depends heavily on sed script to modify weewx.conf. For example, altitude. This should be done using `weectl station reconfigure`. diff --git a/bin/weecfg/station_config.py b/bin/weecfg/station_config.py index 26aeca3ed..8ed3d4a04 100644 --- a/bin/weecfg/station_config.py +++ b/bin/weecfg/station_config.py @@ -28,6 +28,7 @@ def station_create(config_path, *args, + dist_config_path=None, docs_root=None, examples_root=None, user_root=None, @@ -62,12 +63,19 @@ def station_create(config_path, *args, print(f"The configuration file will be created " f"at {bcolors.BOLD}{config_path}{bcolors.ENDC}.") - # Retrieve the configuration file as a ConfigObj - with importlib.resources.open_text('wee_resources', 'weewx.conf', encoding='utf-8') as fd: - dist_config_dict = configobj.ConfigObj(fd, encoding='utf-8', file_error=True) + # Unless we've been given a path to the new configuration file, retrieve it from + # package resources. + if dist_config_path: + dist_config_dict = configobj.ConfigObj(dist_config_path, encoding='utf-8', file_error=True) + else: + # Retrieve the new configuration file from package resources: + with importlib.resources.open_text('wee_resources', 'weewx.conf', encoding='utf-8') as fd: + dist_config_dict = configobj.ConfigObj(fd, encoding='utf-8', file_error=True) config_config(config_path, dist_config_dict, weewx_root=weewx_root, dry_run=dry_run, *args, **kwargs) + copy_skins(dist_config_dict, dry_run=dry_run) + copy_util(config_path, dist_config_dict, dry_run=dry_run) copy_docs(dist_config_dict, docs_root=docs_root, dry_run=dry_run) copy_examples(dist_config_dict, examples_root=examples_root, dry_run=dry_run) copy_user(dist_config_dict, user_root=user_root, dry_run=dry_run) @@ -125,8 +133,6 @@ def config_config(config_path, config_dict, config_units(config_dict, unit_system=unit_system, no_prompt=no_prompt) config_driver(config_dict, driver=driver, no_prompt=no_prompt) config_roots(config_dict, weewx_root, skin_root, html_root, sqlite_root, user_root) - copy_skins(config_dict, dry_run=dry_run) - copy_util(config_path, config_dict, dry_run=dry_run) def config_location(config_dict, location=None, no_prompt=False): @@ -665,7 +671,7 @@ def _patch_file(srcpath, dstpath, re_list): wd.write(line) -def station_upgrade(config_path, docs_root=None, examples_root=None, +def station_upgrade(config_path, dist_config_path=None, docs_root=None, examples_root=None, no_prompt=False, no_backup=False, dry_run=False): """Upgrade the user data for the configuration file found at config_path""" @@ -682,9 +688,14 @@ def station_upgrade(config_path, docs_root=None, examples_root=None, print("Nothing done.") return - # Retrieve the new configuration file as a ConfigObj: - with importlib.resources.open_text('wee_resources', 'weewx.conf', encoding='utf-8') as fd: - dist_config_dict = configobj.ConfigObj(fd, encoding='utf-8', file_error=True) + # Unless we've been given a path to the new configuration file, retrieve it from + # package resources. + if dist_config_path: + dist_config_dict = configobj.ConfigObj(dist_config_path, encoding='utf-8', file_error=True) + else: + # Retrieve the new configuration file from package resources: + with importlib.resources.open_text('wee_resources', 'weewx.conf', encoding='utf-8') as fd: + dist_config_dict = configobj.ConfigObj(fd, encoding='utf-8', file_error=True) weecfg.update_config.update_and_merge(config_dict, dist_config_dict) print(f"Finished upgrading the configuration file found at {config_path}.") diff --git a/bin/weectllib/parse_station.py b/bin/weectllib/parse_station.py index 227745ed8..4f5fee5cd 100644 --- a/bin/weectllib/parse_station.py +++ b/bin/weectllib/parse_station.py @@ -11,6 +11,7 @@ from weeutil.weeutil import bcolors station_create_usage = f"""{bcolors.BOLD}weectl station create [--config=CONFIG-PATH] \\ + [--dist-config=DIST-CONFIG-PATH]] \\ [--driver=DRIVER] \\ [--location=LOCATION] \\ [--altitude=ALTITUDE,{{foot|meter}}] \\ @@ -41,6 +42,7 @@ [--dry-run]{bcolors.ENDC} """ station_upgrade_usage = f"""{bcolors.BOLD}weectl station upgrade [--config=CONFIG-PATH] \\ + [--dist-config=DIST-CONFIG-PATH]] \\ [--docs-root=DOCS_ROOT] \\ [--examples-root=EXAMPLES_ROOT] \\ [--no-prompt] \\ @@ -94,6 +96,12 @@ def add_subparser(subparsers): metavar='CONFIG-PATH', help=f'Path to configuration file. It must not already ' f'exist. Default is "{weecfg.default_config_path}".') + station_create_parser.add_argument('--dist-config', + metavar='DIST-CONFIG-PATH', + help='Use configuration file DIST-CONFIG-PATH as the ' + 'new configuration file. Default is to retrieve it ' + 'from package resources. The average user is ' + 'unlikely to need this option.') _add_common_args(station_create_parser) station_create_parser.add_argument('--user-root', help='Where to put the "user" directory, relative to ' @@ -146,6 +154,12 @@ def add_subparser(subparsers): metavar='CONFIG-PATH', help=f'Path to configuration file. ' f'Default is "{weecfg.default_config_path}"') + station_upgrade_parser.add_argument('--dist-config', + metavar='DIST-CONFIG-PATH', + help='Use configuration file DIST-CONFIG-PATH as the ' + 'new configuration file. Default is to retrieve it ' + 'from package resources. The average user is ' + 'unlikely to need this option.') station_upgrade_parser.add_argument('--docs-root', help='Where to put the new documentation, relative to ' 'WEEWX_ROOT. Default is "docs".') @@ -193,6 +207,7 @@ def create_station(namespace): """Map 'namespace' to a call to station_create()""" try: weecfg.station_config.station_create(config_path=namespace.config, + dist_config_path=namespace.dist_config, driver=namespace.driver, location=namespace.location, altitude=namespace.altitude, @@ -236,6 +251,7 @@ def reconfigure_station(namespace): def upgrade_station(namespace): weecfg.station_config.station_upgrade(config_path=namespace.config, + dist_config_path=namespace.dist_config, docs_root=namespace.docs_root, examples_root=namespace.examples_root, no_prompt=namespace.no_prompt, diff --git a/makefile b/makefile index 0847472b3..a3c291894 100644 --- a/makefile +++ b/makefile @@ -197,10 +197,10 @@ src-tarball: $(DSTDIR)/$(SRCPKG) $(DSTDIR)/$(SRCPKG): mkdir -p $(BLDDIR)/weewx-$(VERSION) - rsync -ar ./ $(BLDDIR)/weewx-$(VERSION) --exclude __pycache__ --exclude .gitignore \ - --exclude .git --exclude tests --exclude dist --exclude build --exclude .editorconfig \ - --exclude poetry.lock --exclude pyproject.toml --exclude mkdocs.yml --exclude .idea \ - --exclude .github + rsync -ar ./ $(BLDDIR)/weewx-$(VERSION) --exclude wee_resources --exclude __pycache__ \ + --exclude .gitignore --exclude .git --exclude tests --exclude dist --exclude build \ + --exclude .editorconfig --exclude poetry.lock --exclude pyproject.toml \ + --exclude mkdocs.yml --exclude .idea --exclude .github mkdir -p $(DSTDIR) tar cfz $(DSTDIR)/$(SRCPKG) -C $(BLDDIR) weewx-$(VERSION) @@ -258,7 +258,7 @@ DEBPKG=python3-weewx_$(DEBVER)_$(DEBARCH).deb ifneq ("$(SIGN)","1") DPKG_OPT=-us -uc endif -debian-package: bin/wee_resources/ deb-package-prep +debian-package: deb-package-prep cp pkg/debian/control $(DEBBLDDIR)/debian/control rm -f $(DEBBLDDIR)/debian/files rm -rf $(DEBBLDDIR)/debian/weewx* diff --git a/pkg/debian/postinst b/pkg/debian/postinst index ebbc05521..6a98617ba 100755 --- a/pkg/debian/postinst +++ b/pkg/debian/postinst @@ -175,7 +175,7 @@ merge_weewxconf() { return fi - NEWVER=`$cfgapp --version` + NEWVER=$(cfgapp --version | awk '{print $2}') OLDVER=$(get_conf_version $cfgfile) if dpkg --compare-versions $OLDVER lt $NEWVER; then # this is an old config, so merge it into a new config @@ -183,11 +183,11 @@ merge_weewxconf() { DSTVER=$(get_conf_version $cfgfile.dist) if dpkg --compare-versions $DSTVER eq $NEWVER; then echo saving previous config file as $cfgfile-$OLDVER - mv $cfgfile $cfgfile-$OLDVER + cp -p $cfgfile $cfgfile-$OLDVER echo saving distribution config file as $cfgfile-$NEWVER cp -p $cfgfile.dist $cfgfile-$NEWVER echo merging previous and distribution into $cfgfile - $cfgapp --upgrade --config=$cfgfile-$OLDVER --dist-config=$cfgfile-$NEWVER --output=$cfgfile --no-prompt --no-backup + $cfgapp station upgrade --config=$cfgfile --dist-config=$cfgfile-$NEWVER --no-prompt --no-backup else echo distribution config file is wrong version for merging fi