asfpy 0.55__tar.gz → 0.56__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: asfpy
3
- Version: 0.55
3
+ Version: 0.56
4
4
  Summary: ASF Common Python Methods
5
5
  Author: ASF Infrastructure
6
6
  Author-email: users@infra.apache.org
@@ -18,6 +18,7 @@ Requires-Dist: cffi (>=1.17.1,<2.0.0)
18
18
  Requires-Dist: cryptography (>=44.0.2,<45.0.0)
19
19
  Requires-Dist: easydict (>=1.13,<2.0)
20
20
  Requires-Dist: ezt (>=1.1,<2.0)
21
+ Requires-Dist: passlib (>=1.7.4,<2.0.0) ; extra == "ldap" or extra == "aioldap"
21
22
  Requires-Dist: python-ldap (>=3.4.4,<4.0.0) ; extra == "ldap"
22
23
  Requires-Dist: pyyaml (>=6.0.2,<7.0.0)
23
24
  Requires-Dist: requests (>=2.32.3,<3.0.0)
@@ -34,6 +35,25 @@ This Python library contains features commonly used at the Apache Software Found
34
35
 
35
36
  (For asfpy 0.37 and below, look at our old [Subversion repository](https://svn.apache.org/repos/infra/infrastructure/trunk/projects/asfpy/))
36
37
 
38
+ ## Package Documention
39
+ - `aioldap`: asynchronous LDAP client (_documentation TBD_)
40
+ - `clitools`: LDAP work via command line tooling (_documentation TBD_)
41
+ - `crypto`: helper for ED25519 work (_documentation TBD_)
42
+ - `db`: high performance simplified SQLite client (_documentation TBD_)
43
+ - `justone`: helper to ensure only one long-running process is operating (_documentation TBD_)
44
+ - `messaging`: helpers to send email (_documentation TBD_)
45
+ - `pubsub`: client for subscribing to the ASF pubsub service (_documentation TBD_)
46
+ - `sqlite`: document-based CRUD using SQLite (_documentation TBD_)
47
+ - `stopwatch`: debug/logging timing for Python code. See [documentation](stopwatch.md)
48
+ - `syslog`: redirect `print()` to syslog (_documentation TBD_)
49
+ - `twatcher`: watch EZT emplates for edits, then reload (_documentation TBD_)
50
+ - `whoami`: fetch hostname of box (_documentation TBD_)
51
+
52
+ --
53
+ - `daemon`: **DEPRECATED** old code to spawn a daemon (obsoleted by pipservice)
54
+ - `ldapadmin`: **DEPRECATED** internal ASF infra tooling (moved to internal infra)
55
+
56
+ ----
37
57
 
38
58
  ## Building asfpy package
39
59
 
@@ -8,6 +8,25 @@ This Python library contains features commonly used at the Apache Software Found
8
8
 
9
9
  (For asfpy 0.37 and below, look at our old [Subversion repository](https://svn.apache.org/repos/infra/infrastructure/trunk/projects/asfpy/))
10
10
 
11
+ ## Package Documention
12
+ - `aioldap`: asynchronous LDAP client (_documentation TBD_)
13
+ - `clitools`: LDAP work via command line tooling (_documentation TBD_)
14
+ - `crypto`: helper for ED25519 work (_documentation TBD_)
15
+ - `db`: high performance simplified SQLite client (_documentation TBD_)
16
+ - `justone`: helper to ensure only one long-running process is operating (_documentation TBD_)
17
+ - `messaging`: helpers to send email (_documentation TBD_)
18
+ - `pubsub`: client for subscribing to the ASF pubsub service (_documentation TBD_)
19
+ - `sqlite`: document-based CRUD using SQLite (_documentation TBD_)
20
+ - `stopwatch`: debug/logging timing for Python code. See [documentation](stopwatch.md)
21
+ - `syslog`: redirect `print()` to syslog (_documentation TBD_)
22
+ - `twatcher`: watch EZT emplates for edits, then reload (_documentation TBD_)
23
+ - `whoami`: fetch hostname of box (_documentation TBD_)
24
+
25
+ --
26
+ - `daemon`: **DEPRECATED** old code to spawn a daemon (obsoleted by pipservice)
27
+ - `ldapadmin`: **DEPRECATED** internal ASF infra tooling (moved to internal infra)
28
+
29
+ ----
11
30
 
12
31
  ## Building asfpy package
13
32
 
@@ -49,14 +49,16 @@ class DB:
49
49
  purpose, instead of mixing them throughout a codebase.
50
50
  """
51
51
 
52
- def __init__(self, fname, yaml_fname=None, yaml_section='queries'):
52
+ def __init__(self, fname, /,
53
+ yaml_fname=None, yaml_section='queries',
54
+ isolation_level=None):
53
55
 
54
56
  def row_factory(cursor, row):
55
57
  "Return an EasyDict of the row, for attribute access."
56
58
  return easydict.EasyDict(sqlite3.Row(cursor, row))
57
59
 
58
- # Note: isolation_level=None means autocommit mode.
59
- self.conn = sqlite3.connect(fname, isolation_level=None)
60
+ # Note: default isolation_level=None means autocommit mode.
61
+ self.conn = sqlite3.connect(fname, isolation_level=isolation_level)
60
62
  self.conn.row_factory = row_factory
61
63
 
62
64
  # If a YAML file containing SQL queries is presented, then create
@@ -26,8 +26,8 @@ assert sys.version_info >= (3, 2)
26
26
  import ldap
27
27
  import ldap.modlist
28
28
  import re
29
- import crypt
30
- import random
29
+ import passlib.hash
30
+ import secrets
31
31
  import string
32
32
 
33
33
  LDAP_SANDBOX = "ldaps://ldap-sandbox.apache.org:636"
@@ -139,7 +139,7 @@ class committer:
139
139
  self.manager.lc.modify_s(dn, [(ldap.MOD_ADD, 'memberUid', self.uid.encode('ascii'))])
140
140
 
141
141
  def add_role(self, role):
142
- """ Add person to basic posixGroup entry """
142
+ """ Add person to role group """
143
143
  dn = LDAP_CN % (role, LDAP_ROLES_BASE)
144
144
  self.manager.lc.modify_s(dn, [(ldap.MOD_ADD, 'member', self.dn_enc)])
145
145
 
@@ -159,7 +159,7 @@ class committer:
159
159
  self.manager.lc.modify_s(dn, [(ldap.MOD_DELETE, 'memberUid', self.uid.encode('ascii'))])
160
160
 
161
161
  def remove_role(self, role):
162
- """ Add person to basic posixGroup entry """
162
+ """ Remove person from role group """
163
163
  dn = LDAP_CN % (role, LDAP_ROLES_BASE)
164
164
  self.manager.lc.modify_s(dn, [(ldap.MOD_DELETE, 'member', self.dn_enc)])
165
165
 
@@ -360,10 +360,12 @@ class manager:
360
360
  raise ValidatorException("Invalid email address supplied!", 'email')
361
361
 
362
362
  # Set password, b64-encoded crypt of random string
363
- password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(16))
363
+ salt_alphabet = string.ascii_letters = string.digits
364
+ salt = ''.join(secrets.choice(salt_alphabet) for _ in range(6))
365
+ password = ''.join(secrets.choice(salt_alphabet) for _ in range(16))
364
366
  if forcePass:
365
367
  password = forcePass
366
- password_crypted = crypt.crypt(password, crypt.mksalt(method=crypt.METHOD_MD5))
368
+ password_crypted = passlib.hash.md5_crypt.using(salt=salt).hash(password)
367
369
 
368
370
  ldiff = {
369
371
  'objectClass': ['person', 'top', 'posixAccount', 'organizationalPerson', 'inetOrgPerson', 'asf-committer', 'hostObject', 'ldapPublicKey'],
@@ -0,0 +1,57 @@
1
+ import time
2
+ import logging
3
+ import inspect
4
+ import asyncio
5
+ import functools
6
+
7
+ class Stopwatch:
8
+ def __init__(self, name=None):
9
+ self.name = name
10
+ # Initialize logger in constructor; default to root logger
11
+ frame = inspect.currentframe().f_back
12
+ module = inspect.getmodule(frame)
13
+ self.logger = getattr(module, '_LOGGER', logging.getLogger())
14
+ self.start_time = time.time() # set this, at exit time from function
15
+
16
+ def format(self, duration_ms=None):
17
+ duration_ms = duration_ms or self.elapsed_ms()
18
+ return f"{self.name or 'Operation'} took {duration_ms:.2f} ms"
19
+
20
+ def elapsed_ms(self):
21
+ return (time.time() - self.start_time) * 1000
22
+
23
+ def reset(self):
24
+ self.start_time = time.time()
25
+
26
+ def __call__(self, func):
27
+ if self.name is None:
28
+ self.name = f"Function '{func.__name__}'"
29
+ # Check if the function is async
30
+ if asyncio.iscoroutinefunction(func):
31
+ @functools.wraps(func)
32
+ async def async_wrapper(*args, **kwargs):
33
+ self.start_time = time.time() # Reset start_time at function call
34
+ try:
35
+ return await func(*args, **kwargs)
36
+ finally:
37
+ self.logger.info(self.format())
38
+ return async_wrapper
39
+ else:
40
+ @functools.wraps(func)
41
+ def sync_wrapper(*args, **kwargs):
42
+ self.start_time = time.time() # Reset start_time at function call
43
+ try:
44
+ return func(*args, **kwargs)
45
+ finally:
46
+ self.logger.info(self.format())
47
+ return sync_wrapper
48
+
49
+ def __enter__(self):
50
+ # Update name for context manager if not set
51
+ if self.name is None:
52
+ frame = inspect.currentframe().f_back
53
+ self.name = f"Block in {frame.f_code.co_name} at line {frame.f_lineno}"
54
+ return self
55
+
56
+ def __exit__(self, exc_type, exc_val, exc_tb):
57
+ self.logger.info(self.format())
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "asfpy"
3
- version = "0.55"
3
+ version = "0.56"
4
4
  description='ASF Common Python Methods'
5
5
  authors = ["ASF Infrastructure <users@infra.apache.org>"]
6
6
  readme = "README.md"
@@ -21,6 +21,7 @@ pyyaml = "^6.0.2"
21
21
  cryptography = "^44.0.2"
22
22
  cffi = "^1.17.1"
23
23
  watchfiles = "^1.0.0"
24
+ passlib = { version = "^1.7.4", optional = true }
24
25
 
25
26
  [build-system]
26
27
  requires = ["poetry-core"]
@@ -31,8 +32,8 @@ pytest = "*"
31
32
  pylint = "*"
32
33
 
33
34
  [tool.poetry.extras]
34
- ldap = ["python-ldap"]
35
- aioldap = ["bonsai"]
35
+ ldap = ["python-ldap", "passlib"]
36
+ aioldap = ["bonsai", "passlib"]
36
37
 
37
38
  [tool.pytest.ini_options]
38
39
  testpaths = ["test"]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes