Skip to content

Commit 51430cd

Browse files
author
Deepika
committed
Updated code as per Python style guide
https://www.python.org/dev/peps/pep-0008/
1 parent 43251e1 commit 51430cd

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

tools/importer/importer.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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
26
import argparse
3-
from os.path import dirname, abspath, basename, join
7+
from os.path import dirname, abspath, join
48

59
# Be sure that the tools directory is in the search path
610
ROOT = abspath(join(dirname(__file__), "../.."))
@@ -21,7 +25,7 @@ def del_file(name):
2125
result.append(os.path.join(root, name))
2226
for file in result:
2327
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))
2529

2630
def copy_folder(src, dest):
2731
""" Copy contents of folder in mbed-os listed path
@@ -75,24 +79,23 @@ def get_curr_sha(repo_path):
7579
sha - last commit SHA
7680
"""
7781
cwd = os.getcwd()
78-
sha = None
7982
os.chdir(abspath(repo_path))
8083

8184
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)
8386

8487
os.chdir(cwd)
8588
return sha
8689

87-
def check_branch(name):
90+
def branch_exists(name):
8891
""" Check if branch already exists in mbed-os local repository.
8992
It will not verify if branch is present in remote repository.
9093
Args:
9194
name - branch name
9295
Returns:
9396
True - If branch is already present
9497
"""
95-
branches = []
98+
9699
cmd = "git branch"
97100
_, output = run_cmd_with_output(cmd, exit_on_failure=False)
98101
if name in output:
@@ -120,16 +123,15 @@ def get_last_cherry_pick_sha(branch):
120123
cmd = "git checkout " + branch
121124
run_cmd_with_output(cmd, exit_on_failure=False)
122125

123-
SHA = None
126+
sha = None
124127
get_commit = "git log -n 1"
125128
_, output = run_cmd_with_output(get_commit, exit_on_failure=True)
126129
lines = output.split('\n')
127130
for line in lines:
128131
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
133135

134136
if __name__ == "__main__":
135137

@@ -138,7 +140,7 @@ def get_last_cherry_pick_sha(branch):
138140
parser.add_argument('-l', '--log-level',
139141
help="Level for providing logging output",
140142
default='INFO')
141-
parser.add_argument('-r', '--repo-path',
143+
parser.add_argument('-r', '--repo-path',
142144
help="Git Repository to be imported",
143145
default=None,
144146
required=True)
@@ -153,13 +155,13 @@ def get_last_cherry_pick_sha(branch):
153155
logging.basicConfig(level=level)
154156
rel_log = logging.getLogger("Importer")
155157

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):
157159
rel_log.error("Repository path and config file required as input. Use \"--help\" for more info.")
158160
exit(1)
159161

160162
json_file = os.path.abspath(args.config_file)
161163
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)
163165
exit(1)
164166

165167
repo = os.path.abspath(args.repo_path)
@@ -178,48 +180,48 @@ def get_last_cherry_pick_sha(branch):
178180

179181
# Read configuration data
180182
with open(json_file, 'r') as config:
181-
json_data = json.load(config)
183+
json_data = json.load(config)
182184

183185
'''
184186
Check if branch exists already, in case branch is present
185187
we will skip all file transfer and merge operations and will
186188
jump to cherry-pick
187189
'''
188-
if check_branch(branch):
190+
if branch_exists(branch):
189191
rel_log.info("Branch present = %s", branch)
190192
else:
191193
data_files = json_data["files"]
192194
data_folders = json_data["folders"]
193195

194196
## Remove all files listed in .json from mbed-os repo to avoid duplications
195197
for file in data_files:
196-
src_file = file['src_file']
198+
src_file = file['src_file']
197199
del_file(os.path.basename(src_file))
198200

199201
for folder in data_folders:
200-
dest_folder = folder['dest_folder']
202+
dest_folder = folder['dest_folder']
201203
delete_dir_files(dest_folder)
202204
rel_log.debug("Deleted = %s", folder)
203205

204206
rel_log.info("Removed files/folders listed in json file")
205207

206208
## Copy all the CMSIS files listed in json file to mbed-os
207209
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'])
210212
mkdir(os.path.dirname(mbed_path))
211213
copy_file(repo_file, mbed_path)
212214
rel_log.debug("Copied = %s", mbed_path)
213215

214216
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'])
217219
copy_folder(repo_folder, mbed_path)
218220
rel_log.debug("Copied = %s", mbed_path)
219221

220222
## Create new branch with all changes
221223
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)
223225
rel_log.info("Branch created = %s", branch)
224226

225227
add_files = "git add -A"

0 commit comments

Comments
 (0)