Skip to content

Commit

Permalink
add python code mode for -m
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Oct 29, 2022
1 parent 3f5025e commit eb8b617
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
- 2022.08.28
- add `-q` for quiet mode
- `-qqqqq` to mute all the logs
- `-m` support source code with ';' like python -c
- `-m` support source code mode while not matched `module.submodule:function` format
- like `python -c "python code"`
- demo: `python -m zipapps -m "import six; print(six.__file__)"`
- WARN: source code should contains `;`
- WARN: only when `-m` not matched regex `r'^\w+(\.\w+)?(:\w+)?$'`

- 2022.04.27
- handle PermissionError for chmod
Expand Down
12 changes: 9 additions & 3 deletions zipapps/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ def prepare_active_zipapps(self):

def make_runner():
if self.main:
if ';' in self.main:
return self.main
else:
if re.match(r'^\w+(\.\w+)?(:\w+)?$', self.main):
module, _, function = self.main.partition(':')
if module:
# main may be: 'module.py:main' or 'module.submodule:main'
Expand All @@ -337,7 +335,15 @@ def make_runner():
runner = f'import {module}'
if function:
runner += f'; {module}.{function}()'
self._log(
f"[INFO]: -m: matches re.match(r'^\w+(\.\w+)?(:\w+)?$', self.main), add as `{runner}`."
)
return runner
else:
self._log(
f"[INFO]: -m: not matches re.match(r'^\w+(\.\w+)?(:\w+)?$', self.main), add as raw code `{self.main}`."
)
return self.main
return ''

kwargs = {
Expand Down

0 comments on commit eb8b617

Please sign in to comment.