Skip to content

Commit 0bb8e2b

Browse files
adbridge0xc0170
authored andcommitted
Updated to allow for new directory structure for mbed-dev source.
Fixed issue where some deleted files/folders where not being removed from the repo. Removed code no longer used/needed.
1 parent 32137fa commit 0bb8e2b

File tree

1 file changed

+88
-116
lines changed

1 file changed

+88
-116
lines changed

tools/synch.py

Lines changed: 88 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"""
2222
import sys
2323
from copy import copy
24-
from os import walk, remove, makedirs
25-
from os.path import join, abspath, dirname, relpath, exists, isfile
24+
from os import walk, remove, makedirs, getcwd, rmdir, listdir
25+
from os.path import join, abspath, dirname, relpath, exists, isfile, normpath, isdir
2626
from shutil import copyfile
2727
from optparse import OptionParser
2828
import re
@@ -46,60 +46,8 @@
4646
# Code that does have a mirror in the mbed SDK
4747
# Tuple data: (repo_name, list_of_code_dirs, [team])
4848
# team is optional - if not specified, the code is published under mbed_official
49-
OFFICIAL_CODE = (
50-
("mbed-dev" , [MBED_DRIVERS, MBED_PLATFORM, MBED_HAL]),
51-
("mbed-rtos", RTOS),
52-
("mbed-dsp" , DSP),
53-
("mbed-rpc" , MBED_RPC),
54-
55-
("lwip" , LWIP_SOURCES+"/lwip"),
56-
("lwip-sys", LWIP_SOURCES+"/lwip-sys"),
57-
("Socket" , LWIP_SOURCES+"/Socket"),
58-
59-
("lwip-eth" , ETH_SOURCES+"/lwip-eth"),
60-
("EthernetInterface", ETH_SOURCES+"/EthernetInterface"),
61-
62-
("USBDevice", USB),
63-
("USBHost" , USB_HOST),
64-
65-
("CellularModem", CELLULAR_SOURCES),
66-
("CellularUSBModem", CELLULAR_USB_SOURCES),
67-
("UbloxUSBModem", UBLOX_SOURCES),
68-
("UbloxModemHTTPClientTest", [TEST_DIR+"/net/cellular/http/common", TEST_DIR+"/net/cellular/http/ubloxusb"]),
69-
("UbloxModemSMSTest", [TEST_DIR+"/net/cellular/sms/common", TEST_DIR+"/net/cellular/sms/ubloxusb"]),
70-
("FATFileSystem", FAT_FS, "mbed-official"),
71-
)
72-
49+
OFFICIAL_CODE = {"mbed-dev" : ["cmsis", "drivers", "hal", "platform", "targets", "mbed.h"]}
7350

74-
# Code that does have dependencies to libraries should point to
75-
# the latest revision. By default, they point to a specific revision.
76-
CODE_WITH_DEPENDENCIES = (
77-
# Libraries
78-
"EthernetInterface",
79-
80-
# RTOS Examples
81-
"rtos_basic",
82-
"rtos_isr",
83-
"rtos_mail",
84-
"rtos_mutex",
85-
"rtos_queue",
86-
"rtos_semaphore",
87-
"rtos_signals",
88-
"rtos_timer",
89-
90-
# Net Examples
91-
"TCPEchoClient",
92-
"TCPEchoServer",
93-
"TCPSocket_HelloWorld",
94-
"UDPSocket_HelloWorld",
95-
"UDPEchoClient",
96-
"UDPEchoServer",
97-
"BroadcastReceive",
98-
"BroadcastSend",
99-
100-
# mbed sources
101-
"mbed-src-program",
102-
)
10351

10452
# A list of regular expressions that will be checked against each directory
10553
# name and skipped if they match.
@@ -125,13 +73,11 @@ def run_and_print(command, cwd):
12573
stdout, _, _ = run_cmd(command, work_dir=cwd, redirect=True)
12674
print(stdout)
12775

128-
def __init__(self, name, team = None):
76+
def __init__(self, name):
12977
self.name = name
13078
self.path = join(MBED_ORG_PATH, name)
131-
if team is None:
132-
self.url = "http://" + MBED_URL + "/users/" + MBED_USER + "/code/%s/"
133-
else:
134-
self.url = "http://" + MBED_URL + "/teams/" + team + "/code/%s/"
79+
self.url = "http://" + MBED_URL + "/users/" + MBED_ORG_USER + "/code/%s/"
80+
13581
if not exists(self.path):
13682
# Checkout code
13783
if not exists(MBED_ORG_PATH):
@@ -158,6 +104,9 @@ def publish(self):
158104
commit = raw_input(push_remote and "Do you want to commit and push? Y/N: " or "Do you want to commit? Y/N: ")
159105
if commit == 'Y':
160106
args = ['hg', 'commit', '-u', MBED_ORG_USER]
107+
108+
109+
# NOTE commit_msg should always come from the relevant mbed 2 release text
161110
if commit_msg:
162111
args = args + ['-m', commit_msg]
163112
self.run_and_print(args, cwd=self.path)
@@ -229,7 +178,7 @@ def copy_with_line_endings(sdk_file, repo_file):
229178
f.close()
230179

231180
def visit_files(path, visit):
232-
for root, dirs, files in walk(path):
181+
for root, dirs, files in walk(path):
233182
# Ignore hidden directories
234183
for d in copy(dirs):
235184
full = join(root, d)
@@ -245,91 +194,122 @@ def visit_files(path, visit):
245194

246195
visit(join(root, file))
247196

197+
def visit_dirs(path, visit):
198+
199+
for root, dirs, files in walk(path, topdown=False):
200+
for d in dirs:
201+
full = join(root, d)
202+
203+
# We don't want to remove the .hg directory
204+
if not '.hg' in full:
205+
visit(full)
206+
248207

249-
def update_repo(repo_name, sdk_paths, team_name):
250-
repo = MbedRepository(repo_name, team_name)
208+
def update_repo(repo_name, sdk_paths, lib=False):
209+
repo = MbedRepository(repo_name)
210+
251211
# copy files from mbed SDK to mbed_official repository
252212
def visit_mbed_sdk(sdk_file):
253-
repo_file = join(repo.path, relpath(sdk_file, sdk_path))
254213

214+
# Source files structure is different for the compiled binary lib
215+
# compared to the mbed-dev sources
216+
if lib:
217+
repo_file = join(repo.path, relpath(sdk_file, sdk_path))
218+
else:
219+
repo_file = join(repo.path, sdk_file)
255220
repo_dir = dirname(repo_file)
256221
if not exists(repo_dir):
222+
print("CREATING: %s" % repo_dir)
257223
makedirs(repo_dir)
258224

259225
copy_with_line_endings(sdk_file, repo_file)
226+
227+
# Go through each path specified in the mbed structure
260228
for sdk_path in sdk_paths:
261-
visit_files(sdk_path, visit_mbed_sdk)
229+
230+
if isfile(sdk_path):
231+
# Single file so just copy directly across
232+
visit_mbed_sdk(sdk_path)
233+
else:
234+
visit_files(sdk_path, visit_mbed_sdk)
235+
236+
def sdk_remove(repo_path):
237+
238+
print("REMOVING: %s" % repo_path)
239+
240+
# Check if this is an empty directory or a file before determining how to
241+
# delete it. As this function should only be called with a directory list
242+
# after being called with a file list, the directory should automatically
243+
# be either valid or empty .
244+
if isfile(repo_path):
245+
remove(repo_path)
246+
elif isdir(repo_path) and not listdir(repo_path):
247+
rmdir(repo_path)
248+
else:
249+
print("ERROR: %s is not empty, please remove manually." % repo_path)
250+
print listdir(repo_path)
251+
exit(1)
262252

263253
# remove repository files that do not exist in the mbed SDK
264-
def visit_repo(repo_file):
254+
def visit_lib_repo(repo_path):
265255
for sdk_path in sdk_paths:
266-
sdk_file = join(sdk_path, relpath(repo_file, repo.path))
267-
if exists(sdk_file):
268-
break
269-
else:
270-
remove(repo_file)
271-
print "remove: %s" % repo_file
272-
visit_files(repo.path, visit_repo)
256+
sdk_file = join(sdk_path, relpath(repo_path, repo.path))
257+
if not exists(sdk_file):
258+
sdk_remove(repo_path)
273259

274-
if repo.publish():
275-
changed.append(repo_name)
260+
# remove repository files that do not exist in the mbed SDK source
261+
def visit_repo(repo_path):
276262

263+
# work out equivalent sdk path from repo file
264+
sdk_path = join(getcwd(), relpath(repo_path, repo.path))
277265

278-
def update_code(repositories):
279-
for r in repositories:
280-
repo_name, sdk_dir = r[0], r[1]
281-
team_name = r[2] if len(r) == 3 else None
282-
print '\n=== Updating "%s" ===' % repo_name
283-
sdk_dirs = [sdk_dir] if type(sdk_dir) != type([]) else sdk_dir
284-
update_repo(repo_name, sdk_dirs, team_name)
266+
if not exists(sdk_path):
267+
sdk_remove(repo_path)
268+
269+
# Go through each path specified in the mbed structure
270+
# Check if there are any files in any of those paths that are no longer part of the SDK
285271

286-
def update_single_repo(repo):
287-
repos = [r for r in OFFICIAL_CODE if r[0] == repo]
288-
if not repos:
289-
print "Repository '%s' not found" % repo
272+
if lib:
273+
visit_files(repo.path, visit_lib_repo)
274+
# Now do the same for directories that may need to be removed. This needs to be done
275+
# bottom up to ensure any lower nested directories can be deleted first
276+
visit_dirs(repo.path, visit_lib_repo)
277+
290278
else:
291-
update_code(repos)
279+
visit_files(repo.path, visit_repo)
292280

293-
def update_dependencies(repositories):
294-
for repo_name in repositories:
295-
print '\n=== Updating "%s" ===' % repo_name
296-
repo = MbedRepository(repo_name)
281+
# Now do the same for directories that may need to be removed. This needs to be done
282+
# bottom up to ensure any lower nested directories can be deleted first
283+
visit_dirs(repo.path, visit_repo)
284+
285+
if repo.publish():
286+
changed.append(repo_name)
297287

298-
# point to the latest libraries
299-
def visit_repo(repo_file):
300-
with open(repo_file, "r") as f:
301-
url = f.read()
302-
with open(repo_file, "w") as f:
303-
f.write(url[:(url.rindex('/')+1)])
304-
visit_files(repo.path, visit_repo, None, MBED_REPO_EXT)
305288

306-
if repo.publish():
307-
changed.append(repo_name)
289+
def update_code(repositories):
290+
for repo_name in repositories.keys():
291+
sdk_dirs = repositories[repo_name]
292+
print '\n=== Updating "%s" ===' % repo_name
293+
update_repo(repo_name, sdk_dirs)
308294

309295

310296
def update_mbed():
311-
update_repo("mbed", [join(BUILD_DIR, "mbed")], None)
297+
update_repo("mbed", [join(BUILD_DIR, "mbed")], lib=True)
312298

313299
def do_sync(options):
314300
global push_remote, quiet, commit_msg, changed
315301

316302
push_remote = not options.nopush
317303
quiet = options.quiet
318304
commit_msg = options.msg
319-
chnaged = []
305+
changed = []
320306

321307
if options.code:
322308
update_code(OFFICIAL_CODE)
323309

324-
if options.dependencies:
325-
update_dependencies(CODE_WITH_DEPENDENCIES)
326-
327310
if options.mbed:
328311
update_mbed()
329312

330-
if options.repo:
331-
update_single_repo(options.repo)
332-
333313
if changed:
334314
print "Repositories with changes:", changed
335315

@@ -342,10 +322,6 @@ def do_sync(options):
342322
action="store_true", default=False,
343323
help="Update the mbed_official code")
344324

345-
parser.add_option("-d", "--dependencies",
346-
action="store_true", default=False,
347-
help="Update the mbed_official code dependencies")
348-
349325
parser.add_option("-m", "--mbed",
350326
action="store_true", default=False,
351327
help="Release a build of the mbed library")
@@ -358,10 +334,6 @@ def do_sync(options):
358334
action="store", type="string", default='', dest='msg',
359335
help="Commit message to use for all the commits")
360336

361-
parser.add_option("-r", "--repository",
362-
action="store", type="string", default='', dest='repo',
363-
help="Synchronize only the given repository")
364-
365337
parser.add_option("-q", "--quiet",
366338
action="store_true", default=False,
367339
help="Don't ask for confirmation before commiting or pushing")

0 commit comments

Comments
 (0)