1
- import os , json , stat , sys , shutil , errno , subprocess , logging , re
1
+ import os
2
+ import json
3
+ import sys
4
+ import subprocess
5
+ import logging
2
6
import argparse
3
- from os .path import dirname , abspath , basename , join
7
+ from os .path import dirname , abspath , join
4
8
5
9
# Be sure that the tools directory is in the search path
6
10
ROOT = abspath (join (dirname (__file__ ), "../.." ))
@@ -21,7 +25,7 @@ def del_file(name):
21
25
result .append (os .path .join (root , name ))
22
26
for file in result :
23
27
os .remove (file )
24
- rel_log .debug ("Deleted: %s" , os .path .relpath (file , ROOT ));
28
+ rel_log .debug ("Deleted: %s" , os .path .relpath (file , ROOT ))
25
29
26
30
def copy_folder (src , dest ):
27
31
""" Copy contents of folder in mbed-os listed path
@@ -75,24 +79,23 @@ def get_curr_sha(repo_path):
75
79
sha - last commit SHA
76
80
"""
77
81
cwd = os .getcwd ()
78
- sha = None
79
82
os .chdir (abspath (repo_path ))
80
83
81
84
cmd = "git log --pretty=format:%h -n 1"
82
- ret , sha = run_cmd_with_output (cmd , exit_on_failure = True )
85
+ _ , sha = run_cmd_with_output (cmd , exit_on_failure = True )
83
86
84
87
os .chdir (cwd )
85
88
return sha
86
89
87
- def check_branch (name ):
90
+ def branch_exists (name ):
88
91
""" Check if branch already exists in mbed-os local repository.
89
92
It will not verify if branch is present in remote repository.
90
93
Args:
91
94
name - branch name
92
95
Returns:
93
96
True - If branch is already present
94
97
"""
95
- branches = []
98
+
96
99
cmd = "git branch"
97
100
_ , output = run_cmd_with_output (cmd , exit_on_failure = False )
98
101
if name in output :
@@ -120,16 +123,15 @@ def get_last_cherry_pick_sha(branch):
120
123
cmd = "git checkout " + branch
121
124
run_cmd_with_output (cmd , exit_on_failure = False )
122
125
123
- SHA = None
126
+ sha = None
124
127
get_commit = "git log -n 1"
125
128
_ , output = run_cmd_with_output (get_commit , exit_on_failure = True )
126
129
lines = output .split ('\n ' )
127
130
for line in lines :
128
131
if 'cherry picked from' in line :
129
- SHA = line .split (' ' )[- 1 ]
130
- SHA = SHA [:- 1 ]
131
- return SHA
132
- #for words in output.split('\n') if 'origin' in line and not '->' in line]
132
+ sha = line .split (' ' )[- 1 ]
133
+ return sha [:- 1 ]
134
+ return sha
133
135
134
136
if __name__ == "__main__" :
135
137
@@ -138,7 +140,7 @@ def get_last_cherry_pick_sha(branch):
138
140
parser .add_argument ('-l' , '--log-level' ,
139
141
help = "Level for providing logging output" ,
140
142
default = 'INFO' )
141
- parser .add_argument ('-r' , '--repo-path' ,
143
+ parser .add_argument ('-r' , '--repo-path' ,
142
144
help = "Git Repository to be imported" ,
143
145
default = None ,
144
146
required = True )
@@ -153,13 +155,13 @@ def get_last_cherry_pick_sha(branch):
153
155
logging .basicConfig (level = level )
154
156
rel_log = logging .getLogger ("Importer" )
155
157
156
- if (args .repo_path is None ) or (args .config_file is None ) :
158
+ if (args .repo_path is None ) or (args .config_file is None ):
157
159
rel_log .error ("Repository path and config file required as input. Use \" --help\" for more info." )
158
160
exit (1 )
159
161
160
162
json_file = os .path .abspath (args .config_file )
161
163
if not os .path .isfile (json_file ):
162
- rel_log .error ("%s not found." , args .config_file )
164
+ rel_log .error ("%s not found." , args .config_file )
163
165
exit (1 )
164
166
165
167
repo = os .path .abspath (args .repo_path )
@@ -178,48 +180,48 @@ def get_last_cherry_pick_sha(branch):
178
180
179
181
# Read configuration data
180
182
with open (json_file , 'r' ) as config :
181
- json_data = json .load (config )
183
+ json_data = json .load (config )
182
184
183
185
'''
184
186
Check if branch exists already, in case branch is present
185
187
we will skip all file transfer and merge operations and will
186
188
jump to cherry-pick
187
189
'''
188
- if check_branch (branch ):
190
+ if branch_exists (branch ):
189
191
rel_log .info ("Branch present = %s" , branch )
190
192
else :
191
193
data_files = json_data ["files" ]
192
194
data_folders = json_data ["folders" ]
193
195
194
196
## Remove all files listed in .json from mbed-os repo to avoid duplications
195
197
for file in data_files :
196
- src_file = file ['src_file' ]
198
+ src_file = file ['src_file' ]
197
199
del_file (os .path .basename (src_file ))
198
200
199
201
for folder in data_folders :
200
- dest_folder = folder ['dest_folder' ]
202
+ dest_folder = folder ['dest_folder' ]
201
203
delete_dir_files (dest_folder )
202
204
rel_log .debug ("Deleted = %s" , folder )
203
205
204
206
rel_log .info ("Removed files/folders listed in json file" )
205
207
206
208
## Copy all the CMSIS files listed in json file to mbed-os
207
209
for file in data_files :
208
- repo_file = os .path .join (repo , file ['src_file' ])
209
- mbed_path = os .path .join (ROOT , file ['dest_file' ])
210
+ repo_file = os .path .join (repo , file ['src_file' ])
211
+ mbed_path = os .path .join (ROOT , file ['dest_file' ])
210
212
mkdir (os .path .dirname (mbed_path ))
211
213
copy_file (repo_file , mbed_path )
212
214
rel_log .debug ("Copied = %s" , mbed_path )
213
215
214
216
for folder in data_folders :
215
- repo_folder = os .path .join (repo , folder ['src_folder' ])
216
- mbed_path = os .path .join (ROOT , folder ['dest_folder' ])
217
+ repo_folder = os .path .join (repo , folder ['src_folder' ])
218
+ mbed_path = os .path .join (ROOT , folder ['dest_folder' ])
217
219
copy_folder (repo_folder , mbed_path )
218
220
rel_log .debug ("Copied = %s" , mbed_path )
219
221
220
222
## Create new branch with all changes
221
223
create_branch = "git checkout -b " + branch
222
- run_cmd_with_output (create_branch , exit_on_failure = False )
224
+ run_cmd_with_output (create_branch , exit_on_failure = True )
223
225
rel_log .info ("Branch created = %s" , branch )
224
226
225
227
add_files = "git add -A"
0 commit comments