🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.python
Article #290

Re: PSA/QUESTION: Porting Windows batch to Python for macOS/Linux users

#290
From: Maria Sophia
Date: Sun, 22 Nov 2026 06:17
123 lines
7715 bytes
What's different between the batch and python is mostly:
 a. SQLite queries primarily use Python's built-in sqlite3 module.
 b. Output tables use the third-party tabulate module.
 c. Archive extraction uses Python's zipfile.
 d. Filename sanitization uses Python regular expressions.
 e. There are no generated PowerShell scripts or BAT subroutines.
 f. The optional missing-package check uses Python sets to
    compare installed packages against Nova database packages.
 g. Debugging can be enabled with DEBUG = True or the ADBFOLDER_DEBUG=1 
    environment variable.

As a result of the rewrite, I'm beginning to less-hate python since
it's actually more intuitive than the Windows batch scripting was. :)

Here's the new flow in the python script... 

#  -------------------------------------------------------------------------
#  Step by step description of what this script does
#  -------------------------------------------------------------------------
#
#  1. Environment & Configuration Setup
#    A. Command environment
#       a. Initializes Python execution environment
#       b. Imports required Python modules and dependencies
#    B. Configuration variables
#       a. Sets default configuration paths for ADB, SQLite
#          and work directories
#       b. Defines the DEBUG toggle flag
#       c. Defines temporary and Android backup directories
#
#  2. Dependency Verification
#    A. Executable checks
#       a. Locates adb.exe and checks system PATH or configured fallback path
#       b. Locates sqlite3.exe and exits with an error if missing
#    B. Directory setup
#       a. Creates the Nova work directory if it does not exist
#
#  3. Source Selection Menu
#    A. Choice [1]: Pull from Android via ADB
#       a. Detects connected ADB device target
#       b. Optionally launches Nova Launcher settings to create a backup
#       c. Lists .novabackup files in the remote backup directory
#          with fallback search of Android storage
#       d. Prompts user to select or enter a backup filename/path
#       e. Pulls the selected .novabackup file to the Windows workspace
#       f. Renames the pulled file to backup.zip and extracts it
#          into backup_extracted to retrieve nova.db
#    B. Choice [2]: Use Local Database / Archive
#       a. Prompts user for a custom path to nova.db, .novabackup or .zip
#          with a retry loop
#       b. Validates file existence
#       c. Automatically extracts archives when necessary
#       d. Optionally offers to restore a .novabackup database to the device
#
#  4. Database Verification & Target Enumeration
#    A. Database validation
#       a. Verifies that nova.db exists at the target path
#    B. Section 1 [Dock]
#       a. Scans dock apps [container = -101]
#       b. Scans and lists any dock folders
#    C. Section 2 [Panes 0-14]
#       a. Iterates through home screen panes 0 to 14
#          via the scan_pane function
#       b. Queries pane folders [itemType = 2, container = -100]
#       c. Checks for loose non-folder apps [itemType <> 2] on each pane
#    D. Section 3 [Other Folders]
#       a. Searches for orphaned folders not categorized under dock or panes
#       b. Lists and registers remaining folders
#
#  5. User Selection & Query Execution [run_query]
#    A. Selection & Sanity
#       a. Prompts user to select a target number or quit [q or 0]
#       b. Sanitizes the selected label to generate a safe filename
#    B. Query Generation & Execution
#       a. Builds a dynamic SQLite query extracting:
#          - Item ID
#          - Display name
#          - Package name
#          - Raw intent
#       b. Executes the query against nova.db using Python sqlite3
#       c. Falls back to the sqlite3 command-line executable if necessary
#    C. Output & Formatting
#       a. Writes raw tab-delimited results to output.txt
#       b. Formats output columns [package_name first, display_name second]
#          into an aligned table via tabulate
#       c. Adds a descriptive pane/folder heading and indents the table
#          by two spaces for easier reading and future concatenation
#       d. Saves formatted results to [target]_apps.txt
#       e. Appends each formatted export with a timestamp to
#          adbfolder_log.txt without overwriting previous exports
#       f. Displays the formatted results and output file locations
#    D. Repeat Export
#       a. Prompts user to export another folder or pane
#       b. Re-enumerates targets in case the database layout has changed
#       c. Repeats the selection and export process as requested
#
#  6. Optional Unplaced Third-Party Package Check [check_missing_3p]
#    A. Pre-check Validation
#       a. Prompts user whether to check for user-installed packages
#          [-3] missing from the Nova layout
#       b. Verifies ADB device connectivity and database availability
#       c. Optionally prompts to resolve app titles via adb/aapt
#    B. Data Collection
#       a. Queries installed third-party packages from device
#          [adb shell pm list packages -3]
#       b. Queries all referenced packages inside nova.db
#       c. Compares installed packages against packages referenced in Nova
#    C. Optional App Title Resolution
#       a. Runs adb shell dumpsys package for each unplaced package
#       b. Searches for nonLocalizedLabel in the package information
#       c. If unavailable, uses adb shell pm path to locate the APK
#       d. Pulls the APK to the PC and uses aapt/aapt2 to read its label
#    D. Diff Analysis & Output
#       a. Identifies installed third-party packages with no Nova icon
#       b. Generates a formatted package_name / app_title table
#       c. Saves results to unplaced_user_apps.txt
#       d. Displays package counts and the resulting unplaced app list
#    E. Cleanup
#       a. Removes temporary package-list files unless DEBUG=1
#       b. Exits cleanly
#
#  -------------------------------------------------------------------------
#  -------------------------------------------------------------------------
Technical Details
Message-ID: <11du1ds$2d2n$1@nnrp.usenet.blueworldhosting.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED.public-nat-02.vpngate.v4.open.ad.jp!not-for-mail
References: <11dsuk8$2dgm$1@nnrp.usenet.blueworldhosting.com> <11dtk8d$2f73$1@nnrp.usenet.blueworldhosting.com> <11dtkua$3j5$1@nnrp.usenet.blueworldhosting.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED.public-nat-02.vpngate.v4.open.ad.jp!not-for-mail
From: Maria Sophia <mariasophia@comprehension.com>
Newsgroups: comp.lang.python,alt.os.linux,comp.sys.mac.system,alt.comp.os.windows-11
Subject: Re: PSA/QUESTION: Porting Windows batch to Python for macOS/Linux users
Date: Sun, 22 Nov 2026 02:17:32 -0400
Organization: BWH Usenet Archive (https://usenet.blueworldhosting.com)
Message-ID: <11du1ds$2d2n$1@nnrp.usenet.blueworldhosting.com>
References: <11dsuk8$2dgm$1@nnrp.usenet.blueworldhosting.com> <11dtk8d$2f73$1@nnrp.usenet.blueworldhosting.com> <11dtkua$3j5$1@nnrp.usenet.blueworldhosting.com>
Reply-To: pusvul@getTjewytR4so+mqe2.invalid
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 22 Nov 2026 06:17:32 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com; posting-account="l//KxG3Ge2LojWvwJg0Igv8F0ChK8RUC9RtOxMPUo3U"; posting-host="IfCZz72xNNlHXBkfV9B6WJEa9xVhaehMJfhAC6fCwnM";
	logging-data="78935"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mojibake v24p7
Cancel-Lock: sha1:s0fESz2iBjEzdK7+upDGSlG0P+E= sha256:P/Pd5wAqVHDhbWN4uK9LZtdCz56wsaAE0C6Nf+lo0s0=
	sha1:J18hyF3UKMCnEkg/6s6d15TQSUQ= sha256:AmlH8SprYwpHwYA5NdY9KtZ+y0EHN9AxSbdNTil13JA=
Content-Language: en-GB