pkgmgr-kunrunic 0.1.2.dev1__py3-none-any.whl → 0.1.2.dev4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pkgmgr/__init__.py CHANGED
@@ -11,6 +11,7 @@ __all__ = [
11
11
  "release",
12
12
  "watch",
13
13
  "points",
14
+ "detection",
14
15
  ]
15
16
 
16
- __version__ = "0.1.2.dev1"
17
+ __version__ = "0.1.2.dev4"
pkgmgr/cli.py CHANGED
@@ -3,13 +3,17 @@ from __future__ import print_function
3
3
 
4
4
  import os
5
5
  import sys
6
+ import io
7
+ import time
8
+ import hashlib
9
+ from contextlib import redirect_stdout
6
10
 
7
11
  try:
8
12
  import argparse
9
13
  except Exception:
10
14
  argparse = None
11
15
 
12
- from . import config, snapshot, release, watch, __version__
16
+ from . import config, snapshot, release, watch, detection, __version__
13
17
 
14
18
 
15
19
  def _add_make_config(sub):
@@ -35,7 +39,7 @@ def _add_install(sub):
35
39
 
36
40
  def _add_snapshot(sub):
37
41
  p = sub.add_parser(
38
- "snapshot", help="take a snapshot (baseline update after install)"
42
+ "snapshot", help="take a standalone snapshot (does not modify baseline)"
39
43
  )
40
44
  p.add_argument(
41
45
  "--config",
@@ -55,6 +59,41 @@ def _add_actions(sub):
55
59
  p.set_defaults(func=_handle_actions)
56
60
 
57
61
 
62
+ def _add_detect(sub):
63
+ p = sub.add_parser("detect", help="detect unmanaged or not-yet-updated changes")
64
+ p.add_argument(
65
+ "--config",
66
+ default=None,
67
+ help="config file path (default: auto-discover under %s)" % config.BASE_DIR,
68
+ )
69
+ p.set_defaults(func=_handle_detect)
70
+
71
+
72
+ def _add_telegram(sub):
73
+ p = sub.add_parser("telegram", help="telegram registration/approval helpers")
74
+ p.add_argument(
75
+ "--config",
76
+ default=None,
77
+ help="config file path (default: auto-discover under %s)" % config.BASE_DIR,
78
+ )
79
+ tsp = p.add_subparsers(dest="telegram_cmd")
80
+
81
+ p_poll = tsp.add_parser("poll", help="poll telegram bot updates and process registration commands")
82
+ p_poll.add_argument("--once", action="store_true", help="process one poll cycle then exit")
83
+ p_poll.set_defaults(func=_handle_telegram_poll)
84
+
85
+ p_list = tsp.add_parser("list", help="show approved/pending telegram subscribers")
86
+ p_list.set_defaults(func=_handle_telegram_list)
87
+
88
+ p_ap = tsp.add_parser("approve", help="approve pending subscriber")
89
+ p_ap.add_argument("chat_id", help="target chat id")
90
+ p_ap.set_defaults(func=_handle_telegram_approve)
91
+
92
+ p_rj = tsp.add_parser("reject", help="reject pending subscriber")
93
+ p_rj.add_argument("chat_id", help="target chat id")
94
+ p_rj.set_defaults(func=_handle_telegram_reject)
95
+
96
+
58
97
  def _add_create_pkg(sub):
59
98
  p = sub.add_parser("create-pkg", help="create a pkg folder and template")
60
99
  p.add_argument("pkg_id", help="package identifier (e.g. 20240601)")
@@ -111,6 +150,17 @@ def _add_close_pkg(sub):
111
150
  p.set_defaults(func=_handle_close_pkg)
112
151
 
113
152
 
153
+ def _add_delete_pkg(sub):
154
+ p = sub.add_parser("delete-pkg", help="delete a closed pkg from local state")
155
+ p.add_argument("pkg_id", help="package identifier to delete (closed only)")
156
+ p.add_argument(
157
+ "--config",
158
+ default=None,
159
+ help="config file path (default: auto-discover under %s)" % config.BASE_DIR,
160
+ )
161
+ p.set_defaults(func=_handle_delete_pkg)
162
+
163
+
114
164
  def _add_watch(sub):
115
165
  p = sub.add_parser("watch", help="start watcher/daemon to monitor pkgs")
116
166
  p.add_argument(
@@ -193,10 +243,15 @@ def build_parser():
193
243
 
194
244
  _add_make_config(sub)
195
245
  _add_install(sub)
246
+ _add_snapshot(sub)
196
247
  _add_create_pkg(sub)
197
248
  _add_update_pkg(sub)
198
249
  _add_close_pkg(sub)
250
+ _add_delete_pkg(sub)
251
+ _add_watch(sub)
199
252
  _add_actions(sub)
253
+ _add_detect(sub)
254
+ _add_telegram(sub)
200
255
  return parser
201
256
 
202
257
 
@@ -238,6 +293,7 @@ def _handle_create_pkg(args):
238
293
  cfg = config.load_main(args.config)
239
294
  release.create_pkg(cfg, args.pkg_id)
240
295
  _run_auto_actions(cfg, "create_pkg", config_path=args.config, context={"pkg_id": args.pkg_id, "event": "create_pkg"})
296
+ watch.notify_lifecycle_event(cfg, "create_pkg", pkg_id=args.pkg_id)
241
297
  return 0
242
298
 
243
299
  def _handle_update_pkg(args):
@@ -294,6 +350,12 @@ def _handle_update_pkg(args):
294
350
  config_path=args.config,
295
351
  context={"pkg_id": args.pkg_id, "event": "cancel_pkg_release", "release": args.cancel},
296
352
  )
353
+ watch.notify_lifecycle_event(
354
+ cfg,
355
+ "cancel_pkg_release",
356
+ pkg_id=args.pkg_id,
357
+ details={"release": args.cancel, "root": args.root or "all"},
358
+ )
297
359
  return 0
298
360
  if args.release:
299
361
  active_roots = release.list_active_release_roots(cfg, args.pkg_id)
@@ -318,9 +380,13 @@ def _handle_update_pkg(args):
318
380
  return 0
319
381
  release.finalize_pkg_release(cfg, args.pkg_id, roots=roots)
320
382
  _run_auto_actions(cfg, "update_pkg_release", config_path=args.config, context={"pkg_id": args.pkg_id, "event": "update_pkg_release"})
383
+ watch.notify_lifecycle_event(
384
+ cfg, "update_pkg_release", pkg_id=args.pkg_id, details={"roots": ",".join(roots)}
385
+ )
321
386
  return 0
322
387
  release.update_pkg(cfg, args.pkg_id)
323
388
  _run_auto_actions(cfg, "update_pkg", config_path=args.config, context={"pkg_id": args.pkg_id, "event": "update_pkg"})
389
+ watch.notify_lifecycle_event(cfg, "update_pkg", pkg_id=args.pkg_id)
324
390
  return 0
325
391
 
326
392
 
@@ -328,6 +394,15 @@ def _handle_close_pkg(args):
328
394
  cfg = config.load_main(args.config)
329
395
  release.close_pkg(cfg, args.pkg_id)
330
396
  _run_auto_actions(cfg, "close_pkg", config_path=args.config, context={"pkg_id": args.pkg_id, "event": "close_pkg"})
397
+ watch.notify_lifecycle_event(cfg, "close_pkg", pkg_id=args.pkg_id)
398
+ return 0
399
+
400
+
401
+ def _handle_delete_pkg(args):
402
+ cfg = config.load_main(args.config)
403
+ release.delete_pkg(cfg, args.pkg_id)
404
+ _run_auto_actions(cfg, "delete_pkg", config_path=args.config, context={"pkg_id": args.pkg_id, "event": "delete_pkg"})
405
+ watch.notify_lifecycle_event(cfg, "delete_pkg", pkg_id=args.pkg_id)
331
406
  return 0
332
407
 
333
408
 
@@ -364,6 +439,70 @@ def _handle_actions(args):
364
439
  return 0
365
440
 
366
441
 
442
+ def _handle_detect(args):
443
+ cfg = config.load_main(args.config)
444
+ buf = io.StringIO()
445
+ with redirect_stdout(buf):
446
+ result = detection.run(cfg)
447
+ text = buf.getvalue()
448
+ if text:
449
+ sys.stdout.write(text)
450
+ if not text.endswith("\n"):
451
+ sys.stdout.write("\n")
452
+ _save_detect_report(cfg, text)
453
+ return int(result.get("exit_code", 0))
454
+
455
+
456
+ def _handle_telegram_poll(args):
457
+ cfg = config.load_main(args.config)
458
+ return watch.telegram_poll(cfg, run_once=bool(args.once))
459
+
460
+
461
+ def _handle_telegram_list(args):
462
+ cfg = config.load_main(args.config)
463
+ return watch.telegram_list(cfg)
464
+
465
+
466
+ def _handle_telegram_approve(args):
467
+ cfg = config.load_main(args.config)
468
+ return watch.telegram_approve(cfg, args.chat_id)
469
+
470
+
471
+ def _handle_telegram_reject(args):
472
+ cfg = config.load_main(args.config)
473
+ return watch.telegram_reject(cfg, args.chat_id)
474
+
475
+
476
+ def _save_detect_report(cfg, text):
477
+ detection_cfg = cfg.get("detection") if isinstance(cfg.get("detection"), dict) else {}
478
+ report_path = detection_cfg.get("report_file")
479
+ if not report_path:
480
+ return
481
+ out_path = os.path.abspath(os.path.expanduser(str(report_path)))
482
+ parent = os.path.dirname(out_path)
483
+ if parent and not os.path.exists(parent):
484
+ os.makedirs(parent)
485
+ existing = None
486
+ if os.path.isfile(out_path):
487
+ try:
488
+ with open(out_path, "r", encoding="utf-8", errors="replace") as f:
489
+ existing = f.read()
490
+ except Exception:
491
+ existing = None
492
+ if existing == text:
493
+ return
494
+ if existing is not None:
495
+ ts = time.strftime("%Y%m%d_%H%M%S", time.localtime())
496
+ backup_path = out_path + ".bak_" + ts
497
+ with open(backup_path, "w", encoding="utf-8") as bf:
498
+ bf.write(existing)
499
+ digest = hashlib.sha256(existing.encode("utf-8")).hexdigest()[:12]
500
+ print("[detect] backup saved: %s (sha256=%s)" % (backup_path, digest))
501
+ with open(out_path, "w", encoding="utf-8") as f:
502
+ f.write(text or "")
503
+ print("[detect] report saved: %s" % out_path)
504
+
505
+
367
506
  def _run_auto_actions(cfg, event, config_path=None, context=None):
368
507
  auto_actions = cfg.get("auto_actions") or {}
369
508
  names = auto_actions.get(event) or []