Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ display.display(pd.DataFrame.from_records([["col 1": 3, "col 2": 5], ["col 1": 8
can import them:

```swift
// Specify SwiftPM flags to use during package installation.
%install-swiftpm-flags -c release

// Install the DeckOfPlayingCards package from GitHub.
%install '.package(url: "https://github.com/NSHipster/DeckOfPlayingCards", from: "4.0.0")' DeckOfPlayingCards

Expand All @@ -202,6 +205,8 @@ The next argument(s) to `%install` are the products that you want to install fro
will refuse to install packages, and print out an error message explaining
why, if you try to install packages in later cells.)
* Downloads and build artifacts are not cached.
* `%install-swiftpm-flags` apply to all packages that you are installing; there
is no way to specify different flags for different packages.

## %include directives

Expand Down
30 changes: 24 additions & 6 deletions swift_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,25 @@ def _process_installs(self, code):
"%install" directives removed."""
processed_lines = []
all_packages = []
all_swiftpm_flags = []
for index, line in enumerate(code.split('\n')):
line, swiftpm_flags = self._process_install_swiftpm_flags_line(
line)
all_swiftpm_flags += swiftpm_flags
line, packages = self._process_install_line(index, line)
processed_lines.append(line)
all_packages += packages
self._install_packages(all_packages)
self._install_packages(all_packages, all_swiftpm_flags)
return '\n'.join(processed_lines)

def _process_install_swiftpm_flags_line(self, line):
install_swiftpm_flags_match = re.match(
r'^\s*%install-swiftpm-flags (.*)$', line)
if install_swiftpm_flags_match is None:
return line, []
flags = shlex.split(install_swiftpm_flags_match.group(1))
return '', flags

def _process_install_line(self, line_index, line):
install_match = re.match(r'^\s*%install (.*)$', line)
if install_match is None:
Expand All @@ -414,8 +426,8 @@ def _process_install_line(self, line_index, line):
'products': parsed[1:],
}]

def _install_packages(self, packages):
if len(packages) == 0:
def _install_packages(self, packages, swiftpm_flags):
if len(packages) == 0 and len(swiftpm_flags) == 0:
return

# Appears to trigger even in the first cell execution.
Expand Down Expand Up @@ -487,6 +499,10 @@ def _install_packages(self, packages):
'name': 'stdout',
'text': 'Installing packages:\n%s' % packages_human_description
})
self.send_response(self.iopub_socket, 'stream', {
'name': 'stdout',
'text': 'With SwiftPM flags: %s\n' % str(swiftpm_flags)
})
self.send_response(self.iopub_socket, 'stream', {
'name': 'stdout',
'text': 'Working in: %s\n' % scratchwork_base_path
Expand All @@ -502,7 +518,8 @@ def _install_packages(self, packages):

# == Ask SwiftPM to build the package ==

build_p = subprocess.Popen([swift_build_path], stdout=subprocess.PIPE,
build_p = subprocess.Popen([swift_build_path] + swiftpm_flags,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=package_base_path)
for build_output_line in iter(build_p.stdout.readline, b''):
Expand All @@ -517,8 +534,9 @@ def _install_packages(self, packages):
'%d.' % build_returncode)

show_bin_path_result = subprocess.run(
[swift_build_path, '--show-bin-path'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=package_base_path)
[swift_build_path, '--show-bin-path'] + swiftpm_flags,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=package_base_path)
bin_dir = show_bin_path_result.stdout.decode('utf8').strip()
lib_filename = os.path.join(bin_dir, 'libjupyterInstalledPackages.so')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
int sillyfunction2() {
return 1337;
return MACRO_DEFINED_IN_COMPILER_FLAG;
}
1 change: 1 addition & 0 deletions test/tests/notebooks/install_package_with_c.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"%install-swiftpm-flags -Xcc -DMACRO_DEFINED_IN_COMPILER_FLAG=1337\n",
"%install '.package(path: \"$cwd/PackageWithC\")' PackageWithC"
]
},
Expand Down