Re: [swift-corelibs-dev] Better integration with UNIX tools

2017-11-17 Thread Brent Royal-Gordon via swift-corelibs-dev
> On Nov 17, 2017, at 11:34 AM, Tony Parker via swift-corelibs-dev 
>  wrote:
> 
> It does seem like there is a possibility of some better convenience API here.
> 
> Any ideas on what form it would take? A class method on Process that returns 
> the output, maybe?

`Process.run(_:arguments:terminationHandler:)` is not a bad basis for this, 
other than the first argument being a URL. I might add a variant which does a 
$PATH search and expands tildes:

extension Process {
class func runInPath(_ commandName: String, with arguments: 
[String], terminationHandler: ((Process) -> Void)? = nil) -> Process
}

(I would *not* add a variant which simply shells out, or at least I wouldn't 
make it the only option. Every scripting language I can think of has this 
feature, and every one of them discourages its use and pushes people towards 
something else with pre-split arguments and no shell attack surface.)

And then add a method which gathers all stdout output and returns it, along 
with the termination status (throwing if it's a signal):

extension Process {
func outputAfterExit() throws -> (output: String, status: Int32)
}

The result is not *as* convenient as PHP, but it's a lot safe than running 
things through a shell, too.

let (output, _) = try Task.runInPath("find", with: ["~/Desktop", 
"-name", "*.png"]).outputAfterExit()

The biggest problem I see with this design is that expanding tildes in the 
arguments, but *not* globbing them, happens to be right for this case, but may 
not be in the general case. One interesting possibility would be to go the 
custom operator route:

/// Returns an absolute path placing `relativePath` in the user's home 
directory.
prefix func ~ (relativePath: String) -> String {
return 
FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(relativePath).path
}

let (output, _) = try Task.runInPath("find", with: [~"Desktop", 
"-name", "*.png"]).outputAfterExit()

But I'm not sure we'd want that available in million-line Mac apps.

On the other hand, maybe Foundation's APIs in general are too verbose for 
scripting. I could imagine a "Foundation.Script" module which added some 
unprincipled but convenient shortcuts:

extension URL: ExpressibleByStringLiteral { // Should be 
interpolatable too, but we need to redesign that.
public init(stringLiteral value: String) {
self.init(fileURLWithPath: value)
}
}
public var FS { return FileManager.default }
public var ENV { return ProcessInfo.processInfo.environment }

That might be a good place for a leading tilde operator, too.

-- 
Brent Royal-Gordon
Architechies

___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


Re: [swift-corelibs-dev] Better integration with UNIX tools

2017-11-17 Thread William Dillon via swift-corelibs-dev
I, to, have wished for such an API.

I think that would be a very welcome addition.

> On Nov 17, 2017, at 11:34 AM, Tony Parker via swift-corelibs-dev 
>  wrote:
> 
> Hi Abhi,
> 
> It does seem like there is a possibility of some better convenience API here.
> 
> Any ideas on what form it would take? A class method on Process that returns 
> the output, maybe?
> 
> - Tony
> 
>> On Nov 16, 2017, at 3:34 PM, Abhi Beckert via swift-corelibs-dev 
>> > wrote:
>> 
>> Swift is a great shell scripting language except for it's lack of any API to 
>> execute UNIX commands. Compare these two shell scripts:
>> 
>>> #!/usr/bin/php
>>> >> 
>>> $files = `find ~/Desktop -name *.png`;
>>> 
>>> foreach (explode("\n", $files) as $file) {
>>>   // do something with $file
>>> }
>> 
>> -
>> 
>>> #!/usr/bin/swift
>>> 
>>> import Foundation
>>> 
>>> let process = Process()
>>> process.launchPath = "/usr/bin/find"
>>> process.arguments = [
>>>   NSString(string:"~/Desktop").expandingTildeInPath,
>>>   "-name",
>>>   "*.png"
>>> ]
>>> 
>>> let output = Pipe()
>>> process.standardOutput = output
>>> 
>>> process.launch()
>>> 
>>> let files: String
>>> if let filesUtf8 = NSString(data: 
>>> output.fileHandleForReading.readDataToEndOfFile(), encoding: 
>>> String.Encoding.utf8.rawValue) {
>>>   files = filesUtf8 as String
>>> } else {
>>>   files = NSString(data: output.fileHandleForReading.readDataToEndOfFile(), 
>>> encoding: String.Encoding.isoLatin1.rawValue) as NSString! as String
>>> }
>>> 
>>> files.enumerateLines { file, _ in
>>>   // do something with file
>>> }
>> 
>> It's a contrived example, I could have used NSFileManager, but I run into 
>> this all the time integrating with more complex tools such as rsync.
>> 
>> Adding my own high level wrapper around the Process command isn't an option 
>> since there is no good way to import code from another file when executing 
>> swift asa shell script. All your code needs to be in one file.
>> 
>> - Abhi
>> ___
>> swift-corelibs-dev mailing list
>> swift-corelibs-dev@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev 
>> 
> ___
> swift-corelibs-dev mailing list
> swift-corelibs-dev@swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


Re: [swift-corelibs-dev] Encountering "Constant strings cannot be deallocated"

2017-11-17 Thread Alex Blewitt via swift-corelibs-dev
Thanks!

Alex

> On 17 Nov 2017, at 17:27, Brandon Williams  wrote:
> 
> I haven’t been able to find a minimal test case, but at least the test case I 
> linked to reproduces every time.
> 
> I’m not sure I’ll have time to search for a minimal case today, and I leave 
> for vacation tomorrow :/ perhaps when I get back.
> 
> I did at least file the bug report: https://bugs.swift.org/browse/SR-6422 
> 
> On Fri, Nov 17, 2017 at 4:48 AM Alex Blewitt  > wrote:
> Can you file a bug at bugs.swift.org  with a minimal 
> test case? It certainly sounds like a bug, and from the looks of the docker 
> files, you're using the swift 4.0.2 release on Ubuntu 16.04.
> 
> If you can run the test under LLDB and put a breakpoint in that function, and 
> get a backtrace (with 'bt') then it might be interesting to see where the 
> deinit is being triggered, and give some kind of clue as to what is happening.
> 
> Alex
> 
> 
>> On 16 Nov 2017, at 22:46, Brandon Williams via swift-corelibs-dev 
>> > wrote:
>> 
>> Oops, sorry, hit send too early!
>> 
>> The docker file Im using can be seen here:
>> 
>> https://github.com/pointfreeco/swift-web/blob/bb-travis/Dockerfile 
>> 
>> 
>> Which is this template:
>> 
>> https://github.com/swiftdocker/docker-swift/blob/5c83628d4696bca62aec3136a4ee9b854e8d548e/4.0/Dockerfile
>>  
>> 
>> 
>> So not using any of the betas...
>> 
>> On Thu, Nov 16, 2017 at 5:45 PM Brandon Williams > > wrote:
>> Ah, sorry for now providing more info!
>> 
>> So the docker file that I am using to run the tests can be see here:
>> 
>> 
>> 
>> On Thu, Nov 16, 2017 at 5:43 PM Philippe Hausler > > wrote:
>> Is this with a recent build? Do you know what commit the 
>> swift-corelibs-foundation is from? That might help nail down what is going 
>> on here.
>> 
>> 
>>> On Nov 16, 2017, at 2:41 PM, Brandon Williams via swift-corelibs-dev 
>>> > wrote:
>>> 
>> 
>>> Hello! I have a test case that when run on Linux somehow encounters the 
>>> "Constant strings cannot be deallocated” fatal error thrown in 
>>> NSCFString.swift, as seen here:
>>> 
>>> https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSCFString.swift#L118
>>>  
>>> 
>>> 
>>> The test that does it is here:
>>> 
>>> https://github.com/pointfreeco/swift-web/blob/5f19a0264be5d369ee0438da8599e3c478a6573b/Tests/ApplicativeRouterTests/SyntaxRouterTests.swift#L88-L93
>>>  
>>> 
>>> 
>>> Now there’s a lot to unravel here and I haven’t been able to quite isolate 
>>> it, but I thought perhaps someone here might know how that could path could 
>>> even be executed.
>>> 
>>> Thanks for any help!
>> 
>>> ___
>>> swift-corelibs-dev mailing list
>>> swift-corelibs-dev@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev 
>>> 
>> ___
>> swift-corelibs-dev mailing list
>> swift-corelibs-dev@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev 
>> 
> 

___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


Re: [swift-corelibs-dev] Encountering "Constant strings cannot be deallocated"

2017-11-17 Thread Brandon Williams via swift-corelibs-dev
I haven’t been able to find a minimal test case, but at least the test case
I linked to reproduces every time.

I’m not sure I’ll have time to search for a minimal case today, and I leave
for vacation tomorrow :/ perhaps when I get back.

I did at least file the bug report: https://bugs.swift.org/browse/SR-6422

On Fri, Nov 17, 2017 at 4:48 AM Alex Blewitt  wrote:

> Can you file a bug at bugs.swift.org with a minimal test case? It
> certainly sounds like a bug, and from the looks of the docker files, you're
> using the swift 4.0.2 release on Ubuntu 16.04.
>
> If you can run the test under LLDB and put a breakpoint in that function,
> and get a backtrace (with 'bt') then it might be interesting to see where
> the deinit is being triggered, and give some kind of clue as to what is
> happening.
>
> Alex
>
>
> On 16 Nov 2017, at 22:46, Brandon Williams via swift-corelibs-dev <
> swift-corelibs-dev@swift.org> wrote:
>
> Oops, sorry, hit send too early!
>
> The docker file Im using can be seen here:
>
> https://github.com/pointfreeco/swift-web/blob/bb-travis/Dockerfile
>
> Which is this template:
>
>
> https://github.com/swiftdocker/docker-swift/blob/5c83628d4696bca62aec3136a4ee9b854e8d548e/4.0/Dockerfile
>
> So not using any of the betas...
>
> On Thu, Nov 16, 2017 at 5:45 PM Brandon Williams  wrote:
>
>> Ah, sorry for now providing more info!
>>
>> So the docker file that I am using to run the tests can be see here:
>>
>>
>>
>> On Thu, Nov 16, 2017 at 5:43 PM Philippe Hausler 
>> wrote:
>>
>>> Is this with a recent build? Do you know what commit the
>>> swift-corelibs-foundation is from? That might help nail down what is going
>>> on here.
>>>
>>> On Nov 16, 2017, at 2:41 PM, Brandon Williams via swift-corelibs-dev <
>>> swift-corelibs-dev@swift.org> wrote:
>>>
>>> Hello! I have a test case that when run on Linux somehow encounters
>>> the "Constant strings cannot be deallocated” fatal error thrown in
>>> NSCFString.swift, as seen here:
>>>
>>>
>>> https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSCFString.swift#L118
>>>
>>> The test that does it is here:
>>>
>>>
>>> https://github.com/pointfreeco/swift-web/blob/5f19a0264be5d369ee0438da8599e3c478a6573b/Tests/ApplicativeRouterTests/SyntaxRouterTests.swift#L88-L93
>>>
>>> Now there’s a lot to unravel here and I haven’t been able to quite
>>> isolate it, but I thought perhaps someone here might know how that could
>>> path could even be executed.
>>>
>>> Thanks for any help!
>>>
>>> ___
>>> swift-corelibs-dev mailing list
>>> swift-corelibs-dev@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev
>>>
>>> ___
> swift-corelibs-dev mailing list
> swift-corelibs-dev@swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev
>
>
>
___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


Re: [swift-corelibs-dev] Encountering "Constant strings cannot be deallocated"

2017-11-17 Thread Alex Blewitt via swift-corelibs-dev
Can you file a bug at bugs.swift.org  with a minimal 
test case? It certainly sounds like a bug, and from the looks of the docker 
files, you're using the swift 4.0.2 release on Ubuntu 16.04.

If you can run the test under LLDB and put a breakpoint in that function, and 
get a backtrace (with 'bt') then it might be interesting to see where the 
deinit is being triggered, and give some kind of clue as to what is happening.

Alex

> On 16 Nov 2017, at 22:46, Brandon Williams via swift-corelibs-dev 
>  wrote:
> 
> Oops, sorry, hit send too early!
> 
> The docker file Im using can be seen here:
> 
> https://github.com/pointfreeco/swift-web/blob/bb-travis/Dockerfile 
> 
> 
> Which is this template:
> 
> https://github.com/swiftdocker/docker-swift/blob/5c83628d4696bca62aec3136a4ee9b854e8d548e/4.0/Dockerfile
>  
> 
> 
> So not using any of the betas...
> 
> On Thu, Nov 16, 2017 at 5:45 PM Brandon Williams  > wrote:
> Ah, sorry for now providing more info!
> 
> So the docker file that I am using to run the tests can be see here:
> 
> 
> 
> On Thu, Nov 16, 2017 at 5:43 PM Philippe Hausler  > wrote:
> Is this with a recent build? Do you know what commit the 
> swift-corelibs-foundation is from? That might help nail down what is going on 
> here.
> 
> 
>> On Nov 16, 2017, at 2:41 PM, Brandon Williams via swift-corelibs-dev 
>> > wrote:
>> 
> 
>> Hello! I have a test case that when run on Linux somehow encounters the 
>> "Constant strings cannot be deallocated” fatal error thrown in 
>> NSCFString.swift, as seen here:
>> 
>> https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSCFString.swift#L118
>>  
>> 
>> 
>> The test that does it is here:
>> 
>> https://github.com/pointfreeco/swift-web/blob/5f19a0264be5d369ee0438da8599e3c478a6573b/Tests/ApplicativeRouterTests/SyntaxRouterTests.swift#L88-L93
>>  
>> 
>> 
>> Now there’s a lot to unravel here and I haven’t been able to quite isolate 
>> it, but I thought perhaps someone here might know how that could path could 
>> even be executed.
>> 
>> Thanks for any help!
> 
>> ___
>> swift-corelibs-dev mailing list
>> swift-corelibs-dev@swift.org 
>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev 
>> 
> ___
> swift-corelibs-dev mailing list
> swift-corelibs-dev@swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev