1
- import os , json , stat , sys , shutil , errno , subprocess
1
+ import os , json , stat , sys , shutil , errno , subprocess , logging
2
2
from os .path import dirname , abspath , basename , join
3
+ import argparse
3
4
4
5
ROOT = abspath (join (dirname (__file__ ), "../.." ))
5
- CMSIS_REPO = "CMSIS_Repo"
6
- CMSIS_PATH = abspath (join (dirname (__file__ ), CMSIS_REPO ))
7
6
RTOS_UPDATE_BRANCH = "rtos_update"
8
7
9
8
def del_file (name ):
@@ -30,36 +29,36 @@ def rmtree(top):
30
29
def copy_file (file , path ):
31
30
try :
32
31
shutil .copy (file , path )
33
- except IOError as e :
32
+ except IOError as e :
34
33
if e .errno != errno .ENOENT :
35
34
raise
36
35
## Create directories
37
36
os .makedirs (os .path .dirname (path ))
38
37
shutil .copy (file , path )
39
38
print os .path .relpath (path , ROOT )
40
-
41
- def copy_folder (folder , path ):
42
- files = os .listdir (folder )
39
+
40
+ def copy_folder (folder , path ):
41
+ files = os .listdir (folder )
43
42
for file in files :
44
43
abs_src_file = os .path .join (folder , file )
45
44
if os .path .isfile (abs_src_file ):
46
45
abs_dst_file = os .path .join (path , file )
47
46
copy_file (abs_src_file , abs_dst_file )
48
-
47
+
49
48
def run_cmd (command , exit_on_failure = False ):
50
- """ Passes a command to the system and returns a True/False result once the
49
+ """ Passes a command to the system and returns a True/False result once the
51
50
command has been executed, indicating success/failure. Commands are passed
52
- as a list of tokens.
51
+ as a list of tokens.
53
52
E.g. The command 'git remote -v' would be passed in as ['git', 'remote', '-v']
54
53
"""
55
54
return_code = subprocess .call (command , shell = True )
56
-
55
+
57
56
if return_code :
58
57
print ("The command %s failed with return code: %s"
59
58
% (' ' .join (command ), return_code ))
60
59
if exit_on_failure :
61
60
sys .exit (1 )
62
-
61
+
63
62
return return_code
64
63
65
64
def remove_repo (folder ):
@@ -76,47 +75,71 @@ def get_repo(repo, branch, folder):
76
75
remove_repo (folder )
77
76
clone_cmd = ['git' , 'clone' , repo , "-b" , branch , "--depth" , '1' , folder ]
78
77
run_cmd (clone_cmd , exit_on_failure = True )
79
-
78
+
80
79
if __name__ == "__main__" :
81
-
80
+
81
+ parser = argparse .ArgumentParser (description = __doc__ ,
82
+ formatter_class = argparse .RawDescriptionHelpFormatter )
83
+ parser .add_argument ('-l' , '--log-level' ,
84
+ help = "Level for providing logging output" ,
85
+ default = 'INFO' )
86
+ parser .add_argument ('-r' , '--repo-path' ,
87
+ help = "Git Repository to be imported" ,
88
+ default = None )
89
+ parser .add_argument ('-c' , '--config-file' ,
90
+ help = "Configuration file" ,
91
+ default = None )
92
+ args = parser .parse_args ()
93
+
94
+ default = getattr (logging , 'INFO' )
95
+ level = getattr (logging , args .log_level .upper (), default )
96
+
97
+ # Set logging level
98
+ logging .basicConfig (level = level )
99
+ rel_log = logging .getLogger ("Importer" )
100
+
101
+ if (args .repo_path is None ) or (args .config_file is None ) :
102
+ rel_log .error ("Repository path and config file required as input. Use \" --help\" for more info." )
103
+ exit (1 )
104
+
105
+ repo = os .path .abspath (args .repo_path )
106
+ if not os .path .exists (repo ):
107
+ rel_log .error ("%s not found." , args .repo_path )
108
+ exit (1 )
109
+
110
+ json_file = os .path .abspath (args .config_file )
111
+ if not os .path .isfile (json_file ):
112
+ rel_log .error ("%s not found." , args .config_file )
113
+ exit (1 )
114
+
82
115
# Read configuration data
83
- with open (os . path . join ( os . path . dirname ( __file__ ), "cmsis_importer.json" ), 'r' ) as config :
116
+ with open (json_file , 'r' ) as config :
84
117
json_data = json .load (config )
85
- config = json_data ["config" ]
86
- cmsis_repo = config ['cmsis_repo' ]
87
- cmsis_branch = config ['cmsis_branch' ]
88
118
data_files = json_data ["files" ]
89
119
data_folders = json_data ["folders" ]
90
-
91
- print "Fetching git repo"
92
- get_repo (cmsis_repo , cmsis_branch , CMSIS_REPO )
93
-
120
+
94
121
## Remove all files listed in .json from mbed-os repo to avoid duplications
95
122
print "Cleaning up:"
96
123
for file in data_files :
97
124
cmsis_file = file ['cmsis_file' ]
98
125
del_file (os .path .basename (cmsis_file ))
99
-
126
+
100
127
for folder in data_folders :
101
- cmsis_folder = os .path .join (CMSIS_PATH , folder ['cmsis_folder' ])
102
- files = os .listdir (cmsis_folder )
103
- for file in files :
128
+ files = os .listdir (repo_path )
129
+ for file in files :
104
130
del_file (os .path .basename (file ))
105
131
106
- ## Copy all the CMSIS files listed in json file to mbed-os
107
- print "Files Copied:"
108
- for file in data_files :
109
- cmsis_file = os .path .join (CMSIS_PATH , file ['cmsis_file' ])
132
+ ## Copy all the CMSIS files listed in json file to mbed-os
133
+ print "Files Copied:"
134
+ for file in data_files :
135
+ repo_file = os .path .join (repo_path , file ['cmsis_file' ])
110
136
mbed_path = os .path .join (ROOT , file ['mbed_file' ])
111
- copy_file (cmsis_file , mbed_path )
137
+ copy_file (repo_file , mbed_path )
112
138
113
139
for folder in data_folders :
114
- cmsis_folder = os .path .join (CMSIS_PATH , folder ['cmsis_folder' ])
115
- mbed_path = os .path .join (ROOT , folder ['mbed_folder' ])
116
- copy_folder (cmsis_folder , mbed_path )
117
-
118
- #Remove CMSIS Repo
119
- remove_repo (CMSIS_REPO )
140
+ repo_folder = os .path .join (repo_path , folder ['cmsis_folder' ])
141
+ mbed_path = os .path .join (ROOT , folder ['mbed_folder' ])
142
+ copy_folder (repo_folder , mbed_path )
120
143
121
144
## Create new branch with all changes
122
145
create_branch = ['git' , 'checkout' , '-b' , RTOS_UPDATE_BRANCH ]
@@ -127,17 +150,10 @@ def get_repo(repo, branch, folder):
127
150
128
151
commit_branch = ['git' , 'commit' , '-m' , "CMSIS/RTX: Update CMSIS/RTX" ]
129
152
run_cmd (commit_branch , exit_on_failure = True )
130
-
153
+
131
154
## Apply commits specific to mbed-os changes
132
155
mbed_sha = json_data ["Mbed_sha" ]
133
156
134
157
for sha in mbed_sha :
135
158
cherry_pick_sha = ['git' , 'cherry-pick' , sha ]
136
159
run_cmd (cherry_pick_sha , exit_on_failure = True )
137
-
138
-
139
-
140
-
141
-
142
-
143
-
0 commit comments