-
Notifications
You must be signed in to change notification settings - Fork 3
@W-22895723 - exclude local modules from scan command #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
diksha-sf
wants to merge
3
commits into
salesforcecli:main
Choose a base branch
from
diksha-sf:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,238 +67,6 @@ const SDK_CONFIG_FILE = 'sdk_config.json'; | |
|
|
||
| const DATA_ACCESS_METHODS = new Set(['read_dlo', 'read_dmo', 'write_to_dlo', 'write_to_dmo']); | ||
|
|
||
| /** | ||
| * Python stdlib module names to exclude from requirements.txt. | ||
| * | ||
| * Mirrors `sys.stdlib_module_names` from the Python 3.11 runtimes the SDK supports. | ||
| * Using the stdlib names of this version; if a package becomes stdlib in a | ||
| * later version we still want to drop it (it's not on PyPI and shipping it as a requirement | ||
| * would break installs on the runtime). | ||
| */ | ||
| const PYTHON_STDLIB_MODULES = new Set<string>([ | ||
| '__future__', | ||
| '_ast', | ||
| '_thread', | ||
| 'abc', | ||
| 'aifc', | ||
| 'antigravity', | ||
| 'argparse', | ||
| 'array', | ||
| 'ast', | ||
| 'asynchat', | ||
| 'asyncio', | ||
| 'asyncore', | ||
| 'atexit', | ||
| 'audioop', | ||
| 'base64', | ||
| 'bdb', | ||
| 'binascii', | ||
| 'binhex', | ||
| 'bisect', | ||
| 'builtins', | ||
| 'bz2', | ||
| 'cProfile', | ||
| 'calendar', | ||
| 'cgi', | ||
| 'cgitb', | ||
| 'chunk', | ||
| 'cmath', | ||
| 'cmd', | ||
| 'code', | ||
| 'codecs', | ||
| 'codeop', | ||
| 'collections', | ||
| 'colorsys', | ||
| 'compileall', | ||
| 'concurrent', | ||
| 'configparser', | ||
| 'contextlib', | ||
| 'contextvars', | ||
| 'copy', | ||
| 'copyreg', | ||
| 'crypt', | ||
| 'csv', | ||
| 'ctypes', | ||
| 'curses', | ||
| 'dataclasses', | ||
| 'datetime', | ||
| 'dbm', | ||
| 'decimal', | ||
| 'difflib', | ||
| 'dis', | ||
| 'distutils', | ||
| 'doctest', | ||
| 'email', | ||
| 'encodings', | ||
| 'ensurepip', | ||
| 'enum', | ||
| 'errno', | ||
| 'faulthandler', | ||
| 'fcntl', | ||
| 'filecmp', | ||
| 'fileinput', | ||
| 'fnmatch', | ||
| 'fractions', | ||
| 'ftplib', | ||
| 'functools', | ||
| 'gc', | ||
| 'genericpath', | ||
| 'getopt', | ||
| 'getpass', | ||
| 'gettext', | ||
| 'glob', | ||
| 'graphlib', | ||
| 'grp', | ||
| 'gzip', | ||
| 'hashlib', | ||
| 'heapq', | ||
| 'hmac', | ||
| 'html', | ||
| 'http', | ||
| 'idlelib', | ||
| 'imaplib', | ||
| 'imghdr', | ||
| 'imp', | ||
| 'importlib', | ||
| 'inspect', | ||
| 'io', | ||
| 'ipaddress', | ||
| 'itertools', | ||
| 'json', | ||
| 'keyword', | ||
| 'lib2to3', | ||
| 'linecache', | ||
| 'locale', | ||
| 'logging', | ||
| 'lzma', | ||
| 'mailbox', | ||
| 'mailcap', | ||
| 'marshal', | ||
| 'math', | ||
| 'mimetypes', | ||
| 'mmap', | ||
| 'modulefinder', | ||
| 'msilib', | ||
| 'msvcrt', | ||
| 'multiprocessing', | ||
| 'netrc', | ||
| 'nis', | ||
| 'nntplib', | ||
| 'nt', | ||
| 'ntpath', | ||
| 'nturl2path', | ||
| 'numbers', | ||
| 'opcode', | ||
| 'operator', | ||
| 'optparse', | ||
| 'os', | ||
| 'ossaudiodev', | ||
| 'pathlib', | ||
| 'pdb', | ||
| 'pickle', | ||
| 'pickletools', | ||
| 'pipes', | ||
| 'pkgutil', | ||
| 'platform', | ||
| 'plistlib', | ||
| 'poplib', | ||
| 'posix', | ||
| 'posixpath', | ||
| 'pprint', | ||
| 'profile', | ||
| 'pstats', | ||
| 'pty', | ||
| 'pwd', | ||
| 'py_compile', | ||
| 'pyclbr', | ||
| 'pydoc', | ||
| 'pydoc_data', | ||
| 'pyexpat', | ||
| 'queue', | ||
| 'quopri', | ||
| 'random', | ||
| 're', | ||
| 'readline', | ||
| 'reprlib', | ||
| 'resource', | ||
| 'rlcompleter', | ||
| 'runpy', | ||
| 'sched', | ||
| 'secrets', | ||
| 'select', | ||
| 'selectors', | ||
| 'shelve', | ||
| 'shlex', | ||
| 'shutil', | ||
| 'signal', | ||
| 'site', | ||
| 'smtpd', | ||
| 'smtplib', | ||
| 'sndhdr', | ||
| 'socket', | ||
| 'socketserver', | ||
| 'spwd', | ||
| 'sqlite3', | ||
| 'sre_compile', | ||
| 'sre_constants', | ||
| 'sre_parse', | ||
| 'ssl', | ||
| 'stat', | ||
| 'statistics', | ||
| 'string', | ||
| 'stringprep', | ||
| 'struct', | ||
| 'subprocess', | ||
| 'sunau', | ||
| 'symtable', | ||
| 'sys', | ||
| 'sysconfig', | ||
| 'syslog', | ||
| 'tabnanny', | ||
| 'tarfile', | ||
| 'telnetlib', | ||
| 'tempfile', | ||
| 'termios', | ||
| 'textwrap', | ||
| 'this', | ||
| 'threading', | ||
| 'time', | ||
| 'timeit', | ||
| 'tkinter', | ||
| 'token', | ||
| 'tokenize', | ||
| 'tomllib', | ||
| 'trace', | ||
| 'traceback', | ||
| 'tracemalloc', | ||
| 'tty', | ||
| 'turtle', | ||
| 'turtledemo', | ||
| 'types', | ||
| 'typing', | ||
| 'unicodedata', | ||
| 'unittest', | ||
| 'urllib', | ||
| 'uu', | ||
| 'uuid', | ||
| 'venv', | ||
| 'warnings', | ||
| 'wave', | ||
| 'weakref', | ||
| 'webbrowser', | ||
| 'winreg', | ||
| 'winsound', | ||
| 'wsgiref', | ||
| 'xdrlib', | ||
| 'xml', | ||
| 'xmlrpc', | ||
| 'zipapp', | ||
| 'zipfile', | ||
| 'zipimport', | ||
| 'zlib', | ||
| 'zoneinfo', | ||
| ]); | ||
|
|
||
| const EXCLUDED_PACKAGES = new Set<string>(['datacustomcode', 'pyspark']); | ||
|
|
||
| /** Mirror of `datacustomcode/scan.py:get_sdk_config_path`. */ | ||
|
|
@@ -552,52 +320,40 @@ function validateAccessLayer(calls: DataAccessLayerCalls): void { | |
| /** | ||
| * Mirror of `datacustomcode/scan.py:ImportVisitor.scan_file_for_imports`. | ||
| * | ||
| * Extracts top-level import names from `import X[, Y]` and `from X import …` statements, | ||
| * dropping stdlib modules, packages starting with `_`, and the SDK's own packages. | ||
| * Skips relative imports (`from . import x`) since they aren't PyPI dependencies. | ||
| * This function uses pipreqs to scan Python files for external package dependencies. | ||
| * - Scans all Python files in the directory | ||
| * - Extracts import statements | ||
| * - Filters out stdlib modules | ||
| * - Filters out local modules | ||
| */ | ||
| export async function scanFileForImports(filePath: string): Promise<Set<string>> { | ||
| const code = await fs.readFile(filePath, 'utf8'); | ||
| const imports = new Set<string>(); | ||
| const fileDir = path.dirname(filePath); | ||
|
|
||
| // Strip docstrings/triple-quoted blocks and `#`-style comments to avoid false positives. | ||
| const stripped = code | ||
| .replace(/"""[\s\S]*?"""/g, '') | ||
| .replace(/'''[\s\S]*?'''/g, '') | ||
| .replace(/(^|\s)#.*$/gm, '$1'); | ||
| // scan the directory containing the entrypoint.py file | ||
| const { execFile } = await import('node:child_process'); | ||
| const { promisify } = await import('node:util'); | ||
| const execFileAsync = promisify(execFile); | ||
|
|
||
| // `import a, b.c as alias` | ||
| const importRegex = /^[ \t]*import[ \t]+([^\n#]+)/gm; | ||
| let m: RegExpExecArray | null; | ||
| while ((m = importRegex.exec(stripped)) !== null) { | ||
| for (const piece of m[1].split(',')) { | ||
| const name = piece | ||
| .trim() | ||
| .split(/\s+as\s+/)[0] | ||
| .trim(); | ||
| if (!name) continue; | ||
| addTopLevelImport(name, imports); | ||
| try { | ||
| const { stdout } = await execFileAsync('pipreqs', ['--print', '--mode', 'no-pin', fileDir]); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running the python command here as a child process. |
||
|
|
||
| const packages = new Set<string>(); | ||
| for (const line of stdout.split('\n')) { | ||
| const pkg = line.trim().toLowerCase(); | ||
| if (pkg && !EXCLUDED_PACKAGES.has(pkg)) { | ||
| packages.add(pkg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // `from a.b import c[, d]`. Skip relative imports (`from . import …`). | ||
| const fromRegex = /^[ \t]*from[ \t]+([^\s]+)[ \t]+import[ \t]/gm; | ||
| while ((m = fromRegex.exec(stripped)) !== null) { | ||
| const moduleName = m[1].trim(); | ||
| if (moduleName.startsWith('.')) continue; | ||
| addTopLevelImport(moduleName, imports); | ||
| return packages; | ||
| } catch (error) { | ||
| const err = error as { message: string; stderr?: string; stdout?: string }; | ||
| const details = err.stderr ?? err.stdout ?? err.message; | ||
| throw new SfError( | ||
| `Failed to scan imports using pipreqs: ${details}. Hint: ensure 'pipreqs' is installed in the Python environment.`, | ||
| 'PipreqsScanError' | ||
| ); | ||
| } | ||
|
|
||
| return imports; | ||
| } | ||
|
|
||
| function addTopLevelImport(qualified: string, into: Set<string>): void { | ||
| const top = qualified.split('.')[0]; | ||
| if (!top) return; | ||
| if (top.startsWith('_')) return; | ||
| if (PYTHON_STDLIB_MODULES.has(top)) return; | ||
| if (EXCLUDED_PACKAGES.has(top)) return; | ||
| into.add(top); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to maintain stdlib modules list now as pipreqs lib scan takes care of this.