1 # Copyright 2016 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from urllib.request import Request, urlopen
21 logger = logging.getLogger(__name__)
25 if not os.path.exists(path):
26 os.makedirs(path, 0o777)
29 def delete_dirs(path):
31 if os.path.exists(path):
33 except Exception as e:
34 logger.error(traceback.format_exc())
35 logger.error("Failed to delete %s:%s", path, e.args[0])
38 def download_file_from_http(url, local_dir, file_name):
39 local_file_name = os.path.join(local_dir, file_name)
40 is_download_ok = False
45 save_file = open(local_file_name, 'wb')
46 save_file.write(req.read())
51 logger.error(traceback.format_exc())
52 logger.error("Failed to download %s to %s.", url, local_file_name)
53 return is_download_ok, local_file_name
56 def read_json_file(file_path):
57 if os.path.exists(file_path):
59 with open(file_path, 'r') as json_file:
60 data = json_file.read()
61 return json.loads(data)
63 logger.error(traceback.format_exc())
64 logger.error("Failed to parse json file %s." % file_path)