|
| 1 | +""" |
| 2 | +MIT License |
| 3 | +Copyright (c) 2014 Scott Kuroda <scott.kuroda@gmail.com> |
| 4 | +
|
| 5 | +SHA: 623a4c1ec46dbbf3268bd88131bf0dfc845af787 |
| 6 | +""" |
| 7 | +import sublime |
| 8 | +import os |
| 9 | +import zipfile |
| 10 | +import tempfile |
| 11 | +import re |
| 12 | +import codecs |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + "get_resource", |
| 16 | + "get_binary_resource", |
| 17 | + "find_resource", |
| 18 | + "list_package_files", |
| 19 | + "get_package_and_resource_name", |
| 20 | + "get_packages_list", |
| 21 | + "extract_package", |
| 22 | + "get_sublime_packages" |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +VERSION = int(sublime.version()) |
| 27 | + |
| 28 | +def get_resource(package_name, resource, encoding="utf-8"): |
| 29 | + return _get_resource(package_name, resource, encoding=encoding) |
| 30 | + |
| 31 | +def get_binary_resource(package_name, resource): |
| 32 | + return _get_resource(package_name, resource, return_binary=True) |
| 33 | + |
| 34 | +def _get_resource(package_name, resource, return_binary=False, encoding="utf-8"): |
| 35 | + packages_path = sublime.packages_path() |
| 36 | + content = None |
| 37 | + if VERSION > 3013: |
| 38 | + try: |
| 39 | + if return_binary: |
| 40 | + content = sublime.load_binary_resource("Packages/" + package_name + "/" + resource) |
| 41 | + else: |
| 42 | + content = sublime.load_resource("Packages/" + package_name + "/" + resource) |
| 43 | + except IOError: |
| 44 | + pass |
| 45 | + else: |
| 46 | + path = None |
| 47 | + if os.path.exists(os.path.join(packages_path, package_name, resource)): |
| 48 | + path = os.path.join(packages_path, package_name, resource) |
| 49 | + content = _get_directory_item_content(path, return_binary, encoding) |
| 50 | + |
| 51 | + if VERSION >= 3006: |
| 52 | + sublime_package = package_name + ".sublime-package" |
| 53 | + |
| 54 | + packages_path = sublime.installed_packages_path() |
| 55 | + if content is None: |
| 56 | + if os.path.exists(os.path.join(packages_path, sublime_package)): |
| 57 | + content = _get_zip_item_content(os.path.join(packages_path, sublime_package), resource, return_binary, encoding) |
| 58 | + |
| 59 | + packages_path = os.path.dirname(sublime.executable_path()) + os.sep + "Packages" |
| 60 | + |
| 61 | + if content is None: |
| 62 | + if os.path.exists(os.path.join(packages_path, sublime_package)): |
| 63 | + content = _get_zip_item_content(os.path.join(packages_path, sublime_package), resource, return_binary, encoding) |
| 64 | + |
| 65 | + return content |
| 66 | + |
| 67 | + |
| 68 | +def find_resource(resource_pattern, package=None): |
| 69 | + file_set = set() |
| 70 | + if package == None: |
| 71 | + for package in get_packages_list(): |
| 72 | + file_set.update(find_resource(resource_pattern, package)) |
| 73 | + |
| 74 | + ret_list = list(file_set) |
| 75 | + else: |
| 76 | + file_set.update(_find_directory_resource(os.path.join(sublime.packages_path(), package), resource_pattern)) |
| 77 | + |
| 78 | + if VERSION >= 3006: |
| 79 | + zip_location = os.path.join(sublime.installed_packages_path(), package + ".sublime-package") |
| 80 | + file_set.update(_find_zip_resource(zip_location, resource_pattern)) |
| 81 | + zip_location = os.path.join(os.path.dirname(sublime.executable_path()), "Packages", package + ".sublime-package") |
| 82 | + file_set.update(_find_zip_resource(zip_location, resource_pattern)) |
| 83 | + ret_list = map(lambda e: package + "/" + e, file_set) |
| 84 | + |
| 85 | + return sorted(ret_list) |
| 86 | + |
| 87 | + |
| 88 | +def list_package_files(package, ignore_patterns=[]): |
| 89 | + """ |
| 90 | + List files in the specified package. |
| 91 | + """ |
| 92 | + package_path = os.path.join(sublime.packages_path(), package, "") |
| 93 | + path = None |
| 94 | + file_set = set() |
| 95 | + file_list = [] |
| 96 | + if os.path.exists(package_path): |
| 97 | + for root, directories, filenames in os.walk(package_path): |
| 98 | + temp = root.replace(package_path, "") |
| 99 | + for filename in filenames: |
| 100 | + file_list.append(os.path.join(temp, filename)) |
| 101 | + |
| 102 | + file_set.update(file_list) |
| 103 | + |
| 104 | + if VERSION >= 3006: |
| 105 | + sublime_package = package + ".sublime-package" |
| 106 | + packages_path = sublime.installed_packages_path() |
| 107 | + |
| 108 | + if os.path.exists(os.path.join(packages_path, sublime_package)): |
| 109 | + file_set.update(_list_files_in_zip(packages_path, sublime_package)) |
| 110 | + |
| 111 | + packages_path = os.path.dirname(sublime.executable_path()) + os.sep + "Packages" |
| 112 | + |
| 113 | + if os.path.exists(os.path.join(packages_path, sublime_package)): |
| 114 | + file_set.update(_list_files_in_zip(packages_path, sublime_package)) |
| 115 | + |
| 116 | + file_list = [] |
| 117 | + |
| 118 | + for filename in file_set: |
| 119 | + if not _ignore_file(filename, ignore_patterns): |
| 120 | + file_list.append(_normalize_to_sublime_path(filename)) |
| 121 | + |
| 122 | + return sorted(file_list) |
| 123 | + |
| 124 | +def _ignore_file(filename, ignore_patterns=[]): |
| 125 | + ignore = False |
| 126 | + directory, base = os.path.split(filename) |
| 127 | + for pattern in ignore_patterns: |
| 128 | + if re.match(pattern, base): |
| 129 | + return True |
| 130 | + |
| 131 | + if len(directory) > 0: |
| 132 | + ignore = _ignore_file(directory, ignore_patterns) |
| 133 | + |
| 134 | + return ignore |
| 135 | + |
| 136 | + |
| 137 | +def _normalize_to_sublime_path(path): |
| 138 | + path = os.path.normpath(path) |
| 139 | + path = re.sub(r"^([a-zA-Z]):", "/\\1", path) |
| 140 | + path = re.sub(r"\\", "/", path) |
| 141 | + return path |
| 142 | + |
| 143 | +def get_package_and_resource_name(path): |
| 144 | + """ |
| 145 | + This method will return the package name and resource name from a path. |
| 146 | +
|
| 147 | + Arguments: |
| 148 | + path Path to parse for package and resource name. |
| 149 | + """ |
| 150 | + package = None |
| 151 | + resource = None |
| 152 | + path = _normalize_to_sublime_path(path) |
| 153 | + if os.path.isabs(path): |
| 154 | + packages_path = _normalize_to_sublime_path(sublime.packages_path()) |
| 155 | + if path.startswith(packages_path): |
| 156 | + package, resource = _search_for_package_and_resource(path, packages_path) |
| 157 | + |
| 158 | + if int(sublime.version()) >= 3006: |
| 159 | + packages_path = _normalize_to_sublime_path(sublime.installed_packages_path()) |
| 160 | + if path.startswith(packages_path): |
| 161 | + package, resource = _search_for_package_and_resource(path, packages_path) |
| 162 | + |
| 163 | + packages_path = _normalize_to_sublime_path(os.path.dirname(sublime.executable_path()) + os.sep + "Packages") |
| 164 | + if path.startswith(packages_path): |
| 165 | + package, resource = _search_for_package_and_resource(path, packages_path) |
| 166 | + else: |
| 167 | + path = re.sub(r"^Packages/", "", path) |
| 168 | + split = re.split(r"/", path, 1) |
| 169 | + package = split[0] |
| 170 | + package = package.replace(".sublime-package", "") |
| 171 | + resource = split[1] |
| 172 | + |
| 173 | + return (package, resource) |
| 174 | + |
| 175 | +def get_packages_list(ignore_packages=True, ignore_patterns=[]): |
| 176 | + """ |
| 177 | + Return a list of packages. |
| 178 | + """ |
| 179 | + package_set = set() |
| 180 | + package_set.update(_get_packages_from_directory(sublime.packages_path())) |
| 181 | + |
| 182 | + if int(sublime.version()) >= 3006: |
| 183 | + package_set.update(_get_packages_from_directory(sublime.installed_packages_path(), ".sublime-package")) |
| 184 | + |
| 185 | + executable_package_path = os.path.dirname(sublime.executable_path()) + os.sep + "Packages" |
| 186 | + package_set.update(_get_packages_from_directory(executable_package_path, ".sublime-package")) |
| 187 | + |
| 188 | + |
| 189 | + if ignore_packages: |
| 190 | + ignored_list = sublime.load_settings( |
| 191 | + "Preferences.sublime-settings").get("ignored_packages", []) |
| 192 | + else: |
| 193 | + ignored_list = [] |
| 194 | + |
| 195 | + for package in package_set: |
| 196 | + for pattern in ignore_patterns: |
| 197 | + if re.match(pattern, package): |
| 198 | + ignored_list.append(package) |
| 199 | + break |
| 200 | + |
| 201 | + for ignored in ignored_list: |
| 202 | + package_set.discard(ignored) |
| 203 | + |
| 204 | + return sorted(list(package_set)) |
| 205 | + |
| 206 | +def get_sublime_packages(ignore_packages=True, ignore_patterns=[]): |
| 207 | + package_list = get_packages_list(ignore_packages, ignore_patterns) |
| 208 | + extracted_list = _get_packages_from_directory(sublime.packages_path()) |
| 209 | + return [x for x in package_list if x not in extracted_list] |
| 210 | + |
| 211 | +def _get_packages_from_directory(directory, file_ext=""): |
| 212 | + package_list = [] |
| 213 | + for package in os.listdir(directory): |
| 214 | + if not package.endswith(file_ext): |
| 215 | + continue |
| 216 | + else: |
| 217 | + package = package.replace(file_ext, "") |
| 218 | + |
| 219 | + package_list.append(package) |
| 220 | + return package_list |
| 221 | + |
| 222 | +def _search_for_package_and_resource(path, packages_path): |
| 223 | + """ |
| 224 | + Derive the package and resource from a path. |
| 225 | + """ |
| 226 | + relative_package_path = path.replace(packages_path + "/", "") |
| 227 | + |
| 228 | + package, resource = re.split(r"/", relative_package_path, 1) |
| 229 | + package = package.replace(".sublime-package", "") |
| 230 | + return (package, resource) |
| 231 | + |
| 232 | + |
| 233 | +def _list_files_in_zip(package_path, package): |
| 234 | + if not os.path.exists(os.path.join(package_path, package)): |
| 235 | + return [] |
| 236 | + |
| 237 | + ret_value = [] |
| 238 | + with zipfile.ZipFile(os.path.join(package_path, package)) as zip_file: |
| 239 | + ret_value = zip_file.namelist() |
| 240 | + return ret_value |
| 241 | + |
| 242 | +def _get_zip_item_content(path_to_zip, resource, return_binary, encoding): |
| 243 | + if not os.path.exists(path_to_zip): |
| 244 | + return None |
| 245 | + |
| 246 | + ret_value = None |
| 247 | + |
| 248 | + with zipfile.ZipFile(path_to_zip) as zip_file: |
| 249 | + namelist = zip_file.namelist() |
| 250 | + if resource in namelist: |
| 251 | + ret_value = zip_file.read(resource) |
| 252 | + if not return_binary: |
| 253 | + ret_value = ret_value.decode(encoding) |
| 254 | + |
| 255 | + return ret_value |
| 256 | + |
| 257 | +def _get_directory_item_content(filename, return_binary, encoding): |
| 258 | + content = None |
| 259 | + if os.path.exists(filename): |
| 260 | + if return_binary: |
| 261 | + mode = "rb" |
| 262 | + encoding = None |
| 263 | + else: |
| 264 | + mode = "r" |
| 265 | + with codecs.open(filename, mode, encoding=encoding) as file_obj: |
| 266 | + content = file_obj.read() |
| 267 | + return content |
| 268 | + |
| 269 | +def _find_zip_resource(path_to_zip, pattern): |
| 270 | + ret_list = [] |
| 271 | + if os.path.exists(path_to_zip): |
| 272 | + with zipfile.ZipFile(path_to_zip) as zip_file: |
| 273 | + namelist = zip_file.namelist() |
| 274 | + for name in namelist: |
| 275 | + if re.search(pattern, name): |
| 276 | + ret_list.append(name) |
| 277 | + |
| 278 | + return ret_list |
| 279 | + |
| 280 | +def _find_directory_resource(path, pattern): |
| 281 | + ret_list = [] |
| 282 | + if os.path.exists(path): |
| 283 | + path = os.path.join(path, "") |
| 284 | + for root, directories, filenames in os.walk(path): |
| 285 | + temp = root.replace(path, "") |
| 286 | + for filename in filenames: |
| 287 | + if re.search(pattern, os.path.join(temp, filename)): |
| 288 | + ret_list.append(os.path.join(temp, filename)) |
| 289 | + return ret_list |
| 290 | + |
| 291 | +def extract_zip_resource(path_to_zip, resource, extract_dir=None): |
| 292 | + if extract_dir is None: |
| 293 | + extract_dir = tempfile.mkdtemp() |
| 294 | + |
| 295 | + file_location = None |
| 296 | + if os.path.exists(path_to_zip): |
| 297 | + with zipfile.ZipFile(path_to_zip) as zip_file: |
| 298 | + file_location = zip_file.extract(resource, extract_dir) |
| 299 | + |
| 300 | + return file_location |
| 301 | + |
| 302 | +def extract_package(package): |
| 303 | + if VERSION >= 3006: |
| 304 | + package_location = os.path.join(sublime.installed_packages_path(), package + ".sublime-package") |
| 305 | + if not os.path.exists(package_location): |
| 306 | + package_location = os.path.join(os.path.dirname(sublime.executable_path()), "Packages", package + ".sublime-package") |
| 307 | + if not os.path.exists(package_location): |
| 308 | + package_location = None |
| 309 | + if package_location: |
| 310 | + with zipfile.ZipFile(package_location) as zip_file: |
| 311 | + extract_location = os.path.join(sublime.packages_path(), package) |
| 312 | + zip_file.extractall(extract_location) |
| 313 | + |
0 commit comments