Skip to content

Commit c4cdaf4

Browse files
committed
Added --dry-run flag to sync command
1 parent 2aa4786 commit c4cdaf4

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ tests/test_2.py
8282
* `mpbridge.ignore` syntax is not as same as `.gitignore` files.
8383
* At this time `mpbridge.ignore` only supports specifying file and directory paths directly.
8484
* You should add a **slash** at the end of directory names: `dir1/`.
85+
* Performing `sync` with `--dry-run` flag can be helpful for debugging your ignore files.
8586

8687
## ✅ Supported platforms
8788

mpbridge/bridge.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ def start_bridge_mode(port: str):
4040
observer.join()
4141

4242

43-
def sync(port: str, path: str, clean: bool):
43+
def sync(port: str, path: str, clean: bool, dry_run: bool):
4444
port = utils.port_abbreviation(port)
4545
print(Fore.YELLOW, f"- Syncing files on {port} with {path}")
4646
utils.reset_term_color()
4747
pyb = SweetPyboard(device=port)
4848
pyb.enter_raw_repl_verbose()
4949
if clean:
5050
print(Fore.YELLOW, "Clean Sync files")
51-
pyb.delete_absent_items(dir_path=path)
52-
pyb.sync_with_dir(dir_path=path)
51+
pyb.delete_absent_items(dir_path=path, dry=dry_run)
52+
pyb.sync_with_dir(dir_path=path, dry=dry_run)
5353
pyb.exit_raw_repl_verbose()
5454

5555

mpbridge/pyboard.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,48 +61,54 @@ def fs_recursive_listdir(self):
6161
dirs[item[0]] = item[2]
6262
return dirs, files
6363

64-
def fs_verbose_get(self, src, dest, chunk_size=1024):
64+
def fs_verbose_get(self, src, dest, chunk_size=1024, dry: bool = False):
6565
def print_prog(written, total):
6666
utils.print_progress_bar(
6767
iteration=written, total=total, decimals=0,
6868
prefix=f"{Fore.LIGHTCYAN_EX} ↓ Getting {src}".ljust(60),
6969
suffix="Complete", length=15)
7070

71-
self.fs_get(src, dest, chunk_size=chunk_size, progress_callback=print_prog)
71+
if not dry:
72+
self.fs_get(src, dest, chunk_size=chunk_size, progress_callback=print_prog)
7273
print_prog(1, 1)
7374
utils.reset_term_color(new_line=True)
7475

75-
def fs_verbose_put(self, src, dest, chunk_size=1024):
76+
def fs_verbose_put(self, src, dest, chunk_size=1024, dry: bool = False):
7677
def print_prog(written, total):
7778
utils.print_progress_bar(
7879
iteration=written, total=total, decimals=0,
7980
prefix=f"{Fore.LIGHTYELLOW_EX} ↑ Putting {dest}".ljust(60),
8081
suffix="Complete", length=15)
8182

82-
self.fs_put(src, dest, chunk_size=chunk_size, progress_callback=print_prog)
83+
if not dry:
84+
self.fs_put(src, dest, chunk_size=chunk_size, progress_callback=print_prog)
8385
print_prog(1, 1)
8486
utils.reset_term_color(new_line=True)
8587

86-
def fs_verbose_rename(self, src, dest):
87-
buf, consumer = generate_buffer()
88-
self.exec_(f'from os import rename; rename("{src}", "{dest}")',
89-
data_consumer=consumer)
88+
def fs_verbose_rename(self, src, dest, dry: bool = False):
89+
if not dry:
90+
buf, consumer = generate_buffer()
91+
self.exec_(f'from os import rename; rename("{src}", "{dest}")',
92+
data_consumer=consumer)
9093
print(Fore.LIGHTBLUE_EX, "O Rename", src, "→", dest)
9194
utils.reset_term_color()
9295

93-
def fs_verbose_mkdir(self, dir_path):
94-
self.fs_mkdir(dir_path)
96+
def fs_verbose_mkdir(self, dir_path, dry: bool = False):
97+
if not dry:
98+
self.fs_mkdir(dir_path)
9599
print(Fore.LIGHTGREEN_EX, "* Created", dir_path)
96100
utils.reset_term_color()
97101

98-
def fs_verbose_rm(self, src):
99-
self.fs_rm(src)
102+
def fs_verbose_rm(self, src, dry: bool = False):
103+
if not dry:
104+
self.fs_rm(src)
100105
print(Fore.LIGHTRED_EX, "✕ Removed", src)
101106
utils.reset_term_color()
102107

103-
def fs_verbose_rmdir(self, dir_path):
108+
def fs_verbose_rmdir(self, dir_path, dry: bool = False):
104109
try:
105-
self.fs_rmdir(dir_path)
110+
if not dry:
111+
self.fs_rmdir(dir_path)
106112
except PyboardError:
107113
print(Fore.RED, "E Cannot remove directory", dir_path, "as it might be mounted")
108114
else:
@@ -118,46 +124,47 @@ def copy_all(self, dest_dir_path):
118124
print(Fore.LIGHTGREEN_EX, "✓ Copied all files successfully")
119125
utils.reset_term_color()
120126

121-
def sync_with_dir(self, dir_path):
127+
def sync_with_dir(self, dir_path, dry: bool = False):
122128
print(Fore.YELLOW, "- Syncing")
123129
self.exec_raw_no_follow(SHA1_FUNC)
124130
dir_path = utils.replace_backslashes(dir_path)
125131
rdirs, rfiles = self.fs_recursive_listdir()
126132
ldirs, lfiles = utils.recursive_list_dir(dir_path)
127133
ignore = IgnoreStorage(dir_path=dir_path)
128-
for rdir in rdirs.keys():
129-
if rdir not in ldirs and not ignore.match_dir(rdir):
130-
os.makedirs(dir_path + rdir, exist_ok=True)
134+
if not dry:
135+
for rdir in rdirs.keys():
136+
if rdir not in ldirs and not ignore.match_dir(rdir):
137+
os.makedirs(dir_path + rdir, exist_ok=True)
131138
for ldir in ldirs.keys():
132139
if ldir not in rdirs and not ignore.match_dir(ldir):
133-
self.fs_verbose_mkdir(ldir)
140+
self.fs_verbose_mkdir(ldir, dry=dry)
134141
for lfile_rel, lfiles_abs in lfiles.items():
135142
if ignore.match_file(lfile_rel):
136143
continue
137144
if rfiles.get(lfile_rel, None) == os.path.getsize(lfiles_abs):
138145
if self.get_sha1(lfile_rel) == utils.get_file_sha1(lfiles_abs):
139146
continue
140-
self.fs_verbose_put(lfiles_abs, lfile_rel, chunk_size=256)
147+
self.fs_verbose_put(lfiles_abs, lfile_rel, chunk_size=256, dry=dry)
141148
for rfile, rsize in rfiles.items():
142149
if ignore.match_file(rfile):
143150
continue
144151
if rfile not in lfiles:
145-
self.fs_verbose_get(rfile, dir_path + rfile, chunk_size=256)
152+
self.fs_verbose_get(rfile, dir_path + rfile, chunk_size=256, dry=dry)
146153
print(Fore.LIGHTGREEN_EX, "✓ Files synced successfully")
147154

148-
def delete_absent_items(self, dir_path):
155+
def delete_absent_items(self, dir_path, dry: bool = False):
149156
dir_path = utils.replace_backslashes(dir_path)
150157
rdirs, rfiles = self.fs_recursive_listdir()
151158
ldirs, lfiles = utils.recursive_list_dir(dir_path)
152159
ignore = IgnoreStorage(dir_path=dir_path)
153160
for rfile, rsize in rfiles.items():
154161
if not ignore.match_file(rfile) and rfile not in lfiles:
155-
self.fs_verbose_rm(rfile)
162+
self.fs_verbose_rm(rfile, dry=dry)
156163
for rdir in rdirs.keys():
157164
if not ignore.match_dir(rdir) and rdir not in ldirs:
158165
# There might be ignored files in folders
159166
with suppress(Exception):
160-
self.fs_verbose_rmdir(rdir)
167+
self.fs_verbose_rmdir(rdir, dry=dry)
161168

162169
def clear_all(self):
163170
print(Fore.YELLOW, "- Deleting all files from MicroPython board")

mpbridge/shell.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def bridge_mode(port):
3333
@click.argument('dir_path', type=click.Path(
3434
exists=True, file_okay=False, dir_okay=True, resolve_path=True), default="")
3535
@click.option('--clean', "-c", is_flag=True, help="Execute Clean Sync")
36-
def sync(port, dir_path, clean):
36+
@click.option('--dry-run', "-d", is_flag=True, help="Test Sync command without performing any actions")
37+
def sync(port, dir_path, clean, dry_run):
3738
"""Sync files of on [PORT] in specified directory [DIR_PATH]
3839
3940
If [DIR_PATH] is not set, it defaults to the current path
@@ -66,7 +67,7 @@ def sync(port, dir_path, clean):
6667
6768
and then push the different files from the local to the device.
6869
"""
69-
bridge.sync(port, dir_path, clean)
70+
bridge.sync(port, dir_path, clean, dry_run)
7071

7172

7273
@main.command("dev", short_help='Start development mode')

0 commit comments

Comments
 (0)