utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import json
  18. import logging
  19. import os
  20. import tempfile
  21. import zipfile
  22. LOGGER = logging.getLogger(__name__)
  23. def format_json(json_struct):
  24. return json.dumps(json_struct, indent=4)
  25. def dump_json(json_struct):
  26. return json.dumps(json_struct)
  27. def load_json(s):
  28. return json.loads(s)
  29. def unzip_to_temp_dir(zip_file_name):
  30. """Unzip zipfile to a temporary directory.
  31. The directory of the unzipped files is returned if success,
  32. otherwise None is returned. """
  33. if not zip_file_name or not os.path.exists(zip_file_name):
  34. return None
  35. zf = zipfile.ZipFile(zip_file_name)
  36. if zf.testzip() is not None:
  37. return None
  38. # Unzip the files into a temporary directory
  39. LOGGER.info("Extracting zipped file: %s" % zip_file_name)
  40. tempdir = tempfile.mkdtemp()
  41. try:
  42. # Create directories that don't exist
  43. for zip_name in zf.namelist():
  44. # We have no knowledge on the os where the zipped file was
  45. # created, so we restrict to zip files with paths without
  46. # charactor "\" and "/".
  47. name = (zip_name.replace("\\", os.path.sep).
  48. replace("/", os.path.sep))
  49. dest = os.path.join(tempdir, name)
  50. if (name.endswith(os.path.sep) and not os.path.exists(dest)):
  51. os.mkdir(dest)
  52. LOGGER.debug("Directory %s created." % dest)
  53. # Copy files
  54. for zip_name in zf.namelist():
  55. # We have no knowledge on the os where the zipped file was
  56. # created, so we restrict to zip files with paths without
  57. # charactor "\" and "/".
  58. name = (zip_name.replace("\\", os.path.sep).
  59. replace("/", os.path.sep))
  60. dest = os.path.join(tempdir, name)
  61. if not (name.endswith(os.path.sep)):
  62. LOGGER.debug("Copying file %s......" % dest)
  63. outfile = open(dest, 'wb')
  64. outfile.write(zf.read(zip_name))
  65. outfile.close()
  66. LOGGER.debug("File %s copied." % dest)
  67. LOGGER.info("Unzipped file can be found at %s" % tempdir)
  68. return tempdir
  69. except IOError as err:
  70. LOGGER.error("Error in extracting webdriver.xpi: %s" % err)
  71. return None