Skip to content

Commit eb8b617

Browse files
author
lidong
committed
add python code mode for -m
1 parent 3f5025e commit eb8b617

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
- 2022.08.28
55
- add `-q` for quiet mode
66
- `-qqqqq` to mute all the logs
7-
- `-m` support source code with ';' like python -c
7+
- `-m` support source code mode while not matched `module.submodule:function` format
8+
- like `python -c "python code"`
89
- demo: `python -m zipapps -m "import six; print(six.__file__)"`
9-
- WARN: source code should contains `;`
10+
- WARN: only when `-m` not matched regex `r'^\w+(\.\w+)?(:\w+)?$'`
1011

1112
- 2022.04.27
1213
- handle PermissionError for chmod

zipapps/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ def prepare_active_zipapps(self):
324324

325325
def make_runner():
326326
if self.main:
327-
if ';' in self.main:
328-
return self.main
329-
else:
327+
if re.match(r'^\w+(\.\w+)?(:\w+)?$', self.main):
330328
module, _, function = self.main.partition(':')
331329
if module:
332330
# main may be: 'module.py:main' or 'module.submodule:main'
@@ -337,7 +335,15 @@ def make_runner():
337335
runner = f'import {module}'
338336
if function:
339337
runner += f'; {module}.{function}()'
338+
self._log(
339+
f"[INFO]: -m: matches re.match(r'^\w+(\.\w+)?(:\w+)?$', self.main), add as `{runner}`."
340+
)
340341
return runner
342+
else:
343+
self._log(
344+
f"[INFO]: -m: not matches re.match(r'^\w+(\.\w+)?(:\w+)?$', self.main), add as raw code `{self.main}`."
345+
)
346+
return self.main
341347
return ''
342348

343349
kwargs = {

0 commit comments

Comments
 (0)