FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-template/scripts/sync.py at main · NWarila/python-template · GitHub
NWarila
/
python-template
Public template
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
2
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
python-template
/
scripts
/
sync.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
94 lines (69 loc) · 3 KB
Breadcrumbs
python-template
/
scripts
/
sync.py
Copy path
File metadata and controls
94 lines (69 loc) · 3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Sync template-managed files into a target repo using sync-manifest.json.
Usage:
python sync.py TEMPLATE_ROOT REPO_ROOT
Reads sync-manifest.json from TEMPLATE_ROOT, copies files into REPO_ROOT using
the mode specified for each mapping:
overwrite Full replacement.
marker-preserve Replaces template-owned // #region sections while
preserving repo-specific content.
Exit codes:
0 Success (prints synced file count).
1 Missing arguments or manifest not found.
"""
from
__future__
import
annotations
import
json
import
re
import
sys
from
pathlib
import
Path
def
marker_preserve_copy
(
src
:
Path
,
dst
:
Path
)
->
None
:
"""Copy src to dst, preserving content outside template marker regions."""
if
not
dst
.
exists
():
dst
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
dst
.
write_text
(
src
.
read_text
(),
encoding
=
"utf-8"
)
return
src_text
=
src
.
read_text
(
encoding
=
"utf-8"
)
dst_text
=
dst
.
read_text
(
encoding
=
"utf-8"
)
pattern
=
re
.
compile
(
r"(//\s*#region\s+Template:\s*\S+.*?\n)(.*?)(//\s*#endregion\s+Template:)"
,
re
.
DOTALL
,
)
src_regions
:
dict
[
str
,
str
]
=
{}
for
match
in
pattern
.
finditer
(
src_text
):
name_match
=
re
.
search
(
r"Template:\s*(\S+)"
,
match
.
group
(
1
))
if
name_match
:
src_regions
[
name_match
.
group
(
1
)]
=
match
.
group
(
2
)
def
replace_region
(
match
:
re
.
Match
[
str
])
->
str
:
name_match
=
re
.
search
(
r"Template:\s*(\S+)"
,
match
.
group
(
1
))
if
name_match
and
name_match
.
group
(
1
)
in
src_regions
:
return
match
.
group
(
1
)
+
src_regions
[
name_match
.
group
(
1
)]
+
match
.
group
(
3
)
return
match
.
group
(
0
)
dst
.
write_text
(
pattern
.
sub
(
replace_region
,
dst_text
),
encoding
=
"utf-8"
)
def
sync
(
template_root
:
Path
,
repo_root
:
Path
)
->
int
:
"""Run the sync and return the number of files synced."""
manifest_path
=
template_root
/
"sync-manifest.json"
if
not
manifest_path
.
exists
():
print
(
f"Error:
{
manifest_path
}
not found"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
manifest
=
json
.
loads
(
manifest_path
.
read_text
(
encoding
=
"utf-8"
))
changed
:
list
[
str
]
=
[]
for
mapping
in
manifest
.
get
(
"files"
, []):
src
=
template_root
/
mapping
[
"src"
]
dst
=
repo_root
/
mapping
[
"dest"
]
mode
=
mapping
.
get
(
"mode"
,
"overwrite"
)
if
not
src
.
exists
():
print
(
f" Skip (source missing):
{
mapping
[
'src'
]
}
"
)
continue
dst
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
if
mode
==
"marker-preserve"
:
marker_preserve_copy
(
src
,
dst
)
else
:
dst
.
write_text
(
src
.
read_text
(
encoding
=
"utf-8"
),
encoding
=
"utf-8"
)
changed
.
append
(
mapping
[
"dest"
])
print
(
f" Synced:
{
mapping
[
'src'
]
}
->
{
mapping
[
'dest'
]
}
(
{
mode
}
)"
)
print
(
f"
\n
{
len
(
changed
)
}
file(s) synced."
)
return
len
(
changed
)
if
__name__
==
"__main__"
:
if
len
(
sys
.
argv
)
!=
3
:
print
(
f"Usage:
{
sys
.
argv
[
0
]
}
TEMPLATE_ROOT REPO_ROOT"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
sync
(
Path
(
sys
.
argv
[
1
]),
Path
(
sys
.
argv
[
2
]))
Back
|
FazBrowse Home
|
New Git URL