gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 line
3.5KB

  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """
  4. Some functions related to the os and os.path module
  5. """
  6. import os
  7. import re
  8. import zipfile
  9. WINDOWS_RESERVED = re.compile(r'''
  10. ^
  11. # forbidden stems: reserved keywords
  12. (:?CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])
  13. # even with an extension this is recommended against
  14. (:?\..*)?
  15. $
  16. ''', flags=re.IGNORECASE | re.VERBOSE)
  17. def clean_filename(name, replacement=''):
  18. """ Strips or replaces possibly problematic or annoying characters our of
  19. the input string, in order to make it a valid filename in most operating
  20. systems (including dropping reserved Windows filenames).
  21. If this results in an empty string, results in "Untitled" (localized).
  22. Allows:
  23. * any alphanumeric character (unicode)
  24. * underscore (_) as that's innocuous
  25. * dot (.) except in leading position to avoid creating dotfiles
  26. * dash (-) except in leading position to avoid annoyance / confusion with
  27. command options
  28. * brackets ([ and ]), while they correspond to shell *character class*
  29. they're a common way to mark / tag files especially on windows
  30. * parenthesis ("(" and ")"), a more natural though less common version of
  31. the former
  32. * space (" ")
  33. :param str name: file name to clean up
  34. :param str replacement:
  35. replacement string to use for sequences of problematic input, by default
  36. an empty string to remove them entirely, each contiguous sequence of
  37. problems is replaced by a single replacement
  38. :rtype: str
  39. """
  40. if WINDOWS_RESERVED.match(name):
  41. return "Untitled"
  42. return re.sub(r'[^\w_.()\[\] -]+', replacement, name).lstrip('.-') or "Untitled"
  43. def zip_dir(path, stream, include_dir=True, fnct_sort=None): # TODO add ignore list
  44. """
  45. : param fnct_sort : Function to be passed to "key" parameter of built-in
  46. python sorted() to provide flexibility of sorting files
  47. inside ZIP archive according to specific requirements.
  48. """
  49. path = os.path.normpath(path)
  50. len_prefix = len(os.path.dirname(path)) if include_dir else len(path)
  51. if len_prefix:
  52. len_prefix += 1
  53. with zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zipf:
  54. for dirpath, dirnames, filenames in os.walk(path):
  55. filenames = sorted(filenames, key=fnct_sort)
  56. for fname in filenames:
  57. bname, ext = os.path.splitext(fname)
  58. ext = ext or bname
  59. if ext not in ['.pyc', '.pyo', '.swp', '.DS_Store']:
  60. path = os.path.normpath(os.path.join(dirpath, fname))
  61. if os.path.isfile(path):
  62. zipf.write(path, path[len_prefix:])
  63. if os.name != 'nt':
  64. is_running_as_nt_service = lambda: False
  65. else:
  66. import win32service as ws
  67. import win32serviceutil as wsu
  68. from contextlib import contextmanager
  69. from odoo.release import nt_service_name
  70. def is_running_as_nt_service():
  71. @contextmanager
  72. def close_srv(srv):
  73. try:
  74. yield srv
  75. finally:
  76. ws.CloseServiceHandle(srv)
  77. try:
  78. with close_srv(ws.OpenSCManager(None, None, ws.SC_MANAGER_ALL_ACCESS)) as hscm:
  79. with close_srv(wsu.SmartOpenService(hscm, nt_service_name, ws.SERVICE_ALL_ACCESS)) as hs:
  80. info = ws.QueryServiceStatusEx(hs)
  81. return info['ProcessId'] == os.getppid()
  82. except Exception:
  83. return False
上海开阖软件有限公司 沪ICP备12045867号-1