library-txt-casing-fixup 1.0.0a1__tar.gz
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.
- library_txt_casing_fixup-1.0.0a1/LICENSE +22 -0
- library_txt_casing_fixup-1.0.0a1/PKG-INFO +100 -0
- library_txt_casing_fixup-1.0.0a1/README.md +84 -0
- library_txt_casing_fixup-1.0.0a1/pyproject.toml +39 -0
- library_txt_casing_fixup-1.0.0a1/pyproject.toml.orig +38 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/__init__.py +0 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/apps/__init__.py +0 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/apps/cmdline.py +163 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/apps/gui.py +110 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/files.py +74 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/regex.py +31 -0
- library_txt_casing_fixup-1.0.0a1/src/library_txt_casing_fixup/verifications.py +23 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nicola Clark
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: library-txt-casing-fixup
|
|
3
|
+
Version: 1.0.0a1
|
|
4
|
+
Summary: Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files.
|
|
5
|
+
Author: Nicola Clark
|
|
6
|
+
Author-email: Nicola Clark <nicola@slottedspoon.dev>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Topic :: Utilities
|
|
13
|
+
Requires-Python: >=3.14
|
|
14
|
+
Project-URL: Repository, https://repos.slottedspoon.dev/nicola/library-txt-casing-fixup
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# library-txt-casing-fixup
|
|
18
|
+
|
|
19
|
+

|
|
20
|
+

|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files.
|
|
24
|
+
|
|
25
|
+
## Motivation
|
|
26
|
+
|
|
27
|
+
The `library.txt` file(s) used to map asset files to aliases included in X-Plane
|
|
28
|
+
12 contain lines where the asset filename doesn't match in casing. For example,
|
|
29
|
+
the on-disk file `Common_Elements/Lighting/Dir_Ramp_Lit_Med.obj` is listed as
|
|
30
|
+
`Common_Elements/Lighting/dir_Ramp_Lit_Med.obj` in my copy of the default
|
|
31
|
+
airport scenery library's `library.txt`.
|
|
32
|
+
|
|
33
|
+
While this doesn't present an issue on Windows and macOS[^1], it leads to errors
|
|
34
|
+
in WED when trying to build sceneries using these assets on a Linux system:
|
|
35
|
+
|
|
36
|
+

|
|
38
|
+
|
|
39
|
+
This tool provides a way to automate fixing the lines with incorrect filename
|
|
40
|
+
casing in `library.txt` files.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
Installation is not necessary; you can simply download the source files from
|
|
45
|
+
[the latest release](https://repos.slottedspoon.dev/nicola/library-txt-casing-fixup/releases)
|
|
46
|
+
and run them from the extracted archive.
|
|
47
|
+
|
|
48
|
+
Assuming you already have Python 3.14 or greater installed, there are no
|
|
49
|
+
external dependencies needed[^2].
|
|
50
|
+
|
|
51
|
+
If desired, [a PyPI package](https://pypi.org/project/library-txt-casing-fixup/)
|
|
52
|
+
is available:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install library-txt-casing-fixup
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
The quickest way to use the tool is by downloading this repository's files, and
|
|
61
|
+
running the file `library-txt-casing-fixup-gui.py`
|
|
62
|
+
([demo screen recording](docs/usage_gui.mp4)).
|
|
63
|
+
|
|
64
|
+
If more customization is desired, the base script can be used from the command
|
|
65
|
+
line:
|
|
66
|
+
|
|
67
|
+
```console
|
|
68
|
+
$ ./library-txt-casing-fixup.py --help
|
|
69
|
+
usage: library-txt-casing-fixup.py [-h] [-a] [-l LIBRARY_FILE] [-o OUTPUT_FILE]
|
|
70
|
+
|
|
71
|
+
options:
|
|
72
|
+
-h, --help show this help message and exit
|
|
73
|
+
-a, --autofix whether to attempt automatically fixing the incorrect paths - this first
|
|
74
|
+
backs up the library file with a `.bak` extension
|
|
75
|
+
-l, --library-file LIBRARY_FILE
|
|
76
|
+
the library file on which to operate - `library.txt` by default
|
|
77
|
+
-o, --output-file OUTPUT_FILE
|
|
78
|
+
where to output the autofixed file - without this option, the input
|
|
79
|
+
`library.txt` will be overwritten by default
|
|
80
|
+
|
|
81
|
+
$ ./library-txt-casing-fixup.py --autofix --library-file ~/XP12/Resources/default\ scenery/airport\ scenery/library.txt
|
|
82
|
+
|
|
83
|
+
missing asset: Common_Elements/Lighting/dir_Flood_Sm.obj
|
|
84
|
+
potential match: Common_Elements/Lighting/Dir_Flood_Sm.obj
|
|
85
|
+
|
|
86
|
+
[a few missing assets later]
|
|
87
|
+
|
|
88
|
+
renaming `/home/nico/XP12/Resources/default scenery/airport scenery/library.txt` to `/home/nico/XP12/Resources/default scenery/airport scenery/library.txt.bak` succeeded
|
|
89
|
+
autofixed library saved as `/home/nico/XP12/Resources/default scenery/airport scenery/library.txt`
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Licensing
|
|
93
|
+
|
|
94
|
+
The code in this repository is available for your use under the terms of
|
|
95
|
+
[the MIT license](LICENSE).
|
|
96
|
+
|
|
97
|
+
[^1]: When APFS is used in the default case-insensitive mode.
|
|
98
|
+
|
|
99
|
+
[^2]: Unless your system's Python doesn't include tkinter - the GUI script needs
|
|
100
|
+
that. The base script can still run without it.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# library-txt-casing-fixup
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files.
|
|
8
|
+
|
|
9
|
+
## Motivation
|
|
10
|
+
|
|
11
|
+
The `library.txt` file(s) used to map asset files to aliases included in X-Plane
|
|
12
|
+
12 contain lines where the asset filename doesn't match in casing. For example,
|
|
13
|
+
the on-disk file `Common_Elements/Lighting/Dir_Ramp_Lit_Med.obj` is listed as
|
|
14
|
+
`Common_Elements/Lighting/dir_Ramp_Lit_Med.obj` in my copy of the default
|
|
15
|
+
airport scenery library's `library.txt`.
|
|
16
|
+
|
|
17
|
+
While this doesn't present an issue on Windows and macOS[^1], it leads to errors
|
|
18
|
+
in WED when trying to build sceneries using these assets on a Linux system:
|
|
19
|
+
|
|
20
|
+

|
|
22
|
+
|
|
23
|
+
This tool provides a way to automate fixing the lines with incorrect filename
|
|
24
|
+
casing in `library.txt` files.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Installation is not necessary; you can simply download the source files from
|
|
29
|
+
[the latest release](https://repos.slottedspoon.dev/nicola/library-txt-casing-fixup/releases)
|
|
30
|
+
and run them from the extracted archive.
|
|
31
|
+
|
|
32
|
+
Assuming you already have Python 3.14 or greater installed, there are no
|
|
33
|
+
external dependencies needed[^2].
|
|
34
|
+
|
|
35
|
+
If desired, [a PyPI package](https://pypi.org/project/library-txt-casing-fixup/)
|
|
36
|
+
is available:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install library-txt-casing-fixup
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
The quickest way to use the tool is by downloading this repository's files, and
|
|
45
|
+
running the file `library-txt-casing-fixup-gui.py`
|
|
46
|
+
([demo screen recording](docs/usage_gui.mp4)).
|
|
47
|
+
|
|
48
|
+
If more customization is desired, the base script can be used from the command
|
|
49
|
+
line:
|
|
50
|
+
|
|
51
|
+
```console
|
|
52
|
+
$ ./library-txt-casing-fixup.py --help
|
|
53
|
+
usage: library-txt-casing-fixup.py [-h] [-a] [-l LIBRARY_FILE] [-o OUTPUT_FILE]
|
|
54
|
+
|
|
55
|
+
options:
|
|
56
|
+
-h, --help show this help message and exit
|
|
57
|
+
-a, --autofix whether to attempt automatically fixing the incorrect paths - this first
|
|
58
|
+
backs up the library file with a `.bak` extension
|
|
59
|
+
-l, --library-file LIBRARY_FILE
|
|
60
|
+
the library file on which to operate - `library.txt` by default
|
|
61
|
+
-o, --output-file OUTPUT_FILE
|
|
62
|
+
where to output the autofixed file - without this option, the input
|
|
63
|
+
`library.txt` will be overwritten by default
|
|
64
|
+
|
|
65
|
+
$ ./library-txt-casing-fixup.py --autofix --library-file ~/XP12/Resources/default\ scenery/airport\ scenery/library.txt
|
|
66
|
+
|
|
67
|
+
missing asset: Common_Elements/Lighting/dir_Flood_Sm.obj
|
|
68
|
+
potential match: Common_Elements/Lighting/Dir_Flood_Sm.obj
|
|
69
|
+
|
|
70
|
+
[a few missing assets later]
|
|
71
|
+
|
|
72
|
+
renaming `/home/nico/XP12/Resources/default scenery/airport scenery/library.txt` to `/home/nico/XP12/Resources/default scenery/airport scenery/library.txt.bak` succeeded
|
|
73
|
+
autofixed library saved as `/home/nico/XP12/Resources/default scenery/airport scenery/library.txt`
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Licensing
|
|
77
|
+
|
|
78
|
+
The code in this repository is available for your use under the terms of
|
|
79
|
+
[the MIT license](LICENSE).
|
|
80
|
+
|
|
81
|
+
[^1]: When APFS is used in the default case-insensitive mode.
|
|
82
|
+
|
|
83
|
+
[^2]: Unless your system's Python doesn't include tkinter - the GUI script needs
|
|
84
|
+
that. The base script can still run without it.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "library-txt-casing-fixup"
|
|
3
|
+
version = "1.0.0a1"
|
|
4
|
+
description = "Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.14"
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 4 - Beta",
|
|
11
|
+
"Intended Audience :: End Users/Desktop",
|
|
12
|
+
"Operating System :: POSIX :: Linux",
|
|
13
|
+
"Topic :: Utilities",
|
|
14
|
+
]
|
|
15
|
+
dependencies = []
|
|
16
|
+
|
|
17
|
+
[[project.authors]]
|
|
18
|
+
name = "Nicola Clark"
|
|
19
|
+
email = "nicola@slottedspoon.dev"
|
|
20
|
+
|
|
21
|
+
[project.scripts]
|
|
22
|
+
library-txt-casing-fixup = "library_txt_casing_fixup.apps.cmdline:run_cmd"
|
|
23
|
+
|
|
24
|
+
[project.gui-scripts]
|
|
25
|
+
library-txt-casing-fixup-gui = "library_txt_casing_fixup.apps.gui:run_gui"
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Repository = "https://repos.slottedspoon.dev/nicola/library-txt-casing-fixup"
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["uv_build>=0.13.8,<0.14.0"]
|
|
32
|
+
build-backend = "uv_build"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
"black>=26.10.1",
|
|
37
|
+
"pytest>=9.2.0",
|
|
38
|
+
"ruff>=0.17.5",
|
|
39
|
+
]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "library-txt-casing-fixup"
|
|
3
|
+
version = "1.0.0a1"
|
|
4
|
+
description = "Fix incorrectly-cased filenames in X-Plane 12 scenery `library.txt` files."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Nicola Clark", email = "nicola@slottedspoon.dev" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.14"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Intended Audience :: End Users/Desktop",
|
|
15
|
+
"Operating System :: POSIX :: Linux",
|
|
16
|
+
"Topic :: Utilities"
|
|
17
|
+
]
|
|
18
|
+
dependencies = []
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
library-txt-casing-fixup = "library_txt_casing_fixup.apps.cmdline:run_cmd"
|
|
22
|
+
|
|
23
|
+
[project.gui-scripts]
|
|
24
|
+
library-txt-casing-fixup-gui = "library_txt_casing_fixup.apps.gui:run_gui"
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Repository = "https://repos.slottedspoon.dev/nicola/library-txt-casing-fixup"
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["uv_build>=0.13.8,<0.14.0"]
|
|
31
|
+
build-backend = "uv_build"
|
|
32
|
+
|
|
33
|
+
[dependency-groups]
|
|
34
|
+
dev = [
|
|
35
|
+
"black>=26.10.1",
|
|
36
|
+
"pytest>=9.2.0",
|
|
37
|
+
"ruff>=0.17.5",
|
|
38
|
+
]
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import pathlib
|
|
3
|
+
import sys
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from ..files import (
|
|
7
|
+
backup_library,
|
|
8
|
+
cd_if_necessary,
|
|
9
|
+
collect_lowercased_files,
|
|
10
|
+
write_library,
|
|
11
|
+
)
|
|
12
|
+
from ..regex import (
|
|
13
|
+
EXPORT_LINE_REGEX,
|
|
14
|
+
fix_missing_asset,
|
|
15
|
+
fix_path_separators,
|
|
16
|
+
)
|
|
17
|
+
from ..verifications import (
|
|
18
|
+
ensure_library_exists,
|
|
19
|
+
ensure_linux,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class CommandArgs:
|
|
24
|
+
"""The arguments passed to the script"""
|
|
25
|
+
|
|
26
|
+
autofix: bool
|
|
27
|
+
library_file: str
|
|
28
|
+
output_file: Optional[pathlib.Path]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def collect_tool_args() -> CommandArgs:
|
|
32
|
+
"""
|
|
33
|
+
Get the script's arguments
|
|
34
|
+
|
|
35
|
+
:return: the arguments passed to the script
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
args_parser = argparse.ArgumentParser(exit_on_error=True)
|
|
39
|
+
|
|
40
|
+
args_parser.register(
|
|
41
|
+
"type",
|
|
42
|
+
"file path",
|
|
43
|
+
lambda s: pathlib.Path(s),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
args_parser.add_argument(
|
|
47
|
+
"-a",
|
|
48
|
+
"--autofix",
|
|
49
|
+
action="store_true",
|
|
50
|
+
help="whether to attempt automatically fixing the incorrect paths - "
|
|
51
|
+
+ "this first backs up the library file with a `.bak` extension",
|
|
52
|
+
)
|
|
53
|
+
args_parser.add_argument(
|
|
54
|
+
"-l",
|
|
55
|
+
"--library-file",
|
|
56
|
+
default="library.txt",
|
|
57
|
+
help="the library file on which to operate - `%(default)s` by default",
|
|
58
|
+
)
|
|
59
|
+
args_parser.add_argument(
|
|
60
|
+
"-o",
|
|
61
|
+
"--output-file",
|
|
62
|
+
type="file path",
|
|
63
|
+
required=False,
|
|
64
|
+
help="where to output the autofixed file - without this option, the "
|
|
65
|
+
+ "input `library.txt` will be overwritten by default",
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
return args_parser.parse_args(namespace=CommandArgs())
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def run_cmd():
|
|
72
|
+
"""Perform the script's actions"""
|
|
73
|
+
|
|
74
|
+
# bail on windows+macOS
|
|
75
|
+
ensure_linux()
|
|
76
|
+
|
|
77
|
+
# parse autofix and library file flags
|
|
78
|
+
args = collect_tool_args()
|
|
79
|
+
|
|
80
|
+
# set up our env to match the library.txt
|
|
81
|
+
lib_path = pathlib.Path(args.library_file).resolve()
|
|
82
|
+
ensure_library_exists(lib_path)
|
|
83
|
+
cd_if_necessary(lib_path)
|
|
84
|
+
|
|
85
|
+
# build a lowercased file tree within the current directory so we can
|
|
86
|
+
# suggest potential matches
|
|
87
|
+
lowercased_files = collect_lowercased_files()
|
|
88
|
+
|
|
89
|
+
with open(lib_path) as lib_file:
|
|
90
|
+
# helps avoid autofixing when no missing assets exist, or we couldn't
|
|
91
|
+
# autofix
|
|
92
|
+
autofix_error = False
|
|
93
|
+
missing_assets = False
|
|
94
|
+
|
|
95
|
+
# helps avoid outputting unnecessary trailing newline
|
|
96
|
+
first_missing_asset = True
|
|
97
|
+
|
|
98
|
+
# keep track of file contents so we can later replace the file if
|
|
99
|
+
# autofix is desired
|
|
100
|
+
autofixed_file_contents = []
|
|
101
|
+
|
|
102
|
+
for line in lib_file:
|
|
103
|
+
match = EXPORT_LINE_REGEX.search(line)
|
|
104
|
+
# if this isn't an EXPORT line, we can skip it
|
|
105
|
+
if not match:
|
|
106
|
+
autofixed_file_contents.append(line)
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
# fix the windows-style relative paths
|
|
110
|
+
obj_filename = fix_path_separators(match)
|
|
111
|
+
|
|
112
|
+
# if we find that the asset exists, we can skip trying to fix this
|
|
113
|
+
# line
|
|
114
|
+
obj_path = pathlib.Path(obj_filename)
|
|
115
|
+
if obj_path.exists():
|
|
116
|
+
autofixed_file_contents.append(line)
|
|
117
|
+
continue
|
|
118
|
+
|
|
119
|
+
# if we made it here, something is missing
|
|
120
|
+
missing_assets = True
|
|
121
|
+
|
|
122
|
+
# separation between missing asset output
|
|
123
|
+
if first_missing_asset:
|
|
124
|
+
first_missing_asset = False
|
|
125
|
+
else:
|
|
126
|
+
print()
|
|
127
|
+
|
|
128
|
+
print(f"missing asset: {obj_path}")
|
|
129
|
+
try:
|
|
130
|
+
# scan through the files in the dir+subdirs to see if we find an
|
|
131
|
+
# (otherwise identical) incorrectly-cased filename
|
|
132
|
+
potential_missing_file = lowercased_files[str(obj_path).lower()]
|
|
133
|
+
print(f"potential match: {potential_missing_file}")
|
|
134
|
+
|
|
135
|
+
# append the line to the autofixed file, replacing it with the
|
|
136
|
+
# case-corrected filename
|
|
137
|
+
autofixed_file_contents.append(
|
|
138
|
+
fix_missing_asset(match, potential_missing_file)
|
|
139
|
+
)
|
|
140
|
+
except KeyError:
|
|
141
|
+
# we couldn't find a matching case-corrected filename
|
|
142
|
+
autofix_error = True
|
|
143
|
+
print("no potential match found")
|
|
144
|
+
|
|
145
|
+
if not missing_assets:
|
|
146
|
+
print("no missing assets found")
|
|
147
|
+
sys.exit()
|
|
148
|
+
elif not args.autofix:
|
|
149
|
+
# we're done if autofix isn't desired
|
|
150
|
+
sys.exit()
|
|
151
|
+
elif autofix_error:
|
|
152
|
+
# autofix is desired, but we encountered an error
|
|
153
|
+
print()
|
|
154
|
+
print("unable to autofix - missing assets with no potential match found")
|
|
155
|
+
sys.exit(1)
|
|
156
|
+
|
|
157
|
+
if args.output_file:
|
|
158
|
+
print()
|
|
159
|
+
write_library(args.output_file, autofixed_file_contents)
|
|
160
|
+
else:
|
|
161
|
+
# try to back up original library file
|
|
162
|
+
backup_library(lib_path)
|
|
163
|
+
write_library(lib_path, autofixed_file_contents)
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from tkinter import filedialog, messagebox
|
|
4
|
+
|
|
5
|
+
from ..files import (
|
|
6
|
+
cd_if_necessary,
|
|
7
|
+
collect_lowercased_files,
|
|
8
|
+
)
|
|
9
|
+
from ..regex import (
|
|
10
|
+
EXPORT_LINE_REGEX,
|
|
11
|
+
fix_missing_asset,
|
|
12
|
+
fix_path_separators,
|
|
13
|
+
)
|
|
14
|
+
from ..verifications import ensure_linux
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def run_gui():
|
|
18
|
+
ensure_linux()
|
|
19
|
+
|
|
20
|
+
# ask user where library.txt is
|
|
21
|
+
lib_file = filedialog.askopenfile(title="Choose library.txt")
|
|
22
|
+
if not lib_file:
|
|
23
|
+
# user cancelled dialogue - exit
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
# move to library.txt's parent dir and index the files there
|
|
27
|
+
cd_if_necessary(Path(lib_file.name).resolve())
|
|
28
|
+
lowercased_files = collect_lowercased_files()
|
|
29
|
+
|
|
30
|
+
autofixed_lines = []
|
|
31
|
+
num_fixed_lines = 0
|
|
32
|
+
|
|
33
|
+
for line in lib_file:
|
|
34
|
+
match = EXPORT_LINE_REGEX.search(line)
|
|
35
|
+
# if this isn't an EXPORT line, we can skip it
|
|
36
|
+
if not match:
|
|
37
|
+
autofixed_lines.append(line)
|
|
38
|
+
continue
|
|
39
|
+
|
|
40
|
+
# fix the windows-style relative paths
|
|
41
|
+
asset_filename = fix_path_separators(match)
|
|
42
|
+
|
|
43
|
+
# if we find that the asset exists, we can skip trying to fix this line
|
|
44
|
+
asset_path = Path(asset_filename)
|
|
45
|
+
if asset_path.exists():
|
|
46
|
+
autofixed_lines.append(line)
|
|
47
|
+
continue
|
|
48
|
+
|
|
49
|
+
# if we made it here, something is missing
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
# scan through the files in the dir+subdirs to see if we find an
|
|
53
|
+
# (otherwise identical) incorrectly-cased filename
|
|
54
|
+
potential_missing_file = lowercased_files[str(asset_path).lower()]
|
|
55
|
+
except KeyError:
|
|
56
|
+
# we couldn't find a matching case-corrected filename - display
|
|
57
|
+
# error and exit
|
|
58
|
+
messagebox.showerror(
|
|
59
|
+
"Error correcting library.txt",
|
|
60
|
+
"No Potential Match",
|
|
61
|
+
detail="No asset with an incorrectly-cased (yet otherwise "
|
|
62
|
+
+ f"identical) filename found for asset `{asset_path}`.",
|
|
63
|
+
)
|
|
64
|
+
sys.exit(1)
|
|
65
|
+
else:
|
|
66
|
+
# append the line to the autofixed file, replacing it with the
|
|
67
|
+
# case-corrected filename
|
|
68
|
+
autofixed_lines.append(fix_missing_asset(match, potential_missing_file))
|
|
69
|
+
num_fixed_lines += 1
|
|
70
|
+
|
|
71
|
+
lib_file.close()
|
|
72
|
+
|
|
73
|
+
if num_fixed_lines == 0:
|
|
74
|
+
# no changes necessary - inform user and exit
|
|
75
|
+
messagebox.showinfo(
|
|
76
|
+
"library.txt already correct",
|
|
77
|
+
"No Missing Assets",
|
|
78
|
+
detail=f"All assets present in `{lib_file.name}` were found. The "
|
|
79
|
+
+ "library file will not be modified.",
|
|
80
|
+
)
|
|
81
|
+
return
|
|
82
|
+
|
|
83
|
+
messagebox.showinfo(
|
|
84
|
+
"library.txt needs modification",
|
|
85
|
+
f"{num_fixed_lines} Lines Corrected.",
|
|
86
|
+
detail="The next step is to choose a file in which to save the "
|
|
87
|
+
+ "corrected library.",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
while True:
|
|
91
|
+
# ask user where to save modifications
|
|
92
|
+
output_file = filedialog.asksaveasfile(
|
|
93
|
+
title="Choose file to save modifications to"
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if output_file:
|
|
97
|
+
# they picked a file path - we can keep going
|
|
98
|
+
break
|
|
99
|
+
elif not messagebox.askyesno(
|
|
100
|
+
"No output file selected",
|
|
101
|
+
"Retry output file selection?",
|
|
102
|
+
detail="Changes will not be saved if an output filename isn't "
|
|
103
|
+
+ "selected.",
|
|
104
|
+
):
|
|
105
|
+
# user cancelled the dialogue, and they elected not to reattempt
|
|
106
|
+
# file path selection. we can bail now.
|
|
107
|
+
return
|
|
108
|
+
|
|
109
|
+
output_file.writelines(autofixed_lines)
|
|
110
|
+
output_file.close()
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pathlib
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def cd_if_necessary(lib: pathlib.Path):
|
|
7
|
+
"""
|
|
8
|
+
Change the process working directory to the library file's parent directory
|
|
9
|
+
if we aren't already there
|
|
10
|
+
|
|
11
|
+
This ensures that the relative paths in the library file resolve to the
|
|
12
|
+
case-corrected filenames properly.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
if lib.parent != pathlib.Path.cwd():
|
|
16
|
+
os.chdir(lib.parent)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def backup_library(lib: pathlib.Path):
|
|
20
|
+
"""
|
|
21
|
+
Back up the existing library
|
|
22
|
+
|
|
23
|
+
The backup will be created with the existing library's filename plus a
|
|
24
|
+
``.bak`` extension.
|
|
25
|
+
|
|
26
|
+
:param lib: the path to the existing library
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
backup_filename = str(lib) + ".bak"
|
|
30
|
+
|
|
31
|
+
print()
|
|
32
|
+
print(f"renaming `{lib}` to `{backup_filename}` ", end="")
|
|
33
|
+
|
|
34
|
+
try:
|
|
35
|
+
os.rename(lib, backup_filename)
|
|
36
|
+
print("succeeded")
|
|
37
|
+
except IsADirectoryError:
|
|
38
|
+
print("failed")
|
|
39
|
+
sys.exit(1)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def collect_lowercased_files() -> dict[str, str]:
|
|
43
|
+
"""
|
|
44
|
+
Get lowercased and actual members of the current directory and subdirectory
|
|
45
|
+
|
|
46
|
+
:return: a :class:`dict` with the lowercased filenames as keys and the
|
|
47
|
+
actual filenames as values.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
lowercased_files: dict[str, str] = {}
|
|
51
|
+
|
|
52
|
+
cwd = pathlib.Path(".")
|
|
53
|
+
for current_dir, _dirs, files in cwd.walk():
|
|
54
|
+
if current_dir == cwd or not files:
|
|
55
|
+
continue
|
|
56
|
+
|
|
57
|
+
for file in files:
|
|
58
|
+
filename = str(current_dir / file)
|
|
59
|
+
lowercased_files[filename.lower()] = filename
|
|
60
|
+
|
|
61
|
+
return lowercased_files
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def write_library(lib: pathlib.Path, lines: list[str]):
|
|
65
|
+
"""
|
|
66
|
+
Write the corrected lines to the library file
|
|
67
|
+
|
|
68
|
+
:param lib: the path to the library file
|
|
69
|
+
:param lines: the corrected contents to write
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
with open(lib, "w") as lib_file:
|
|
73
|
+
lib_file.writelines(lines)
|
|
74
|
+
print(f"autofixed library saved as `{lib}`")
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
EXPORT_LINE_REGEX = re.compile("^EXPORT (\\S*)\t+(\\S*)$")
|
|
4
|
+
"""RegEx matching lines exporting assets"""
|
|
5
|
+
|
|
6
|
+
_asset_filename_group = 2
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def fix_missing_asset(match: re.Match, fixed_path: str) -> str:
|
|
10
|
+
"""
|
|
11
|
+
Get an ``EXPORT`` line, updated with the case-corrected asset filename
|
|
12
|
+
|
|
13
|
+
:param match: the RegEx match containing the ``EXPORT`` line
|
|
14
|
+
:param fixed_path: the asset's corrected filename
|
|
15
|
+
:return: the ``EXPORT`` line with the new case-corrected filename
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
line = match.string
|
|
19
|
+
asset_filename_start, asset_filename_end = match.span(_asset_filename_group)
|
|
20
|
+
return line[:asset_filename_start] + fixed_path + line[asset_filename_end:]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def fix_path_separators(match: re.Match) -> str:
|
|
24
|
+
"""
|
|
25
|
+
Replace the Windows-style ``\\`` path separators with POSIX-style ``/``
|
|
26
|
+
|
|
27
|
+
:param match: the RegEx match containing the ``EXPORT`` line
|
|
28
|
+
:return: the filename with replaced path separators
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
return match.group(_asset_filename_group).replace("\\", "/")
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import pathlib
|
|
2
|
+
import platform
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def ensure_library_exists(lib: pathlib.Path):
|
|
7
|
+
"""Output an error and exit the script if the library file can't be found"""
|
|
8
|
+
|
|
9
|
+
if not lib.exists():
|
|
10
|
+
print(f"Unable to find library file `{lib}`")
|
|
11
|
+
sys.exit(1)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def ensure_linux():
|
|
15
|
+
"""Output an explanation and exit the script if the host OS isn't Linux"""
|
|
16
|
+
|
|
17
|
+
if platform.system() != "Linux":
|
|
18
|
+
print(
|
|
19
|
+
"This tool is intended for use on Linux; Windows and macOS (by "
|
|
20
|
+
+ "default) use case-insensitive filesystems and users will see no "
|
|
21
|
+
+ "benefit from correcting their library.txt.",
|
|
22
|
+
)
|
|
23
|
+
sys.exit(1)
|