Skip to content

Commit ca5dff6

Browse files
author
Oren Cohen
committed
Extract import logic to a separate method
1 parent b24f42f commit ca5dff6

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

tools/importer/importer.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,46 @@ def copy_folder(src, dst):
9696
copy_file(abs_src_file, abs_dst_file)
9797

9898

99+
def import_files(repo_path, data_files, data_folders):
100+
"""
101+
Imports files and directories to mbed-os
102+
103+
:param repo_path: Path to the repo copying from.
104+
:param data_files: List of files to be copied. (with destination)
105+
:param data_folders: List of directories to be copied. (with destination)
106+
:return: None
107+
"""
108+
109+
# Remove all files listed in .json from mbed-os repo to avoid duplications
110+
for fh in data_files:
111+
src_file = fh['src_file']
112+
del_file(os.path.basename(src_file))
113+
dest_file = join(ROOT, fh['dest_file'])
114+
if isfile(dest_file):
115+
os.remove(join(ROOT, dest_file))
116+
rel_log.debug("Deleted %s", fh['dest_file'])
117+
for folder in data_folders:
118+
dest_folder = folder['dest_folder']
119+
delete_dir_files(dest_folder)
120+
rel_log.debug("Deleted: %s", folder['dest_folder'])
121+
rel_log.info("Removed files/folders listed in json file")
122+
123+
# Copy all the files listed in json file to mbed-os
124+
for fh in data_files:
125+
repo_file = join(repo_path, fh['src_file'])
126+
mbed_path = join(ROOT, fh['dest_file'])
127+
mkdir(dirname(mbed_path))
128+
copy_file(repo_file, mbed_path)
129+
rel_log.debug("Copied %s to %s", normpath(repo_file),
130+
normpath(mbed_path))
131+
for folder in data_folders:
132+
repo_folder = join(repo_path, folder['src_folder'])
133+
mbed_path = join(ROOT, folder['dest_folder'])
134+
copy_folder(repo_folder, mbed_path)
135+
rel_log.debug("Copied %s to %s", normpath(repo_folder),
136+
normpath(mbed_path))
137+
138+
99139
def run_cmd_with_output(command, exit_on_failure=False):
100140
"""
101141
Passes a command to the system and returns a True/False result once the
@@ -247,38 +287,7 @@ def normalize_commit_sha(sha_lst):
247287
else:
248288
data_files = json_data["files"]
249289
data_folders = json_data["folders"]
250-
251-
# Remove all files listed in .json from mbed-os repo to avoid duplications
252-
for fh in data_files:
253-
src_file = fh['src_file']
254-
del_file(os.path.basename(src_file))
255-
dest_file = join(ROOT, fh['dest_file'])
256-
if isfile(dest_file):
257-
os.remove(join(ROOT, dest_file))
258-
rel_log.debug("Deleted %s", fh['dest_file'])
259-
260-
for folder in data_folders:
261-
dest_folder = folder['dest_folder']
262-
delete_dir_files(dest_folder)
263-
rel_log.debug("Deleted: %s", folder['dest_folder'])
264-
265-
rel_log.info("Removed files/folders listed in json file")
266-
267-
# Copy all the files listed in json file to mbed-os
268-
for fh in data_files:
269-
repo_file = join(args.repo_path, fh['src_file'])
270-
mbed_path = join(ROOT, fh['dest_file'])
271-
mkdir(dirname(mbed_path))
272-
copy_file(repo_file, mbed_path)
273-
rel_log.debug("Copied %s to %s", normpath(repo_file),
274-
normpath(mbed_path))
275-
276-
for folder in data_folders:
277-
repo_folder = join(args.repo_path, folder['src_folder'])
278-
mbed_path = join(ROOT, folder['dest_folder'])
279-
copy_folder(repo_folder, mbed_path)
280-
rel_log.debug("Copied %s to %s", normpath(repo_folder),
281-
normpath(mbed_path))
290+
import_files(args.repo_path, data_files, data_folders)
282291

283292
# Create new branch with all changes
284293
create_branch = ['git', 'checkout', '-b', branch]

0 commit comments

Comments
 (0)